
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "usage_examples/graph/plot_resnet18.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_usage_examples_graph_plot_resnet18.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_usage_examples_graph_plot_resnet18.py:

ResNet-18
=======================================

A real, torchvision-provided architecture (not a toy model) - `resnet18` has 8 residual blocks
across 4 stages, with a projection shortcut (an extra Conv2d+BatchNorm2d in the skip path) at
the first block of each of the last 3 stages, where the spatial size is downsampled and the
channel count changes. Each residual skip connection is correctly routed above the diagram.

Conv2d is orange, BatchNorm2d is green, and ReLU is sky blue.

.. GENERATED FROM PYTHON SOURCE LINES 11-36



.. image-sg:: /usage_examples/graph/images/sphx_glr_plot_resnet18_001.png
   :alt: plot resnet18
   :srcset: /usage_examples/graph/images/sphx_glr_plot_resnet18_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    from collections import defaultdict

    import matplotlib.pyplot as plt
    import visualtorch
    from torch import nn
    from torchvision.models import resnet18

    model = resnet18(weights=None, num_classes=10)

    input_shape = (1, 3, 64, 64)

    color_map: dict = defaultdict(dict)
    color_map[nn.Conv2d]["fill"] = "#E69F00"
    color_map[nn.BatchNorm2d]["fill"] = "#009E73"
    color_map[nn.ReLU]["fill"] = "#56B4E9"

    img = visualtorch.render(model, input_shape, style="graph", show_neurons=False, color_map=color_map, layer_spacing=60)

    dpi = 150  # rendered at 2x this in the final doc build (savefig.dpi=300 in conf.py)
    plt.figure(figsize=(img.width / dpi, img.height / dpi), dpi=dpi)
    plt.imshow(img)
    plt.axis("off")
    plt.tight_layout()
    plt.show()


.. _sphx_glr_download_usage_examples_graph_plot_resnet18.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_resnet18.ipynb <plot_resnet18.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_resnet18.py <plot_resnet18.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_resnet18.zip <plot_resnet18.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
