在8.1 節(jié)中,我們提到大型數(shù)據(jù)集是深度神經(jīng)網(wǎng)絡(luò)在各種應(yīng)用中取得成功的先決條件。圖像增強(qiáng)在對(duì)訓(xùn)練圖像進(jìn)行一系列隨機(jī)變化后生成相似但不同的訓(xùn)練示例,從而擴(kuò)大了訓(xùn)練集的大小。或者,圖像增強(qiáng)的動(dòng)機(jī)可能是訓(xùn)練示例的隨機(jī)調(diào)整允許模型減少對(duì)某些屬性的依賴,從而提高它們的泛化能力。例如,我們可以通過(guò)不同的方式裁剪圖像,使感興趣的對(duì)象出現(xiàn)在不同的位置,從而減少模型對(duì)對(duì)象位置的依賴。我們還可以調(diào)整亮度和顏色等因素,以降低模型對(duì)顏色的敏感度。圖像增強(qiáng)對(duì)于當(dāng)時(shí) AlexNet 的成功來(lái)說(shuō)可能是不可或缺的。在本節(jié)中,我們將討論這種在計(jì)算機(jī)視覺(jué)中廣泛使用的技術(shù)。
%matplotlib inline import torch import torchvision from torch import nn from d2l import torch as d2l
%matplotlib inline from mxnet import autograd, gluon, image, init, np, npx from mxnet.gluon import nn from d2l import mxnet as d2l npx.set_np()
14.1.1。常見(jiàn)的圖像增強(qiáng)方法
在我們對(duì)常見(jiàn)圖像增強(qiáng)方法的研究中,我們將使用以下方法400×500形象一個(gè)例子。
d2l.set_figsize() img = d2l.Image.open('../img/cat1.jpg') d2l.plt.imshow(img);
d2l.set_figsize() img = image.imread('../img/cat1.jpg') d2l.plt.imshow(img.asnumpy());
大多數(shù)圖像增強(qiáng)方法都具有一定的隨機(jī)性。為了方便我們觀察圖像增強(qiáng)的效果,接下來(lái)我們定義一個(gè)輔助函數(shù)apply。aug此函數(shù)在輸入圖像上多次運(yùn)行圖像增強(qiáng)方法img 并顯示所有結(jié)果。
def apply(img, aug, num_rows=2, num_cols=4, scale=1.5): Y = [aug(img) for _ in range(num_rows * num_cols)] d2l.show_images(Y, num_rows, num_cols, scale=scale)
def apply(img, aug, num_rows=2, num_cols=4, scale=1.5): Y = [aug(img) for _ in range(num_rows * num_cols)] d2l.show_images(Y, num_rows, num_cols, scale=scale)
14.1.1.1。翻轉(zhuǎn)和裁剪
左右翻轉(zhuǎn)圖像通常不會(huì)改變對(duì)象的類別。這是最早和最廣泛使用的圖像增強(qiáng)方法之一。接下來(lái),我們使用該transforms模塊創(chuàng)建實(shí)例RandomHorizontalFlip,它以 50% 的幾率左右翻轉(zhuǎn)圖像。
apply(img, torchvision.transforms.RandomHorizontalFlip())
上下翻轉(zhuǎn)不像左右翻轉(zhuǎn)那樣常見(jiàn)。但至少對(duì)于這個(gè)示例圖像,上下翻轉(zhuǎn)并不妨礙識(shí)別。接下來(lái),我們創(chuàng)建一個(gè)RandomVerticalFlip實(shí)例,以 50% 的幾率上下翻轉(zhuǎn)圖像。
apply(img, torchvision.transforms.RandomVerticalFlip())
Flipping the image left and right usually does not change the category of the object. This is one of the earliest and most widely used methods of image augmentation. Next, we use the transforms module to create the RandomFlipLeftRight instance, which flips an image left and right with a 50% chance.
apply(img, gluon.data.vision.transforms.RandomFlipLeftRight())
Flipping up and down is not as common as flipping left and right. But at least for this example image, flipping up and down does not hinder recognition. Next, we create a RandomFlipTopBottom instance to flip an image up and down with a 50% chance.
apply(img, gluon.data.vision.transforms.RandomFlipTopBottom())
在我們使用的示例圖像中,貓位于圖像的中間,但一般情況下可能并非如此。在7.5 節(jié)中,我們解釋了池化層可以降低卷積層對(duì)目標(biāo)位置的敏感性。此外,我們還可以隨機(jī)裁剪圖像,讓物體以不同的尺度出現(xiàn)在圖像中的不同位置,這樣也可以降低模型對(duì)目標(biāo)位置的敏感度。
在下面的代碼中,我們隨機(jī)裁剪一個(gè)面積為 10%~100%每次都是原始區(qū)域的大小,這個(gè)區(qū)域的寬高比是隨機(jī)選擇的 0.5~2. 然后,該區(qū)域的寬度和高度都縮放為 200 像素。除非另有說(shuō)明,之間的隨機(jī)數(shù)a和b本節(jié)中指的是從區(qū)間中隨機(jī)均勻采樣得到的連續(xù)值 [a,b].
shape_aug = torchvision.transforms.RandomResizedCrop( (200, 200), scale=(0.1, 1), ratio=(0.5, 2)) apply(img, shape_aug)
shape_aug = gluon.data.vision.transforms.RandomResizedCrop( (200, 200), scale=(0.1, 1), ratio=(0.5, 2)) apply(img, shape_aug)
14.1.1.2。改變顏色
另一種增強(qiáng)方法是改變顏色。我們可以改變圖像顏色的四個(gè)方面:亮度、對(duì)比度、飽和度和色調(diào)。在下面的示例中,我們將圖像的亮度隨機(jī)更改為 50% (1?0.5) 和 150% (1+0.5) 的原始圖像。
apply(img, torchvision.transforms.ColorJitter( brightness=0.5, contrast=0, saturation=0, hue=0))
apply(img, gluon.data.vision.transforms.RandomBrightness(0.5))
同樣,我們可以隨機(jī)改變圖像的色調(diào)。
apply(img, torchvision.transforms.ColorJitter( brightness=0, contrast=0, saturation=0, hue=0.5))
apply(img, gluon.data.vision.transforms.RandomHue(0.5))
我們也可以創(chuàng)建一個(gè)RandomColorJitter實(shí)例,同時(shí)設(shè)置如何隨機(jī)改變圖片的brightness, contrast, saturation, 。hue
color_aug = torchvision.transforms.ColorJitter( brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5) apply(img, color_aug)
color_aug = gluon.data.vision.transforms.RandomColorJitter( brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5) apply(img, color_aug)
14.1.1.3。結(jié)合多種圖像增強(qiáng)方法
在實(shí)踐中,我們將結(jié)合多種圖像增強(qiáng)方法。例如,我們可以組合上面定義的不同圖像增強(qiáng)方法,并通過(guò)實(shí)例將它們應(yīng)用于每個(gè)圖像Compose。
augs = torchvision.transforms.Compose([ torchvision.transforms.RandomHorizontalFlip(), color_aug, shape_aug]) apply(img, augs)
augs = gluon.data.vision.transforms.Compose([ gluon.data.vision.transforms.RandomFlipLeftRight(), color_aug, shape_aug]) apply(img, augs)
14.1.2。圖像增強(qiáng)訓(xùn)練
讓我們訓(xùn)練一個(gè)具有圖像增強(qiáng)功能的模型。這里我們使用 CIFAR-10 數(shù)據(jù)集而不是我們之前使用的 Fashion-MNIST 數(shù)據(jù)集。這是因?yàn)?Fashion-MNIST 數(shù)據(jù)集中物體的位置和大小已經(jīng)歸一化,而 CIFAR-10 數(shù)據(jù)集中物體的顏色和大小有更顯著的差異。CIFAR-10 數(shù)據(jù)集中的前 32 個(gè)訓(xùn)練圖像如下所示。
all_images = torchvision.datasets.CIFAR10(train=True, root="../data", download=True) d2l.show_images([all_images[i][0] for i in range(32)], 4, 8, scale=0.8);
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ../data/cifar-10-python.tar.gz
0%| | 0/170498071 [00:00
Extracting ../data/cifar-10-python.tar.gz to ../data
d2l.show_images(gluon.data.vision.CIFAR10( train=True)[:32][0], 4, 8, scale=0.8);
為了在預(yù)測(cè)過(guò)程中獲得確定的結(jié)果,我們通常只對(duì)訓(xùn)練樣例進(jìn)行圖像增強(qiáng),而不會(huì)在預(yù)測(cè)過(guò)程中使用帶有隨機(jī)操作的圖像增強(qiáng)。這里我們只使用最簡(jiǎn)單的隨機(jī)左右翻轉(zhuǎn)的方法。此外,我們使用 ToTensor實(shí)例將一小批圖像轉(zhuǎn)換為深度學(xué)習(xí)框架所需的格式,即 0 到 1 之間的 32 位浮點(diǎn)數(shù),形狀為(批量大小,通道數(shù),高度,寬度).
train_augs = torchvision.transforms.Compose([ torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor()]) test_augs = torchvision.transforms.Compose([ torchvision.transforms.ToTensor()])
接下來(lái),我們定義一個(gè)輔助函數(shù)以方便讀取圖像和應(yīng)用圖像增強(qiáng)。PyTorch 數(shù)據(jù)集提供的參數(shù)transform應(yīng)用增強(qiáng)來(lái)轉(zhuǎn)換圖像。詳細(xì)介紹DataLoader請(qǐng)參考 4.2節(jié)。
def load_cifar10(is_train, augs, batch_size): dataset = torchvision.datasets.CIFAR10(root="../data", train=is_train, transform=augs, download=True) dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=is_train, num_workers=d2l.get_dataloader_workers()) return dataloader
train_augs = gluon.data.vision.transforms.Compose([ gluon.data.vision.transforms.RandomFlipLeftRight(), gluon.data.vision.transforms.ToTensor()]) test_augs = gluon.data.vision.transforms.Compose([ gluon.data.vision.transforms.ToTensor()])
Next, we define an auxiliary function to facilitate reading the image and applying image augmentation. The transform_first function provided by Gluon’s datasets applies image augmentation to the first element of each training example (image and label), i.e., the image. For a detailed introduction to DataLoader, please refer to Section 4.2.
def load_cifar10(is_train, augs, batch_size): return gluon.data.DataLoader( gluon.data.vision.CIFAR10(train=is_train).transform_first(augs), batch_size=batch_size, shuffle=is_train, num_workers=d2l.get_dataloader_workers())
14.1.2.1。多 GPU 訓(xùn)練
我們?cè)?CIFAR-10 數(shù)據(jù)集上訓(xùn)練第 8.6 節(jié)中的 ResNet-18 模型。回想一下13.6 節(jié)中對(duì)多 GPU 訓(xùn)練的介紹 。在下文中,我們定義了一個(gè)函數(shù)來(lái)使用多個(gè) GPU 訓(xùn)練和評(píng)估模型。
#@save def train_batch_ch13(net, X, y, loss, trainer, devices): """Train for a minibatch with multiple GPUs (defined in Chapter 13).""" if isinstance(X, list): # Required for BERT fine-tuning (to be covered later) X = [x.to(devices[0]) for x in X] else: X = X.to(devices[0]) y = y.to(devices[0]) net.train() trainer.zero_grad() pred = net(X) l = loss(pred, y) l.sum().backward() trainer.step() train_loss_sum = l.sum() train_acc_sum = d2l.accuracy(pred, y) return train_loss_sum, train_acc_sum #@save def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices=d2l.try_all_gpus()): """Train a model with multiple GPUs (defined in Chapter 13).""" timer, num_batches = d2l.Timer(), len(train_iter) animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1], legend=['train loss', 'train acc', 'test acc']) net = nn.DataParallel(net, device_ids=devices).to(devices[0]) for epoch in range(num_epochs): # Sum of training loss, sum of training accuracy, no. of examples, # no. of predictions metric = d2l.Accumulator(4) for i, (features, labels) in enumerate(train_iter): timer.start() l, acc = train_batch_ch13( net, features, labels, loss, trainer, devices) metric.add(l, acc, labels.shape[0], labels.numel()) timer.stop() if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1: animator.add(epoch + (i + 1) / num_batches, (metric[0] / metric[2], metric[1] / metric[3], None)) test_acc = d2l.evaluate_accuracy_gpu(net, test_iter) animator.add(epoch + 1, (None, None, test_acc)) print(f'loss {metric[0] / metric[2]:.3f}, train acc ' f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}') print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on ' f'{str(devices)}')
#@save def train_batch_ch13(net, features, labels, loss, trainer, devices, split_f=d2l.split_batch): """Train for a minibatch with multiple GPUs (defined in Chapter 13).""" X_shards, y_shards = split_f(features, labels, devices) with autograd.record(): pred_shards = [net(X_shard) for X_shard in X_shards] ls = [loss(pred_shard, y_shard) for pred_shard, y_shard in zip(pred_shards, y_shards)] for l in ls: l.backward() # The `True` flag allows parameters with stale gradients, which is useful # later (e.g., in fine-tuning BERT) trainer.step(labels.shape[0], ignore_stale_grad=True) train_loss_sum = sum([float(l.sum()) for l in ls]) train_acc_sum = sum(d2l.accuracy(pred_shard, y_shard) for pred_shard, y_shard in zip(pred_shards, y_shards)) return train_loss_sum, train_acc_sum #@save def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices=d2l.try_all_gpus(), split_f=d2l.split_batch): """Train a model with multiple GPUs (defined in Chapter 13).""" timer, num_batches = d2l.Timer(), len(train_iter) animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1], legend=['train loss', 'train acc', 'test acc']) for epoch in range(num_epochs): # Sum of training loss, sum of training accuracy, no. of examples, # no. of predictions metric = d2l.Accumulator(4) for i, (features, labels) in enumerate(train_iter): timer.start() l, acc = train_batch_ch13( net, features, labels, loss, trainer, devices, split_f) metric.add(l, acc, labels.shape[0], labels.size) timer.stop() if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1: animator.add(epoch + (i + 1) / num_batches, (metric[0] / metric[2], metric[1] / metric[3], None)) test_acc = d2l.evaluate_accuracy_gpus(net, test_iter, split_f) animator.add(epoch + 1, (None, None, test_acc)) print(f'loss {metric[0] / metric[2]:.3f}, train acc ' f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}') print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on ' f'{str(devices)}')
現(xiàn)在我們可以定義train_with_data_aug函數(shù)來(lái)訓(xùn)練帶有圖像增強(qiáng)的模型。該函數(shù)獲取所有可用的 GPU,使用 Adam 作為優(yōu)化算法,對(duì)訓(xùn)練數(shù)據(jù)集應(yīng)用圖像增強(qiáng),最后調(diào)用train_ch13剛剛定義的函數(shù)來(lái)訓(xùn)練和評(píng)估模型。
batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10, 3) net.apply(d2l.init_cnn) def train_with_data_aug(train_augs, test_augs, net, lr=0.001): train_iter = load_cifar10(True, train_augs, batch_size) test_iter = load_cifar10(False, test_augs, batch_size) loss = nn.CrossEntropyLoss(reduction="none") trainer = torch.optim.Adam(net.parameters(), lr=lr) net(next(iter(train_iter))[0]) train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices)
batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10) net.initialize(init=init.Xavier(), ctx=devices) def train_with_data_aug(train_augs, test_augs, net, lr=0.001): train_iter = load_cifar10(True, train_augs, batch_size) test_iter = load_cifar10(False, test_augs, batch_size) loss = gluon.loss.SoftmaxCrossEntropyLoss() trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': lr}) train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices)
讓我們使用基于隨機(jī)左右翻轉(zhuǎn)的圖像增強(qiáng)來(lái)訓(xùn)練模型。
train_with_data_aug(train_augs, test_augs, net)
loss 0.224, train acc 0.922, test acc 0.820 3237.5 examples/sec on [device(type='cuda', index=0), device(type='cuda', index=1)]
train_with_data_aug(train_augs, test_augs, net)
loss 0.168, train acc 0.942, test acc 0.837 1933.3 examples/sec on [gpu(0), gpu(1)]
14.1.3。概括
圖像增強(qiáng)是在現(xiàn)有訓(xùn)練數(shù)據(jù)的基礎(chǔ)上生成隨機(jī)圖像,以提高模型的泛化能力。
為了在預(yù)測(cè)過(guò)程中獲得確定的結(jié)果,我們通常只對(duì)訓(xùn)練樣例進(jìn)行圖像增強(qiáng),而不會(huì)在預(yù)測(cè)過(guò)程中使用帶有隨機(jī)操作的圖像增強(qiáng)。
深度學(xué)習(xí)框架提供了許多不同的圖像增強(qiáng)方法,可以同時(shí)應(yīng)用。
14.1.4。練習(xí)
在不使用圖像增強(qiáng)的情況下訓(xùn)練模型: 。比較使用和不使用圖像增強(qiáng)時(shí)的訓(xùn)練和測(cè)試準(zhǔn)確性。這個(gè)對(duì)比實(shí)驗(yàn)?zāi)芊裰С謭D像增強(qiáng)可以減輕過(guò)擬合的論點(diǎn)?為什么?train_with_data_aug(test_augs, test_augs)
在 CIFAR-10 數(shù)據(jù)集的模型訓(xùn)練中結(jié)合多種不同的圖像增強(qiáng)方法。它會(huì)提高測(cè)試準(zhǔn)確性嗎?
參考深度學(xué)習(xí)框架的在線文檔。它還提供了哪些其他圖像增強(qiáng)方法?
-
圖像增強(qiáng)
+關(guān)注
關(guān)注
0文章
54瀏覽量
10026 -
pytorch
+關(guān)注
關(guān)注
2文章
803瀏覽量
13152
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論