8--语义分割

本文介绍了语义分割的基本概念,包括其与目标检测的区别,以及图像分割和实例分割的不同之处。详细解析了PascalVOC2012数据集的构成,并展示了如何读取和预处理数据,最后给出了自定义语义分割数据集类的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        语义分割关注于如何将图像分割成属于不同语义类别的区域(即将一类事务标注为同一颜色),与目标检测不同的,语义分割识别的是像素级的,所有语义分割标注的像素级框比目标检测更加贴合物体。如下图,右图为左边图片的语义分割的标签。

6.1 图像分割和实例分割的区别:

        图像分割将图像划分为若干组成区域,这类问题的方法通常利用图像中像素之间的相关性。它在训练时不需要有关图像像素的标签信息,在预测时也无法保证分割出的区域具有我们希望得到的语义。以上图中左边图像作为输入,图像分割可能会将狗分为两个区域:一个覆盖以黑色为主的嘴和眼睛,另一个覆盖以黄色为主的其余部分身体。

        实例分割也叫同时检测并分割,它研究如何识别图像中各个目标实例的像素级区域。与语义分割不同,实例分割不仅需要区分语义,还要区分不同的目标实例。例如,如果图像中有两条狗,则实例分割需要区分像素属于的两条狗中的哪一条。

6.2 Pascal VOC2012 语义分割数据集

        该数据集中的ImageSets/Segmentation路径包含用于训练和测试样本的文本文件,而JPEGImagesSegmentationClass路径分别存储着每个示例的输入图像和标签。此处的标签也采用图像格式,其尺寸和它所标注的输入图像的尺寸相同。 此外,标签中颜色相同的像素属于同一个语义类别。

        将所有输入的图像和标签读入内存,并对前6个图像以及标签进行展示:

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):
        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)
#画出前6个图像以及其标签
n = 6
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,scale=2)

运行结果如下,可以看见对于不同类别的物体标签中的颜色是不一样的。

 来对数据中RGB数值以及类别进行一个映射:

#给出所有的颜色以及类别
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']

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]) * 256
           + colormap[:, :, 2])
    return colormap2label[idx]

6.3 语义分割的数据预处理 

        在语义分割中由于标签也是图像,如果直接对原始数据进行缩放来使得其符合输入模型的形状,需要将预测的像素类别重新映射回原始尺寸的输入图像,这将导致类别的不准确(例如一个红色的类别拉伸后变成了粉红色 就会导致类别的变化)。所以将图像裁剪为固定尺寸,并将标签也进行相应的裁剪。

def voc_rand_crop(features,label,height,width):
  """随机裁剪特征和标签图像"""
  #这里由于label也是图片形式,所有对图片进行随机剪裁的同时也要对label的图片进行相同的剪裁
  rect = torchvision.transforms.RandomCrop.get_params(
      features,(height,width))
  feature = torchvision.transforms.functional.crop(features,*rect)
  label = torchvision.transforms.functional.crop(label,*rect)
  return feature,label

imgs = []
for _ in range(n):
  imgs += voc_rand_crop(train_features[5],train_labels[5],200,300)
imgs = [img.permute(1,2,0) for img in imgs]
d2l.show_images(imgs[::2]+imgs[1::2],2,n)

运行结果如下,可以看见随着原始图像被裁剪,对应的标签也会做出同样的裁剪。

6.4自定义语义分割数据集类 

         通过继承高级API提供的Dataset类,自定义了一个语义分割数据集类VOCSegDataset。 通过实现__getitem__函数,我们可以任意访问数据集中索引为idx的输入图像及其每个像素的类别索引。 由于数据集中有些图像的尺寸可能小于随机裁剪所指定的输出尺寸,这些样本可以通过自定义的filter函数移除掉。 此外,我们还定义了normalize_image函数,从而对输入图像的RGB三个通道的值分别做标准化。

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)

6.5 构造训练集和测试集的数据迭代器 

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
### 如何在语义分割任务中应用 Grad-CAM #### 1. Grad-CAM 的基本原理 Grad-CAM 是一种基于梯度的可视化方法,通过计算特定类别相对于最后一个卷积层特征图的梯度来生成热力图。这种方法可以突出显示输入图像中的重要区域,帮助理解模型的关注点[^2]。 #### 2. 语义分割任务的特点 语义分割任务的目标是对图像中的每一个像素进行分类,因此与传统的分类任务不同,输出是一个具有多个类别的概率分布矩阵。为了适应这种特性,在语义分割模型中应用 Grad-CAM 需要一些调整[^1]。 #### 3. 实现步骤 以下是具体实现过程中需要注意的关键点: - **选择合适的层**:对于语义分割网络来说,通常会选择靠近输出端的一个或几个卷积层作为目标层。这些层包含了更高级别的语义信息。 - **修改损失函数**:由于语义分割的任务性质,不再像传统 CNN 中那样只针对单个标签计算梯度;而是应该考虑整个预测掩码(mask),并将其转换成适合 Grad-CAM 计算的形式。 - **处理多通道输出**:因为语义分割的结果通常是多通道的概率映射,所以需要对每个感兴趣类别单独执行 Grad-CAM 过程,并最终合成一张综合性的热力图。 ```python import torch from torchvision import models, transforms from grad_cam import GradCAM # 假设已经实现了 GradCAM 类 def apply_gradcam_segmentation(model, input_image_tensor, target_layer_name='layer4', class_idx=None): """ 应用 Grad-CAM 到语义分割模型上 参数: model (nn.Module): 已训练好的语义分割模型实例 input_image_tensor (Tensor): 输入图片张量 target_layer_name (str): 要提取激活值的最后一层名称,默认为 'layer4' class_idx (int or None): 如果指定,则仅对该类创建 Grad-CAM 图; 若未提供,则返回所有类的最大响应位置 返回: Tensor: 形状为 (H,W,C) 的热力图张量 """ device = next(model.parameters()).device input_image_tensor = input_image_tensor.to(device) # 初始化 Grad-CAM 对象并与给定模型绑定 cam_extractor = GradCAM.from_config( model_type="unet", arch=model, layer_name=target_layer_name ) with torch.no_grad(): output_masks = model(input_image_tensor.unsqueeze(0)) if isinstance(output_masks, tuple): output_masks = output_masks[0] heatmap_list = [] num_classes = output_masks.shape[1] for c in range(num_classes): mask = output_masks[:,c,:,:].squeeze() activation_map = cam_extractor(mask)[0] normalized_activation_map = normalize_heatmap(activation_map.cpu().numpy()) heatmap_list.append(normalized_activation_map) combined_heatmaps = np.stack(heatmap_list).max(axis=0) return torch.tensor(combined_heatmaps) def normalize_heatmap(x): """Normalize the heatmap to [0, 1]""" min_val = x.min() max_val = x.max() norm_x = (x - min_val)/(max_val-min_val+1e-8) return norm_x if __name__ == "__main__": transform = transforms.Compose([ transforms.Resize((256, 256)), transforms.ToTensor(), ]) img_path = "path_to_your_test_image" image = Image.open(img_path).convert('RGB') tensor_img = transform(image) unet_model = UNet(n_channels=3, n_classes=21).eval() # 替换成实际使用的预训练 U-Net 模型 result_heatmap = apply_gradcam_segmentation(unet_model, tensor_img) ``` 此代码片段展示了如何在一个假设存在的 `UNet` 架构下实施上述逻辑。请注意,具体的细节可能会因所采用的具体框架版本和个人需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值