{
"cells": [
{
"cell_type": "markdown",
"id": "27513e5c",
"metadata": {
"origin_pos": 0
},
"source": [
"# 图像增广\n",
":label:`sec_image_augmentation`\n",
"\n",
" :numref:`sec_alexnet`提到过大型数据集是成功应用深度神经网络的先决条件。\n",
"图像增广在对训练图像进行一系列的随机变化之后,生成相似但不同的训练样本,从而扩大了训练集的规模。\n",
"此外,应用图像增广的原因是,随机改变训练样本可以减少模型对某些属性的依赖,从而提高模型的泛化能力。\n",
"例如,我们可以以不同的方式裁剪图像,使感兴趣的对象出现在不同的位置,减少模型对于对象出现位置的依赖。\n",
"我们还可以调整亮度、颜色等因素来降低模型对颜色的敏感度。\n",
"可以说,图像增广技术对于AlexNet的成功是必不可少的。本节将讨论这项广泛应用于计算机视觉的技术。\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "0227f8b8",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:01.202303Z",
"iopub.status.busy": "2023-08-18T07:09:01.202034Z",
"iopub.status.idle": "2023-08-18T07:09:03.210342Z",
"shell.execute_reply": "2023-08-18T07:09:03.209427Z"
},
"origin_pos": 2,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import torch\n",
"import torchvision\n",
"from torch import nn\n",
"from d2l import torch as d2l"
]
},
{
"cell_type": "markdown",
"id": "25366201",
"metadata": {
"origin_pos": 4
},
"source": [
"## 常用的图像增广方法\n",
"\n",
"在对常用图像增广方法的探索时,我们将使用下面这个尺寸为$400\\times 500$的图像作为示例。\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e4eb15f9",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:03.279539Z",
"iopub.status.busy": "2023-08-18T07:09:03.278648Z",
"iopub.status.idle": "2023-08-18T07:09:03.462850Z",
"shell.execute_reply": "2023-08-18T07:09:03.461996Z"
},
"origin_pos": 6,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"d2l.set_figsize()\n",
"img = d2l.Image.open('../img/cat1.jpg')\n",
"d2l.plt.imshow(img);"
]
},
{
"cell_type": "markdown",
"id": "8f9958f0",
"metadata": {
"origin_pos": 7
},
"source": [
"大多数图像增广方法都具有一定的随机性。为了便于观察图像增广的效果,我们下面定义辅助函数`apply`。\n",
"此函数在输入图像`img`上多次运行图像增广方法`aug`并显示所有结果。\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "94a4deb4",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:03.467453Z",
"iopub.status.busy": "2023-08-18T07:09:03.466794Z",
"iopub.status.idle": "2023-08-18T07:09:03.471847Z",
"shell.execute_reply": "2023-08-18T07:09:03.471048Z"
},
"origin_pos": 8,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"def apply(img, aug, num_rows=2, num_cols=4, scale=1.5):\n",
" Y = [aug(img) for _ in range(num_rows * num_cols)]\n",
" d2l.show_images(Y, num_rows, num_cols, scale=scale)"
]
},
{
"cell_type": "markdown",
"id": "f053987e",
"metadata": {
"origin_pos": 9
},
"source": [
"### 翻转和裁剪\n",
"\n",
"[**左右翻转图像**]通常不会改变对象的类别。这是最早且最广泛使用的图像增广方法之一。\n",
"接下来,我们使用`transforms`模块来创建`RandomFlipLeftRight`实例,这样就各有50%的几率使图像向左或向右翻转。\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b3e9e785",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:03.475017Z",
"iopub.status.busy": "2023-08-18T07:09:03.474731Z",
"iopub.status.idle": "2023-08-18T07:09:04.001353Z",
"shell.execute_reply": "2023-08-18T07:09:04.000464Z"
},
"origin_pos": 11,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"apply(img, torchvision.transforms.RandomHorizontalFlip())"
]
},
{
"cell_type": "markdown",
"id": "0ba3f1b5",
"metadata": {
"origin_pos": 13
},
"source": [
"[**上下翻转图像**]不如左右图像翻转那样常用。但是,至少对于这个示例图像,上下翻转不会妨碍识别。接下来,我们创建一个`RandomFlipTopBottom`实例,使图像各有50%的几率向上或向下翻转。\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e7899eb1",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:04.005369Z",
"iopub.status.busy": "2023-08-18T07:09:04.004737Z",
"iopub.status.idle": "2023-08-18T07:09:04.594173Z",
"shell.execute_reply": "2023-08-18T07:09:04.593347Z"
},
"origin_pos": 15,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"apply(img, torchvision.transforms.RandomVerticalFlip())"
]
},
{
"cell_type": "markdown",
"id": "00ccc140",
"metadata": {
"origin_pos": 17
},
"source": [
"在我们使用的示例图像中,猫位于图像的中间,但并非所有图像都是这样。\n",
"在 :numref:`sec_pooling`中,我们解释了汇聚层可以降低卷积层对目标位置的敏感性。\n",
"另外,我们可以通过对图像进行随机裁剪,使物体以不同的比例出现在图像的不同位置。\n",
"这也可以降低模型对目标位置的敏感性。\n",
"\n",
"下面的代码将[**随机裁剪**]一个面积为原始面积10%到100%的区域,该区域的宽高比从0.5~2之间随机取值。\n",
"然后,区域的宽度和高度都被缩放到200像素。\n",
"在本节中(除非另有说明),$a$和$b$之间的随机数指的是在区间$[a, b]$中通过均匀采样获得的连续值。\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7c50f6a4",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:04.598134Z",
"iopub.status.busy": "2023-08-18T07:09:04.597541Z",
"iopub.status.idle": "2023-08-18T07:09:04.957843Z",
"shell.execute_reply": "2023-08-18T07:09:04.956957Z"
},
"origin_pos": 19,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"shape_aug = torchvision.transforms.RandomResizedCrop(\n",
" (200, 200), scale=(0.1, 1), ratio=(0.5, 2))\n",
"apply(img, shape_aug)"
]
},
{
"cell_type": "markdown",
"id": "ae160672",
"metadata": {
"origin_pos": 21
},
"source": [
"### 改变颜色\n",
"\n",
"另一种增广方法是改变颜色。\n",
"我们可以改变图像颜色的四个方面:亮度、对比度、饱和度和色调。\n",
"在下面的示例中,我们[**随机更改图像的亮度**],随机值为原始图像的50%($1-0.5$)到150%($1+0.5$)之间。\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f38c67b5",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:04.961969Z",
"iopub.status.busy": "2023-08-18T07:09:04.961381Z",
"iopub.status.idle": "2023-08-18T07:09:05.497682Z",
"shell.execute_reply": "2023-08-18T07:09:05.496805Z"
},
"origin_pos": 23,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"apply(img, torchvision.transforms.ColorJitter(\n",
" brightness=0.5, contrast=0, saturation=0, hue=0))"
]
},
{
"cell_type": "markdown",
"id": "bead8ed4",
"metadata": {
"origin_pos": 25
},
"source": [
"同样,我们可以[**随机更改图像的色调**]。\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "72d28919",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:05.501452Z",
"iopub.status.busy": "2023-08-18T07:09:05.500816Z",
"iopub.status.idle": "2023-08-18T07:09:06.125837Z",
"shell.execute_reply": "2023-08-18T07:09:06.124959Z"
},
"origin_pos": 27,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"apply(img, torchvision.transforms.ColorJitter(\n",
" brightness=0, contrast=0, saturation=0, hue=0.5))"
]
},
{
"cell_type": "markdown",
"id": "de49c0fc",
"metadata": {
"origin_pos": 29
},
"source": [
"我们还可以创建一个`RandomColorJitter`实例,并设置如何同时[**随机更改图像的亮度(`brightness`)、对比度(`contrast`)、饱和度(`saturation`)和色调(`hue`)**]。\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "8f62c430",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:06.129806Z",
"iopub.status.busy": "2023-08-18T07:09:06.129220Z",
"iopub.status.idle": "2023-08-18T07:09:06.890747Z",
"shell.execute_reply": "2023-08-18T07:09:06.889906Z"
},
"origin_pos": 31,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"color_aug = torchvision.transforms.ColorJitter(\n",
" brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)\n",
"apply(img, color_aug)"
]
},
{
"cell_type": "markdown",
"id": "da1a57e0",
"metadata": {
"origin_pos": 33
},
"source": [
"### [**结合多种图像增广方法**]\n",
"\n",
"在实践中,我们将结合多种图像增广方法。比如,我们可以通过使用一个`Compose`实例来综合上面定义的不同的图像增广方法,并将它们应用到每个图像。\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "2b060294",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:06.894513Z",
"iopub.status.busy": "2023-08-18T07:09:06.893920Z",
"iopub.status.idle": "2023-08-18T07:09:07.407841Z",
"shell.execute_reply": "2023-08-18T07:09:07.406996Z"
},
"origin_pos": 35,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"augs = torchvision.transforms.Compose([\n",
" torchvision.transforms.RandomHorizontalFlip(), color_aug, shape_aug])\n",
"apply(img, augs)"
]
},
{
"cell_type": "markdown",
"id": "7ad3846c",
"metadata": {
"origin_pos": 37
},
"source": [
"## [**使用图像增广进行训练**]\n",
"\n",
"让我们使用图像增广来训练模型。\n",
"这里,我们使用CIFAR-10数据集,而不是我们之前使用的Fashion-MNIST数据集。\n",
"这是因为Fashion-MNIST数据集中对象的位置和大小已被规范化,而CIFAR-10数据集中对象的颜色和大小差异更明显。\n",
"CIFAR-10数据集中的前32个训练图像如下所示。\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8a539625",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:09:07.412048Z",
"iopub.status.busy": "2023-08-18T07:09:07.411327Z",
"iopub.status.idle": "2023-08-18T07:12:53.687290Z",
"shell.execute_reply": "2023-08-18T07:12:53.686473Z"
},
"origin_pos": 39,
"tab": [
"pytorch"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ../data/cifar-10-python.tar.gz\n"
]
},
{
"data": {
"application/json": {
"ascii": false,
"bar_format": null,
"colour": null,
"elapsed": 0.007704257965087891,
"initial": 0,
"n": 0,
"ncols": null,
"nrows": null,
"postfix": null,
"prefix": "",
"rate": null,
"total": 170498071,
"unit": "it",
"unit_divisor": 1000,
"unit_scale": false
},
"application/vnd.jupyter.widget-view+json": {
"model_id": "70cfad15aa4b4a2bb6c24a3f6facf105",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/170498071 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Extracting ../data/cifar-10-python.tar.gz to ../data\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"all_images = torchvision.datasets.CIFAR10(train=True, root=\"../data\",\n",
" download=True)\n",
"d2l.show_images([all_images[i][0] for i in range(32)], 4, 8, scale=0.8);"
]
},
{
"cell_type": "markdown",
"id": "efa3598c",
"metadata": {
"origin_pos": 41
},
"source": [
"为了在预测过程中得到确切的结果,我们通常对训练样本只进行图像增广,且在预测过程中不使用随机操作的图像增广。\n",
"在这里,我们[**只使用最简单的随机左右翻转**]。\n",
"此外,我们使用`ToTensor`实例将一批图像转换为深度学习框架所要求的格式,即形状为(批量大小,通道数,高度,宽度)的32位浮点数,取值范围为0~1。\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "afeeb232",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:12:53.692393Z",
"iopub.status.busy": "2023-08-18T07:12:53.691826Z",
"iopub.status.idle": "2023-08-18T07:12:53.696255Z",
"shell.execute_reply": "2023-08-18T07:12:53.695504Z"
},
"origin_pos": 43,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"train_augs = torchvision.transforms.Compose([\n",
" torchvision.transforms.RandomHorizontalFlip(),\n",
" torchvision.transforms.ToTensor()])\n",
"\n",
"test_augs = torchvision.transforms.Compose([\n",
" torchvision.transforms.ToTensor()])"
]
},