×

PyTorch教程14.9之语义分割和数据集

消耗积分:0 | 格式:pdf | 大小:0.26 MB | 2023-06-05

潘飘稚

分享资料个

第 14.3 节-第 14.8 节讨论对象检测任务时,矩形边界框用于标记和预测图像中的对象。本节将讨论语义分割问题,重点关注如何将图像划分为属于不同语义类的区域。与目标检测不同,语义分割在像素级别识别和理解图像中的内容:它对语义区域的标记和预测是在像素级别。 图 14.9.1显示了语义分割中图像的狗、猫和背景的标签。与目标检测相比,语义分割中标记的像素级边界明显更细粒度。

https://file.elecfans.com/web2/M00/A9/CD/poYBAGR9O9WAJnnkAAdSBrW48yA985.svg

图 14.9.1语义分割中图像的狗、猫和背景的标签。

14.9.1。图像分割和实例分割

计算机视觉领域还有两个与语义分割类似的重要任务,即图像分割和实例分割。我们将如下简要地将它们与语义分割区分开来。

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

  • 实例分割也称为同时检测和分割它研究如何识别图像中每个对象实例的像素级区域。与语义分割不同,实例分割不仅需要区分语义,还需要区分不同的对象实例。例如,如果图像中有两只狗,实例分割需要区分一个像素属于这两只狗中的哪一只。

14.9.2。Pascal VOC2012 语义分割数据集

最重要的语义分割数据集之一是Pascal VOC2012下面,我们将看看这个数据集。

%matplotlib inline
import os
import torch
import torchvision
from d2l import torch as d2l
%matplotlib inline
import os
from mxnet import gluon, image, np, npx
from d2l import mxnet as d2l

npx.set_np()

数据集的 tar 文件大约 2 GB,因此下载文件可能需要一段时间。提取的数据集位于 ../data/VOCdevkit/VOC2012.

#@save
d2l.DATA_HUB['voc2012'] = (d2l.DATA_URL + 'VOCtrainval_11-May-2012.tar',
              '4e443f8a2eca6b1dac8a6c57641b67dd40621a49')

voc_dir = d2l.download_extract('voc2012', 'VOCdevkit/VOC2012')
Downloading ../data/VOCtrainval_11-May-2012.tar from http://d2l-data.s3-accelerate.amazonaws.com/VOCtrainval_11-May-2012.tar...
#@save
d2l.DATA_HUB['voc2012'] = (d2l.DATA_URL + 'VOCtrainval_11-May-2012.tar',
              '4e443f8a2eca6b1dac8a6c57641b67dd40621a49')

voc_dir = d2l.download_extract('voc2012', 'VOCdevkit/VOC2012')

进入路径后../data/VOCdevkit/VOC2012,我们可以看到数据集的不同组成部分。ImageSets/Segmentation路径包含指定训练和测试样本的文本文件,而 JPEGImagesSegmentationClass路径分别存储每个示例的输入图像和标签。这里的label也是image格式的,和它的labeled input image大小一样。此外,任何标签图像中具有相同颜色的像素属于同一语义类。下面定义了read_voc_images将所有输入图像和标签读入内存的函数。

#@save
def read_voc_images(voc_dir, is_train=True):
  """Read all VOC feature and label images."""
  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)
#@save
def read_voc_images(voc_dir, is_train=True):
  """Read all VOC feature and label images."""
  txt_fname = os.path.join(voc_dir, 'ImageSets', 'Segmentation',
               'train.txt' if is_train else 'val.txt')
  with open(txt_fname, 'r') as f:
    images = f.read().split()
  features, labels = [], []
  for i, fname in enumerate(images):
    features.append(image.imread(os.path.join(
      voc_dir, 'JPEGImages', f'{fname}.jpg')))
    labels.append(image.imread(os.path.join(
      voc_dir, 'SegmentationClass', f'{fname}.png')))
  return features, labels

train_features, train_labels = read_voc_images(voc_dir, True)

我们绘制前五个输入图像及其标签。在标签图像中,白色和黑色分别代表边框和背景,而其他颜色对应不同的类别。

n = 5
imgs = train_features[:n] + train_labels[:n]
imgs = [img.permute(1,2,0) for img in imgs]
d2l.show_images(imgs, 2, n);
https://file.elecfans.com/web2/M00/A9/00/poYBAGR4YpiAUiS-AAFQfESlL94544.png
n = 5
imgs = train_features[:n] + train_labels[:n]
d2l.show_images(imgs, 2, n);
https://file.elecfans.com/web2/M00/A9/00/poYBAGR4YpiAUiS-AAFQfESlL94544.png

接下来,我们枚举该数据集中所有标签的 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']
#@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']

使用上面定义的两个常量,我们可以方便地找到标签中每个像素的类索引。我们定义了voc_colormap2label 构建从上述 RGB 颜色值到类索引的映射的函数,以及voc_label_indices将任何 RGB 值映射到此 Pascal VOC2012 数据集中它们的类索引的函数。

#@save
def voc_colormap2label():
  """Build the mapping from RGB to class indices for VOC labels."""
  colormap2label = torch.zeros(256 ** 3, dtype=torch.long)
  for i

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !