{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## GANs" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hide_input": true }, "outputs": [], "source": [ "%matplotlib inline\n", "from fastai.gen_doc.nbdoc import *\n", "from fastai.vision import * \n", "from fastai.vision.gan import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "GAN stands for [Generative Adversarial Nets](https://arxiv.org/pdf/1406.2661.pdf) and were invented by Ian Goodfellow. The concept is that we will train two models at the same time: a generator and a critic. The generator will try to make new images similar to the ones in our dataset, and the critic's job will try to classify real images from the fake ones the generator does. The generator returns images, the discriminator a feature map (it can be a single number depending on the input size). Usually the discriminator will be trained to return 0. everywhere for fake images and 1. everywhere for real ones.\n", "\n", "This module contains all the necessary function to create a GAN." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We train them against each other in the sense that at each step (more or less), we:\n", "1. Freeze the generator and train the discriminator for one step by:\n", " - getting one batch of true images (let's call that `real`)\n", " - generating one batch of fake images (let's call that `fake`)\n", " - have the discriminator evaluate each batch and compute a loss function from that; the important part is that it rewards positively the detection of real images and penalizes the fake ones\n", " - update the weights of the discriminator with the gradients of this loss\n", " \n", " \n", "2. Freeze the discriminator and train the generator for one step by:\n", " - generating one batch of fake images\n", " - evaluate the discriminator on it\n", " - return a loss that rewards posisitivly the discriminator thinking those are real images; the important part is that it rewards positively the detection of real images and penalizes the fake ones\n", " - update the weights of the generator with the gradients of this loss" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class GANLearner[source][test]

\n", "\n", "> GANLearner(**`data`**:[`DataBunch`](/basic_data.html#DataBunch), **`generator`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`critic`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`gen_loss_func`**:`LossFunction`, **`crit_loss_func`**:`LossFunction`, **`switcher`**:[`Callback`](/callback.html#Callback)=***`None`***, **`gen_first`**:`bool`=***`False`***, **`switch_eval`**:`bool`=***`True`***, **`show_img`**:`bool`=***`True`***, **`clip`**:`float`=***`None`***, **\\*\\*`learn_kwargs`**) :: [`Learner`](/basic_train.html#Learner)\n", "\n", "
×

No tests found for GANLearner. To contribute a test please refer to this guide and this discussion.

\n", "\n", "A [`Learner`](/basic_train.html#Learner) suitable for GANs. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANLearner)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the general constructor to create a GAN, you might want to use one of the factory methods that are easier to use. Create a GAN from [`data`](/vision.data.html#vision.data), a `generator` and a `critic`. The [`data`](/vision.data.html#vision.data) should have the inputs the `generator` will expect and the images wanted as targets.\n", "\n", "`gen_loss_func` is the loss function that will be applied to the `generator`. It takes three argument `fake_pred`, `target`, `output` and should return a rank 0 tensor. `output` is the result of the `generator` applied to the input (the xs of the batch), `target` is the ys of the batch and `fake_pred` is the result of the `discriminator` being given `output`. `output`and `target` can be used to add a specific loss to the GAN loss (pixel loss, feature loss) and for a good training of the gan, the loss should encourage `fake_pred` to be as close to 1 as possible (the `generator` is trained to fool the `critic`).\n", "\n", "`crit_loss_func` is the loss function that will be applied to the `critic`. It takes two arguments `real_pred` and `fake_pred`. `real_pred` is the result of the `critic` on the target images (the ys of the batch) and `fake_pred` is the result of the `critic` applied on a batch of fake, generated byt the `generator` from the xs of the batch.\n", "\n", "`switcher` is a [`Callback`](/callback.html#Callback) that should tell the GAN when to switch from critic to generator and vice versa. By default it does 5 iterations of the critic for 1 iteration of the generator. The model begins the training with the `generator` if `gen_first=True`. If `switch_eval=True`, the model that isn't trained is switched on eval mode (left in training mode otherwise, which means some statistics like the running mean in batchnorm layers are updated, or the dropouts are applied).\n", "\n", "`clip` should be set to a certain value if one wants to clip the weights (see the [Wassertein GAN](https://arxiv.org/pdf/1701.07875.pdf) for instance).\n", "\n", "If `show_img=True`, one image generated by the GAN is shown at the end of each epoch." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Factory methods" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

from_learners[source][test]

\n", "\n", "> from_learners(**`learn_gen`**:[`Learner`](/basic_train.html#Learner), **`learn_crit`**:[`Learner`](/basic_train.html#Learner), **`switcher`**:[`Callback`](/callback.html#Callback)=***`None`***, **`weights_gen`**:`Point`=***`None`***, **\\*\\*`learn_kwargs`**)\n", "\n", "
×

No tests found for from_learners. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Create a GAN from `learn_gen` and `learn_crit`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANLearner.from_learners)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Directly creates a [`GANLearner`](/vision.gan.html#GANLearner) from two [`Learner`](/basic_train.html#Learner): one for the `generator` and one for the `critic`. The `switcher` and all `kwargs` will be passed to the initialization of [`GANLearner`](/vision.gan.html#GANLearner) along with the following loss functions:\n", "\n", "- `loss_func_crit` is the mean of `learn_crit.loss_func` applied to `real_pred` and a target of ones with `learn_crit.loss_func` applied to `fake_pred` and a target of zeros\n", "- `loss_func_gen` is the mean of `learn_crit.loss_func` applied to `fake_pred` and a target of ones (to full the discriminator) with `learn_gen.loss_func` applied to `output` and `target`. The weights of each of those contributions can be passed in `weights_gen` (default is 1. and 1.)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

wgan[source][test]

\n", "\n", "> wgan(**`data`**:[`DataBunch`](/basic_data.html#DataBunch), **`generator`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`critic`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`switcher`**:[`Callback`](/callback.html#Callback)=***`None`***, **`clip`**:`float`=***`0.01`***, **\\*\\*`learn_kwargs`**)\n", "\n", "
×

No tests found for wgan. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Create a WGAN from `data`, `generator` and `critic`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANLearner.wgan)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Wasserstein GAN is detailed in [this article]. `switcher` and the `kwargs` will be passed to the [`GANLearner`](/vision.gan.html#GANLearner) init, `clip`is the weight clipping." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Switchers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In any GAN training, you will need to tell the [`Learner`](/basic_train.html#Learner) when to switch from generator to critic and vice versa. The two following [`Callback`](/callback.html#Callback) are examples to help you with that.\n", "\n", "As usual, don't call the `on_something` methods directly, the fastai library will do it for you during training." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class FixedGANSwitcher[source][test]

\n", "\n", "> FixedGANSwitcher(**`learn`**:[`Learner`](/basic_train.html#Learner), **`n_crit`**:`Union`\\[`int`, `Callable`\\]=***`1`***, **`n_gen`**:`Union`\\[`int`, `Callable`\\]=***`1`***) :: [`LearnerCallback`](/basic_train.html#LearnerCallback)\n", "\n", "
×

No tests found for FixedGANSwitcher. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Switcher to do `n_crit` iterations of the critic then `n_gen` iterations of the generator. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(FixedGANSwitcher, title_level=3)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_train_begin[source][test]

\n", "\n", "> on_train_begin(**\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_train_begin. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Initiate the iteration counts. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(FixedGANSwitcher.on_train_begin)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_batch_end[source][test]

\n", "\n", "> on_batch_end(**`iteration`**, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_batch_end. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Switch the model if necessary. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(FixedGANSwitcher.on_batch_end)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class AdaptiveGANSwitcher[source][test]

\n", "\n", "> AdaptiveGANSwitcher(**`learn`**:[`Learner`](/basic_train.html#Learner), **`gen_thresh`**:`float`=***`None`***, **`critic_thresh`**:`float`=***`None`***) :: [`LearnerCallback`](/basic_train.html#LearnerCallback)\n", "\n", "
×

No tests found for AdaptiveGANSwitcher. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Switcher that goes back to generator/critic when the loss goes below `gen_thresh`/`crit_thresh`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(AdaptiveGANSwitcher, title_level=3)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_batch_end[source][test]

\n", "\n", "> on_batch_end(**`last_loss`**, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_batch_end. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Switch the model if necessary. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(AdaptiveGANSwitcher.on_batch_end)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Discriminative LR" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to train your critic at a different learning rate than the generator, this will let you do it automatically (even if you have a learning rate schedule)." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class GANDiscriminativeLR[source][test]

\n", "\n", "> GANDiscriminativeLR(**`learn`**:[`Learner`](/basic_train.html#Learner), **`mult_lr`**:`float`=***`5.0`***) :: [`LearnerCallback`](/basic_train.html#LearnerCallback)\n", "\n", "
×

No tests found for GANDiscriminativeLR. To contribute a test please refer to this guide and this discussion.

\n", "\n", "[`Callback`](/callback.html#Callback) that handles multiplying the learning rate by `mult_lr` for the critic. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANDiscriminativeLR, title_level=3)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_batch_begin[source][test]

\n", "\n", "> on_batch_begin(**`train`**, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_batch_begin. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Multiply the current lr if necessary. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANDiscriminativeLR.on_batch_begin)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_step_end[source][test]

\n", "\n", "> on_step_end(**\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_step_end. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Put the LR back to its value if necessary. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANDiscriminativeLR.on_step_end)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Specific models" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

basic_critic[source][test]

\n", "\n", "> basic_critic(**`in_size`**:`int`, **`n_channels`**:`int`, **`n_features`**:`int`=***`64`***, **`n_extra_layers`**:`int`=***`0`***, **\\*\\*`conv_kwargs`**)\n", "\n", "
×

Tests found for basic_critic:

  • pytest -sv tests/test_vision_gan.py::test_basic_critic [source]

Some other tests where basic_critic is used:

  • pytest -sv tests/test_vision_gan.py::test_gan_module [source]

To run tests please refer to this guide.

\n", "\n", "A basic critic for images `n_channels` x `in_size` x `in_size`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(basic_critic)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This model contains a first 4 by 4 convolutional layer of stride 2 from `n_channels` to `n_features` followed by `n_extra_layers` 3 by 3 convolutional layer of stride 1. Then we put as many 4 by 4 convolutional layer of stride 2 with a number of features multiplied by 2 at each stage so that the `in_size` becomes 1. `kwargs` can be used to customize the convolutional layers and are passed to [`conv_layer`](/layers.html#conv_layer)." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

basic_generator[source][test]

\n", "\n", "> basic_generator(**`in_size`**:`int`, **`n_channels`**:`int`, **`noise_sz`**:`int`=***`100`***, **`n_features`**:`int`=***`64`***, **`n_extra_layers`**=***`0`***, **\\*\\*`conv_kwargs`**)\n", "\n", "
×

Tests found for basic_generator:

  • pytest -sv tests/test_vision_gan.py::test_basic_generator [source]

Some other tests where basic_generator is used:

  • pytest -sv tests/test_vision_gan.py::test_gan_module [source]

To run tests please refer to this guide.

\n", "\n", "A basic generator from `noise_sz` to images `n_channels` x `in_size` x `in_size`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(basic_generator)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This model contains a first 4 by 4 transposed convolutional layer of stride 1 from `noise_size` to the last numbers of features of the corresponding critic. Then we put as many 4 by 4 transposed convolutional layer of stride 2 with a number of features divided by 2 at each stage so that the image ends up being of height and widht `in_size//2`. At the end, we add`n_extra_layers` 3 by 3 convolutional layer of stride 1. The last layer is a transpose convolution of size 4 by 4 and stride 2 followed by `tanh`. `kwargs` can be used to customize the convolutional layers and are passed to [`conv_layer`](/layers.html#conv_layer)." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

gan_critic[source][test]

\n", "\n", "> gan_critic(**`n_channels`**:`int`=***`3`***, **`nf`**:`int`=***`128`***, **`n_blocks`**:`int`=***`3`***, **`p`**:`int`=***`0.15`***)\n", "\n", "
×

No tests found for gan_critic. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Critic to train a `GAN`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(gan_critic)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class GANTrainer[source][test]

\n", "\n", "> GANTrainer(**`learn`**:[`Learner`](/basic_train.html#Learner), **`switch_eval`**:`bool`=***`False`***, **`clip`**:`float`=***`None`***, **`beta`**:`float`=***`0.98`***, **`gen_first`**:`bool`=***`False`***, **`show_img`**:`bool`=***`True`***) :: [`LearnerCallback`](/basic_train.html#LearnerCallback)\n", "\n", "
×

Tests found for GANTrainer:

  • pytest -sv tests/test_vision_gan.py::test_gan_trainer [source]

To run tests please refer to this guide.

\n", "\n", "Handles GAN Training. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANTrainer)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[`LearnerCallback`](/basic_train.html#LearnerCallback) that will be responsible to handle the two different optimizers (one for the generator and one for the critic), and do all the work behind the scenes so that the generator (or the critic) are in training mode with parameters requirement gradients each time we switch.\n", "\n", "`switch_eval=True` means that the [`GANTrainer`](/vision.gan.html#GANTrainer) will put the model that isn't training into eval mode (if it's `False` its running statistics like in batchnorm layers will be updated and dropout will be applied). `clip` is the clipping applied to the weights (if not `None`). `beta` is the coefficient for the moving averages as the [`GANTrainer`](/vision.gan.html#GANTrainer)tracks separately the generator loss and the critic loss. `gen_first=True` means the training begins with the generator (with the critic if it's `False`). If `show_img=True` we show a generated image at the end of each epoch." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

switch[source][test]

\n", "\n", "> switch(**`gen_mode`**:`bool`=***`None`***)\n", "\n", "
×

Tests found for switch:

Some other tests where switch is used:

  • pytest -sv tests/test_vision_gan.py::test_gan_module [source]

To run tests please refer to this guide.

\n", "\n", "Switch the model, if `gen_mode` is provided, in the desired mode. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANTrainer.switch)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If `gen_mode` is left as `None`, just put the model in the other mode (critic if it was in generator mode and vice versa)." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_train_begin[source][test]

\n", "\n", "> on_train_begin(**\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_train_begin. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Create the optimizers for the generator and critic if necessary, initialize smootheners. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANTrainer.on_train_begin)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_epoch_begin[source][test]

\n", "\n", "> on_epoch_begin(**`epoch`**, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_epoch_begin. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Put the critic or the generator back to eval if necessary. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANTrainer.on_epoch_begin)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_batch_begin[source][test]

\n", "\n", "> on_batch_begin(**`last_input`**, **`last_target`**, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_batch_begin. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Clamp the weights with `self.clip` if it's not None, return the correct input. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANTrainer.on_batch_begin)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_backward_begin[source][test]

\n", "\n", "> on_backward_begin(**`last_loss`**, **`last_output`**, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_backward_begin. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Record `last_loss` in the proper list. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANTrainer.on_backward_begin)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_epoch_end[source][test]

\n", "\n", "> on_epoch_end(**`pbar`**, **`epoch`**, **`last_metrics`**, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_epoch_end. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Put the various losses in the recorder and show a sample image. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANTrainer.on_epoch_end)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_train_end[source][test]

\n", "\n", "> on_train_end(**\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for on_train_end. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Switch in generator mode for showing results. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANTrainer.on_train_end)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Specific modules" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class GANModule[source][test]

\n", "\n", "> GANModule(**`generator`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)=***`None`***, **`critic`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)=***`None`***, **`gen_mode`**:`bool`=***`False`***) :: [`PrePostInitMeta`](/core.html#PrePostInitMeta) :: [`Module`](/torch_core.html#Module)\n", "\n", "
×

Tests found for GANModule:

  • pytest -sv tests/test_vision_gan.py::test_gan_module [source]

To run tests please refer to this guide.

\n", "\n", "Wrapper around a `generator` and a `critic` to create a GAN. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANModule, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If `gen_mode` is left as `None`, just put the model in the other mode (critic if it was in generator mode and vice versa)." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

switch[source][test]

\n", "\n", "> switch(**`gen_mode`**:`bool`=***`None`***)\n", "\n", "
×

Tests found for switch:

Some other tests where switch is used:

  • pytest -sv tests/test_vision_gan.py::test_gan_module [source]

To run tests please refer to this guide.

\n", "\n", "Put the model in generator mode if `gen_mode`, in critic mode otherwise. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANModule.switch)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class GANLoss[source][test]

\n", "\n", "> GANLoss(**`loss_funcG`**:`Callable`, **`loss_funcC`**:`Callable`, **`gan_model`**:[`GANModule`](/vision.gan.html#GANModule)) :: [`PrePostInitMeta`](/core.html#PrePostInitMeta) :: [`GANModule`](/vision.gan.html#GANModule)\n", "\n", "
×

No tests found for GANLoss. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Wrapper around `loss_funcC` (for the critic) and `loss_funcG` (for the generator). " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANLoss, title_level=3)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class AdaptiveLoss[source][test]

\n", "\n", "> AdaptiveLoss(**`crit`**) :: [`PrePostInitMeta`](/core.html#PrePostInitMeta) :: [`Module`](/torch_core.html#Module)\n", "\n", "
×

No tests found for AdaptiveLoss. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Expand the `target` to match the `output` size before applying `crit`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(AdaptiveLoss, title_level=3)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

accuracy_thresh_expand[source][test]

\n", "\n", "> accuracy_thresh_expand(**`y_pred`**:`Tensor`, **`y_true`**:`Tensor`, **`thresh`**:`float`=***`0.5`***, **`sigmoid`**:`bool`=***`True`***) → `Rank0Tensor`\n", "\n", "
×

No tests found for accuracy_thresh_expand. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Compute accuracy after expanding `y_true` to the size of `y_pred`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(accuracy_thresh_expand)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Block API" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class NoisyItem[source][test]

\n", "\n", "> NoisyItem(**`noise_sz`**) :: [`ItemBase`](/core.html#ItemBase)\n", "\n", "
×

Tests found for NoisyItem:

  • pytest -sv tests/test_vision_gan.py::test_noisy_item [source]

To run tests please refer to this guide.

\n", "\n", "An random [`ItemBase`](/core.html#ItemBase) of size `noise_sz`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(NoisyItem, title_level=3)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class GANItemList[source][test]

\n", "\n", "> GANItemList(**`items`**, **`noise_sz`**:`int`=***`100`***, **\\*\\*`kwargs`**) :: [`ImageList`](/vision.data.html#ImageList)\n", "\n", "
×

Tests found for GANItemList:

Some other tests where GANItemList is used:

  • pytest -sv tests/test_vision_gan.py::test_gan_datasets [source]

To run tests please refer to this guide.

\n", "\n", "`ItemList` suitable for GANs. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANItemList, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inputs will be [`NoisyItem`](/vision.gan.html#NoisyItem) of `noise_sz` while the default class for target is [`ImageList`](/vision.data.html#ImageList)." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

show_xys[source][test]

\n", "\n", "> show_xys(**`xs`**, **`ys`**, **`imgsize`**:`int`=***`4`***, **`figsize`**:`Optional`\\[`Tuple`\\[`int`, `int`\\]\\]=***`None`***, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for show_xys. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Shows `ys` (target images) on a figure of `figsize`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANItemList.show_xys)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

show_xyzs[source][test]

\n", "\n", "> show_xyzs(**`xs`**, **`ys`**, **`zs`**, **`imgsize`**:`int`=***`4`***, **`figsize`**:`Optional`\\[`Tuple`\\[`int`, `int`\\]\\]=***`None`***, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for show_xyzs. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Shows `zs` (generated images) on a figure of `figsize`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANItemList.show_xyzs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Undocumented Methods - Methods moved below this line will intentionally be hidden" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

critic[source][test]

\n", "\n", "> critic(**`real_pred`**, **`input`**)\n", "\n", "
×

Tests found for critic:

Some other tests where critic is used:

  • pytest -sv tests/test_vision_gan.py::test_basic_critic [source]

To run tests please refer to this guide.

\n", "\n", "Create some `fake_pred` with the generator from `input` and compare them to `real_pred` in `self.loss_funcD`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANLoss.critic)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

forward[source][test]

\n", "\n", "> forward(**\\*`args`**)\n", "\n", "
×

No tests found for forward. To contribute a test please refer to this guide and this discussion.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANModule.forward)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

generator[source][test]

\n", "\n", "> generator(**`output`**, **`target`**)\n", "\n", "
×

Tests found for generator:

Some other tests where generator is used:

  • pytest -sv tests/test_vision_gan.py::test_basic_generator [source]

To run tests please refer to this guide.

\n", "\n", "Evaluate the `output` with the critic then uses `self.loss_funcG` to combine it with `target`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANLoss.generator)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

apply_tfms[source][test]

\n", "\n", "> apply_tfms(**`tfms`**, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for apply_tfms. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Subclass this method if you want to apply data augmentation with `tfms` to this [`ItemBase`](/core.html#ItemBase). " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(NoisyItem.apply_tfms)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

forward[source][test]

\n", "\n", "> forward(**`output`**, **`target`**)\n", "\n", "
×

No tests found for forward. To contribute a test please refer to this guide and this discussion.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(AdaptiveLoss.forward)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

get[source][test]

\n", "\n", "> get(**`i`**)\n", "\n", "
×

No tests found for get. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Subclass if you want to customize how to create item `i` from `self.items`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANItemList.get)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

reconstruct[source][test]

\n", "\n", "> reconstruct(**`t`**)\n", "\n", "
×

No tests found for reconstruct. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Reconstruct one of the underlying item for its data `t`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(GANItemList.reconstruct)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

forward[source][test]

\n", "\n", "> forward(**`output`**, **`target`**)\n", "\n", "
×

No tests found for forward. To contribute a test please refer to this guide and this discussion.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(AdaptiveLoss.forward)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## New Methods - Please document or move to the undocumented section" ] } ], "metadata": { "jekyll": { "keywords": "fastai", "summary": "All the modules and callbacks necessary to train a GAN", "title": "vision.gan" }, "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.8.2" } }, "nbformat": 4, "nbformat_minor": 2 }