| 104 | } |
| 105 | |
| 106 | int main(int argc, char** argv) try |
| 107 | { |
| 108 | // This example is going to run on the MNIST dataset. |
| 109 | if (argc != 2) |
| 110 | { |
| 111 | cout << "This example needs the MNIST dataset to run!" << endl; |
| 112 | cout << "You can get MNIST from http://yann.lecun.com/exdb/mnist/" << endl; |
| 113 | cout << "Download the 4 files that comprise the dataset, decompress them, and" << endl; |
| 114 | cout << "put them in a folder. Then give that folder as input to this program." << endl; |
| 115 | return EXIT_FAILURE; |
| 116 | } |
| 117 | |
| 118 | // MNIST is broken into two parts, a training set of 60000 images and a test set of 10000 |
| 119 | // images. Each image is labeled so that we know what hand written digit is depicted. |
| 120 | // These next statements load the dataset into memory. |
| 121 | std::vector<matrix<unsigned char>> training_images; |
| 122 | std::vector<unsigned long> training_labels; |
| 123 | std::vector<matrix<unsigned char>> testing_images; |
| 124 | std::vector<unsigned long> testing_labels; |
| 125 | load_mnist_dataset(argv[1], training_images, training_labels, testing_images, testing_labels); |
| 126 | |
| 127 | // Fix the random generator seeds for network initialization and noise |
| 128 | srand(1234); |
| 129 | dlib::rand rnd(std::rand()); |
| 130 | |
| 131 | // Instantiate both generator and discriminator |
| 132 | generator_type generator; |
| 133 | discriminator_type discriminator; |
| 134 | // setup all leaky_relu_ layers in the discriminator to have alpha = 0.2 |
| 135 | visit_computational_layers(discriminator, [](leaky_relu_& l){ l = leaky_relu_(0.2); }); |
| 136 | // Remove the bias learning from all bn_ inputs in both networks |
| 137 | disable_duplicative_biases(generator); |
| 138 | disable_duplicative_biases(discriminator); |
| 139 | // Forward random noise so that we see the tensor size at each layer |
| 140 | discriminator(generate_image(generator, make_noise(rnd))); |
| 141 | cout << "generator (" << count_parameters(generator) << " parameters)" << endl; |
| 142 | cout << generator << endl; |
| 143 | cout << "discriminator (" << count_parameters(discriminator) << " parameters)" << endl; |
| 144 | cout << discriminator << endl; |
| 145 | |
| 146 | // The solvers for the generator and discriminator networks. In this example, we are going to |
| 147 | // train the networks manually, so we don't need to use a dnn_trainer. Note that the |
| 148 | // discriminator could be trained using a dnn_trainer, but not the generator, since its |
| 149 | // training process is a bit particular. |
| 150 | std::vector<adam> g_solvers(generator.num_computational_layers, adam(0, 0.5, 0.999)); |
| 151 | std::vector<adam> d_solvers(discriminator.num_computational_layers, adam(0, 0.5, 0.999)); |
| 152 | double learning_rate = 2e-4; |
| 153 | |
| 154 | // Resume training from last sync file |
| 155 | size_t iteration = 0; |
| 156 | if (file_exists("dcgan_sync")) |
| 157 | { |
| 158 | deserialize("dcgan_sync") >> generator >> discriminator >> iteration; |
| 159 | } |
| 160 | |
| 161 | const size_t minibatch_size = 64; |
| 162 | const std::vector<float> real_labels(minibatch_size, 1); |
| 163 | const std::vector<float> fake_labels(minibatch_size, -1); |
nothing calls this directly
no test coverage detected