导入
使用卷积神经网络进行图像分割
从基础到高级的卷积神经网络技术,深入探索图像分割,理解模型架构,并使用Python实现一个简单的分割模型。
...
图像分割类型
分割技术对比
| 方面 | 语义分割 | 实例分割 | 全景分割 |
|---|---|---|---|
| 目标 | 将每个像素分类到特定类别 | 区分不同的物体实例 | 融合语义分割与实例分割 |
| 表示方式 | 像素级分类 | 精确的边界划分与分类 | 类别与实例的唯一标识 |
| 模型示例 | U-Net, SegNet | Mask R-CNN, PointRend | 视觉Transformer(ViTs) |
| 上下文信息 | 能有效捕捉上下文信息 | 关注局部与全局上下文 | 同时整合类别与实例信息 |
...
特征图金字塔
1import tensorflow as tf 2import matplotlib.pyplot as plt 3from tensorflow.keras.preprocessing import image 4 5# 加载预训练的U-Net模型(本例中使用DenseNet121),并预处理示例图像 6model = tf.keras.applications.DenseNet121(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) 7 8# 加载并预处理示例图像 9img_path = 'test2.jpg' 10img = image.load_img(img_path, target_size=(256, 256)) 11img_array = image.img_to_array(img) 12img_array = tf.expand_dims(img_array, 0) # 增加批次维度 13img_array /= 255.0 # 归一化像素值 14 15# 使用预训练模型对示例图像进行预测 16predictions = model.predict(img_array) 17 18# 并排显示原始图像与分割图 19plt.figure(figsize=(10, 5)) 20 21plt.subplot(1, 2, 1) 22plt.imshow(img) 23plt.title('原始图像') 24 25plt.subplot(1, 2, 2) 26plt.imshow(predictions[0][:, :, 0], cmap='gray') # 为简化,假设输出为单通道 27plt.title('分割图') 28 29plt.show()