
{
"cell_type": "markdown",
"id": "272f6e78",
"metadata": {
"origin_pos": 46,
"tab": [
"pytorch"
]
},
"source": [
"接下来,我们[**定义一个辅助函数,以便于读取图像和应用图像增广**]。PyTorch数据集提供的`transform`参数应用图像增广来转化图像。有关`DataLoader`的详细介绍,请参阅 :numref:`sec_fashion_mnist`。\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e78239ec",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:12:53.699803Z",
"iopub.status.busy": "2023-08-18T07:12:53.699195Z",
"iopub.status.idle": "2023-08-18T07:12:53.703723Z",
"shell.execute_reply": "2023-08-18T07:12:53.702970Z"
},
"origin_pos": 48,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"def load_cifar10(is_train, augs, batch_size):\n",
" dataset = torchvision.datasets.CIFAR10(root=\"../data\", train=is_train,\n",
" transform=augs, download=True)\n",
" dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,\n",
" shuffle=is_train, num_workers=d2l.get_dataloader_workers())\n",
" return dataloader"
]
},
{
"cell_type": "markdown",
"id": "4ddf567c",
"metadata": {
"origin_pos": 50
},
"source": [
"### 多GPU训练\n",
"\n",
"我们在CIFAR-10数据集上训练 :numref:`sec_resnet`中的ResNet-18模型。\n",
"回想一下 :numref:`sec_multi_gpu_concise`中对多GPU训练的介绍。\n",
"接下来,我们[**定义一个函数,使用多GPU对模型进行训练和评估**]。\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "bcf2640f",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:12:53.707111Z",
"iopub.status.busy": "2023-08-18T07:12:53.706612Z",
"iopub.status.idle": "2023-08-18T07:12:53.712395Z",
"shell.execute_reply": "2023-08-18T07:12:53.711671Z"
},
"origin_pos": 52,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"#@save\n",
"def train_batch_ch13(net, X, y, loss, trainer, devices):\n",
" \"\"\"用多GPU进行小批量训练\"\"\"\n",
" if isinstance(X, list):\n",
" # 微调BERT中所需\n",
" X = [x.to(devices[0]) for x in X]\n",
" else:\n",
" X = X.to(devices[0])\n",
" y = y.to(devices[0])\n",
" net.train()\n",
" trainer.zero_grad()\n",
" pred = net(X)\n",
" l = loss(pred, y)\n",
" l.sum().backward()\n",
" trainer.step()\n",
" train_loss_sum = l.sum()\n",
" train_acc_sum = d2l.accuracy(pred, y)\n",
" return train_loss_sum, train_acc_sum"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "94b378e3",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:12:53.715853Z",
"iopub.status.busy": "2023-08-18T07:12:53.715212Z",
"iopub.status.idle": "2023-08-18T07:12:53.723420Z",
"shell.execute_reply": "2023-08-18T07:12:53.722651Z"
},
"origin_pos": 55,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"#@save\n",
"def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,\n",
" devices=d2l.try_all_gpus()):\n",
" \"\"\"用多GPU进行模型训练\"\"\"\n",
" timer, num_batches = d2l.Timer(), len(train_iter)\n",
" animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1],\n",
" legend=['train loss', 'train acc', 'test acc'])\n",
" net = nn.DataParallel(net, device_ids=devices).to(devices[0])\n",
" for epoch in range(num_epochs):\n",
" # 4个维度:储存训练损失,训练准确度,实例数,特点数\n",
" metric = d2l.Accumulator(4)\n",
" for i, (features, labels) in enumerate(train_iter):\n",
" timer.start()\n",
" l, acc = train_batch_ch13(\n",
" net, features, labels, loss, trainer, devices)\n",
" metric.add(l, acc, labels.shape[0], labels.numel())\n",
" timer.stop()\n",
" if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:\n",
" animator.add(epoch + (i + 1) / num_batches,\n",
" (metric[0] / metric[2], metric[1] / metric[3],\n",
" None))\n",
" test_acc = d2l.evaluate_accuracy_gpu(net, test_iter)\n",
" animator.add(epoch + 1, (None, None, test_acc))\n",
" print(f'loss {metric[0] / metric[2]:.3f}, train acc '\n",
" f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}')\n",
" print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on '\n",
" f'{str(devices)}')"
]
},
{
"cell_type": "markdown",
"id": "76c2f527",
"metadata": {
"origin_pos": 57
},
"source": [
"现在,我们可以[**定义`train_with_data_aug`函数,使用图像增广来训练模型**]。该函数获取所有的GPU,并使用Adam作为训练的优化算法,将图像增广应用于训练集,最后调用刚刚定义的用于训练和评估模型的`train_ch13`函数。\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "3b314880",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:12:53.726893Z",
"iopub.status.busy": "2023-08-18T07:12:53.726383Z",
"iopub.status.idle": "2023-08-18T07:12:53.922963Z",
"shell.execute_reply": "2023-08-18T07:12:53.922077Z"
},
"origin_pos": 59,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10, 3)\n",
"\n",
"def init_weights(m):\n",
" if type(m) in [nn.Linear, nn.Conv2d]:\n",
" nn.init.xavier_uniform_(m.weight)\n",
"\n",
"net.apply(init_weights)\n",
"\n",
"def train_with_data_aug(train_augs, test_augs, net, lr=0.001):\n",
" train_iter = load_cifar10(True, train_augs, batch_size)\n",
" test_iter = load_cifar10(False, test_augs, batch_size)\n",
" loss = nn.CrossEntropyLoss(reduction=\"none\")\n",
" trainer = torch.optim.Adam(net.parameters(), lr=lr)\n",
" train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices)"
]
},
{
"cell_type": "markdown",
"id": "83461623",
"metadata": {
"origin_pos": 61
},
"source": [
"让我们使用基于随机左右翻转的图像增广来[**训练模型**]。\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "00b33b59",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T07:12:53.927203Z",
"iopub.status.busy": "2023-08-18T07:12:53.926516Z",
"iopub.status.idle": "2023-08-18T07:15:26.031335Z",
"shell.execute_reply": "2023-08-18T07:15:26.030442Z"
},
"origin_pos": 62,
"tab": [
"pytorch"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss 0.173, train acc 0.941, test acc 0.854\n",
"4183.9 examples/sec on [device(type='cuda', index=0), device(type='cuda', index=1)]\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"train_with_data_aug(train_augs, test_augs, net)"
]
},
{
"cell_type": "markdown",
"id": "5a6fb3b7",
"metadata": {
"origin_pos": 63
},
"source": [
"## 小结\n",
"\n",
"* 图像增广基于现有的训练数据生成随机图像,来提高模型的泛化能力。\n",
"* 为了在预测过程中得到确切的结果,我们通常对训练样本只进行图像增广,而在预测过程中不使用带随机操作的图像增广。\n",
"* 深度学习框架提供了许多不同的图像增广方法,这些方法可以被同时应用。\n",
"\n",
"## 练习\n",
"\n",
"1. 在不使用图像增广的情况下训练模型:`train_with_data_aug(no_aug, no_aug)`。比较使用和不使用图像增广的训练结果和测试精度。这个对比实验能支持图像增广可以减轻过拟合的论点吗?为什么?\n",
"2. 在基于CIFAR-10数据集的模型训练中结合多种不同的图像增广方法。它能提高测试准确性吗?\n",
"3. 参阅深度学习框架的在线文档。它还提供了哪些其他的图像增广方法?\n"
]
},
{
"cell_type": "markdown",
"id": "1f1b51a5",
"metadata": {
"origin_pos": 65,
"tab": [
"pytorch"
]
},
"source": [
"[Discussions](https://discuss.d2l.ai/t/2829)\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"required_libs": [],
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {
"08f763cf49924997a87a5a46a9788c1e": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HTMLModel",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "2.0.0",
"_view_name": "HTMLView",
"description": "",
"description_allow_html": false,
"layout": "IPY_MODEL_8d1a6775399f4ef9a26c02a6231b0ecd",
"placeholder": "",
"style": "IPY_MODEL_ca199929c0e34daebf811dad645e5009",
"tabbable": null,
"tooltip": null,
"value": " 170498071/170498071 [03:41<00:00, 851471.98it/s]"
}
},
"4972deebfb2a4d2a9734e2ab01c66ba2": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HTMLStyleModel",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HTMLStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "StyleView",
"background": null,
"description_width": "",
"font_size": null,
"text_color": null
}
},
"4f28a20259ec4915a088d3d14a65e682": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "ProgressStyleModel",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "ProgressStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "StyleView",
"bar_color": null,
"description_width": ""
}
},
"6a733089d6d2498c9a6a8ed1ef27d1cc": {
"model_module": "@jupyter-widgets/base",
"model_module_version": "2.0.0",
"model_name": "LayoutModel",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "2.0.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border_bottom": null,
"border_left": null,
"border_right": null,
"border_top": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"70cfad15aa4b4a2bb6c24a3f6facf105": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HBoxModel",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HBoxModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "2.0.0",
"_view_name": "HBoxView",
"box_style": "",
"children": [
"IPY_MODEL_7322129003934c93aaf4ea7ddc8ce2c4",
"IPY_MODEL_f77582f12b764879bcc8e55b0ecaa4be",
"IPY_MODEL_08f763cf49924997a87a5a46a9788c1e"
],
"layout": "IPY_MODEL_6a733089d6d2498c9a6a8ed1ef27d1cc",
"tabbable": null,
"tooltip": null
}
},
"7322129003934c93aaf4ea7ddc8ce2c4": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HTMLModel",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "2.0.0",
"_view_name": "HTMLView",
"description": "",
"description_allow_html": false,
"layout": "IPY_MODEL_f47e956000474e719b987aa23303756b",
"placeholder": "",
"style": "IPY_MODEL_4972deebfb2a4d2a9734e2ab01c66ba2",
"tabbable": null,
"tooltip": null,
"value": "100%"
}
},
"8d1a6775399f4ef9a26c02a6231b0ecd": {
"model_module": "@jupyter-widgets/base",
"model_module_version": "2.0.0",
"model_name": "LayoutModel",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "2.0.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border_bottom": null,
"border_left": null,
"border_right": null,
"border_top": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"ca199929c0e34daebf811dad645e5009": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HTMLStyleModel",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HTMLStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "StyleView",
"background": null,
"description_width": "",
"font_size": null,
"text_color": null
}
},
"f47e956000474e719b987aa23303756b": {
"model_module": "@jupyter-widgets/base",
"model_module_version": "2.0.0",
"model_name": "LayoutModel",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "2.0.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border_bottom": null,
"border_left": null,
"border_right": null,
"border_top": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"f6647d383884470e91ddd971c9335e6f": {
"model_module": "@jupyter-widgets/base",
"model_module_version": "2.0.0",
"model_name": "LayoutModel",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "2.0.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border_bottom": null,
"border_left": null,
"border_right": null,
"border_top": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"f77582f12b764879bcc8e55b0ecaa4be": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "FloatProgressModel",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "FloatProgressModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "2.0.0",
"_view_name": "ProgressView",
"bar_style": "success",
"description": "",
"description_allow_html": false,
"layout": "IPY_MODEL_f6647d383884470e91ddd971c9335e6f",
"max": 170498071.0,
"min": 0.0,
"orientation": "horizontal",
"style": "IPY_MODEL_4f28a20259ec4915a088d3d14a65e682",
"tabbable": null,
"tooltip": null,
"value": 170498071.0
}
}
},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}