第14章コンピュータビジョン
14.9.2. The Pascal VOC2012 Semantic Segmentation Dataset14.9.2 Pascal VOC2012のセマンティックセグメンテーションのデータセット
On of the most important semantic segmentation dataset is Pascal VOC2012. In the following, we will take a look at this dataset.
英語のヒント
最も重要なセマンティックセグメンテーションのデータセットの一つはPascal VOC2012である。以下では、このデータセットを見てみよう。
%matplotlib inline
import os
import torch
import torchvision
from d2l import torch as d2lThe tar file of the dataset is about 2 GB, so it may take a while to download the file. The extracted dataset is located at
../data/VOCdevkit/VOC2012.英語のヒント
データセットの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...After entering the path
../data/VOCdevkit/VOC2012, we can see the different components of the dataset. The ImageSets/Segmentation path contains text files that specify training and test samples, while the JPEGImages and SegmentationClass paths store the input image and label for each example, respectively. The label here is also in the image format, with the same size as its labeled input image. Besides, pixels with the same color in any label image belong to the same semantic class. The following defines the read_voc_images function to read all the input images and labels into the memory.英語のヒント
パス
../data/VOCdevkit/VOC2012に入ると、データセットのさまざまな構成要素が見える。ImageSets/Segmentationのパスには訓練とテストの標本を指定するテキストファイルがあり、JPEGImagesとSegmentationClassのパスには、それぞれ各事例の入力画像とラベルが格納されている。ここでのラベルも画像の形式で、ラベルづけされた入力画像と同じ大きさである。さらに、どのラベル画像でも、同じ色の画素は同じ意味のクラスに属する。以下では、すべての入力画像とラベルをメモリに読み込む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)We draw the first five input images and their labels. In the label images, white and black represent borders and background, respectively, while the other colors correspond to different classes.
英語のヒント
最初の五つの入力画像とそのラベルを描く。ラベル画像では、白と黒がそれぞれ境界と背景を表し、ほかの色は異なるクラスに対応する。
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);
Next, we enumerate the RGB color values and class names for all the labels in this dataset.
英語のヒント
次に、このデータセットのすべてのラベルについて、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']With the two constants defined above, we can conveniently find the class index for each pixel in a label. We define the
voc_colormap2label function to build the mapping from the above RGB color values to class indices, and the voc_label_indices function to map any RGB values to their class indices in this Pascal VOC2012 dataset.英語のヒント
上で定義した二つの定数を使うと、ラベルの各画素のクラスの添字を手軽に見つけられる。上のRGBの色の値からクラスの添字への対応を作る
voc_colormap2label関数と、このPascal VOC2012のデータセットで任意のRGBの値をそのクラスの添字に対応させるvoc_label_indices関数を定義する。#@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, colormap in enumerate(VOC_COLORMAP):
colormap2label[
(colormap[0] * 256 + colormap[1]) * 256 + colormap[2]] = i
return colormap2label
#@save
def voc_label_indices(colormap, colormap2label):
"""Map any RGB values in VOC labels to their class indices."""
colormap = colormap.permute(1, 2, 0).numpy().astype('int32')
idx = ((colormap[:, :, 0] * 256 + colormap[:, :, 1]) * 256
+ colormap[:, :, 2])
return colormap2label[idx]For example, in the first example image, the class index for the front part of the airplane is 1, while the background index is 0.
英語のヒント
たとえば、最初の例の画像では、飛行機の前の部分のクラスの添字は1であり、背景の添字は0である。
y = voc_label_indices(train_labels[0], voc_colormap2label())
y[105:115, 130:140], VOC_CLASSES[1](tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1]]),
'aeroplane')14.9.2.1. Data Preprocessing14.9.2.1 データの前処理
In previous experiments such as in Section 8.1–Section 8.4, images are rescaled to fit the model’s required input shape. However, in semantic segmentation, doing so requires rescaling the predicted pixel classes back to the original shape of the input image. Such rescaling may be inaccurate, especially for segmented regions with different classes. To avoid this issue, we crop the image to a fixed shape instead of rescaling. Specifically, using random cropping from image augmentation, we crop the same area of the input image and the label.
英語のヒント
#@save
def voc_rand_crop(feature, label, height, width):
"""Randomly crop both feature and label images."""
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);
14.9.2.2. Custom Semantic Segmentation Dataset Class14.9.2.2 カスタムのセマンティックセグメンテーションのデータセットのクラス
We define a custom semantic segmentation dataset class
VOCSegDataset by inheriting the Dataset class provided by high-level APIs. By implementing the __getitem__ function, we can arbitrarily access the input image indexed as idx in the dataset and the class index of each pixel in this image. Since some images in the dataset have a smaller size than the output size of random cropping, these examples are filtered out by a custom filter function. In addition, we also define the normalize_image function to standardize the values of the three RGB channels of input images.英語のヒント
高水準のAPIが与える
Datasetクラスを継承して、カスタムのセマンティックセグメンテーションのデータセットのクラスVOCSegDatasetを定義する。__getitem__関数を実装すると、データセットの中でidxという添字の入力画像と、この画像の各画素のクラスの添字に、任意にアクセスできる。データセットの一部の画像はランダムな切り抜きの出力の大きさより小さいので、そうした事例はカスタムのfilter関数で取り除かれる。さらに、入力画像の三つのRGBチャネルの値を標準化するnormalize_image関数も定義する。#@save
class VOCSegDataset(torch.utils.data.Dataset):
"""A customized dataset to load the VOC dataset."""
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)14.9.2.3. Reading the Dataset14.9.2.3 データセットを読む
We use the custom
VOCSegDataset class to create instances of the training set and test set, respectively. Suppose that we specify that the output shape of randomly cropped images is . Below we can view the number of examples that are retained in the training set and test set.英語のヒント
カスタムの
VOCSegDataseクラスを使って、訓練集合とテスト集合のインスタンスをそれぞれ作る。ランダムに切り抜いた画像の出力の形状をと指定するとしよう。下で、訓練集合とテスト集合に残った事例の数を見られる。crop_size = (320, 480)
voc_train = VOCSegDataset(True, crop_size, voc_dir)
voc_test = VOCSegDataset(False, crop_size, voc_dir)read 1114 examples
read 1078 examplesSetting the batch size to 64, we define the data iterator for the training set. Let’s print the shape of the first minibatch. Different from in image classification or object detection, labels here are three-dimensional tensors.
英語のヒント
バッチの大きさを64に設定して、訓練集合のデータイテレータを定義する。最初のミニバッチの形状を出力しよう。画像分類や物体検出とは違って、ここでのラベルは3次元のテンソルである。
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)
breaktorch.Size([64, 3, 320, 480])
torch.Size([64, 320, 480])14.9.2.4. Putting It All Together14.9.2.4 すべてをまとめる
Finally, we define the following
load_data_voc function to download and read the Pascal VOC2012 semantic segmentation dataset. It returns data iterators for both the training and test datasets.英語のヒント
最後に、Pascal VOC2012のセマンティックセグメンテーションのデータセットをダウンロードして読む、次の
load_data_voc関数を定義する。それは、訓練データセットとテストデータセットの両方のデータイテレータを返す。#@save
def load_data_voc(batch_size, crop_size):
"""Load the VOC semantic segmentation dataset."""
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