{
"cells": [
{
"cell_type": "markdown",
"id": "3c358c3f",
"metadata": {
"origin_pos": 0
},
"source": [
"# Fully Convolutional Networks\n",
":label:`sec_fcn`\n",
"\n",
"As discussed in :numref:`sec_semantic_segmentation`,\n",
"semantic segmentation\n",
"classifies images in pixel level.\n",
"A fully convolutional network (FCN)\n",
"uses a convolutional neural network to\n",
"transform image pixels to pixel classes :cite:`Long.Shelhamer.Darrell.2015`.\n",
"Unlike the CNNs that we encountered earlier\n",
"for image classification \n",
"or object detection,\n",
"a fully convolutional network\n",
"transforms \n",
"the height and width of intermediate feature maps\n",
"back to those of the input image:\n",
"this is achieved by\n",
"the transposed convolutional layer\n",
"introduced in :numref:`sec_transposed_conv`.\n",
"As a result,\n",
"the classification output\n",
"and the input image \n",
"have a one-to-one correspondence \n",
"in pixel level:\n",
"the channel dimension at any output pixel \n",
"holds the classification results\n",
"for the input pixel at the same spatial position.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "805a6df5",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:22.296389Z",
"iopub.status.busy": "2023-08-18T19:34:22.296055Z",
"iopub.status.idle": "2023-08-18T19:34:25.836644Z",
"shell.execute_reply": "2023-08-18T19:34:25.835421Z"
},
"origin_pos": 2,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import torch\n",
"import torchvision\n",
"from torch import nn\n",
"from torch.nn import functional as F\n",
"from d2l import torch as d2l"
]
},
{
"cell_type": "markdown",
"id": "05a33edc",
"metadata": {
"origin_pos": 3
},
"source": [
"## The Model\n",
"\n",
"Here we describe the basic design of the fully convolutional network model. \n",
"As shown in :numref:`fig_fcn`,\n",
"this model first uses a CNN to extract image features,\n",
"then transforms the number of channels into\n",
"the number of classes\n",
"via a $1\\times 1$ convolutional layer,\n",
"and finally transforms the height and width of\n",
"the feature maps\n",
"to those\n",
"of the input image via\n",
"the transposed convolution introduced in :numref:`sec_transposed_conv`. \n",
"As a result,\n",
"the model output has the same height and width as the input image,\n",
"where the output channel contains the predicted classes\n",
"for the input pixel at the same spatial position.\n",
"\n",
"\n",
"\n",
":label:`fig_fcn`\n",
"\n",
"Below, we [**use a ResNet-18 model pretrained on the ImageNet dataset to extract image features**]\n",
"and denote the model instance as `pretrained_net`.\n",
"The last few layers of this model\n",
"include a global average pooling layer\n",
"and a fully connected layer:\n",
"they are not needed\n",
"in the fully convolutional network.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a2a9a1c6",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:25.842583Z",
"iopub.status.busy": "2023-08-18T19:34:25.841424Z",
"iopub.status.idle": "2023-08-18T19:34:27.191787Z",
"shell.execute_reply": "2023-08-18T19:34:27.190380Z"
},
"origin_pos": 5,
"tab": [
"pytorch"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading: \"https://download.pytorch.org/models/resnet18-f37072fd.pth\" to /home/ci/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
" 0%| | 0.00/44.7M [00:00, ?B/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
" 11%|█ | 4.98M/44.7M [00:00<00:00, 51.0MB/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
" 22%|██▏ | 9.86M/44.7M [00:00<00:00, 50.6MB/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
" 34%|███▍ | 15.2M/44.7M [00:00<00:00, 52.8MB/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
" 45%|████▌ | 20.3M/44.7M [00:00<00:00, 52.2MB/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
" 60%|██████ | 26.9M/44.7M [00:00<00:00, 58.2MB/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
" 74%|███████▍ | 33.2M/44.7M [00:00<00:00, 60.9MB/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
" 88%|████████▊ | 39.2M/44.7M [00:00<00:00, 61.5MB/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"100%|██████████| 44.7M/44.7M [00:00<00:00, 56.3MB/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"text/plain": [
"[Sequential(\n",
" (0): BasicBlock(\n",
" (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
" (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" (relu): ReLU(inplace=True)\n",
" (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
" (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" (downsample): Sequential(\n",
" (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
" (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" )\n",
" )\n",
" (1): BasicBlock(\n",
" (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
" (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" (relu): ReLU(inplace=True)\n",
" (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
" (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" )\n",
" ),\n",
" AdaptiveAvgPool2d(output_size=(1, 1)),\n",
" Linear(in_features=512, out_features=1000, bias=True)]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pretrained_net = torchvision.models.resnet18(pretrained=True)\n",
"list(pretrained_net.children())[-3:]"
]
},
{
"cell_type": "markdown",
"id": "ef9bca9e",
"metadata": {
"origin_pos": 6
},
"source": [
"Next, we [**create the fully convolutional network instance `net`**].\n",
"It copies all the pretrained layers in the ResNet-18\n",
"except for the final global average pooling layer\n",
"and the fully connected layer that are closest\n",
"to the output.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "be7ad27f",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:27.197675Z",
"iopub.status.busy": "2023-08-18T19:34:27.196065Z",
"iopub.status.idle": "2023-08-18T19:34:27.204322Z",
"shell.execute_reply": "2023-08-18T19:34:27.203107Z"
},
"origin_pos": 8,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"net = nn.Sequential(*list(pretrained_net.children())[:-2])"
]
},
{
"cell_type": "markdown",
"id": "446c3798",
"metadata": {
"origin_pos": 9
},
"source": [
"Given an input with height and width of 320 and 480 respectively,\n",
"the forward propagation of `net`\n",
"reduces the input height and width to 1/32 of the original, namely 10 and 15.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5bb828bc",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:27.208600Z",
"iopub.status.busy": "2023-08-18T19:34:27.207953Z",
"iopub.status.idle": "2023-08-18T19:34:27.301540Z",
"shell.execute_reply": "2023-08-18T19:34:27.300587Z"
},
"origin_pos": 11,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([1, 512, 10, 15])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = torch.rand(size=(1, 3, 320, 480))\n",
"net(X).shape"
]
},
{
"cell_type": "markdown",
"id": "f7c2da65",
"metadata": {
"origin_pos": 12
},
"source": [
"Next, we [**use a $1\\times 1$ convolutional layer to transform the number of output channels into the number of classes (21) of the Pascal VOC2012 dataset.**]\n",
"Finally, we need to (**increase the height and width of the feature maps by 32 times**) to change them back to the height and width of the input image. \n",
"Recall how to calculate \n",
"the output shape of a convolutional layer in :numref:`sec_padding`. \n",
"Since $(320-64+16\\times2+32)/32=10$ and $(480-64+16\\times2+32)/32=15$, we construct a transposed convolutional layer with stride of $32$, \n",
"setting\n",
"the height and width of the kernel\n",
"to $64$, the padding to $16$.\n",
"In general,\n",
"we can see that\n",
"for stride $s$,\n",
"padding $s/2$ (assuming $s/2$ is an integer),\n",
"and the height and width of the kernel $2s$, \n",
"the transposed convolution will increase\n",
"the height and width of the input by $s$ times.\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a6fefe2f",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:27.305909Z",
"iopub.status.busy": "2023-08-18T19:34:27.304968Z",
"iopub.status.idle": "2023-08-18T19:34:27.332225Z",
"shell.execute_reply": "2023-08-18T19:34:27.331098Z"
},
"origin_pos": 14,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"num_classes = 21\n",
"net.add_module('final_conv', nn.Conv2d(512, num_classes, kernel_size=1))\n",
"net.add_module('transpose_conv', nn.ConvTranspose2d(num_classes, num_classes,\n",
" kernel_size=64, padding=16, stride=32))"
]
},
{
"cell_type": "markdown",
"id": "9c402b62",
"metadata": {
"origin_pos": 15
},
"source": [
"## [**Initializing Transposed Convolutional Layers**]\n",
"\n",
"\n",
"We already know that\n",
"transposed convolutional layers can increase\n",
"the height and width of\n",
"feature maps.\n",
"In image processing, we may need to scale up\n",
"an image, i.e., *upsampling*.\n",
"*Bilinear interpolation*\n",
"is one of the commonly used upsampling techniques.\n",
"It is also often used for initializing transposed convolutional layers.\n",
"\n",
"To explain bilinear interpolation,\n",
"say that \n",
"given an input image\n",
"we want to \n",
"calculate each pixel \n",
"of the upsampled output image.\n",
"In order to calculate the pixel of the output image\n",
"at coordinate $(x, y)$, \n",
"first map $(x, y)$ to coordinate $(x', y')$ on the input image, for example, according to the ratio of the input size to the output size. \n",
"Note that the mapped $x'$ and $y'$ are real numbers. \n",
"Then, find the four pixels closest to coordinate\n",
"$(x', y')$ on the input image. \n",
"Finally, the pixel of the output image at coordinate $(x, y)$ is calculated based on these four closest pixels\n",
"on the input image and their relative distance from $(x', y')$. \n",
"\n",
"Upsampling of bilinear interpolation\n",
"can be implemented by the transposed convolutional layer \n",
"with the kernel constructed by the following `bilinear_kernel` function. \n",
"Due to space limitations, we only provide the implementation of the `bilinear_kernel` function below\n",
"without discussions on its algorithm design.\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8acfd5fc",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:27.337253Z",
"iopub.status.busy": "2023-08-18T19:34:27.336271Z",
"iopub.status.idle": "2023-08-18T19:34:27.347187Z",
"shell.execute_reply": "2023-08-18T19:34:27.346126Z"
},
"origin_pos": 17,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"def bilinear_kernel(in_channels, out_channels, kernel_size):\n",
" factor = (kernel_size + 1) // 2\n",
" if kernel_size % 2 == 1:\n",
" center = factor - 1\n",
" else:\n",
" center = factor - 0.5\n",
" og = (torch.arange(kernel_size).reshape(-1, 1),\n",
" torch.arange(kernel_size).reshape(1, -1))\n",
" filt = (1 - torch.abs(og[0] - center) / factor) * \\\n",
" (1 - torch.abs(og[1] - center) / factor)\n",
" weight = torch.zeros((in_channels, out_channels,\n",
" kernel_size, kernel_size))\n",
" weight[range(in_channels), range(out_channels), :, :] = filt\n",
" return weight"
]
},
{
"cell_type": "markdown",
"id": "7c68424b",
"metadata": {
"origin_pos": 18
},
"source": [
"Let's [**experiment with upsampling of bilinear interpolation**] \n",
"that is implemented by a transposed convolutional layer. \n",
"We construct a transposed convolutional layer that \n",
"doubles the height and weight,\n",
"and initialize its kernel with the `bilinear_kernel` function.\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "98efd2f9",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:27.350768Z",
"iopub.status.busy": "2023-08-18T19:34:27.350438Z",
"iopub.status.idle": "2023-08-18T19:34:27.357663Z",
"shell.execute_reply": "2023-08-18T19:34:27.356565Z"
},
"origin_pos": 20,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"conv_trans = nn.ConvTranspose2d(3, 3, kernel_size=4, padding=1, stride=2,\n",
" bias=False)\n",
"conv_trans.weight.data.copy_(bilinear_kernel(3, 3, 4));"
]
},
{
"cell_type": "markdown",
"id": "4ce526ac",
"metadata": {
"origin_pos": 21
},
"source": [
"Read the image `X` and assign the upsampling output to `Y`. In order to print the image, we need to adjust the position of the channel dimension.\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "7d8d6903",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:27.361307Z",
"iopub.status.busy": "2023-08-18T19:34:27.360856Z",
"iopub.status.idle": "2023-08-18T19:34:27.475914Z",
"shell.execute_reply": "2023-08-18T19:34:27.472680Z"
},
"origin_pos": 23,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"img = torchvision.transforms.ToTensor()(d2l.Image.open('../img/catdog.jpg'))\n",
"X = img.unsqueeze(0)\n",
"Y = conv_trans(X)\n",
"out_img = Y[0].permute(1, 2, 0).detach()"
]
},
{
"cell_type": "markdown",
"id": "ddcbc658",
"metadata": {
"origin_pos": 24
},
"source": [
"As we can see, the transposed convolutional layer increases both the height and width of the image by a factor of two.\n",
"Except for the different scales in coordinates,\n",
"the image scaled up by bilinear interpolation and the original image printed in :numref:`sec_bbox` look the same.\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a5c3366a",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:27.481229Z",
"iopub.status.busy": "2023-08-18T19:34:27.480557Z",
"iopub.status.idle": "2023-08-18T19:34:28.367719Z",
"shell.execute_reply": "2023-08-18T19:34:28.366579Z"
},
"origin_pos": 26,
"tab": [
"pytorch"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"input image shape: torch.Size([561, 728, 3])\n",
"output image shape: torch.Size([1122, 1456, 3])\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"d2l.set_figsize()\n",
"print('input image shape:', img.permute(1, 2, 0).shape)\n",
"d2l.plt.imshow(img.permute(1, 2, 0));\n",
"print('output image shape:', out_img.shape)\n",
"d2l.plt.imshow(out_img);"
]
},
{
"cell_type": "markdown",
"id": "e85f4c76",
"metadata": {
"origin_pos": 27
},
"source": [
"In a fully convolutional network, we [**initialize the transposed convolutional layer with upsampling of bilinear interpolation. For the $1\\times 1$ convolutional layer, we use Xavier initialization.**]\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "1ae40200",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:28.372559Z",
"iopub.status.busy": "2023-08-18T19:34:28.371880Z",
"iopub.status.idle": "2023-08-18T19:34:28.381788Z",
"shell.execute_reply": "2023-08-18T19:34:28.380642Z"
},
"origin_pos": 29,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"W = bilinear_kernel(num_classes, num_classes, 64)\n",
"net.transpose_conv.weight.data.copy_(W);"
]
},
{
"cell_type": "markdown",
"id": "fbc76cde",
"metadata": {
"origin_pos": 30
},
"source": [
"## [**Reading the Dataset**]\n",
"\n",
"We read\n",
"the semantic segmentation dataset\n",
"as introduced in :numref:`sec_semantic_segmentation`. \n",
"The output image shape of random cropping is\n",
"specified as $320\\times 480$: both the height and width are divisible by $32$.\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0bdc2a20",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:34:28.386035Z",
"iopub.status.busy": "2023-08-18T19:34:28.385440Z",
"iopub.status.idle": "2023-08-18T19:35:21.373422Z",
"shell.execute_reply": "2023-08-18T19:35:21.369676Z"
},
"origin_pos": 31,
"tab": [
"pytorch"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"read 1114 examples\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"read 1078 examples\n"
]
}
],
"source": [
"batch_size, crop_size = 32, (320, 480)\n",
"train_iter, test_iter = d2l.load_data_voc(batch_size, crop_size)"
]
},
{
"cell_type": "markdown",
"id": "6654107c",
"metadata": {
"origin_pos": 32
},
"source": [
"## [**Training**]\n",
"\n",
"\n",
"Now we can train our constructed\n",
"fully convolutional network. \n",
"The loss function and accuracy calculation here\n",
"are not essentially different from those in image classification of earlier chapters. \n",
"Because we use the output channel of the\n",
"transposed convolutional layer to\n",
"predict the class for each pixel,\n",
"the channel dimension is specified in the loss calculation.\n",
"In addition, the accuracy is calculated\n",
"based on correctness\n",
"of the predicted class for all the pixels.\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b65f6226",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:35:21.378517Z",
"iopub.status.busy": "2023-08-18T19:35:21.377599Z",
"iopub.status.idle": "2023-08-18T19:36:21.659017Z",
"shell.execute_reply": "2023-08-18T19:36:21.657836Z"
},
"origin_pos": 34,
"tab": [
"pytorch"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss 0.449, train acc 0.861, test acc 0.852\n",
"226.7 examples/sec on [device(type='cuda', index=0), device(type='cuda', index=1)]\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def loss(inputs, targets):\n",
" return F.cross_entropy(inputs, targets, reduction='none').mean(1).mean(1)\n",
"\n",
"num_epochs, lr, wd, devices = 5, 0.001, 1e-3, d2l.try_all_gpus()\n",
"trainer = torch.optim.SGD(net.parameters(), lr=lr, weight_decay=wd)\n",
"d2l.train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices)"
]
},
{
"cell_type": "markdown",
"id": "9c0c7f12",
"metadata": {
"origin_pos": 35
},
"source": [
"## [**Prediction**]\n",
"\n",
"\n",
"When predicting, we need to standardize the input image\n",
"in each channel and transform the image into the four-dimensional input format required by the CNN.\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e7f1ceba",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:36:21.663792Z",
"iopub.status.busy": "2023-08-18T19:36:21.662705Z",
"iopub.status.idle": "2023-08-18T19:36:21.669589Z",
"shell.execute_reply": "2023-08-18T19:36:21.668481Z"
},
"origin_pos": 37,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"def predict(img):\n",
" X = test_iter.dataset.normalize_image(img).unsqueeze(0)\n",
" pred = net(X.to(devices[0])).argmax(dim=1)\n",
" return pred.reshape(pred.shape[1], pred.shape[2])"
]
},
{
"cell_type": "markdown",
"id": "27b9364c",
"metadata": {
"origin_pos": 38
},
"source": [
"To [**visualize the predicted class**] of each pixel, we map the predicted class back to its label color in the dataset.\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "88b09f25",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:36:21.673578Z",
"iopub.status.busy": "2023-08-18T19:36:21.672677Z",
"iopub.status.idle": "2023-08-18T19:36:21.678207Z",
"shell.execute_reply": "2023-08-18T19:36:21.677171Z"
},
"origin_pos": 40,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"def label2image(pred):\n",
" colormap = torch.tensor(d2l.VOC_COLORMAP, device=devices[0])\n",
" X = pred.long()\n",
" return colormap[X, :]"
]
},
{
"cell_type": "markdown",
"id": "db007d32",
"metadata": {
"origin_pos": 41
},
"source": [
"Images in the test dataset vary in size and shape.\n",
"Since the model uses a transposed convolutional layer with stride of 32,\n",
"when the height or width of an input image is indivisible by 32,\n",
"the output height or width of the\n",
"transposed convolutional layer will deviate from the shape of the input image.\n",
"In order to address this issue,\n",
"we can crop multiple rectangular areas with height and width that are integer multiples of 32 in the image,\n",
"and perform forward propagation\n",
"on the pixels in these areas separately.\n",
"Note that\n",
"the union of these rectangular areas needs to completely cover the input image.\n",
"When a pixel is covered by multiple rectangular areas,\n",
"the average of the transposed convolution outputs\n",
"in separate areas for this same pixel\n",
"can be input to\n",
"the softmax operation\n",
"to predict the class.\n",
"\n",
"\n",
"For simplicity, we only read a few larger test images,\n",
"and crop a $320\\times480$ area for prediction starting from the upper-left corner of an image.\n",
"For these test images, we\n",
"print their cropped areas,\n",
"prediction results,\n",
"and ground-truth row by row.\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "8f780e21",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T19:36:21.682056Z",
"iopub.status.busy": "2023-08-18T19:36:21.681351Z",
"iopub.status.idle": "2023-08-18T19:36:53.274281Z",
"shell.execute_reply": "2023-08-18T19:36:53.273369Z"
},
"origin_pos": 43,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"voc_dir = d2l.download_extract('voc2012', 'VOCdevkit/VOC2012')\n",
"test_images, test_labels = d2l.read_voc_images(voc_dir, False)\n",
"n, imgs = 4, []\n",
"for i in range(n):\n",
" crop_rect = (0, 0, 320, 480)\n",
" X = torchvision.transforms.functional.crop(test_images[i], *crop_rect)\n",
" pred = label2image(predict(X))\n",
" imgs += [X.permute(1,2,0), pred.cpu(),\n",
" torchvision.transforms.functional.crop(\n",
" test_labels[i], *crop_rect).permute(1,2,0)]\n",
"d2l.show_images(imgs[::3] + imgs[1::3] + imgs[2::3], 3, n, scale=2);"
]
},
{
"cell_type": "markdown",
"id": "f4076170",
"metadata": {
"origin_pos": 44
},
"source": [
"## Summary\n",
"\n",
"* The fully convolutional network first uses a CNN to extract image features, then transforms the number of channels into the number of classes via a $1\\times 1$ convolutional layer, and finally transforms the height and width of the feature maps to those of the input image via the transposed convolution.\n",
"* In a fully convolutional network, we can use upsampling of bilinear interpolation to initialize the transposed convolutional layer.\n",
"\n",
"\n",
"## Exercises\n",
"\n",
"1. If we use Xavier initialization for the transposed convolutional layer in the experiment, how does the result change?\n",
"1. Can you further improve the accuracy of the model by tuning the hyperparameters?\n",
"1. Predict the classes of all pixels in test images.\n",
"1. The original fully convolutional network paper also uses outputs of some intermediate CNN layers :cite:`Long.Shelhamer.Darrell.2015`. Try to implement this idea.\n"
]
},
{
"cell_type": "markdown",
"id": "a4ba1b83",
"metadata": {
"origin_pos": 46,
"tab": [
"pytorch"
]
},
"source": [
"[Discussions](https://discuss.d2l.ai/t/1582)\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"required_libs": []
},
"nbformat": 4,
"nbformat_minor": 5
}