.. _sec_image_augmentation: 图像增广 ======== :numref:`sec_alexnet`\ 提到过大型数据集是成功应用深度神经网络的先决条件。 图像增广在对训练图像进行一系列的随机变化之后,生成相似但不同的训练样本,从而扩大了训练集的规模。 此外,应用图像增广的原因是,随机改变训练样本可以减少模型对某些属性的依赖,从而提高模型的泛化能力。 例如,我们可以以不同的方式裁剪图像,使感兴趣的对象出现在不同的位置,减少模型对于对象出现位置的依赖。 我们还可以调整亮度、颜色等因素来降低模型对颜色的敏感度。 可以说,图像增广技术对于AlexNet的成功是必不可少的。本节将讨论这项广泛应用于计算机视觉的技术。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %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() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline import torch import torchvision from torch import nn from d2l import torch as d2l .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline import warnings from d2l import paddle as d2l warnings.filterwarnings("ignore") import paddle import paddle.vision as paddlevision from paddle import nn .. raw:: html
.. raw:: html
常用的图像增广方法 ------------------ 在对常用图像增广方法的探索时,我们将使用下面这个尺寸为\ :math:`400\times 500`\ 的图像作为示例。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python d2l.set_figsize() img = image.imread('../img/cat1.jpg') d2l.plt.imshow(img.asnumpy()); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [07:07:52] ../src/storage/storage.cc:196: Using Pooled (Naive) StorageManager for CPU .. figure:: output_image-augmentation_7d0887_15_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python d2l.set_figsize() img = d2l.Image.open('../img/cat1.jpg') d2l.plt.imshow(img); .. figure:: output_image-augmentation_7d0887_18_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python d2l.set_figsize() img = d2l.Image.open('../img/cat1.jpg') d2l.plt.imshow(img); .. figure:: output_image-augmentation_7d0887_21_0.svg .. raw:: html
.. raw:: html
大多数图像增广方法都具有一定的随机性。为了便于观察图像增广的效果,我们下面定义辅助函数\ ``apply``\ 。 此函数在输入图像\ ``img``\ 上多次运行图像增广方法\ ``aug``\ 并显示所有结果。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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) .. raw:: html
.. raw:: html
翻转和裁剪 ~~~~~~~~~~ 左右翻转图像通常不会改变对象的类别。这是最早且最广泛使用的图像增广方法之一。 接下来,我们使用\ ``transforms``\ 模块来创建\ ``RandomFlipLeftRight``\ 实例,这样就各有50%的几率使图像向左或向右翻转。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, gluon.data.vision.transforms.RandomFlipLeftRight()) .. figure:: output_image-augmentation_7d0887_39_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, torchvision.transforms.RandomHorizontalFlip()) .. figure:: output_image-augmentation_7d0887_42_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, paddlevision.transforms.RandomHorizontalFlip()) .. figure:: output_image-augmentation_7d0887_45_0.svg .. raw:: html
.. raw:: html
上下翻转图像不如左右图像翻转那样常用。但是,至少对于这个示例图像,上下翻转不会妨碍识别。接下来,我们创建一个\ ``RandomFlipTopBottom``\ 实例,使图像各有50%的几率向上或向下翻转。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, gluon.data.vision.transforms.RandomFlipTopBottom()) .. figure:: output_image-augmentation_7d0887_51_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, torchvision.transforms.RandomVerticalFlip()) .. figure:: output_image-augmentation_7d0887_54_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, paddlevision.transforms.RandomVerticalFlip()) .. figure:: output_image-augmentation_7d0887_57_0.svg .. raw:: html
.. raw:: html
在我们使用的示例图像中,猫位于图像的中间,但并非所有图像都是这样。 在 :numref:`sec_pooling`\ 中,我们解释了汇聚层可以降低卷积层对目标位置的敏感性。 另外,我们可以通过对图像进行随机裁剪,使物体以不同的比例出现在图像的不同位置。 这也可以降低模型对目标位置的敏感性。 下面的代码将随机裁剪一个面积为原始面积10%到100%的区域,该区域的宽高比从0.5~2之间随机取值。 然后,区域的宽度和高度都被缩放到200像素。 在本节中(除非另有说明),\ :math:`a`\ 和\ :math:`b`\ 之间的随机数指的是在区间\ :math:`[a, b]`\ 中通过均匀采样获得的连续值。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python shape_aug = gluon.data.vision.transforms.RandomResizedCrop( (200, 200), scale=(0.1, 1), ratio=(0.5, 2)) apply(img, shape_aug) .. figure:: output_image-augmentation_7d0887_63_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python shape_aug = torchvision.transforms.RandomResizedCrop( (200, 200), scale=(0.1, 1), ratio=(0.5, 2)) apply(img, shape_aug) .. figure:: output_image-augmentation_7d0887_66_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python shape_aug = paddlevision.transforms.RandomResizedCrop( (200, 200), scale=(0.1, 1), ratio=(0.5, 2)) apply(img, shape_aug) .. figure:: output_image-augmentation_7d0887_69_0.svg .. raw:: html
.. raw:: html
改变颜色 ~~~~~~~~ 另一种增广方法是改变颜色。 我们可以改变图像颜色的四个方面:亮度、对比度、饱和度和色调。 在下面的示例中,我们随机更改图像的亮度,随机值为原始图像的50%(\ :math:`1-0.5`\ )到150%(\ :math:`1+0.5`\ )之间。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, gluon.data.vision.transforms.RandomBrightness(0.5)) .. figure:: output_image-augmentation_7d0887_75_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, torchvision.transforms.ColorJitter( brightness=0.5, contrast=0, saturation=0, hue=0)) .. figure:: output_image-augmentation_7d0887_78_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, paddlevision.transforms.ColorJitter( brightness=0.5, contrast=0, saturation=0, hue=0)) .. figure:: output_image-augmentation_7d0887_81_0.svg .. raw:: html
.. raw:: html
同样,我们可以随机更改图像的色调。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, gluon.data.vision.transforms.RandomHue(0.5)) .. figure:: output_image-augmentation_7d0887_87_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, torchvision.transforms.ColorJitter( brightness=0, contrast=0, saturation=0, hue=0.5)) .. figure:: output_image-augmentation_7d0887_90_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python apply(img, paddlevision.transforms.ColorJitter( brightness=0, contrast=0, saturation=0, hue=0.5)) .. figure:: output_image-augmentation_7d0887_93_0.svg .. raw:: html
.. raw:: html
我们还可以创建一个\ ``RandomColorJitter``\ 实例,并设置如何同时随机更改图像的亮度(\ ``brightness``\ )、对比度(\ ``contrast``\ )、饱和度(\ ``saturation``\ )和色调(\ ``hue``\ )。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python color_aug = gluon.data.vision.transforms.RandomColorJitter( brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5) apply(img, color_aug) .. figure:: output_image-augmentation_7d0887_99_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python color_aug = torchvision.transforms.ColorJitter( brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5) apply(img, color_aug) .. figure:: output_image-augmentation_7d0887_102_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python color_aug = paddlevision.transforms.ColorJitter( brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5) apply(img, color_aug) .. figure:: output_image-augmentation_7d0887_105_0.svg .. raw:: html
.. raw:: html
结合多种图像增广方法 ~~~~~~~~~~~~~~~~~~~~ 在实践中,我们将结合多种图像增广方法。比如,我们可以通过使用一个\ ``Compose``\ 实例来综合上面定义的不同的图像增广方法,并将它们应用到每个图像。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python augs = gluon.data.vision.transforms.Compose([ gluon.data.vision.transforms.RandomFlipLeftRight(), color_aug, shape_aug]) apply(img, augs) .. figure:: output_image-augmentation_7d0887_111_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python augs = torchvision.transforms.Compose([ torchvision.transforms.RandomHorizontalFlip(), color_aug, shape_aug]) apply(img, augs) .. figure:: output_image-augmentation_7d0887_114_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python augs = paddlevision.transforms.Compose([ paddle.vision.transforms.RandomHorizontalFlip(), color_aug, shape_aug]) apply(img, augs) .. figure:: output_image-augmentation_7d0887_117_0.svg .. raw:: html
.. raw:: html
使用图像增广进行训练 -------------------- 让我们使用图像增广来训练模型。 这里,我们使用CIFAR-10数据集,而不是我们之前使用的Fashion-MNIST数据集。 这是因为Fashion-MNIST数据集中对象的位置和大小已被规范化,而CIFAR-10数据集中对象的颜色和大小差异更明显。 CIFAR-10数据集中的前32个训练图像如下所示。 .. raw:: html
mxnetpytorchpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python d2l.show_images(gluon.data.vision.CIFAR10( train=True)[0:32][0], 4, 8, scale=0.8); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Downloading /opt/mxnet/datasets/cifar10/cifar-10-binary.tar.gz from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/cifar10/cifar-10-binary.tar.gz... .. figure:: output_image-augmentation_7d0887_123_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ../data/cifar-10-python.tar.gz .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output 0%| | 0/170498071 [00:00 .. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python all_images = paddlevision.datasets.Cifar10(mode='train' , download=True) print(len(all_images)) d2l.show_images([all_images[i][0] for i in range(32)], 4, 8, scale=0.8); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Cache file /home/ci/.cache/paddle/dataset/cifar/cifar-10-python.tar.gz not found, downloading https://dataset.bj.bcebos.com/cifar/cifar-10-python.tar.gz Begin to download item 41626/41626 [============================>.] - ETA: 0s - 1ms/item Download finished 50000 .. figure:: output_image-augmentation_7d0887_129_1.svg .. raw:: html
.. raw:: html
为了在预测过程中得到确切的结果,我们通常对训练样本只进行图像增广,且在预测过程中不使用随机操作的图像增广。 在这里,我们只使用最简单的随机左右翻转。 此外,我们使用\ ``ToTensor``\ 实例将一批图像转换为深度学习框架所要求的格式,即形状为(批量大小,通道数,高度,宽度)的32位浮点数,取值范围为0~1。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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()]) 接下来,我们定义了一个辅助函数,以便于读取图像和应用图像增广。Gluon数据集提供的\ ``transform_first``\ 函数将图像增广应用于每个训练样本的第一个元素(由图像和标签组成),即应用在图像上。有关\ ``DataLoader``\ 的详细介绍,请参阅 :numref:`sec_fashion_mnist`\ 。 .. raw:: latex \diilbookstyleinputcell .. code:: python 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()) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python train_augs = torchvision.transforms.Compose([ torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor()]) test_augs = torchvision.transforms.Compose([ torchvision.transforms.ToTensor()]) 接下来,我们定义一个辅助函数,以便于读取图像和应用图像增广。PyTorch数据集提供的\ ``transform``\ 参数应用图像增广来转化图像。有关\ ``DataLoader``\ 的详细介绍,请参阅 :numref:`sec_fashion_mnist`\ 。 .. raw:: latex \diilbookstyleinputcell .. code:: python 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 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python train_augs = paddlevision.transforms.Compose([ paddlevision.transforms.RandomHorizontalFlip(), paddlevision.transforms.ToTensor()]) test_augs = paddlevision.transforms.Compose([ paddlevision.transforms.ToTensor()]) def load_cifar10(is_train, augs, batch_size): dataset = paddlevision.datasets.Cifar10(mode="train", transform=augs, download=True) dataloader = paddle.io.DataLoader(dataset, batch_size=batch_size, num_workers=d2l.get_dataloader_workers(), shuffle=is_train) return dataloader .. raw:: html
.. raw:: html
多GPU训练 ~~~~~~~~~ 我们在CIFAR-10数据集上训练 :numref:`sec_resnet`\ 中的ResNet-18模型。 回想一下 :numref:`sec_multi_gpu_concise`\ 中对多GPU训练的介绍。 接下来,我们定义一个函数,使用多GPU对模型进行训练和评估。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def train_batch_ch13(net, features, labels, loss, trainer, devices, split_f=d2l.split_batch): """用多GPU进行小批量训练""" 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() # True标志允许使用过时的梯度,这很有用(例如,在微调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): """用多GPU进行模型训练""" 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): # 4个维度:储存训练损失,训练准确度,实例数,特点数 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)}') .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def train_batch_ch13(net, X, y, loss, trainer, devices): """用多GPU进行小批量训练""" if isinstance(X, list): # 微调BERT中所需 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()): """用多GPU进行模型训练""" 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): # 4个维度:储存训练损失,训练准确度,实例数,特点数 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)}') .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def train_batch_ch13(net, X, y, loss, trainer, devices): """用多GPU进行小批量训练 飞桨不支持在notebook上进行多GPU训练 Defined in :numref:`sec_image_augmentation`""" if isinstance(X, list): # 微调BERT中所需(稍后讨论) X = [paddle.to_tensor(x, place=devices[0]) for x in X] else: X = paddle.to_tensor(X, place=devices[0]) y = paddle.to_tensor(y, place=devices[0]) net.train() trainer.clear_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()): """用多GPU进行模型训练 Defined in :numref:`sec_image_augmentation`""" 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 = paddle.DataParallel(net) for epoch in range(num_epochs): # 4个维度:储存训练损失,训练准确度,实例数,特点数 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)}') .. raw:: html
.. raw:: html
现在,我们可以定义\ ``train_with_data_aug``\ 函数,使用图像增广来训练模型。该函数获取所有的GPU,并使用Adam作为训练的优化算法,将图像增广应用于训练集,最后调用刚刚定义的用于训练和评估模型的\ ``train_ch13``\ 函数。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [07:08:22] ../src/storage/storage.cc:196: Using Pooled (Naive) StorageManager for GPU [07:08:22] ../src/storage/storage.cc:196: Using Pooled (Naive) StorageManager for GPU .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10, 3) def init_weights(m): if type(m) in [nn.Linear, nn.Conv2d]: nn.init.xavier_uniform_(m.weight) net.apply(init_weights) 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) train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10, 3) def init_weights(m): if type(m) in [nn.Linear, nn.Conv2D]: nn.initializer.XavierUniform(m.weight) net.apply(init_weights) 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 = paddle.optimizer.Adam(learning_rate=lr, parameters=net.parameters()) train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices[:1]) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output W0818 09:18:14.902511 73688 gpu_resources.cc:61] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.8, Runtime API Version: 11.8 W0818 09:18:14.932250 73688 gpu_resources.cc:91] device: 0, cuDNN Version: 8.7. .. raw:: html
.. raw:: html
让我们使用基于随机左右翻转的图像增广来训练模型。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python train_with_data_aug(train_augs, test_augs, net) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss 0.173, train acc 0.940, test acc 0.855 2158.6 examples/sec on [gpu(0), gpu(1)] .. figure:: output_image-augmentation_7d0887_175_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python train_with_data_aug(train_augs, test_augs, net) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss 0.173, train acc 0.941, test acc 0.854 4183.9 examples/sec on [device(type='cuda', index=0), device(type='cuda', index=1)] .. figure:: output_image-augmentation_7d0887_178_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python train_with_data_aug(train_augs, test_augs, net) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss 0.166, train acc 0.944, test acc 0.936 3465.5 examples/sec on [Place(gpu:0)] .. figure:: output_image-augmentation_7d0887_181_1.svg .. raw:: html
.. raw:: html
小结 ---- - 图像增广基于现有的训练数据生成随机图像,来提高模型的泛化能力。 - 为了在预测过程中得到确切的结果,我们通常对训练样本只进行图像增广,而在预测过程中不使用带随机操作的图像增广。 - 深度学习框架提供了许多不同的图像增广方法,这些方法可以被同时应用。 练习 ---- 1. 在不使用图像增广的情况下训练模型:\ ``train_with_data_aug(no_aug, no_aug)``\ 。比较使用和不使用图像增广的训练结果和测试精度。这个对比实验能支持图像增广可以减轻过拟合的论点吗?为什么? 2. 在基于CIFAR-10数据集的模型训练中结合多种不同的图像增广方法。它能提高测试准确性吗? 3. 参阅深度学习框架的在线文档。它还提供了哪些其他的图像增广方法? .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html