语义分割(semantic segmentation)
语义分割(semantic segmentation)
文章目录
- 语义分割(semantic segmentation)
-
- 图像分割和实例分割
-
- 代码实现
语义分割是指将图像中的每一个像素分配到相应的类别中,在这一过程中** pixel-wise annotation and prediction **被特别关注并被视为关键步骤之一;通过语义分割技术得到的结果呈现出明显的边界线性特征;其应用实例包括背景虚化的处理等技术细节

图像分割和实例分割
- 图像划分 将一张图片分成多个区域,在机器学习中这类问题通常基于像素间的关联性来建模。该方法在训练过程中无需依赖对图片真实像素标签信息的知识,在实际应用中难以确保分割出的区域具备预期的意义(如下图所示)。假设输入是一张狗的照片,在这种情况下模型可能会将狗的身体分为两个部分:一个覆盖以黑色为主的嘴和眼睛部位、另一个则涵盖以黄色为主的大部分身体。

实例分割 也被视为一种联合检测与分割技术( joint detection and segmentation),它主要关注的是通过分析图像来确定每个目标的具体像素区域。与一般的语义分割方法相比,在实际应用中还需要进一步区分类别间差异较小的目标对象。举个例子来说,在一张图片中有两只狗的情况下,则需要明确每只狗所占据的特定像素区域属于哪一只狗的范围。
语义分割vs实例分割:
语义分割:只关心像素是哪一个类别
实例分割:区别具体对每个实例的识别

代码实现
%matplotlib inline
import os
import torch
import torchvision
from d2l import torch as d2l
# 导入文件
d2l.DATA_HUB['voc2012'] = (d2l.DATA_URL + 'VOCtrainval_11-May-2012.tar',
'4e443f8a2eca6b1dac8a6c57641b67dd40621a49')
voc_dir = d2l.download_extract('voc2012', 'VOCdevkit/VOC2012')
#加载图片
def read_voc_images(voc_dir, is_train=True):
"""读取所有VOC图像并标注"""
txt_fname = os.path.join(voc_dir, 'ImageSets', 'Segmentation',
'train.txt' if is_train else 'val.txt')
mode = torchvision.io.image.ImageReadMode.RGB
with open(txt_fname, 'r') as f:
images = f.read().split()
features, labels = [], []
for i, fname in enumerate(images):
# 分别读取图片和像素特征点,png的文件大保存了图像质量,jpg文件小,适合存储色彩丰富、细节复杂的照片
features.append(torchvision.io.read_image(os.path.join(
voc_dir, 'JPEGImages', f'{fname}.jpg')))
labels.append(torchvision.io.read_image(os.path.join(
voc_dir, 'SegmentationClass' ,f'{fname}.png'), mode))
return features, labels
train_features, train_labels = read_voc_images(voc_dir, True)
python

- 展示图片
n = 5
imgs = train_features[0:n] + train_labels[0:n]
imgs = [img.permute(1,2,0) for img in imgs]
d2l.show_images(imgs, 2, n);
python

- 标签名与RGB色调相对应(标签中对应的图片与其标号相对应)
一个标签类别对应一个RGB颜色
#@save
VOC_COLORMAP = [[0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0],
[0, 0, 128], [128, 0, 128], [0, 128, 128], [128, 128, 128],
[64, 0, 0], [192, 0, 0], [64, 128, 0], [192, 128, 0],
[64, 0, 128], [192, 0, 128], [64, 128, 128], [192, 128, 128],
[0, 64, 0], [128, 64, 0], [0, 192, 0], [128, 192, 0],
[0, 64, 128]]
#@save
VOC_CLASSES = ['background', 'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair', 'cow',
'diningtable', 'dog', 'horse', 'motorbike', 'person',
'potted plant', 'sheep', 'sofa', 'train', 'tv/monitor']
python

- 标签中像素的类索引和RGB的转换
利用了进制数之间的转化关系,将RGB转化为单标量索引
#@save
def voc_colormap2label():
"""构建从RGB到VOC类别索引的映射"""
colormap2label = torch.zeros(256 ** 3, dtype=torch.long)
for i, colormap in enumerate(VOC_COLORMAP):
colormap2label[
(colormap[0] * 256 + colormap[1]) * 256 + colormap[2]] = i
return colormap2label
#@save
def voc_label_indices(colormap, colormap2label):
"""将VOC标签中的RGB值映射到它们的类别索引"""
colormap = colormap.permute(1, 2, 0).numpy().astype('int32')
idx = ((colormap[:, :, 0] * 256 + colormap[:, :, 1])
+ colormap[:, :, 2])
return colormap2label[idx]
python

- 展示数据
0是背景而1是飞机的像素点,由png照片中读取得到
y = voc_label_indices(train_labels[0], voc_colormap2label())
y[105:115, 130:140], VOC_CLASSES[1]
python

- 预处理数据
采用图像增强技术中的随机裁剪方法,在对输入图像进行随机裁剪的同时,并对对应的标签进行相应的调整;确保所提取的特征与其所属的类别信息保持一致。
#@save
def voc_rand_crop(feature, label, height, width):
"""随机裁剪特征和标签图像"""
# 随机处理记为rect,对特征图片和标签图片进行同样的rect处理
rect = torchvision.transforms.RandomCrop.get_params(
feature, (height, width))
feature = torchvision.transforms.functional.crop(feature, *rect)
label = torchvision.transforms.functional.crop(label, *rect)
return feature, label
# 图像增广处理
imgs = []
for _ in range(n):
imgs += voc_rand_crop(train_features[0], train_labels[0], 200, 300)
imgs = [img.permute(1, 2, 0) for img in imgs]
d2l.show_images(imgs[::2] + imgs[1::2], 2, n);
python


- 自定义语义分割数据集类(Dataset)
class VOCSegDataset(torch.utils.data.Dataset):
"""一个用于加载VOC数据集的自定义数据集"""
def __init__(self, is_train, crop_size, voc_dir):
self.transform = torchvision.transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
self.crop_size = crop_size
features, labels = read_voc_images(voc_dir, is_train=is_train)
self.features = [self.normalize_image(feature)
for feature in self.filter(features)]
self.labels = self.filter(labels)
self.colormap2label = voc_colormap2label()
print('read ' + str(len(self.features)) + ' examples')
# 图片归一化
def normalize_image(self, img):
return self.transform(img.float() / 255)
# 去掉图片尺寸较小的图片
def filter(self, imgs):
return [img for img in imgs if (
img.shape[1] >= self.crop_size[0] and
img.shape[2] >= self.crop_size[1])]
def __getitem__(self, idx):
feature, label = voc_rand_crop(self.features[idx], self.labels[idx], *self.crop_size)
return (feature, voc_label_indices(label, self.colormap2label))
def __len__(self):
return len(self.features)
# 读取数据集
crop_size = (320, 480)
voc_train = VOCSegDataset(True, crop_size, voc_dir)
voc_test = VOCSegDataset(False, crop_size, voc_dir)
batch_size = 64
train_iter = torch.utils.data.DataLoader(voc_train, batch_size, shuffle=True,
drop_last=True,
num_workers=d2l.get_dataloader_workers())
for X, Y in train_iter:
print(X.shape)
print(Y.shape)
break
-------------------------------------
torch.Size([64, 3, 320, 480])
torch.Size([64, 320, 480])
# 整合所有组件
def load_data_voc(batch_size, crop_size):
"""加载VOC语义分割数据集"""
voc_dir = d2l.download_extract('voc2012', os.path.join('VOCdevkit', 'VOC2012'))
num_workers = d2l.get_dataloader_workers()
train_iter = torch.utils.data.DataLoader(
VOCSegDataset(True, crop_size, voc_dir), batch_size,
shuffle=True, drop_last=True, num_workers=num_workers)
test_iter = torch.utils.data.DataLoader(
VOCSegDataset(False, crop_size, voc_dir), batch_size,
drop_last=True, num_workers=num_workers)
return train_iter, test_iter
python

语义分割通过将图像划分成属于不同语义类别的区域来进行图像的识别与解析。
由于输入图像与标签在像素级别存在一一对应关系,在执行语义分割任务时,输入图像会被裁剪成固定的尺寸以避免进行缩放处理。
尽管如此,在实际应用中发现该方法能够显著提升模型对复杂场景的理解能力。
Q&A:
Q1:更细致的语义分割
A1:通过关键点分割技术,在计算机视觉领域内。具体而言,在这一领域内,关键点分割指的是提取并分析图像的关键特征信息,并实现图像的空间分解与标记过程。该技术能够覆盖物体识别、姿态估计等多方面的应用场景。
Q2:三维语义分割该如何去做,与二维图像之间的差别有哪些
A2:将三维图像压缩为二维图像;将二维卷积扩展为三维卷积;对3D医学图像进行处理时,在z轴上进行切片,并逐个分割后叠加起来。
