"The aim of this assignment is to discover GANs, understand how they are implemented and then explore one specific architecture of GANs that allows us to perform image to image translation (which corresponds to the picture that you can see above this text ! )\n",
"\n",
"Before starting the exploration of the world of GANs, here's what students should do and send back for this assignement : \n",
"* In the \"tutorial\" parts of this assignement that focus on explaining new concepts, you'll find <font color='red'>**questions**</font> that aim to test your understanding of those concepts. \n",
"* In some of the code cells, you'll have to complete the code and you'll find a \"TO DO\" explaining what you should implement."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "M-WNKvhOP1ED"
},
"source": [
"# Part1: DC-GAN"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "y_r8nMTGQI9a"
},
"source": [
"In this part, we aim to learn and understand the basic concepts of **Generative Adversarial Networks** through a DCGAN and generate new celebrities from the learned network after showing it real celebrities. For this purpose, please study the tutorial here: https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "jiHCy4_UUBFb"
},
"source": [
"##Work to do\n",
"Now we want to generate handwritten digits using the MNIST dataset. It is available within torvision package (https://pytorch.org/vision/stable/generated/torchvision.datasets.MNIST.html#torchvision.datasets.MNIST)\n",
"\n",
"Please re-train the DCGAN and display some automatically generated handwritten digits.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "sIL7UvYAZx6L"
},
"outputs": [],
"source": [
"#TO DO: your code here to adapt the code from the tutorial to experiment on MNIST dataset"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "5fbSgsrE1GqC"
},
"source": [
"# Part2: Conditional GAN (cGAN)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "7SjXNoT7BUey"
},
"source": [
"Let's take the example of the set described in the next picture.\n",
"\n",
"\n",
"We have a picture of a map (from Google Maps) and we want to create an image of what the satellite view may look like.\n",
"\n",
"As we are not only trying to generate a random picture but a mapping between a picture to another one, we can't use the standard GAN architecture. We will then use a cGAN.\n",
"\n",
"A cGAN is a supervised GAN aiming at mapping a label picture to a real one or a real picture to a label one. As you can see in the diagram below, the discriminator will take as input a pair of images and try to predict if the pair was generated or not. The generator will not only generate an image from noise but will also use an image (label or real) to generate another one (real or label).\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "0JRaeHfzl6cO"
},
"source": [
"### Generator\n",
"\n",
"In the cGAN architecture, the generator chosen is a U-Net.\n",
"A U-Net takes as input an image, and outputs another image. \n",
"\n",
"It can be divided into 2 subparts : an encoder and a decoder. \n",
"* The encoder takes the input image and reduces its dimension to encode the main features into a vector. \n",
"* The decoder takes this vector and map the features stored into an image.\n",
"\n",
"A U-Net architecture is different from a classic encoder-decoder in that every layer of the decoder takes as input the previous decoded output as well as the output vector from the encoder layers of the same level. It allows the decoder to map low frequencies information encoded during the descent as well as high frequencies from the original picture. \n",
"The encoder will take as input a colored picture (3 channels: RGB), it will pass through a series of convolution layers to encode the features of the picture. It will then be decoded by the decoder using transposed convolutional layers. These layers will take as input the previous decoded vector AND the encoded features of the same level. "
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "yzy7y4hmbbX3"
},
"source": [
"Now, let's create or cGAN to generate facades from a template image. For this purpose, we will use the \"Facade\" dataset available at http://cmp.felk.cvut.cz/~tylecr1/facade/.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "Q_jf9H_NDESm"
},
"source": [
"Let's first create a few classes describing the layers we will use in the U-Net."
" # At this stage x8 is our encoded vector, we will now decode it\n",
" x = self.up7(x8, x7)\n",
" x = self.up6(x, x6)\n",
" x = self.up5(x, x5)\n",
" x = self.up4(x, x4)\n",
" x = self.up3(x, x3)\n",
" x = self.up2(x, x2)\n",
" x = self.up1(x, x1)\n",
" x = self.outc(x)\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "1hmcejTWJSYY"
},
"outputs": [],
"source": [
"# We take images that have 3 channels (RGB) as input and output an image that also have 3 channels (RGB)\n",
"generator=U_Net(3,3)\n",
"# Check that the architecture is as expected\n",
"generator"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "xIXFtHzcBUfO"
},
"source": [
"You should now have a working U-Net."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "RqD1katYBUfP"
},
"source": [
"<font color='red'>**Question 1**</font> \n",
"Knowing the input and output images will be 256x256, what will be the dimension of the encoded vector x8 ?\n",
"\n",
"<font color='red'>**Question 2**</font> \n",
"As you can see, U-net has an encoder-decoder architecture with skip connections. Explain why it works better than a traditional encoder-decoder."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "cchTp3thBUfR"
},
"source": [
"### Discriminator\n",
"\n",
"In the cGAN architecture, the chosen discriminator is a Patch GAN. It is a convolutional discriminator which enables to produce a map of the input pictures where each pixel represents a patch of size NxN of the input.\n",
"The size N is given by the depth of the net. According to this table :\n",
"\n",
"| Number of layers | N |\n",
"| ---- | ---- |\n",
"| 1 | 16 |\n",
"| 2 | 34 |\n",
"| 3 | 70 |\n",
"| 4 | 142 |\n",
"| 5 | 286 |\n",
"| 6 | 574 |\n",
"\n",
"The number of layers actually means the number of layers with `kernel=(4,4)`, `padding=(1,1)` and `stride=(2,2)`. These layers are followed by 2 layers with `kernel=(4,4)`, `padding=(1,1)` and `stride=(1,1)`.\n",
"In our case we are going to create a 70x70 PatchGAN."
"Now let's create the Patch GAN discriminator.\n",
"As we want a 70x70 Patch GAN, the architecture will be as follows :\n",
"```\n",
"1. C64 - K4, P1, S2\n",
"2. C128 - K4, P1, S2\n",
"3. C256 - K4, P1, S2\n",
"4. C512 - K4, P1, S1\n",
"5. C1 - K4, P1, S1 (output)\n",
"```\n",
"Where Ck denotes a convolution block with k filters, Kk a kernel of size k, Pk is the padding size and Sk the stride applied.\n",
"*Note :* For the first layer, we do not use batchnorm."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "AH6u5a-PBUfg"
},
"source": [
"<font color='red'>**Question 3**</font> \n",
"Knowing the input and output images will be 256x256, what will be the dimension of the encoded vector x8 ?Knowing input images will be 256x256 with 3 channels each, how many parameters are there to learn ?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "g_9LxNhGBUfi"
},
"outputs": [],
"source": [
"class PatchGAN(nn.Module):\n",
" def __init__(self, n_channels, n_classes):\n",
" super(PatchGAN, self).__init__()\n",
" # TODO :\n",
" # create the 4 first layers named conv1 to conv4\n",
" self.conv1 =\n",
" self.conv2 =\n",
" self.conv3 =\n",
" self.conv4 =\n",
" # output layer\n",
" self.out = out_block(512, n_classes)\n",
" \n",
" def forward(self, x1, x2):\n",
" x = torch.cat([x2, x1], dim=1)\n",
" x = self.conv1(x)\n",
" x = self.conv2(x)\n",
" x = self.conv3(x)\n",
" x = self.conv4(x)\n",
" x = self.out(x)\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "W_sevZRnBUfn"
},
"outputs": [],
"source": [
"# We have 6 input channels as we concatenate 2 images (with 3 channels each)\n",
"discriminator = PatchGAN(6,1)\n",
"discriminator"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "v_QubOycBUfv"
},
"source": [
"You should now have a working discriminator."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "DiI2CByRBUfz"
},
"source": [
"### Loss functions\n",
"\n",
"As we have seen in the choice of the various architectures for this GAN, the issue is to map both low and high frequencies.\n",
"To tackle this problem, this GAN rely on the architecture to map the high frequencies (U-Net + PatchGAN) and the loss function to learn low frequencies features. The global loss function will indeed be made of 2 parts :\n",
"* the first part to map hight frequencies, will try to optimize the mean squared error of the GAN.\n",
"* the second part to map low frequencies, will minimize the $\\mathcal{L}_1$ norm of the generated picture.\n",
"\n",
"So the loss can be defined as $$ G^* = arg\\ \\underset{G}{min}\\ \\underset{D}{max}\\ \\mathcal{L}_{cGAN}(G,D) + \\lambda \\mathcal{L}_1(G)$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "k4G_xewPBUf4"
},
"outputs": [],
"source": [
"# Loss functions\n",
"criterion_GAN = torch.nn.MSELoss()\n",
"criterion_pixelwise = torch.nn.L1Loss()\n",
"\n",
"# Loss weight of L1 pixel-wise loss between translated image and real image\n",
"lambda_pixel = 100"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "c12q2NwkBUf7"
},
"source": [
"### Training and evaluating models "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "vGKjO0UMBUf9"
},
"outputs": [],
"source": [
"# parameters\n",
"epoch = 0 # epoch to start training from\n",
"n_epoch = 200 # number of epochs of training\n",
"batch_size =10 # size of the batches\n",
"lr = 0.0002 # adam: learning rate\n",
"b1 =0.5 # adam: decay of first order momentum of gradient\n",
"b2 = 0.999 # adam: decay of first order momentum of gradient\n",
"decay_epoch = 100 # epoch from which to start lr decay\n",
"img_height = 256 # size of image height\n",
"img_width = 256 # size of image width\n",
"channels = 3 # number of image channels\n",
"sample_interval = 500 # interval between sampling of images from generators\n",
"checkpoint_interval = -1 # interval between model checkpoints\n",
"cuda = True if torch.cuda.is_available() else False # do you have cuda ?"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "PhPkU7BDYooV"
},
"source": [
"Download the dataset. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "8wyPjAxPYsNF"
},
"outputs": [],
"source": [
"import urllib.request\n",
"from tqdm import tqdm\n",
"import os\n",
"import zipfile\n",
"\n",
"def download_hook(t):\n",
" \"\"\"Wraps tqdm instance.\n",
" Don't forget to close() or __exit__()\n",
" the tqdm instance once you're done with it (easiest using `with` syntax).\n",
" print('There isn\\' a training available with this number of epochs')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "4V0DwQomBUg9"
},
"outputs": [],
"source": [
"load_model(epoch=200)\n",
"\n",
"# switching mode\n",
"generator.eval()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "gyvmvkIvBUhB"
},
"outputs": [],
"source": [
"# show a sample evaluation image on the training base\n",
"image, mask = next(iter(dataloader))\n",
"output = generator(mask.type(Tensor))\n",
"output = output.view(16, 3, 256, 256)\n",
"output = output.cpu().detach()\n",
"for i in range(8):\n",
" image_plot = reverse_transform(image[i])\n",
" output_plot = reverse_transform(output[i])\n",
" mask_plot = reverse_transform(mask[i])\n",
" plot2x3Array(mask_plot,image_plot,output_plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "nqvrxBoGBUhD"
},
"outputs": [],
"source": [
"# show a sample evaluation image on the validation dataset\n",
"image, mask = next(iter(val_dataloader))\n",
"output = generator(mask.type(Tensor))\n",
"output = output.view(8, 3, 256, 256)\n",
"output = output.cpu().detach()\n",
"for i in range(8):\n",
" image_plot = reverse_transform(image[i])\n",
" output_plot = reverse_transform(output[i])\n",
" mask_plot = reverse_transform(mask[i])\n",
" plot2x3Array(mask_plot,image_plot,output_plot)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "qkFVjRsOBUhG"
},
"source": [
"<font color='red'>**Question 4**</font> \n",
"Compare results for 100 and 200 epochs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "k85Cl5_UDWyv"
},
"outputs": [],
"source": [
"# TO DO : Your code here to load and evaluate with a few samples\n",
"# a model after 100 epochs\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "_GbMIfRXBUhH"
},
"outputs": [],
"source": [
"# And finally :\n",
"if cuda:\n",
" torch.cuda.empty_cache()"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "rVxSSPJgK60P"
},
"source": [
"# How to submit your Work ?\n",
"Your work should be uploaded within 3 weeks into the Moodle section \"Devoir 2 - GAN et Conditional GAN\". It can be either a notebook containing your code and a description of your work, experiments and results or a \".zip\" file containing your report in a \"pdf\" format describing your work, experiments and results as well as your code (\".py\" Python files). "
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "BE2 - GAN and cGAN.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
%% Cell type:markdown id: tags:
<h1><big><center>MSO 3.4 - Deep Structured Learning</center></big></h1>
<h2><big><center> BE 2 - GANs and cGAN </center></big></h2>
<h5><big><center>Adapted from <i>Projet d'Option</i> of : Mhamed Jabri, Martin Chauvin, Ahmed Sahraoui, Zakariae Moustaïne and Taoufik Bouchikhi
The aim of this assignment is to discover GANs, understand how they are implemented and then explore one specific architecture of GANs that allows us to perform image to image translation (which corresponds to the picture that you can see above this text ! )
Before starting the exploration of the world of GANs, here's what students should do and send back for this assignement :
* In the "tutorial" parts of this assignement that focus on explaining new concepts, you'll find <fontcolor='red'>**questions**</font> that aim to test your understanding of those concepts.
* In some of the code cells, you'll have to complete the code and you'll find a "TO DO" explaining what you should implement.
%% Cell type:markdown id: tags:
# Part1: DC-GAN
%% Cell type:markdown id: tags:
In this part, we aim to learn and understand the basic concepts of **Generative Adversarial Networks** through a DCGAN and generate new celebrities from the learned network after showing it real celebrities. For this purpose, please study the tutorial here: https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html
%% Cell type:markdown id: tags:
##Work to do
Now we want to generate handwritten digits using the MNIST dataset. It is available within torvision package (https://pytorch.org/vision/stable/generated/torchvision.datasets.MNIST.html#torchvision.datasets.MNIST)
Please re-train the DCGAN and display some automatically generated handwritten digits.
%% Cell type:code id: tags:
``` python
#TO DO: your code here to adapt the code from the tutorial to experiment on MNIST dataset
```
%% Cell type:markdown id: tags:
# Part2: Conditional GAN (cGAN)
%% Cell type:markdown id: tags:
Let's take the example of the set described in the next picture.

We have a picture of a map (from Google Maps) and we want to create an image of what the satellite view may look like.
As we are not only trying to generate a random picture but a mapping between a picture to another one, we can't use the standard GAN architecture. We will then use a cGAN.
A cGAN is a supervised GAN aiming at mapping a label picture to a real one or a real picture to a label one. As you can see in the diagram below, the discriminator will take as input a pair of images and try to predict if the pair was generated or not. The generator will not only generate an image from noise but will also use an image (label or real) to generate another one (real or label).

%% Cell type:markdown id: tags:
### Generator
In the cGAN architecture, the generator chosen is a U-Net.
A U-Net takes as input an image, and outputs another image.
It can be divided into 2 subparts : an encoder and a decoder.
* The encoder takes the input image and reduces its dimension to encode the main features into a vector.
* The decoder takes this vector and map the features stored into an image.
A U-Net architecture is different from a classic encoder-decoder in that every layer of the decoder takes as input the previous decoded output as well as the output vector from the encoder layers of the same level. It allows the decoder to map low frequencies information encoded during the descent as well as high frequencies from the original picture.
The encoder will take as input a colored picture (3 channels: RGB), it will pass through a series of convolution layers to encode the features of the picture. It will then be decoded by the decoder using transposed convolutional layers. These layers will take as input the previous decoded vector AND the encoded features of the same level.
%% Cell type:markdown id: tags:
Now, let's create or cGAN to generate facades from a template image. For this purpose, we will use the "Facade" dataset available at http://cmp.felk.cvut.cz/~tylecr1/facade/.
%% Cell type:markdown id: tags:
Let's first create a few classes describing the layers we will use in the U-Net.
%% Cell type:code id: tags:
``` python
# Importing all the libraries needed
importmatplotlib.pyplotasplt
importimageio
importglob
importrandom
importos
importnumpyasnp
importmath
importitertools
importtime
importdatetime
importcv2
frompathlibimportPath
fromPILimportImage
fromtorch.utils.dataimportDataset,DataLoader
importtorchvision.transformsastransforms
fromtorchvision.utilsimportsave_image,make_grid
fromtorchvisionimportdatasets
fromtorch.autogradimportVariable
importtorch.nnasnn
importtorch.nn.functionalasF
importtorch
```
%% Cell type:code id: tags:
``` python
# code adapted from https://github.com/milesial/Pytorch-UNet/blob/master/unet/unet_parts.py
# At this stage x8 is our encoded vector, we will now decode it
x=self.up7(x8,x7)
x=self.up6(x,x6)
x=self.up5(x,x5)
x=self.up4(x,x4)
x=self.up3(x,x3)
x=self.up2(x,x2)
x=self.up1(x,x1)
x=self.outc(x)
returnx
```
%% Cell type:code id: tags:
``` python
# We take images that have 3 channels (RGB) as input and output an image that also have 3 channels (RGB)
generator=U_Net(3,3)
# Check that the architecture is as expected
generator
```
%% Cell type:markdown id: tags:
You should now have a working U-Net.
%% Cell type:markdown id: tags:
<fontcolor='red'>**Question 1**</font>
Knowing the input and output images will be 256x256, what will be the dimension of the encoded vector x8 ?
<fontcolor='red'>**Question 2**</font>
As you can see, U-net has an encoder-decoder architecture with skip connections. Explain why it works better than a traditional encoder-decoder.
%% Cell type:markdown id: tags:
### Discriminator
In the cGAN architecture, the chosen discriminator is a Patch GAN. It is a convolutional discriminator which enables to produce a map of the input pictures where each pixel represents a patch of size NxN of the input.
The size N is given by the depth of the net. According to this table :
| Number of layers | N |
| ---- | ---- |
| 1 | 16 |
| 2 | 34 |
| 3 | 70 |
| 4 | 142 |
| 5 | 286 |
| 6 | 574 |
The number of layers actually means the number of layers with `kernel=(4,4)`, `padding=(1,1)` and `stride=(2,2)`. These layers are followed by 2 layers with `kernel=(4,4)`, `padding=(1,1)` and `stride=(1,1)`.
In our case we are going to create a 70x70 PatchGAN.
As we want a 70x70 Patch GAN, the architecture will be as follows :
```
1. C64 - K4, P1, S2
2. C128 - K4, P1, S2
3. C256 - K4, P1, S2
4. C512 - K4, P1, S1
5. C1 - K4, P1, S1 (output)
```
Where Ck denotes a convolution block with k filters, Kk a kernel of size k, Pk is the padding size and Sk the stride applied.
*Note :* For the first layer, we do not use batchnorm.
%% Cell type:markdown id: tags:
<fontcolor='red'>**Question 3**</font>
Knowing the input and output images will be 256x256, what will be the dimension of the encoded vector x8 ?Knowing input images will be 256x256 with 3 channels each, how many parameters are there to learn ?
%% Cell type:code id: tags:
``` python
classPatchGAN(nn.Module):
def__init__(self,n_channels,n_classes):
super(PatchGAN,self).__init__()
# TODO :
# create the 4 first layers named conv1 to conv4
self.conv1=
self.conv2=
self.conv3=
self.conv4=
# output layer
self.out=out_block(512,n_classes)
defforward(self,x1,x2):
x=torch.cat([x2,x1],dim=1)
x=self.conv1(x)
x=self.conv2(x)
x=self.conv3(x)
x=self.conv4(x)
x=self.out(x)
returnx
```
%% Cell type:code id: tags:
``` python
# We have 6 input channels as we concatenate 2 images (with 3 channels each)
discriminator=PatchGAN(6,1)
discriminator
```
%% Cell type:markdown id: tags:
You should now have a working discriminator.
%% Cell type:markdown id: tags:
### Loss functions
As we have seen in the choice of the various architectures for this GAN, the issue is to map both low and high frequencies.
To tackle this problem, this GAN rely on the architecture to map the high frequencies (U-Net + PatchGAN) and the loss function to learn low frequencies features. The global loss function will indeed be made of 2 parts :
* the first part to map hight frequencies, will try to optimize the mean squared error of the GAN.
* the second part to map low frequencies, will minimize the $\mathcal{L}_1$ norm of the generated picture.
So the loss can be defined as $$ G^* = arg\ \underset{G}{min}\ \underset{D}{max}\ \mathcal{L}_{cGAN}(G,D) + \lambda \mathcal{L}_1(G)$$
%% Cell type:code id: tags:
``` python
# Loss functions
criterion_GAN=torch.nn.MSELoss()
criterion_pixelwise=torch.nn.L1Loss()
# Loss weight of L1 pixel-wise loss between translated image and real image
lambda_pixel=100
```
%% Cell type:markdown id: tags:
### Training and evaluating models
%% Cell type:code id: tags:
``` python
# parameters
epoch=0# epoch to start training from
n_epoch=200# number of epochs of training
batch_size=10# size of the batches
lr=0.0002# adam: learning rate
b1=0.5# adam: decay of first order momentum of gradient
b2=0.999# adam: decay of first order momentum of gradient
decay_epoch=100# epoch from which to start lr decay
img_height=256# size of image height
img_width=256# size of image width
channels=3# number of image channels
sample_interval=500# interval between sampling of images from generators
checkpoint_interval=-1# interval between model checkpoints
cuda=Trueiftorch.cuda.is_available()elseFalse# do you have cuda ?
```
%% Cell type:markdown id: tags:
Download the dataset.
%% Cell type:code id: tags:
``` python
importurllib.request
fromtqdmimporttqdm
importos
importzipfile
defdownload_hook(t):
"""Wraps tqdm instance.
Don't forget to close() or __exit__()
the tqdm instance once you're done with it (easiest using `with` syntax).
print('There isn\' a training available with this number of epochs')
```
%% Cell type:code id: tags:
``` python
load_model(epoch=200)
# switching mode
generator.eval()
```
%% Cell type:code id: tags:
``` python
# show a sample evaluation image on the training base
image,mask=next(iter(dataloader))
output=generator(mask.type(Tensor))
output=output.view(16,3,256,256)
output=output.cpu().detach()
foriinrange(8):
image_plot=reverse_transform(image[i])
output_plot=reverse_transform(output[i])
mask_plot=reverse_transform(mask[i])
plot2x3Array(mask_plot,image_plot,output_plot)
```
%% Cell type:code id: tags:
``` python
# show a sample evaluation image on the validation dataset
image,mask=next(iter(val_dataloader))
output=generator(mask.type(Tensor))
output=output.view(8,3,256,256)
output=output.cpu().detach()
foriinrange(8):
image_plot=reverse_transform(image[i])
output_plot=reverse_transform(output[i])
mask_plot=reverse_transform(mask[i])
plot2x3Array(mask_plot,image_plot,output_plot)
```
%% Cell type:markdown id: tags:
<fontcolor='red'>**Question 4**</font>
Compare results for 100 and 200 epochs
%% Cell type:code id: tags:
``` python
# TO DO : Your code here to load and evaluate with a few samples
# a model after 100 epochs
```
%% Cell type:code id: tags:
``` python
# And finally :
ifcuda:
torch.cuda.empty_cache()
```
%% Cell type:markdown id: tags:
# How to submit your Work ?
Your work should be uploaded within 3 weeks into the Moodle section "Devoir 2 - GAN et Conditional GAN". It can be either a notebook containing your code and a description of your work, experiments and results or a ".zip" file containing your report in a "pdf" format describing your work, experiments and results as well as your code (".py" Python files).
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
-[ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
-[ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
-[ ] [Set up project integrations](https://gitlab.ec-lyon.fr/edelland/mso3_4-be2_cgan/-/settings/integrations)
## Collaborate with your team
-[ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
-[ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
-[ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
-[ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
-[ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
-[ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
-[ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
-[ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
-[ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
Python notebook and file for the GAN & cGAN tutorial