{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nPixel interpolation learning\n============================\n\nwe demonstrate in this toy example how to use the coordinate\ninterpolation techniques with learnable parameter to\nmap one image to another one simply by interpolation the original\nimage values from learned coordinates\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import symjax\nimport symjax.tensor as T\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport os\n\nos.environ[\"DATASET_PATH\"] = \"/home/vrael/DATASETS/\"\n\nsymjax.current_graph().reset()\n\n\nmnist = symjax.data.mnist()\n# 2d image\nimages = mnist[\"train_set/images\"][mnist[\"train_set/labels\"] == 2][:2, 0]\nimages /= images.max()\n\nnp.random.seed(0)\n\ncoordinates = T.meshgrid(T.range(28), T.range(28))\ncoordinates = T.Variable(\n    T.stack([coordinates[1].flatten(), coordinates[0].flatten()]).astype(\"float32\")\n)\ninterp = T.interpolation.map_coordinates(images[0], coordinates, order=1).reshape(\n    (28, 28)\n)\n\nloss = ((interp - images[1]) ** 2).mean()\n\nlr = symjax.nn.schedules.PiecewiseConstant(0.05, {5000: 0.01, 8000: 0.005})\nsymjax.nn.optimizers.Adam(loss, lr)\n\ntrain = symjax.function(outputs=loss, updates=symjax.get_updates())\n\nrec = symjax.function(outputs=interp)\n\nlosses = list()\n\noriginal = coordinates.value\n\nfor i in range(100):\n    losses.append(train())\n\nreconstruction = rec()\n\nafter = coordinates.value\n\n\nplt.figure(figsize=(12, 6))\n\nplt.subplot(311)\nplt.semilogy(losses, \"-x\")\nplt.ylabel(\"loss (l2)\")\nplt.title(\"Training loss\")\n\n\nplt.subplot(334)\nplt.imshow(images[0], aspect=\"auto\", cmap=\"plasma\")\nplt.xticks([])\nplt.yticks([])\nplt.title(\"input\")\n\nplt.subplot(335)\nplt.imshow(images[1], aspect=\"auto\", cmap=\"plasma\")\nplt.xticks([])\nplt.yticks([])\nplt.title(\"target\")\n\nplt.subplot(336)\nplt.imshow(reconstruction, aspect=\"auto\", cmap=\"plasma\")\nplt.xticks([])\nplt.yticks([])\nplt.title(\"reconstruction\")\n\n\nprint(original)\n\nplt.subplot(325)\nplt.scatter(original[1][::-1], original[0], s=3)\nplt.xticks([])\nplt.yticks([])\nplt.title(\"Initialized coordinates\")\n\nplt.subplot(326)\nplt.scatter(after[1][::-1], after[0], s=3)\nplt.xticks([])\nplt.yticks([])\nplt.title(\"Learned coordinates\")\n\n\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}