{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "execute:\n",
    "  enabled: false\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-1 fw=all -->\n",
    "\n",
    "# Fully Convolutional Networks\n",
    "\n",
    "As discussed in that section,\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 [@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 that section.\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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-fully-convolutional-networks"
   },
   "source": [
    "%matplotlib inline\n",
    "from d2l import jax as d2l\n",
    "from d2l.nnx_resnet import ResNet50\n",
    "from flax import nnx\n",
    "import jax\n",
    "from jax import numpy as jnp\n",
    "import optax\n",
    "import numpy as np\n",
    "from PIL import Image"
   ],
   "execution_count": 1,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-2 fw=all -->\n",
    "\n",
    "## The Model\n",
    "\n",
    "Here we describe the basic design of the fully convolutional network model. \n",
    "As shown in the figure,\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 that section. \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",
    "![Fully convolutional network.](../img/fcn.svg)\n",
    "\n",
    "Below, we use a ResNet 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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-the-model-1"
   },
   "source": [
    "pretrained_net = ResNet50.from_pretrained()\n",
    "dummy = jnp.ones((1, 320, 480, 3))\n",
    "print('Feature extractor output shape:',\n",
    "      pretrained_net.feature_map(dummy).shape)"
   ],
   "execution_count": 2,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "text/plain": "Downloading bytes:           |  0.00B            "
     },
     "metadata": {}
    },
    {
     "output_type": "display_data",
     "data": {
      "text/plain": "Reconstructing (incomplete total...): |          |  0.00B /  0.00B            "
     },
     "metadata": {}
    },
    {
     "output_type": "display_data",
     "data": {
      "text/plain": "Fetching 1 files:   0%|          | 0/1 [00:00<?, ?it/s]"
     },
     "metadata": {}
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Feature extractor output shape: (1, 10, 15, 2048)\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-3 fw=all -->\n",
    "\n",
    "Next, we create the fully convolutional network instance `net`.\n",
    "It copies all the pretrained convolutional layers in the ResNet\n",
    "except for the final global average pooling layer\n",
    "and the fully connected layer that are closest\n",
    "to the output."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-the-model-2"
   },
   "source": [
    "# `feature_map` omits the global average pooling and classification head."
   ],
   "execution_count": 3,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-4 fw=all -->\n",
    "\n",
    "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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-the-model-3"
   },
   "source": [
    "X = jnp.ones((1, 320, 480, 3))\n",
    "pretrained_net.feature_map(X).shape"
   ],
   "execution_count": 4,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(1, 10, 15, 2048)"
     },
     "metadata": {},
     "execution_count": 4
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-5 fw=all -->\n",
    "\n",
    "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 that section. \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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-the-model-4"
   },
   "source": [
    "num_classes = 21\n",
    "\n",
    "class FCN(nnx.Module):\n",
    "    \"\"\"Fully Convolutional Network for semantic segmentation.\"\"\"\n",
    "    def __init__(self, backbone, num_classes, *, rngs):\n",
    "        self.backbone = backbone\n",
    "        self.classifier = nnx.Conv(2048, num_classes, (1, 1), rngs=rngs)\n",
    "        self.upsample = nnx.ConvTranspose(\n",
    "            num_classes, num_classes, (64, 64), strides=32, padding='SAME',\n",
    "            use_bias=False, rngs=rngs)\n",
    "\n",
    "    def __call__(self, X):\n",
    "        X = self.backbone.feature_map(X)\n",
    "        return self.upsample(self.classifier(X))\n",
    "\n",
    "net = FCN(pretrained_net, num_classes, rngs=nnx.Rngs(0))\n",
    "print('FCN output shape:',\n",
    "      net(jnp.ones((1, 320, 480, 3))).shape)"
   ],
   "execution_count": 5,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "FCN output shape: (1, 320, 480, 21)\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-6 fw=all -->\n",
    "\n",
    "## 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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-initializing-transposed-convolutional-layers-1"
   },
   "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 = (np.arange(kernel_size).reshape(-1, 1),\n",
    "          np.arange(kernel_size).reshape(1, -1))\n",
    "    filt = (1 - np.abs(og[0] - center) / factor) * \\\n",
    "           (1 - np.abs(og[1] - center) / factor)\n",
    "    # Flax uses HWIO format for ConvTranspose kernels\n",
    "    weight = np.zeros((kernel_size, kernel_size, in_channels, out_channels))\n",
    "    for i in range(min(in_channels, out_channels)):\n",
    "        weight[:, :, i, i] = filt\n",
    "    return jnp.array(weight)"
   ],
   "execution_count": 6,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-7 fw=all -->\n",
    "\n",
    "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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-initializing-transposed-convolutional-layers-2"
   },
   "source": [
    "class BilinearConvTranspose(nnx.Module):\n",
    "    \"\"\"A transposed conv layer initialized with bilinear interpolation.\"\"\"\n",
    "    def __init__(self, channels, kernel_size, strides, *, rngs):\n",
    "        self.layer = nnx.ConvTranspose(\n",
    "            channels, channels, (kernel_size, kernel_size), strides=strides,\n",
    "            padding='SAME', use_bias=False, rngs=rngs)\n",
    "        self.layer.kernel[...] = bilinear_kernel(\n",
    "            channels, channels, kernel_size)\n",
    "\n",
    "    def __call__(self, X):\n",
    "        return self.layer(X)\n",
    "\n",
    "conv_trans = BilinearConvTranspose(3, 4, (2, 2), rngs=nnx.Rngs(0))"
   ],
   "execution_count": 7,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-8 fw=all -->\n",
    "\n",
    "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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-initializing-transposed-convolutional-layers-3"
   },
   "source": [
    "img = np.array(Image.open('../img/catdog.jpg')).astype(np.float32) / 255\n",
    "X = jnp.expand_dims(jnp.array(img), axis=0)  # NHWC\n",
    "Y = conv_trans(X)\n",
    "out_img = np.array(Y[0])"
   ],
   "execution_count": 8,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "E0719 14:24:24.060179   40677 cuda_timer.cc:88] Delay kernel timed out: measured time has sub-optimal accuracy. There may be a missing warmup execution, please investigate in Nsight Systems.\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-9 fw=all -->\n",
    "\n",
    "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 that section look the same."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-initializing-transposed-convolutional-layers-4"
   },
   "source": [
    "d2l.set_figsize()\n",
    "print('input image shape:', img.shape)\n",
    "d2l.plt.imshow(img);\n",
    "print('output image shape:', out_img.shape)\n",
    "d2l.plt.imshow(out_img);"
   ],
   "execution_count": 9,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "input image shape: (561, 728, 3)\noutput image shape: (1122, 1456, 3)\n"
     ]
    },
    {
     "output_type": "display_data",
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"226.708824pt\" height=\"173.415579pt\" viewBox=\"0 0 226.708824 173.415579\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2026-07-19T14:24:24.509313</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.10.8, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 173.415579 \nL 226.708824 173.415579 \nL 226.708824 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 39.65 149.537454 \nL 219.508824 149.537454 \nL 219.508824 10.937454 \nL 39.65 10.937454 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p998200fb57)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 75221 characters]\" id=\"imageb506711028\" transform=\"scale(1 -1) translate(0 -138.96)\" x=\"40\" y=\"-10.455579\" width=\"180\" height=\"138.96\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <defs>\n       <path id=\"mc6caaa5a43\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mc6caaa5a43\" x=\"39.711765\" y=\"149.537454\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 0 -->\n      <g transform=\"translate(36.530515 164.135892) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_2\">\n      <g>\n       <use xlink:href=\"#mc6caaa5a43\" x=\"101.476471\" y=\"149.537454\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 500 -->\n      <g transform=\"translate(91.932721 164.135892) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-35\" d=\"M 691 4666 \nL 3169 4666 \nL 3169 4134 \nL 1269 4134 \nL 1269 2991 \nQ 1406 3038 1543 3061 \nQ 1681 3084 1819 3084 \nQ 2600 3084 3056 2656 \nQ 3513 2228 3513 1497 \nQ 3513 744 3044 326 \nQ 2575 -91 1722 -91 \nQ 1428 -91 1123 -41 \nQ 819 9 494 109 \nL 494 744 \nQ 775 591 1075 516 \nQ 1375 441 1709 441 \nQ 2250 441 2565 725 \nQ 2881 1009 2881 1497 \nQ 2881 1984 2565 2268 \nQ 2250 2553 1709 2553 \nQ 1456 2553 1204 2497 \nQ 953 2441 691 2322 \nL 691 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-35\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_3\">\n      <g>\n       <use xlink:href=\"#mc6caaa5a43\" x=\"163.241176\" y=\"149.537454\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 1000 -->\n      <g transform=\"translate(150.516176 164.135892) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(190.869141 0)\"/>\n      </g>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_4\">\n      <defs>\n       <path id=\"mbfa2bb1d67\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mbfa2bb1d67\" x=\"39.65\" y=\"10.999219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 0 -->\n      <g transform=\"translate(26.2875 14.798438) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_5\">\n      <g>\n       <use xlink:href=\"#mbfa2bb1d67\" x=\"39.65\" y=\"35.705101\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 200 -->\n      <g transform=\"translate(13.5625 39.50432) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#mbfa2bb1d67\" x=\"39.65\" y=\"60.410983\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 400 -->\n      <g transform=\"translate(13.5625 64.210202) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-34\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_7\">\n      <g>\n       <use xlink:href=\"#mbfa2bb1d67\" x=\"39.65\" y=\"85.116866\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 600 -->\n      <g transform=\"translate(13.5625 88.916085) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \nQ 1688 2584 1439 2293 \nQ 1191 2003 1191 1497 \nQ 1191 994 1439 701 \nQ 1688 409 2113 409 \nQ 2538 409 2786 701 \nQ 3034 994 3034 1497 \nQ 3034 2003 2786 2293 \nQ 2538 2584 2113 2584 \nz\nM 3366 4563 \nL 3366 3988 \nQ 3128 4100 2886 4159 \nQ 2644 4219 2406 4219 \nQ 1781 4219 1451 3797 \nQ 1122 3375 1075 2522 \nQ 1259 2794 1537 2939 \nQ 1816 3084 2150 3084 \nQ 2853 3084 3261 2657 \nQ 3669 2231 3669 1497 \nQ 3669 778 3244 343 \nQ 2819 -91 2113 -91 \nQ 1303 -91 875 529 \nQ 447 1150 447 2328 \nQ 447 3434 972 4092 \nQ 1497 4750 2381 4750 \nQ 2619 4750 2861 4703 \nQ 3103 4656 3366 4563 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-36\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_5\">\n     <g id=\"line2d_8\">\n      <g>\n       <use xlink:href=\"#mbfa2bb1d67\" x=\"39.65\" y=\"109.822748\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- 800 -->\n      <g transform=\"translate(13.5625 113.621967) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \nQ 1584 2216 1326 1975 \nQ 1069 1734 1069 1313 \nQ 1069 891 1326 650 \nQ 1584 409 2034 409 \nQ 2484 409 2743 651 \nQ 3003 894 3003 1313 \nQ 3003 1734 2745 1975 \nQ 2488 2216 2034 2216 \nz\nM 1403 2484 \nQ 997 2584 770 2862 \nQ 544 3141 544 3541 \nQ 544 4100 942 4425 \nQ 1341 4750 2034 4750 \nQ 2731 4750 3128 4425 \nQ 3525 4100 3525 3541 \nQ 3525 3141 3298 2862 \nQ 3072 2584 2669 2484 \nQ 3125 2378 3379 2068 \nQ 3634 1759 3634 1313 \nQ 3634 634 3220 271 \nQ 2806 -91 2034 -91 \nQ 1263 -91 848 271 \nQ 434 634 434 1313 \nQ 434 1759 690 2068 \nQ 947 2378 1403 2484 \nz\nM 1172 3481 \nQ 1172 3119 1398 2916 \nQ 1625 2713 2034 2713 \nQ 2441 2713 2670 2916 \nQ 2900 3119 2900 3481 \nQ 2900 3844 2670 4047 \nQ 2441 4250 2034 4250 \nQ 1625 4250 1398 4047 \nQ 1172 3844 1172 3481 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-38\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_6\">\n     <g id=\"line2d_9\">\n      <g>\n       <use xlink:href=\"#mbfa2bb1d67\" x=\"39.65\" y=\"134.528631\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 1000 -->\n      <g transform=\"translate(7.2 138.327849) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(190.869141 0)\"/>\n      </g>\n     </g>\n    </g>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 39.65 149.537454 \nL 39.65 10.937454 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 219.508824 149.537454 \nL 219.508824 10.937454 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 39.65 149.537454 \nL 219.508824 149.537454 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 39.65 10.937454 \nL 219.508824 10.937454 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p998200fb57\">\n   <rect x=\"39.65\" y=\"10.937454\" width=\"179.858824\" height=\"138.6\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-10 fw=all -->\n",
    "\n",
    "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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-initializing-transposed-convolutional-layers-5"
   },
   "source": [
    "# Initialize the FCN with bilinear weights for the transposed conv layer\n",
    "# and Xavier initialization for the 1x1 conv layer\n",
    "W = bilinear_kernel(num_classes, num_classes, 64)\n",
    "net.upsample.kernel[...] = W"
   ],
   "execution_count": 10,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-11 fw=all -->\n",
    "\n",
    "## Reading the Dataset\n",
    "\n",
    "We read\n",
    "the semantic segmentation dataset\n",
    "as introduced in that section. \n",
    "The output image shape of random cropping is\n",
    "specified as $320\\times 480$: both the height and width are divisible by $32$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-reading-the-dataset"
   },
   "source": [
    "# The NNX ResNet-50 is deeper than the ResNet-18 used in the PyTorch tab, so\n",
    "# use a smaller minibatch while keeping the same crops and number of epochs.\n",
    "batch_size, crop_size = 4, (320, 480)\n",
    "train_iter, test_iter = d2l.load_data_voc(batch_size, crop_size)"
   ],
   "execution_count": 11,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "read 1114 examples\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "read 1078 examples\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-12 fw=all -->\n",
    "\n",
    "## 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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-training"
   },
   "source": [
    "num_epochs, lr, wd = 5, 0.001, 1e-3\n",
    "def parameter_labels(params):\n",
    "    return jax.tree_util.tree_map_with_path(\n",
    "        lambda path, _: ('head' if any(\n",
    "            getattr(p, 'key', None) in ('classifier', 'upsample')\n",
    "            for p in path) else 'backbone'), params)\n",
    "\n",
    "optimizer = nnx.Optimizer(\n",
    "    net, optax.multi_transform(\n",
    "        {'head': optax.chain(optax.add_decayed_weights(wd),\n",
    "                             optax.sgd(lr * 10)),\n",
    "         'backbone': optax.chain(optax.add_decayed_weights(wd),\n",
    "                                 optax.sgd(lr))},\n",
    "        parameter_labels),\n",
    "    wrt=nnx.Param)\n",
    "eval_net = nnx.view(net, use_running_average=True, raise_if_not_found=False)\n",
    "\n",
    "@nnx.jit\n",
    "def train_step(model, optimizer, X, Y):\n",
    "    def loss_fn(model):\n",
    "        logits = model(X)\n",
    "        loss = optax.softmax_cross_entropy_with_integer_labels(logits, Y)\n",
    "        return loss.mean(), logits\n",
    "    (loss, logits), grads = nnx.value_and_grad(loss_fn, has_aux=True)(model)\n",
    "    optimizer.update(model, grads)\n",
    "    correct = (jnp.argmax(logits, axis=-1) == Y).sum()\n",
    "    return loss, correct\n",
    "\n",
    "@nnx.jit\n",
    "def eval_step(model, X, Y):\n",
    "    logits = model(X)\n",
    "    losses = optax.softmax_cross_entropy_with_integer_labels(logits, Y)\n",
    "    correct = (jnp.argmax(logits, axis=-1) == Y).sum()\n",
    "    return losses.sum(), correct\n",
    "\n",
    "for epoch in range(num_epochs):\n",
    "    loss_terms, correct_terms, num_pixels = [], [], 0\n",
    "    for X, Y in train_iter:\n",
    "        X = jnp.transpose(jnp.array(X), (0, 2, 3, 1))\n",
    "        Y = jnp.array(Y)\n",
    "        loss, correct = train_step(net, optimizer, X, Y)\n",
    "        loss_terms.append(loss * Y.size)\n",
    "        correct_terms.append(correct)\n",
    "        num_pixels += Y.size\n",
    "    val_loss_terms, val_correct_terms, val_pixels = [], [], 0\n",
    "    for X, Y in test_iter:\n",
    "        X = jnp.transpose(jnp.array(X), (0, 2, 3, 1))\n",
    "        Y = jnp.array(Y)\n",
    "        val_loss, val_correct = eval_step(eval_net, X, Y)\n",
    "        val_loss_terms.append(val_loss)\n",
    "        val_correct_terms.append(val_correct)\n",
    "        val_pixels += Y.size\n",
    "    loss_sum = float(jnp.stack(loss_terms).sum())\n",
    "    correct = int(jnp.stack(correct_terms).sum())\n",
    "    val_loss_sum = float(jnp.stack(val_loss_terms).sum())\n",
    "    val_correct = int(jnp.stack(val_correct_terms).sum())\n",
    "    print(f'epoch {epoch + 1}, loss {loss_sum / num_pixels:.3f}, '\n",
    "          f'pixel acc {correct / num_pixels:.3f}, '\n",
    "          f'val loss {val_loss_sum / val_pixels:.3f}, '\n",
    "          f'val pixel acc {val_correct / val_pixels:.3f}')"
   ],
   "execution_count": 12,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "epoch 1, loss 1.635, pixel acc 0.706, val loss 1.202, val pixel acc 0.731\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "epoch 2, loss 1.132, pixel acc 0.738, val loss 1.059, val pixel acc 0.747\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "epoch 3, loss 1.016, pixel acc 0.757, val loss 0.974, val pixel acc 0.765\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "epoch 4, loss 0.924, pixel acc 0.775, val loss 0.886, val pixel acc 0.781\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "epoch 5, loss 0.852, pixel acc 0.790, val loss 0.826, val pixel acc 0.790\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-13 fw=all -->\n",
    "\n",
    "## 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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-prediction-1"
   },
   "source": [
    "def predict(img):\n",
    "    rgb_mean = np.array([0.485, 0.456, 0.406])\n",
    "    rgb_std = np.array([0.229, 0.224, 0.225])\n",
    "    X = (img.astype(np.float32) / 255 - rgb_mean) / rgb_std\n",
    "    X = jnp.expand_dims(jnp.array(X), axis=0)  # NHWC\n",
    "    pred = nnx.view(net, use_running_average=True,\n",
    "                    raise_if_not_found=False)(X)\n",
    "    return jnp.argmax(pred, axis=-1).reshape(pred.shape[1], pred.shape[2])"
   ],
   "execution_count": 13,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-14 fw=all -->\n",
    "\n",
    "To visualize the predicted class of each pixel, we map the predicted class back to its label color in the dataset."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-prediction-2"
   },
   "source": [
    "def label2image(pred):\n",
    "    colormap = jnp.array(d2l.VOC_COLORMAP, dtype=jnp.uint8)\n",
    "    X = pred.astype(jnp.int32)\n",
    "    return colormap[X, :]"
   ],
   "execution_count": 14,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-15 fw=all -->\n",
    "\n",
    "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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "fcn-prediction-3"
   },
   "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 HWC arrays: top=0, left=0, height=320, width=480\n",
    "    X = test_images[i][:320, :480, :]\n",
    "    pred = label2image(predict(X))\n",
    "    label_crop = test_labels[i][:320, :480, :]\n",
    "    imgs += [X, np.array(pred), label_crop]\n",
    "d2l.show_images(imgs[::3] + imgs[1::3] + imgs[2::3], 3, n, scale=2);"
   ],
   "execution_count": 15,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"460.8pt\" height=\"313.900358pt\" viewBox=\"0 0 460.8 313.900358\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2026-07-19T14:27:13.258878</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.10.8, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 313.900358 \nL 460.8 313.900358 \nL 460.8 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 7.2 71.895652 \nL 104.243478 71.895652 \nL 104.243478 7.2 \nL 7.2 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p6f6443ffee)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 23105 characters]\" id=\"image1e08cbd63c\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"7.2\" y=\"-7.095652\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 7.2 71.895652 \nL 7.2 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 104.243478 71.895652 \nL 104.243478 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 7.2 71.895652 \nL 104.243478 71.895652 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 7.2 7.2 \nL 104.243478 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_2\">\n   <g id=\"patch_7\">\n    <path d=\"M 123.652174 71.895652 \nL 220.695652 71.895652 \nL 220.695652 7.2 \nL 123.652174 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#pe2540d2da1)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 31713 characters]\" id=\"image38df666aaa\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"123.652174\" y=\"-7.095652\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_8\">\n    <path d=\"M 123.652174 71.895652 \nL 123.652174 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_9\">\n    <path d=\"M 220.695652 71.895652 \nL 220.695652 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_10\">\n    <path d=\"M 123.652174 71.895652 \nL 220.695652 71.895652 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_11\">\n    <path d=\"M 123.652174 7.2 \nL 220.695652 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_3\">\n   <g id=\"patch_12\">\n    <path d=\"M 240.104348 71.895652 \nL 337.147826 71.895652 \nL 337.147826 7.2 \nL 240.104348 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p6304c865e6)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 30161 characters]\" id=\"image6e76965c1b\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"240.104348\" y=\"-7.095652\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_13\">\n    <path d=\"M 240.104348 71.895652 \nL 240.104348 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_14\">\n    <path d=\"M 337.147826 71.895652 \nL 337.147826 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_15\">\n    <path d=\"M 240.104348 71.895652 \nL 337.147826 71.895652 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_16\">\n    <path d=\"M 240.104348 7.2 \nL 337.147826 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_4\">\n   <g id=\"patch_17\">\n    <path d=\"M 356.556522 71.895652 \nL 453.6 71.895652 \nL 453.6 7.2 \nL 356.556522 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#pf71f53c8e4)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 23709 characters]\" id=\"image82d3a78a5a\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"356.556522\" y=\"-7.095652\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_18\">\n    <path d=\"M 356.556522 71.895652 \nL 356.556522 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_19\">\n    <path d=\"M 453.6 71.895652 \nL 453.6 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_20\">\n    <path d=\"M 356.556522 71.895652 \nL 453.6 71.895652 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_21\">\n    <path d=\"M 356.556522 7.2 \nL 453.6 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_5\">\n   <g id=\"patch_22\">\n    <path d=\"M 7.2 189.298005 \nL 104.243478 189.298005 \nL 104.243478 124.602353 \nL 7.2 124.602353 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#pbb8f02e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EZl4TAqC4dR/QudsppQuqQ4yQAAAABJRU5ErkJggg==\" id=\"imagebfa06fe89a\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"7.2\" y=\"-124.498005\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_23\">\n    <path d=\"M 7.2 189.298005 \nL 7.2 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_24\">\n    <path d=\"M 104.243478 189.298005 \nL 104.243478 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_25\">\n    <path d=\"M 7.2 189.298005 \nL 104.243478 189.298005 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_26\">\n    <path d=\"M 7.2 124.602353 \nL 104.243478 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_6\">\n   <g id=\"patch_27\">\n    <path d=\"M 123.652174 189.298005 \nL 220.695652 189.298005 \nL 220.695652 124.602353 \nL 123.652174 124.602353 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#pd4b13f4e8d)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 4653 characters]\" id=\"imagebfe3a1b689\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"123.652174\" y=\"-124.498005\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_28\">\n    <path d=\"M 123.652174 189.298005 \nL 123.652174 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_29\">\n    <path d=\"M 220.695652 189.298005 \nL 220.695652 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_30\">\n    <path d=\"M 123.652174 189.298005 \nL 220.695652 189.298005 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_31\">\n    <path d=\"M 123.652174 124.602353 \nL 220.695652 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_7\">\n   <g id=\"patch_32\">\n    <path d=\"M 240.104348 189.298005 \nL 337.147826 189.298005 \nL 337.147826 124.602353 \nL 240.104348 124.602353 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p687e73c25f)\">\n    <image xlink:href=\"data:image/png;base64,\niVBORw0KGgoAAAANSUhEUgAAAIcAAABaCAYAAACSR0X7AAAA60lEQVR4nO3SMQHAIBDAwAf/nlsDZIbhTkGGrJn5Bg727QDeZQ6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmDZA6SOUjmIJmD9AMapwGz83RStwAAAABJRU5ErkJggg==\" id=\"image36326922a6\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"240.104348\" y=\"-124.498005\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_33\">\n    <path d=\"M 240.104348 189.298005 \nL 240.104348 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_34\">\n    <path d=\"M 337.147826 189.298005 \nL 337.147826 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_35\">\n    <path d=\"M 240.104348 189.298005 \nL 337.147826 189.298005 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_36\">\n    <path d=\"M 240.104348 124.602353 \nL 337.147826 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_8\">\n   <g id=\"patch_37\">\n    <path d=\"M 356.556522 189.298005 \nL 453.6 189.298005 \nL 453.6 124.602353 \nL 356.556522 124.602353 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p3f52c45c7f)\">\n    <image xlink:href=\"data:image/png;base64,\niVBORw0KGgoAAAANSUhEUgAAAIcAAABaCAYAAACSR0X7AAAHn0lEQVR4nO2dWWxc1RmAvzsz9nhJHOME4+A4JiGOY3AWAoEsTaqCQwu0QSy1iNQ+0XR56VMrVe0LbcVDH9pKrYqQaKtWbaGiSUvFUlQaICEhJAGTBeOYxImXLCRe8DJxxvbMnD6cMU3BF4/H9547c+//SSPL8sw9v+795qz/ObYAhSBMQcjrAITcReQQbBE5BFtEDsEWkUOwReQQbBE5BFtEDsEWkUOwReQQbBE5BFtEDsEWkUOwReQQbBE5BFtEDsEWkUOwJeflsCyvIwguEa8DwIJIARTNgXAEKpfqn9fXQ3EZpJLw2u8hHvM60ODhmRyl5bDxEVhYB4UlUHE9WCEoKgXStYVlgVIwpwJe+AVcGfYq2mBiYTjBuLgMlt4Kn9sOVXWZNRsqBRdOwvM/1z8lJdoMxuQIReCOB2Hd/VBepWuJmfQnlNI1x75n4OAuSE64F6ugMSbHtbXw6B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id=\"image418d240f01\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"356.556522\" y=\"-124.498005\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_38\">\n    <path d=\"M 356.556522 189.298005 \nL 356.556522 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_39\">\n    <path d=\"M 453.6 189.298005 \nL 453.6 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_40\">\n    <path d=\"M 356.556522 189.298005 \nL 453.6 189.298005 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_41\">\n    <path d=\"M 356.556522 124.602353 \nL 453.6 124.602353 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_9\">\n   <g id=\"patch_42\">\n    <path d=\"M 7.2 306.700358 \nL 104.243478 306.700358 \nL 104.243478 242.004706 \nL 7.2 242.004706 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p9bee66d9ef)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 6493 characters]\" id=\"image00ec9ff400\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"7.2\" y=\"-241.900358\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_43\">\n    <path d=\"M 7.2 306.700358 \nL 7.2 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_44\">\n    <path d=\"M 104.243478 306.700358 \nL 104.243478 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_45\">\n    <path d=\"M 7.2 306.700358 \nL 104.243478 306.700358 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_46\">\n    <path d=\"M 7.2 242.004706 \nL 104.243478 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_10\">\n   <g id=\"patch_47\">\n    <path d=\"M 123.652174 306.700358 \nL 220.695652 306.700358 \nL 220.695652 242.004706 \nL 123.652174 242.004706 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p0073758685)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 7233 characters]\" id=\"image0dc7528162\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"123.652174\" y=\"-241.900358\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_48\">\n    <path d=\"M 123.652174 306.700358 \nL 123.652174 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_49\">\n    <path d=\"M 220.695652 306.700358 \nL 220.695652 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_50\">\n    <path d=\"M 123.652174 306.700358 \nL 220.695652 306.700358 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_51\">\n    <path d=\"M 123.652174 242.004706 \nL 220.695652 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_11\">\n   <g id=\"patch_52\">\n    <path d=\"M 240.104348 306.700358 \nL 337.147826 306.700358 \nL 337.147826 242.004706 \nL 240.104348 242.004706 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#pafe7c84466)\">\n    <image xlink:href=\"data:image/png;base64,\niVBORw0KGgoAAAANSUhEUgAAAIcAAABaCAYAAACSR0X7AAAK40lEQVR4nO3de2wUdQLA8e/Mbruvvgt0224ftFgoUDhQjBAUoVZietELaU4uQTEqQoqIKIQQ5cQT5EQ9EDGKQlCRw6BH1CPgSYwiXsEer4KUtkdXQKCl2wd97W73MXN/9CwCHeluS7uU3yfpH7t0fjtlv92Z+c12RwJUBKETcl+vgBC6RByCJhGHoEnEIWgScQiaRByCJhGHoEnEIWgScQiaRByCJn1fr4CgTZZlDIZwJCn4MdravPj9/qCWFXH0MkmSkGWJ2NhoEhMHkpaW3PFvNpuVzMzUjtsREWaysjK69XjffVfMihVv4/P5Al9XxIm360ankzGZjAwaFI/NZmX06GGkpiaRnm4jOTkRvd5ETY2C260AoCgqR4404XQqHWOUl7fgcHiDenybzcDSpWk89NB8SktPBry8eOXoAZIkodPJDBgQi9U6kMzMVLKzM8nMTCUjIw2z2UJrq47Tp9uorm7jk08aqa09T3l5K3V1XrzeSzEEuQXoVGmpjqeeSiMtLUnE0ZskSSI7O5MxY4YzblwO6ek2rNZBSJKRqio/9fVeDh5s4sMPHdTUnOPMGRcul4LP13sv1G63n/JyJ1lZg9m167uAlxdxBOmeeyawYsVi6upkSkqa+OCDRmprz1JR0UpDgxevV0Xt4w22ooDD4SE7OxNJklADXCERB+2vAtHREchy50f2Ho+PlpbWjtsWi4knn5zBjh1NrFpl79hnCEWHDjXyxBNJGI0GXC53QMveNHGEh4cRHh5GcnICVutAMjJSSE5OIDMzFVmWychIQ5I6/+9wu1v5+efzAPh8fg4fLsVmS+eZZ46FdBgATU0+EhJsxMREijj0eh0jR2aRkDCAW28dgdlsAmD48CFER0cRFRVHW5uE3e7G6fRTVNSE0+mnrs7ByZPOq8aTZYmcnAiMxjgAcnPjWbhwPF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SKOENcXfxfzCxGHoEnEIWgScQiaRByCJhGHoEnEIWgScQiaRByCJhGHoEnEIWgScQiaRBwhSlVVrFZDt67S1F0ijhB16NBxhgwxI8t9V4eII0S1tFzfD6LrChGHoEnEIWgScQiaRBw3ALPZRFhY7/9Zs4gjROh0Omw2K5GRl3/gvt3uJDExkejoyF5fJxFHiCgomMoXX7zHunUvYLGYOu6vq/Pg9yPeYHwzs9kS0euNjB37O+bOnYFO1/dPTd+vgQC0f66pw1HNhQteHnzwAaZMGc/AgeEdn7bcF0QcIaK62sFrr20gLk7ip5/8TJ48npgYPRaLrs/WScQRQnbv/p7du78hNjYMu92neZ3b3iLiCCE+n581a97H6TxLW5ty3a/lci0ijhBTVeVg5cq3yczUceBAI05nD1/eMgAijhC0f38J27b9k0mTYgny2n09QsQRghRFYdu2nURGKkREiB1S4Qoej7fb12jrLhGHoEnEIWgScQiaRByCJhGHoEnEIWgScYQwSfrl8uN9Q8QRopxON83NDSxcOBjw4ff3/jS6RPtlIoUQNGxYBpMn38GJE5Xs2VPc65NiIg5Bk9isCJr+Byi4a2KlbDP1AAAAAElFTkSuQmCC\" id=\"image6aefd78cfa\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"240.104348\" y=\"-241.900358\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_53\">\n    <path d=\"M 240.104348 306.700358 \nL 240.104348 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_54\">\n    <path d=\"M 337.147826 306.700358 \nL 337.147826 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_55\">\n    <path d=\"M 240.104348 306.700358 \nL 337.147826 306.700358 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_56\">\n    <path d=\"M 240.104348 242.004706 \nL 337.147826 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n  <g id=\"axes_12\">\n   <g id=\"patch_57\">\n    <path d=\"M 356.556522 306.700358 \nL 453.6 306.700358 \nL 453.6 242.004706 \nL 356.556522 242.004706 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p8593c9eb25)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 4677 characters]\" id=\"imaged181c2eb36\" transform=\"scale(1 -1) translate(0 -64.8)\" x=\"356.556522\" y=\"-241.900358\" width=\"97.2\" height=\"64.8\"/>\n   </g>\n   <g id=\"patch_58\">\n    <path d=\"M 356.556522 306.700358 \nL 356.556522 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_59\">\n    <path d=\"M 453.6 306.700358 \nL 453.6 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_60\">\n    <path d=\"M 356.556522 306.700358 \nL 453.6 306.700358 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_61\">\n    <path d=\"M 356.556522 242.004706 \nL 453.6 242.004706 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p6f6443ffee\">\n   <rect x=\"7.2\" y=\"7.2\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"pe2540d2da1\">\n   <rect x=\"123.652174\" y=\"7.2\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"p6304c865e6\">\n   <rect x=\"240.104348\" y=\"7.2\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"pf71f53c8e4\">\n   <rect x=\"356.556522\" y=\"7.2\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"pbb8f02e454\">\n   <rect x=\"7.2\" y=\"124.602353\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"pd4b13f4e8d\">\n   <rect x=\"123.652174\" y=\"124.602353\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"p687e73c25f\">\n   <rect x=\"240.104348\" y=\"124.602353\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"p3f52c45c7f\">\n   <rect x=\"356.556522\" y=\"124.602353\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"p9bee66d9ef\">\n   <rect x=\"7.2\" y=\"242.004706\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"p0073758685\">\n   <rect x=\"123.652174\" y=\"242.004706\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"pafe7c84466\">\n   <rect x=\"240.104348\" y=\"242.004706\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n  <clipPath id=\"p8593c9eb25\">\n   <rect x=\"356.556522\" y=\"242.004706\" width=\"97.043478\" height=\"64.695652\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-16 fw=all -->\n",
    "\n",
    "## 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 [@Long.Shelhamer.Darrell.2015]. Try to implement this idea."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=fcn-md-17 fw=jax -->\n",
    "\n",
    "[Discussions](https://d2l.discourse.group/t/1582)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "d2l-jax",
   "display_name": "d2l (JAX)",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
