{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dynamic U-Net" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This module builds a dynamic [U-Net](https://arxiv.org/abs/1505.04597) from any backbone pretrained on ImageNet, automatically inferring the intermediate sizes." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.gen_doc.nbdoc import *\n", "from fastai.vision.models.unet import * " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![U-Net architecure](imgs/u-net-architecture.png)\n", "\n", "This is the original U-Net. The difference here is that the left part is a pretrained model." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class DynamicUnet[source]

\n", "\n", "> DynamicUnet(`encoder`:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), `n_classes`:`int`, `all_wn`:`bool`=`False`, `blur`:`bool`=`False`) :: [`Sequential`](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(DynamicUnet, doc_string=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Builds a U-Net from a given `encoder` (that can be a pretrained model) and with a final output of `n_classes`. During the initialization, it uses [`Hooks`](/callbacks.hooks.html#Hooks) to determine the intermediate features sizes by passing a dummy input throught the model. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class UnetBlock[source]

\n", "\n", "> UnetBlock(`up_in_c`:`int`, `x_in_c`:`int`, `hook`:[`Hook`](/callbacks.hooks.html#Hook), `final_div`:`bool`=`True`, `all_wn`:`bool`=`False`, `blur`:`bool`=`False`) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(UnetBlock, doc_string=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Builds a U-Net block that receives the output of the last block to be upsampled (size `up_in_c`) and the activations features from an intermediate layer of the `encoder` (size `x_in_c`, this is the lateral connection). The `hook` is set to this intermediate layer to store the output needed for this block." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Undocumented Methods - Methods moved below this line will intentionally be hidden" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## New Methods - Please document or move to the undocumented section" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

forward[source]

\n", "\n", "> forward(`up_in`:`Tensor`) → `Tensor`\n", "\n", "Defines the computation performed at every call. Should be overridden by all subclasses.\n", "\n", ".. note::\n", " Although the recipe for forward pass needs to be defined within\n", " this function, one should call the :class:`Module` instance afterwards\n", " instead of this since the former takes care of running the\n", " registered hooks while the latter silently ignores them. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(UnetBlock.forward)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "jekyll": { "keywords": "fastai", "summary": "Dynamic Unet that can use any pretrained model as a backbone.", "title": "vision.models.unet" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }