{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\npynet network helpers overview\n==============================\n\nCredit: A Grigis\n\npynet is a Python package related to deep learning and its application in\nMRI mediacal data analysis. It is accessible to everybody, and is reusable\nin various contexts. The project is hosted on github:\nhttps://github.com/neurospin/pynet.\n\nFirst checks\n------------\n\nIn order to test if the 'pynet' package is installed on your machine, you can\ncheck the package version.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pynet\nprint(pynet.__version__)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now you can run the the configuration info function to see if all the\ndependencies are installed properly.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pynet.configure\nprint(pynet.configure.info())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load a network\n--------------\n\nFrom the available netwoks load the UNet.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport torch\nfrom pynet.models import UNet\nfrom pynet.plotting.network import plot_net, plot_net_rescue\n\nmodel = UNet(\n    num_classes=2,\n    in_channels=1,\n    depth=3,\n    start_filts=8,\n    up_mode=\"upsample\",\n    merge_mode=\"concat\",\n    batchnorm=True)\nif \"CI_MODE\" not in os.environ:\n    plot_net_rescue(model, shape=(1, 1, 64, 64, 64), outfileroot=None)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Inspect a network\n-----------------\n\nThe module propose utilities to inspect easyly some layers of the network.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pynet.utils import test_model\nfrom pprint import pprint\nimport numpy as np\nfrom pynet.utils import get_named_layers\nfrom pynet.utils import layer_at\nfrom pynet.plotting import plot_data\n\nout = test_model(model, shape=(1, 1, 64, 64, 64))\nlayers = get_named_layers(model)\npprint(layers)\nhook_x, weight = layer_at(\n    model=model,\n    layer_name=\"down.1.doubleconv.conv1-8.16\",\n    x=torch.FloatTensor(np.random.random((1, 1, 64, 64, 64))))\nprint(hook_x.shape)\nprint(weight.shape)\nplot_data(hook_x[:, :1])\n\nif \"CI_MODE\" not in os.environ:\n    import matplotlib.pyplot as plt\n    plt.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.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}