Training a DCGAN using PyTorch
We have discussed the architectures of the generator and discriminator models within the DCGAN model in the previous section. In this section, we will build, train, and test a DCGAN model using PyTorch in the form of an exercise. We will use an image dataset to train the model and test how well the generator of the trained DCGAN model performs when producing fake images.
Defining the generator
In the following exercise, we will only show the important parts of the code for demonstration purposes. In order to access the full code, you can refer to https://github.com/PacktPublishing/Mastering-PyTorch/blob/master/Chapter08/dcgan.ipynb:
- First, we need to
import
the required libraries, as follows:import os import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.data import DataLoader from torch.autograd import Variable import torchvision.transforms as transforms from torchvision.utils import save_image...