{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "execute:\n",
    "  enabled: false\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-1 fw=all -->\n",
    "\n",
    "# Neural Style Transfer\n",
    "\n",
    "If you are a photography enthusiast,\n",
    "you may be familiar with filters.\n",
    "It can change the color style of photos\n",
    "so that landscape photos become sharper\n",
    "or portrait photos have whitened skins.\n",
    "However,\n",
    "one filter usually only changes\n",
    "one aspect of the photo.\n",
    "To apply an ideal style\n",
    "to a photo,\n",
    "you probably need to\n",
    "try many different filter combinations.\n",
    "This process is\n",
    "as complex as tuning the hyperparameters of a model.\n",
    "\n",
    "\n",
    "In this section, we will\n",
    "leverage layerwise representations of a CNN\n",
    "to automatically apply the style of one image\n",
    "to another image, i.e., *style transfer* [@Gatys.Ecker.Bethge.2016].\n",
    "This task needs two input images:\n",
    "one is the *content image* and\n",
    "the other is the *style image*.\n",
    "We will use neural networks\n",
    "to modify the content image\n",
    "to make it close to the style image in style.\n",
    "For example,\n",
    "the content image in the figure is a landscape photo taken by us\n",
    "in Mount Rainier National Park in the suburbs of Seattle, while the style image is an oil painting\n",
    "with the theme of autumn oak trees.\n",
    "In the output synthesized image,\n",
    "the oil brush strokes of the style image\n",
    "are applied, leading to more vivid colors,\n",
    "while preserving the main shape of the objects\n",
    "in the content image.\n",
    "\n",
    "![Given content and style images, style transfer outputs a synthesized image.](../img/style-transfer.svg)\n",
    "\n",
    "## Method\n",
    "\n",
    "the figure illustrates\n",
    "the CNN-based style transfer method with a simplified example.\n",
    "First, we initialize the synthesized image,\n",
    "for example, into the content image.\n",
    "This synthesized image is the only variable that needs to be updated during the style transfer process,\n",
    "i.e., the model parameters to be updated during training.\n",
    "Then we choose a pretrained CNN\n",
    "to extract image features and freeze its\n",
    "model parameters during training.\n",
    "This deep CNN uses multiple layers\n",
    "to extract\n",
    "hierarchical features for images.\n",
    "We can choose the output of some of these layers as content features or style features.\n",
    "Take the figure as an example.\n",
    "The pretrained neural network here has 3 convolutional layers,\n",
    "where the second layer outputs the content features,\n",
    "and the first and third layers output the style features.\n",
    "\n",
    "![CNN-based style transfer process. Solid lines show the direction of forward propagation and dotted lines show backward propagation. ](../img/neural-style.svg)\n",
    "\n",
    "Next, we calculate the loss function of style transfer through forward propagation (direction of solid arrows), and update the model parameters (the synthesized image for output) through backpropagation (direction of dashed arrows).\n",
    "The loss function commonly used in style transfer consists of three parts:\n",
    "(i) *content loss* makes the synthesized image and the content image close in content features;\n",
    "(ii) *style loss* makes the synthesized image and style image close in style features;\n",
    "and (iii) *total variation loss* helps to reduce the noise in the synthesized image.\n",
    "Finally, when the model training is over, we output the model parameters of the style transfer to generate\n",
    "the final synthesized image.\n",
    "\n",
    "\n",
    "In the following,\n",
    "we will explain the technical details of style transfer via a concrete experiment.\n",
    "\n",
    "\n",
    "## Reading the Content and Style Images\n",
    "\n",
    "First, we read the content and style images.\n",
    "From their printed coordinate axes,\n",
    "we can tell that these images have different sizes."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-reading-the-content-and-style-images-1"
   },
   "source": [
    "%matplotlib inline\n",
    "from d2l import mxnet as d2l\n",
    "from mxnet import autograd, gluon, image, init, np, npx\n",
    "from mxnet.gluon import nn\n",
    "\n",
    "npx.set_np()\n",
    "\n",
    "d2l.set_figsize()\n",
    "content_img = image.imread('../img/rainier.jpg')\n",
    "d2l.plt.imshow(content_img.asnumpy());"
   ],
   "execution_count": 1,
   "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=\"250.345337pt\" height=\"164.997876pt\" viewBox=\"0 0 250.345337 164.997876\" 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-19T15:21:24.057730</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 164.997876 \nL 250.345337 164.997876 \nL 250.345337 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 141.119751 \nL 234.95 141.119751 \nL 234.95 10.951538 \nL 39.65 10.951538 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#pd26242edd1)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 132177 characters]\" id=\"image7e7b2d4cd4\" transform=\"scale(1 -1) translate(0 -130.32)\" x=\"39.65\" y=\"-10.799751\" width=\"195.84\" height=\"130.32\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <defs>\n       <path id=\"mbd61ada34b\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mbd61ada34b\" x=\"39.697681\" y=\"141.119751\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 0 -->\n      <g transform=\"translate(36.516431 155.718188) 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=\"#mbd61ada34b\" x=\"87.378345\" y=\"141.119751\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 500 -->\n      <g transform=\"translate(77.834595 155.718188) 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=\"#mbd61ada34b\" x=\"135.059009\" y=\"141.119751\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 1000 -->\n      <g transform=\"translate(122.334009 155.718188) 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 id=\"xtick_4\">\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#mbd61ada34b\" x=\"182.739673\" y=\"141.119751\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 1500 -->\n      <g transform=\"translate(170.014673 155.718188) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-35\" 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 id=\"xtick_5\">\n     <g id=\"line2d_5\">\n      <g>\n       <use xlink:href=\"#mbd61ada34b\" x=\"230.420337\" y=\"141.119751\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 2000 -->\n      <g transform=\"translate(217.695337 155.718188) 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       <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_6\">\n      <defs>\n       <path id=\"m746b38851a\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m746b38851a\" x=\"39.65\" y=\"10.999219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\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_7\">\n      <g>\n       <use xlink:href=\"#m746b38851a\" x=\"39.65\" y=\"34.839551\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 250 -->\n      <g transform=\"translate(13.5625 38.63877) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-35\" 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_8\">\n      <g>\n       <use xlink:href=\"#m746b38851a\" x=\"39.65\" y=\"58.679883\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- 500 -->\n      <g transform=\"translate(13.5625 62.479102) scale(0.1 -0.1)\">\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=\"ytick_4\">\n     <g id=\"line2d_9\">\n      <g>\n       <use xlink:href=\"#m746b38851a\" x=\"39.65\" y=\"82.520215\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 750 -->\n      <g transform=\"translate(13.5625 86.319434) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-37\" d=\"M 525 4666 \nL 3525 4666 \nL 3525 4397 \nL 1831 0 \nL 1172 0 \nL 2766 4134 \nL 525 4134 \nL 525 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-37\"/>\n       <use xlink:href=\"#DejaVuSans-35\" 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_10\">\n      <g>\n       <use xlink:href=\"#m746b38851a\" x=\"39.65\" y=\"106.360547\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- 1000 -->\n      <g transform=\"translate(7.2 110.159766) 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 id=\"ytick_6\">\n     <g id=\"line2d_11\">\n      <g>\n       <use xlink:href=\"#m746b38851a\" x=\"39.65\" y=\"130.200879\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_11\">\n      <!-- 1250 -->\n      <g transform=\"translate(7.2 134.000098) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-35\" 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 141.119751 \nL 39.65 10.951538 \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 234.95 141.119751 \nL 234.95 10.951538 \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 141.119751 \nL 234.95 141.119751 \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.951538 \nL 234.95 10.951538 \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=\"pd26242edd1\">\n   <rect x=\"39.65\" y=\"10.951538\" width=\"195.3\" height=\"130.168213\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-reading-the-content-and-style-images-2"
   },
   "source": [
    "style_img = image.imread('../img/autumn-oak.jpg')\n",
    "d2l.plt.imshow(style_img.asnumpy());"
   ],
   "execution_count": 2,
   "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=\"242.15pt\" height=\"171.314356pt\" viewBox=\"0 0 242.15 171.314356\" 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-19T15:21:24.461094</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 171.314356 \nL 242.15 171.314356 \nL 242.15 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 147.436231 \nL 234.95 147.436231 \nL 234.95 10.942346 \nL 39.65 10.942346 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p195a5b401c)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 185193 characters]\" id=\"image2595e49940\" transform=\"scale(1 -1) translate(0 -136.8)\" x=\"39.65\" y=\"-10.636231\" width=\"195.84\" height=\"136.8\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <defs>\n       <path id=\"m7902e0fa39\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m7902e0fa39\" x=\"39.706872\" y=\"147.436231\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 0 -->\n      <g transform=\"translate(36.525622 162.034668) 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=\"#m7902e0fa39\" x=\"96.579324\" y=\"147.436231\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 500 -->\n      <g transform=\"translate(87.035574 162.034668) 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=\"#m7902e0fa39\" x=\"153.451776\" y=\"147.436231\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 1000 -->\n      <g transform=\"translate(140.726776 162.034668) 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 id=\"xtick_4\">\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#m7902e0fa39\" x=\"210.324228\" y=\"147.436231\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 1500 -->\n      <g transform=\"translate(197.599228 162.034668) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-35\" 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_5\">\n      <defs>\n       <path id=\"m4866b4f4b2\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m4866b4f4b2\" x=\"39.65\" y=\"10.999219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\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_6\">\n      <g>\n       <use xlink:href=\"#m4866b4f4b2\" x=\"39.65\" y=\"33.7482\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 200 -->\n      <g transform=\"translate(13.5625 37.547418) 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_7\">\n      <g>\n       <use xlink:href=\"#m4866b4f4b2\" x=\"39.65\" y=\"56.49718\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 400 -->\n      <g transform=\"translate(13.5625 60.296399) 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_8\">\n      <g>\n       <use xlink:href=\"#m4866b4f4b2\" x=\"39.65\" y=\"79.246161\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- 600 -->\n      <g transform=\"translate(13.5625 83.04538) 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_9\">\n      <g>\n       <use xlink:href=\"#m4866b4f4b2\" x=\"39.65\" y=\"101.995142\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 800 -->\n      <g transform=\"translate(13.5625 105.794361) 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_10\">\n      <g>\n       <use xlink:href=\"#m4866b4f4b2\" x=\"39.65\" y=\"124.744123\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- 1000 -->\n      <g transform=\"translate(7.2 128.543341) 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 147.436231 \nL 39.65 10.942346 \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 234.95 147.436231 \nL 234.95 10.942346 \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 147.436231 \nL 234.95 147.436231 \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.942346 \nL 234.95 10.942346 \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=\"p195a5b401c\">\n   <rect x=\"39.65\" y=\"10.942346\" width=\"195.3\" height=\"136.493885\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-2 fw=all -->\n",
    "\n",
    "## Preprocessing and Postprocessing\n",
    "\n",
    "Below, we define two functions for preprocessing and postprocessing images.\n",
    "The `preprocess` function standardizes\n",
    "each of the three RGB channels of the input image and transforms the results into the CNN input format.\n",
    "The `postprocess` function restores the pixel values in the output image to their original values before standardization.\n",
    "Since the image printing function requires that each pixel has a floating point value from 0 to 1,\n",
    "we replace any value smaller than 0 or greater than 1 with 0 or 1, respectively."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-preprocessing-and-postprocessing"
   },
   "source": [
    "rgb_mean = np.array([0.485, 0.456, 0.406])\n",
    "rgb_std = np.array([0.229, 0.224, 0.225])\n",
    "\n",
    "def preprocess(img, image_shape):\n",
    "    img = image.imresize(img, *image_shape)\n",
    "    img = (img.astype('float32') / 255 - rgb_mean) / rgb_std\n",
    "    return np.expand_dims(img.transpose(2, 0, 1), axis=0)\n",
    "\n",
    "def postprocess(img):\n",
    "    img = img[0].as_in_ctx(rgb_std.ctx)\n",
    "    return (img.transpose(1, 2, 0) * rgb_std + rgb_mean).clip(0, 1)"
   ],
   "execution_count": 3,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-3 fw=all -->\n",
    "\n",
    "## Extracting Features\n",
    "\n",
    "We use the VGG-19 model pretrained on the ImageNet dataset to extract image features [@Gatys.Ecker.Bethge.2016]."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-extracting-features-1"
   },
   "source": [
    "pretrained_net = gluon.model_zoo.vision.vgg19(pretrained=True)"
   ],
   "execution_count": 4,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-4 fw=all -->\n",
    "\n",
    "In order to extract the content features and style features of the image, we can select the output of certain layers in the VGG network.\n",
    "Generally speaking, the closer to the input layer, the easier to extract details of the image, and vice versa, the easier to extract the global information of the image. In order to avoid excessively\n",
    "retaining the details of the content image in the synthesized image,\n",
    "we choose a VGG layer that is closer to the output as the *content layer* to output the content features of the image.\n",
    "We also select the output of different VGG layers for extracting local and global style features.\n",
    "These layers are also called *style layers*.\n",
    "As mentioned in that section,\n",
    "the VGG network uses 5 convolutional blocks.\n",
    "In the experiment, we choose the last convolutional layer of the fourth convolutional block as the content layer, and the first convolutional layer of each convolutional block as the style layer.\n",
    "The indices of these layers can be obtained by printing the `pretrained_net` instance."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-extracting-features-2"
   },
   "source": [
    "style_layers, content_layers = [0, 5, 10, 19, 28], [25]"
   ],
   "execution_count": 5,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-5 fw=all -->\n",
    "\n",
    "When extracting features using VGG layers,\n",
    "we only need to use all those\n",
    "from the input layer to the content layer or style layer that is closest to the output layer.\n",
    "Let's construct a new network instance `net`, which only retains all the VGG layers to be\n",
    "used for feature extraction."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-extracting-features-3"
   },
   "source": [
    "net = nn.Sequential()\n",
    "for i in range(max(content_layers + style_layers) + 1):\n",
    "    net.add(pretrained_net.features[i])"
   ],
   "execution_count": 6,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-6 fw=all -->\n",
    "\n",
    "Given the input `X`, if we simply invoke\n",
    "the forward propagation `net(X)`, we can only get the output of the last layer.\n",
    "Since we also need the outputs of intermediate layers,\n",
    "we need to perform layer-by-layer computation and keep\n",
    "the content and style layer outputs."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-extracting-features-4"
   },
   "source": [
    "def extract_features(X, content_layers, style_layers):\n",
    "    contents = []\n",
    "    styles = []\n",
    "    for i in range(len(net)):\n",
    "        X = net[i](X)\n",
    "        if i in style_layers:\n",
    "            styles.append(X)\n",
    "        if i in content_layers:\n",
    "            contents.append(X)\n",
    "    return contents, styles"
   ],
   "execution_count": 7,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-7 fw=all -->\n",
    "\n",
    "Two functions are defined below:\n",
    "the `get_contents` function extracts content features from the content image,\n",
    "and the `get_styles` function extracts style features from the style image.\n",
    "Since there is no need to update the model parameters of the pretrained VGG during training,\n",
    "we can extract the content and the style features\n",
    "even before the training starts.\n",
    "Since the synthesized image\n",
    "is a set of model parameters to be updated\n",
    "for style transfer,\n",
    "we can only extract the content and style features of the synthesized image by calling the `extract_features` function during training."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-extracting-features-5"
   },
   "source": [
    "def get_contents(image_shape, device):\n",
    "    content_X = preprocess(content_img, image_shape).copyto(device)\n",
    "    contents_Y, _ = extract_features(content_X, content_layers, style_layers)\n",
    "    return content_X, contents_Y\n",
    "\n",
    "def get_styles(image_shape, device):\n",
    "    style_X = preprocess(style_img, image_shape).copyto(device)\n",
    "    _, styles_Y = extract_features(style_X, content_layers, style_layers)\n",
    "    return style_X, styles_Y"
   ],
   "execution_count": 8,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-8 fw=all -->\n",
    "\n",
    "## Defining the Loss Function\n",
    "\n",
    "Now we will describe the loss function for style transfer. The loss function consists of\n",
    "the content loss, style loss, and total variation loss.\n",
    "\n",
    "### Content Loss\n",
    "\n",
    "Similar to the loss function in linear regression,\n",
    "the content loss measures the difference\n",
    "in content features\n",
    "between the synthesized image and the content image via\n",
    "the squared loss function.\n",
    "The two inputs of the squared loss function\n",
    "are both\n",
    "outputs of the content layer computed by the `extract_features` function."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-content-loss"
   },
   "source": [
    "def content_loss(Y_hat, Y):\n",
    "    return np.square(Y_hat - Y).mean()"
   ],
   "execution_count": 9,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-9 fw=all -->\n",
    "\n",
    "### Style Loss\n",
    "\n",
    "Style loss, similar to content loss,\n",
    "also uses the squared loss function to measure the difference in style between the synthesized image and the style image.\n",
    "To express the style output of any style layer,\n",
    "we first use the `extract_features` function to\n",
    "compute the style layer output.\n",
    "Suppose that the output has\n",
    "1 example, $c$ channels,\n",
    "height $h$, and width $w$,\n",
    "we can transform this output into\n",
    "matrix $\\mathbf{X}$ with $c$ rows and $hw$ columns.\n",
    "This matrix can be thought of as\n",
    "the concatenation of\n",
    "$c$ vectors $\\mathbf{x}_1, \\ldots, \\mathbf{x}_c$,\n",
    "each of which has a length of $hw$.\n",
    "Here, vector $\\mathbf{x}_i$ represents the style feature of channel $i$.\n",
    "\n",
    "In the *Gram matrix* of these vectors $\\mathbf{X}\\mathbf{X}^\\top \\in \\mathbb{R}^{c \\times c}$, element $x_{ij}$ in row $i$ and column $j$ is the dot product of vectors $\\mathbf{x}_i$ and $\\mathbf{x}_j$.\n",
    "It represents the correlation of the style features of channels $i$ and $j$.\n",
    "We use this Gram matrix to represent the style output of any style layer.\n",
    "Note that when the value of $hw$ is larger,\n",
    "it likely leads to larger values in the Gram matrix.\n",
    "Note also that the height and width of the Gram matrix are both the number of channels $c$.\n",
    "To allow style loss not to be affected\n",
    "by these values,\n",
    "the `gram` function below divides\n",
    "the Gram matrix by the number of its elements, i.e., $chw$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-style-loss-1"
   },
   "source": [
    "def gram(X):\n",
    "    num_channels, n = X.shape[1], d2l.size(X) // X.shape[1]\n",
    "    X = d2l.reshape(X, (num_channels, n))\n",
    "    return d2l.matmul(X, d2l.transpose(X)) / (num_channels * n)"
   ],
   "execution_count": 10,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-10 fw=all -->\n",
    "\n",
    "Obviously,\n",
    "the two Gram matrix inputs of the squared loss function for style loss are based on\n",
    "the style layer outputs for\n",
    "the synthesized image and the style image.\n",
    "It is assumed here that the Gram matrix `gram_Y` based on the style image has been precomputed."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-style-loss-2"
   },
   "source": [
    "def style_loss(Y_hat, gram_Y):\n",
    "    return np.square(gram(Y_hat) - gram_Y).mean()"
   ],
   "execution_count": 11,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-11 fw=all -->\n",
    "\n",
    "### Total Variation Loss\n",
    "\n",
    "Sometimes, the learned synthesized image\n",
    "has a lot of high-frequency noise,\n",
    "i.e., particularly bright or dark pixels.\n",
    "One common noise reduction method is\n",
    "*total variation denoising*.\n",
    "Denote by $x_{i, j}$ the pixel value at coordinate $(i, j)$.\n",
    "Reducing total variation loss\n",
    "\n",
    "$$\\sum_{i, j} \\left|x_{i, j} - x_{i+1, j}\\right| + \\left|x_{i, j} - x_{i, j+1}\\right|$$\n",
    "\n",
    "makes values of neighboring pixels on the synthesized image closer."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-total-variation-loss"
   },
   "source": [
    "def tv_loss(Y_hat):\n",
    "    return 0.5 * (d2l.reduce_mean(\n",
    "        d2l.abs(Y_hat[:, :, 1:, :] - Y_hat[:, :, :-1, :])) +\n",
    "                  d2l.reduce_mean(\n",
    "        d2l.abs(Y_hat[:, :, :, 1:] - Y_hat[:, :, :, :-1])))"
   ],
   "execution_count": 12,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-12 fw=all -->\n",
    "\n",
    "### Loss Function\n",
    "\n",
    "The loss function of style transfer is the weighted sum of content loss, style loss, and total variation loss.\n",
    "By adjusting these weight hyperparameters,\n",
    "we can balance among\n",
    "content retention,\n",
    "style transfer,\n",
    "and noise reduction on the synthesized image."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-loss-function"
   },
   "source": [
    "content_weight, style_weight, tv_weight = 1, 1e4, 10\n",
    "\n",
    "def compute_loss(X, contents_Y_hat, styles_Y_hat, contents_Y, styles_Y_gram):\n",
    "    # Calculate the content, style, and total variance losses respectively\n",
    "    contents_l = [content_loss(Y_hat, Y) * content_weight for Y_hat, Y in zip(\n",
    "        contents_Y_hat, contents_Y)]\n",
    "    styles_l = [style_loss(Y_hat, Y) * style_weight for Y_hat, Y in zip(\n",
    "        styles_Y_hat, styles_Y_gram)]\n",
    "    tv_l = tv_loss(X) * tv_weight\n",
    "    # Add up all the losses\n",
    "    l = sum(styles_l + contents_l + [tv_l])\n",
    "    return contents_l, styles_l, tv_l, l"
   ],
   "execution_count": 13,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-13 fw=all -->\n",
    "\n",
    "## Initializing the Synthesized Image\n",
    "\n",
    "In style transfer,\n",
    "the synthesized image is the only variable that needs to be updated during training.\n",
    "Thus, we can define a simple model, `SynthesizedImage`, and treat the synthesized image as the model parameters.\n",
    "In this model, forward propagation just returns the model parameters."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-initializing-the-synthesized-image-1"
   },
   "source": [
    "class SynthesizedImage(nn.Block):\n",
    "    def __init__(self, img_shape):\n",
    "        super(SynthesizedImage, self).__init__()\n",
    "        self.weight = gluon.Parameter('weight', shape=img_shape)\n",
    "\n",
    "    def forward(self):\n",
    "        return self.weight.data()"
   ],
   "execution_count": 14,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-14 fw=all -->\n",
    "\n",
    "Next, we define the `get_inits` function.\n",
    "This function creates a synthesized image model instance and initializes it to the image `X`.\n",
    "Gram matrices for the style image at various style layers, `styles_Y_gram`, are computed prior to training."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-initializing-the-synthesized-image-2"
   },
   "source": [
    "def get_inits(X, device, lr, styles_Y):\n",
    "    gen_img = SynthesizedImage(X.shape)\n",
    "    gen_img.initialize(init.Constant(X), ctx=device, force_reinit=True)\n",
    "    trainer = gluon.Trainer(gen_img.collect_params(), 'adam',\n",
    "                            {'learning_rate': lr})\n",
    "    styles_Y_gram = [gram(Y) for Y in styles_Y]\n",
    "    return gen_img(), styles_Y_gram, trainer"
   ],
   "execution_count": 15,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-15 fw=all -->\n",
    "\n",
    "## Training\n",
    "\n",
    "\n",
    "When training the model for style transfer,\n",
    "we continuously extract\n",
    "content features and style features of the synthesized image, and calculate the loss function.\n",
    "Below defines the training loop."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-training-1"
   },
   "source": [
    "def train(X, contents_Y, styles_Y, device, lr, num_epochs, lr_decay_epoch):\n",
    "    X, styles_Y_gram, trainer = get_inits(X, device, lr, styles_Y)\n",
    "    animator = d2l.Animator(xlabel='epoch', ylabel='loss',\n",
    "                            xlim=[10, num_epochs], ylim=[0, 20],\n",
    "                            legend=['content', 'style', 'TV'],\n",
    "                            ncols=2, figsize=(7, 2.5))\n",
    "    for epoch in range(num_epochs):\n",
    "        with autograd.record():\n",
    "            contents_Y_hat, styles_Y_hat = extract_features(\n",
    "                X, content_layers, style_layers)\n",
    "            contents_l, styles_l, tv_l, l = compute_loss(\n",
    "                X, contents_Y_hat, styles_Y_hat, contents_Y, styles_Y_gram)\n",
    "        l.backward()\n",
    "        trainer.step(1)\n",
    "        if (epoch + 1) % lr_decay_epoch == 0:\n",
    "            trainer.set_learning_rate(trainer.learning_rate * 0.8)\n",
    "        if (epoch + 1) % 10 == 0:\n",
    "            animator.axes[1].imshow(postprocess(X).asnumpy())\n",
    "            animator.add(epoch + 1, [float(sum(contents_l)),\n",
    "                                     float(sum(styles_l)), float(tv_l)])\n",
    "    return X"
   ],
   "execution_count": 16,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-16 fw=all -->\n",
    "\n",
    "Now we start to train the model.\n",
    "We rescale the height and width of the content and style images to 300 by 450 pixels.\n",
    "We use the content image to initialize the synthesized image."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "neural-style-training-2"
   },
   "source": [
    "device, image_shape = d2l.try_gpu(), (450, 300)\n",
    "net.reset_ctx(device)\n",
    "content_X, contents_Y = get_contents(image_shape, device)\n",
    "_, styles_Y = get_styles(image_shape, device)\n",
    "output = train(content_X, contents_Y, styles_Y, device, 0.9, 500, 50)"
   ],
   "execution_count": 17,
   "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=\"438.403125pt\" height=\"187.155469pt\" viewBox=\"0 0 438.403125 187.155469\" 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-19T15:22:14.566023</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 187.155469 \nL 438.403125 187.155469 \nL 438.403125 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 40.603125 149.599219 \nL 218.14858 149.599219 \nL 218.14858 10.999219 \nL 40.603125 10.999219 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <path d=\"M 73.213515 149.599219 \nL 73.213515 10.999219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_2\">\n      <defs>\n       <path id=\"m8f4227f133\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"73.213515\" y=\"149.599219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 100 -->\n      <g transform=\"translate(63.669765 164.197656) 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        <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-31\"/>\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_2\">\n     <g id=\"line2d_3\">\n      <path d=\"M 109.447281 149.599219 \nL 109.447281 10.999219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"109.447281\" y=\"149.599219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 200 -->\n      <g transform=\"translate(99.903531 164.197656) 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=\"xtick_3\">\n     <g id=\"line2d_5\">\n      <path d=\"M 145.681047 149.599219 \nL 145.681047 10.999219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"145.681047\" y=\"149.599219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 300 -->\n      <g transform=\"translate(136.137297 164.197656) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \nQ 3050 2419 3304 2112 \nQ 3559 1806 3559 1356 \nQ 3559 666 3084 287 \nQ 2609 -91 1734 -91 \nQ 1441 -91 1130 -33 \nQ 819 25 488 141 \nL 488 750 \nQ 750 597 1062 519 \nQ 1375 441 1716 441 \nQ 2309 441 2620 675 \nQ 2931 909 2931 1356 \nQ 2931 1769 2642 2001 \nQ 2353 2234 1838 2234 \nL 1294 2234 \nL 1294 2753 \nL 1863 2753 \nQ 2328 2753 2575 2939 \nQ 2822 3125 2822 3475 \nQ 2822 3834 2567 4026 \nQ 2313 4219 1838 4219 \nQ 1578 4219 1281 4162 \nQ 984 4106 628 3988 \nL 628 4550 \nQ 988 4650 1302 4700 \nQ 1616 4750 1894 4750 \nQ 2613 4750 3031 4423 \nQ 3450 4097 3450 3541 \nQ 3450 3153 3228 2886 \nQ 3006 2619 2597 2516 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-33\"/>\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_4\">\n     <g id=\"line2d_7\">\n      <path d=\"M 181.914813 149.599219 \nL 181.914813 10.999219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_8\">\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"181.914813\" y=\"149.599219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 400 -->\n      <g transform=\"translate(172.371063 164.197656) 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=\"xtick_5\">\n     <g id=\"line2d_9\">\n      <path d=\"M 218.14858 149.599219 \nL 218.14858 10.999219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"218.14858\" y=\"149.599219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 500 -->\n      <g transform=\"translate(208.60483 164.197656) 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=\"text_6\">\n     <!-- epoch -->\n     <g transform=\"translate(114.147727 177.875781) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \nL 3597 1613 \nL 953 1613 \nQ 991 1019 1311 708 \nQ 1631 397 2203 397 \nQ 2534 397 2845 478 \nQ 3156 559 3463 722 \nL 3463 178 \nQ 3153 47 2828 -22 \nQ 2503 -91 2169 -91 \nQ 1331 -91 842 396 \nQ 353 884 353 1716 \nQ 353 2575 817 3079 \nQ 1281 3584 2069 3584 \nQ 2775 3584 3186 3129 \nQ 3597 2675 3597 1894 \nz\nM 3022 2063 \nQ 3016 2534 2758 2815 \nQ 2500 3097 2075 3097 \nQ 1594 3097 1305 2825 \nQ 1016 2553 972 2059 \nL 3022 2063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \nL 1159 -1331 \nL 581 -1331 \nL 581 3500 \nL 1159 3500 \nL 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nz\nM 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \nQ 1497 3097 1228 2736 \nQ 959 2375 959 1747 \nQ 959 1119 1226 758 \nQ 1494 397 1959 397 \nQ 2419 397 2687 759 \nQ 2956 1122 2956 1747 \nQ 2956 2369 2687 2733 \nQ 2419 3097 1959 3097 \nz\nM 1959 3584 \nQ 2709 3584 3137 3096 \nQ 3566 2609 3566 1747 \nQ 3566 888 3137 398 \nQ 2709 -91 1959 -91 \nQ 1206 -91 779 398 \nQ 353 888 353 1747 \nQ 353 2609 779 3096 \nQ 1206 3584 1959 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-68\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 4863 \nL 1159 4863 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-65\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(61.523438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(186.181641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(241.162109 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_11\">\n      <path d=\"M 40.603125 149.599219 \nL 218.14858 149.599219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_12\">\n      <defs>\n       <path id=\"m20fe3fa939\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m20fe3fa939\" x=\"40.603125\" y=\"149.599219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 0 -->\n      <g transform=\"translate(27.240625 153.398438) 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_13\">\n      <path d=\"M 40.603125 114.949219 \nL 218.14858 114.949219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_14\">\n      <g>\n       <use xlink:href=\"#m20fe3fa939\" x=\"40.603125\" y=\"114.949219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- 5 -->\n      <g transform=\"translate(27.240625 118.748437) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-35\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_15\">\n      <path d=\"M 40.603125 80.299219 \nL 218.14858 80.299219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_16\">\n      <g>\n       <use xlink:href=\"#m20fe3fa939\" x=\"40.603125\" y=\"80.299219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 10 -->\n      <g transform=\"translate(20.878125 84.098438) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_17\">\n      <path d=\"M 40.603125 45.649219 \nL 218.14858 45.649219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_18\">\n      <g>\n       <use xlink:href=\"#m20fe3fa939\" x=\"40.603125\" y=\"45.649219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- 15 -->\n      <g transform=\"translate(20.878125 49.448438) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_5\">\n     <g id=\"line2d_19\">\n      <path d=\"M 40.603125 10.999219 \nL 218.14858 10.999219 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_20\">\n      <g>\n       <use xlink:href=\"#m20fe3fa939\" x=\"40.603125\" y=\"10.999219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_11\">\n      <!-- 20 -->\n      <g transform=\"translate(20.878125 14.798438) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_12\">\n     <!-- loss -->\n     <g transform=\"translate(14.798438 89.957031) rotate(-90) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-6c\" d=\"M 603 4863 \nL 1178 4863 \nL 1178 0 \nL 603 0 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-73\" d=\"M 2834 3397 \nL 2834 2853 \nQ 2591 2978 2328 3040 \nQ 2066 3103 1784 3103 \nQ 1356 3103 1142 2972 \nQ 928 2841 928 2578 \nQ 928 2378 1081 2264 \nQ 1234 2150 1697 2047 \nL 1894 2003 \nQ 2506 1872 2764 1633 \nQ 3022 1394 3022 966 \nQ 3022 478 2636 193 \nQ 2250 -91 1575 -91 \nQ 1294 -91 989 -36 \nQ 684 19 347 128 \nL 347 722 \nQ 666 556 975 473 \nQ 1284 391 1588 391 \nQ 1994 391 2212 530 \nQ 2431 669 2431 922 \nQ 2431 1156 2273 1281 \nQ 2116 1406 1581 1522 \nL 1381 1569 \nQ 847 1681 609 1914 \nQ 372 2147 372 2553 \nQ 372 3047 722 3315 \nQ 1072 3584 1716 3584 \nQ 2034 3584 2315 3537 \nQ 2597 3491 2834 3397 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-6c\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(27.783203 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(88.964844 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(141.064453 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_21\">\n    <path d=\"M 69.391082 -1 \nL 69.590138 1.016592 \nL 73.213515 18.887532 \nL 76.836891 32.095134 \nL 80.460268 40.592001 \nL 84.083644 49.8524 \nL 87.707021 54.222379 \nL 91.330398 60.640402 \nL 94.953774 62.966202 \nL 98.577151 65.986868 \nL 102.200528 67.975254 \nL 105.823904 67.032003 \nL 109.447281 68.82974 \nL 113.070657 71.116673 \nL 116.694034 73.816143 \nL 120.317411 74.585678 \nL 123.940787 76.621926 \nL 127.564164 77.23336 \nL 131.187541 78.198276 \nL 134.810917 79.096771 \nL 138.434294 79.659465 \nL 142.05767 81.071298 \nL 145.681047 81.23171 \nL 149.304424 81.062237 \nL 152.9278 81.947415 \nL 156.551177 82.187697 \nL 160.174554 83.304565 \nL 163.79793 82.926539 \nL 167.421307 83.245336 \nL 171.044683 83.924189 \nL 174.66806 84.295864 \nL 178.291437 84.462066 \nL 181.914813 85.002097 \nL 185.53819 85.11285 \nL 189.161567 85.220193 \nL 192.784943 85.486898 \nL 196.40832 85.789357 \nL 200.031696 85.951964 \nL 203.655073 86.188386 \nL 207.27845 86.363002 \nL 210.901826 86.525734 \nL 214.525203 86.684429 \nL 218.14858 86.840195 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_22\">\n    <path d=\"M 75.971069 -1 \nL 76.836891 21.43727 \nL 80.460268 55.682226 \nL 84.083644 73.230523 \nL 87.707021 77.133063 \nL 91.330398 86.697356 \nL 94.953774 92.60678 \nL 98.577151 96.345864 \nL 102.200528 59.912405 \nL 105.823904 97.23048 \nL 109.447281 96.592361 \nL 113.070657 101.433622 \nL 116.694034 104.425793 \nL 120.317411 106.401602 \nL 123.940787 107.533559 \nL 127.564164 109.419314 \nL 131.187541 110.29942 \nL 134.810917 110.972632 \nL 138.434294 111.870648 \nL 142.05767 107.011107 \nL 145.681047 110.84116 \nL 149.304424 113.555901 \nL 152.9278 113.72278 \nL 156.551177 114.600451 \nL 160.174554 112.461347 \nL 163.79793 115.350389 \nL 167.421307 115.69123 \nL 171.044683 115.870376 \nL 174.66806 116.160817 \nL 178.291437 116.666247 \nL 181.914813 116.608111 \nL 185.53819 117.113667 \nL 189.161567 117.423013 \nL 192.784943 117.629854 \nL 196.40832 117.783321 \nL 200.031696 118.033073 \nL 203.655073 118.155663 \nL 207.27845 118.308085 \nL 210.901826 118.468098 \nL 214.525203 118.626687 \nL 218.14858 118.782341 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"line2d_23\">\n    <path d=\"M 40.603125 67.195925 \nL 44.226502 73.830842 \nL 47.849878 80.051482 \nL 51.473255 79.003452 \nL 55.096631 82.186712 \nL 58.720008 86.273801 \nL 62.343385 86.399774 \nL 65.966761 88.052894 \nL 69.590138 89.854927 \nL 73.213515 91.63094 \nL 76.836891 92.91224 \nL 80.460268 93.9407 \nL 84.083644 94.886258 \nL 87.707021 95.770091 \nL 91.330398 96.666663 \nL 94.953774 97.396679 \nL 98.577151 98.112185 \nL 102.200528 98.740952 \nL 105.823904 99.383277 \nL 109.447281 100.075589 \nL 113.070657 100.676919 \nL 116.694034 101.281639 \nL 120.317411 101.874889 \nL 123.940787 102.475908 \nL 127.564164 103.068861 \nL 131.187541 103.55568 \nL 134.810917 104.032517 \nL 138.434294 104.505587 \nL 142.05767 104.978104 \nL 145.681047 105.433812 \nL 149.304424 105.820516 \nL 152.9278 106.204774 \nL 156.551177 106.585626 \nL 160.174554 106.970766 \nL 163.79793 107.335198 \nL 167.421307 107.648796 \nL 171.044683 107.958251 \nL 174.66806 108.265964 \nL 178.291437 108.573668 \nL 181.914813 108.883327 \nL 185.53819 109.135155 \nL 189.161567 109.381994 \nL 192.784943 109.630134 \nL 196.40832 109.878756 \nL 200.031696 110.126738 \nL 203.655073 110.330925 \nL 207.27845 110.530403 \nL 210.901826 110.73003 \nL 214.525203 110.930004 \nL 218.14858 111.130279 \n\" clip-path=\"url(#pe2083f8908)\" style=\"fill: none; stroke-dasharray: 9.6,2.4,1.5,2.4; stroke-dashoffset: 0; stroke: #008000; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 40.603125 149.599219 \nL 40.603125 10.999219 \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 218.14858 149.599219 \nL 218.14858 10.999219 \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 40.603125 149.599219 \nL 218.14858 149.599219 \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 40.603125 10.999219 \nL 218.14858 10.999219 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"legend_1\">\n    <g id=\"patch_7\">\n     <path d=\"M 140.862642 63.033594 \nL 211.14858 63.033594 \nQ 213.14858 63.033594 213.14858 61.033594 \nL 213.14858 17.999219 \nQ 213.14858 15.999219 211.14858 15.999219 \nL 140.862642 15.999219 \nQ 138.862642 15.999219 138.862642 17.999219 \nL 138.862642 61.033594 \nQ 138.862642 63.033594 140.862642 63.033594 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_24\">\n     <path d=\"M 142.862642 24.097656 \nL 152.862642 24.097656 \nL 162.862642 24.097656 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_13\">\n     <!-- content -->\n     <g transform=\"translate(170.862642 27.597656) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-6e\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-74\" d=\"M 1172 4494 \nL 1172 3500 \nL 2356 3500 \nL 2356 3053 \nL 1172 3053 \nL 1172 1153 \nQ 1172 725 1289 603 \nQ 1406 481 1766 481 \nL 2356 481 \nL 2356 0 \nL 1766 0 \nQ 1100 0 847 248 \nQ 594 497 594 1153 \nL 594 3053 \nL 172 3053 \nL 172 3500 \nL 594 3500 \nL 594 4494 \nL 1172 4494 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-63\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(54.980469 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(116.162109 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(179.541016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(218.75 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(280.273438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(343.652344 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_25\">\n     <path d=\"M 142.862642 38.775781 \nL 152.862642 38.775781 \nL 162.862642 38.775781 \n\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_14\">\n     <!-- style -->\n     <g transform=\"translate(170.862642 42.275781) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-79\" d=\"M 2059 -325 \nQ 1816 -950 1584 -1140 \nQ 1353 -1331 966 -1331 \nL 506 -1331 \nL 506 -850 \nL 844 -850 \nQ 1081 -850 1212 -737 \nQ 1344 -625 1503 -206 \nL 1606 56 \nL 191 3500 \nL 800 3500 \nL 1894 763 \nL 2988 3500 \nL 3597 3500 \nL 2059 -325 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-73\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(52.099609 0)\"/>\n      <use xlink:href=\"#DejaVuSans-79\" transform=\"translate(91.308594 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(150.488281 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(178.271484 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_26\">\n     <path d=\"M 142.862642 53.453906 \nL 152.862642 53.453906 \nL 162.862642 53.453906 \n\" style=\"fill: none; stroke-dasharray: 9.6,2.4,1.5,2.4; stroke-dashoffset: 0; stroke: #008000; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_15\">\n     <!-- TV -->\n     <g transform=\"translate(170.862642 56.953906) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-54\" d=\"M -19 4666 \nL 3928 4666 \nL 3928 4134 \nL 2272 4134 \nL 2272 0 \nL 1638 0 \nL 1638 4134 \nL -19 4134 \nL -19 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-56\" d=\"M 1831 0 \nL 50 4666 \nL 709 4666 \nL 2188 738 \nL 3669 4666 \nL 4325 4666 \nL 2547 0 \nL 1831 0 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-54\"/>\n      <use xlink:href=\"#DejaVuSans-56\" transform=\"translate(61.083984 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n  <g id=\"axes_2\">\n   <g id=\"patch_8\">\n    <path d=\"M 253.65767 139.481037 \nL 431.203125 139.481037 \nL 431.203125 21.117401 \nL 253.65767 21.117401 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g clip-path=\"url(#p5fbcee7dd7)\">\n    <image xlink:href=\"data:image/png;base64,\[encoded data, 151733 characters]\" id=\"image0b6094f318\" transform=\"scale(1 -1) translate(0 -118.8)\" x=\"254\" y=\"-20.355469\" width=\"177.84\" height=\"118.8\"/>\n   </g>\n   <g id=\"matplotlib.axis_3\">\n    <g id=\"xtick_6\">\n     <g id=\"line2d_27\">\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"253.854943\" y=\"139.481037\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_16\">\n      <!-- 0 -->\n      <g transform=\"translate(250.673693 154.079474) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_7\">\n     <g id=\"line2d_28\">\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"293.309489\" y=\"139.481037\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_17\">\n      <!-- 100 -->\n      <g transform=\"translate(283.765739 154.079474) 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      </g>\n     </g>\n    </g>\n    <g id=\"xtick_8\">\n     <g id=\"line2d_29\">\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"332.764034\" y=\"139.481037\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_18\">\n      <!-- 200 -->\n      <g transform=\"translate(323.220284 154.079474) scale(0.1 -0.1)\">\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=\"xtick_9\">\n     <g id=\"line2d_30\">\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"372.21858\" y=\"139.481037\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_19\">\n      <!-- 300 -->\n      <g transform=\"translate(362.67483 154.079474) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-33\"/>\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_10\">\n     <g id=\"line2d_31\">\n      <g>\n       <use xlink:href=\"#m8f4227f133\" x=\"411.673125\" y=\"139.481037\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_20\">\n      <!-- 400 -->\n      <g transform=\"translate(402.129375 154.079474) scale(0.1 -0.1)\">\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>\n   <g id=\"matplotlib.axis_4\">\n    <g id=\"ytick_6\">\n     <g id=\"line2d_32\">\n      <g>\n       <use xlink:href=\"#m20fe3fa939\" x=\"253.65767\" y=\"21.314673\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_21\">\n      <!-- 0 -->\n      <g transform=\"translate(240.29517 25.113892) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_7\">\n     <g id=\"line2d_33\">\n      <g>\n       <use xlink:href=\"#m20fe3fa939\" x=\"253.65767\" y=\"60.769219\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_22\">\n      <!-- 100 -->\n      <g transform=\"translate(227.57017 64.568438) 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      </g>\n     </g>\n    </g>\n    <g id=\"ytick_8\">\n     <g id=\"line2d_34\">\n      <g>\n       <use xlink:href=\"#m20fe3fa939\" x=\"253.65767\" y=\"100.223764\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_23\">\n      <!-- 200 -->\n      <g transform=\"translate(227.57017 104.022983) scale(0.1 -0.1)\">\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>\n   <g id=\"patch_9\">\n    <path d=\"M 253.65767 139.481037 \nL 253.65767 21.117401 \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 431.203125 139.481037 \nL 431.203125 21.117401 \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 253.65767 139.481037 \nL 431.203125 139.481037 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_12\">\n    <path d=\"M 253.65767 21.117401 \nL 431.203125 21.117401 \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=\"pe2083f8908\">\n   <rect x=\"40.603125\" y=\"10.999219\" width=\"177.545455\" height=\"138.6\"/>\n  </clipPath>\n  <clipPath id=\"p5fbcee7dd7\">\n   <rect x=\"253.65767\" y=\"21.117401\" width=\"177.545455\" height=\"118.363636\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-17 fw=all -->\n",
    "\n",
    "We can see that the synthesized image\n",
    "retains the scenery and objects of the content image,\n",
    "and transfers the color of the style image\n",
    "at the same time.\n",
    "For example,\n",
    "the synthesized image has blocks of color like\n",
    "those in the style image.\n",
    "Some of these blocks even have the subtle texture of brush strokes.\n",
    "\n",
    "\n",
    "## Summary\n",
    "\n",
    "* The loss function commonly used in style transfer consists of three parts: (i) content loss makes the synthesized image and the content image close in content features; (ii) style loss makes the synthesized image and style image close in style features; and (iii) total variation loss helps to reduce the noise in the synthesized image.\n",
    "* We can use a pretrained CNN to extract image features and minimize the loss function to continuously update the synthesized image as model parameters during training.\n",
    "* We use Gram matrices to represent the style outputs from the style layers.\n",
    "\n",
    "\n",
    "## Exercises\n",
    "\n",
    "1. How does the output change when you select different content and style layers?\n",
    "1. Adjust the weight hyperparameters in the loss function. Does the output retain more content or have less noise?\n",
    "1. Use different content and style images. Can you create more interesting synthesized images?\n",
    "1. Can we apply style transfer for text? Hint: you may refer to the survey paper by @10.1145/3544903.3544906."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=neural-style-md-18 fw=mxnet -->\n",
    "\n",
    "[Discussions](https://d2l.discourse.group/t/378)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "d2l-mxnet",
   "display_name": "d2l (MXNet)",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
