Creating a DCGAN with PyTorch
Create a DCGAN with PyTorch from scratch.
We'll cover the following...
Let’s start writing PyTorch code to create a DCGAN model. Here, we assume that the Python environment is being used in Ubuntu.
First, let’s create a Python source file called dcgan.py and import the packages that we need:
Here, numpy is only used to initialize a random seed. If we don't have numpy installed, simply replace np.random with random and insert the import random line after import os. In the last line of code, we import a module called utils, which is a custom utility package defined in the utils.py file. The full source code of utils.py is as follows:
We will put most of the PyTorch-independent helper functions (including file organization, learning rate adjustment, logging, tensor visualization, and so on) in this utils.py file.
Then, we define the output path and hyperparameters. Note that here we set the minimal channel size of hidden layers in both the generator and discriminator to 64:
If we don't have a CUDA-enabled graphics card and want to train the networks on the CPU, we can change CUDA to False. DATA_PATH points to the root directory of the MNIST dataset. If MNIST hasn't been downloaded and properly preprocessed yet, simply point it to any directory (such as ...