46 語(yǔ)義分割和數(shù)據(jù)集【動(dòng)手學(xué)深度學(xué)習(xí)v2】

圖片分類:識(shí)別圖中主要目標(biāo)
目標(biāo)檢測(cè):識(shí)別圖中多個(gè)目標(biāo)位置,并將目標(biāo)用方框框出。但是用方框識(shí)別物體位置具有局限,例如無(wú)法對(duì)物體形狀進(jìn)行識(shí)別。
語(yǔ)義分割:精細(xì)識(shí)別圖片,對(duì)每個(gè)像素進(jìn)行l(wèi)abel

應(yīng)用:背景虛化

無(wú)人車路面分割

實(shí)例分割:對(duì)每個(gè)物體實(shí)例進(jìn)行分割

代碼實(shí)現(xiàn)
%matplotlib inline import os import torch import torchvision from d2l import torch as d2l
數(shù)據(jù)集的tar文件大約為2GB,所以下載可能需要一段時(shí)間。 提取出的數(shù)據(jù)集位于../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')
進(jìn)入路徑../data/VOCdevkit/VOC2012
之后,我們可以看到數(shù)據(jù)集的不同組件。?ImageSets/Segmentation
路徑包含用于訓(xùn)練和測(cè)試樣本的文本文件,而JPEGImages
和SegmentationClass
路徑分別存儲(chǔ)著每個(gè)示例的輸入圖像和標(biāo)簽。 此處的標(biāo)簽也采用圖像格式,其尺寸和它所標(biāo)注的輸入圖像的尺寸相同。 此外,標(biāo)簽中顏色相同的像素屬于同一個(gè)語(yǔ)義類別。 下面將read_voc_images
函數(shù)定義為將所有輸入的圖像和標(biāo)簽讀入內(nèi)存。
VOC格式是一種用得比較廣泛的數(shù)據(jù)集格式。
#@save def read_voc_images(voc_dir, is_train=True): """讀取所有VOC圖像并標(biāo)注""" 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)
下面我們繪制前5個(gè)輸入圖像及其標(biāo)簽。 在標(biāo)簽圖像中,白色和黑色分別表示邊框和背景,而其他顏色則對(duì)應(yīng)不同的類別。
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);

列舉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']
通過(guò)上面定義的兩個(gè)常量,我們可以方便地查找標(biāo)簽中每個(gè)像素的類索引。 我們定義了voc_colormap2label
函數(shù)來(lái)構(gòu)建從上述RGB顏色值到類別索引的映射,而voc_label_indices
函數(shù)將RGB值映射到在Pascal VOC2012數(shù)據(jù)集中的類別索引。
#@save def voc_colormap2label(): """構(gòu)建從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標(biāo)簽中的RGB值映射到它們的類別索引""" colormap = colormap.permute(1, 2, 0).numpy().astype('int32') idx = ((colormap[:, :, 0] * 256 + colormap[:, :, 1]) * 256 + colormap[:, :, 2]) return colormap2label[idx]
例如,在第一張樣本圖像中,飛機(jī)頭部區(qū)域的類別索引為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')
在之前的實(shí)驗(yàn),例如?7.1節(jié)—?7.4節(jié)中,我們通過(guò)再縮放圖像使其符合模型的輸入形狀。 然而在語(yǔ)義分割中,這樣做需要將預(yù)測(cè)的像素類別重新映射回原始尺寸的輸入圖像。 這樣的映射可能不夠精確,尤其在不同語(yǔ)義的分割區(qū)域。 為了避免這個(gè)問(wèn)題,我們將圖像裁剪為固定尺寸,而不是再縮放。 具體來(lái)說(shuō),我們使用圖像增廣中的隨機(jī)裁剪,裁剪輸入圖像和標(biāo)簽的相同區(qū)域。
#@save def voc_rand_crop(feature, label, height, width): """隨機(jī)裁剪特征和標(biāo)簽圖像""" 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);
自定義了一個(gè)語(yǔ)義分割數(shù)據(jù)集類VOCSegDataset
。 通過(guò)實(shí)現(xiàn)__getitem__
函數(shù),我們可以任意訪問(wèn)數(shù)據(jù)集中索引為idx
的輸入圖像及其每個(gè)像素的類別索引。 由于數(shù)據(jù)集中有些圖像的尺寸可能小于隨機(jī)裁剪所指定的輸出尺寸,這些樣本可以通過(guò)自定義的filter
函數(shù)移除掉。 此外,我們還定義了normalize_image
函數(shù),從而對(duì)輸入圖像的RGB三個(gè)通道的值分別做標(biāo)準(zhǔn)化。
#@save class VOCSegDataset(torch.utils.data.Dataset): """一個(gè)用于加載VOC數(shù)據(jù)集的自定義數(shù)據(jù)集""" 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)
讀取數(shù)據(jù)集
我們通過(guò)自定義的VOCSegDataset
類來(lái)分別創(chuàng)建訓(xùn)練集和測(cè)試集的實(shí)例。 假設(shè)我們指定隨機(jī)裁剪的輸出圖像的形狀為320×480
, 下面我們可以查看訓(xùn)練集和測(cè)試集所保留的樣本個(gè)數(shù)。
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 examples
設(shè)批量大小為64,我們定義訓(xùn)練集的迭代器。 打印第一個(gè)小批量的形狀會(huì)發(fā)現(xiàn):與圖像分類或目標(biāo)檢測(cè)不同,這里的標(biāo)簽是一個(gè)三維數(shù)組。
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])
最后,我們定義以下load_data_voc
函數(shù)來(lái)下載并讀取Pascal VOC2012語(yǔ)義分割數(shù)據(jù)集。 它返回訓(xùn)練集和測(cè)試集的數(shù)據(jù)迭代器。
#@save def load_data_voc(batch_size, crop_size): """加載VOC語(yǔ)義分割數(shù)據(jù)集""" 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
