|
| 1 | +/** |
| 2 | + * @file vae_generate.cpp |
| 3 | + * @author Atharva Khandait |
| 4 | + * |
| 5 | + * Generate MNIST using trained VAE model. |
| 6 | + * |
| 7 | + * mlpack is free software; you may redistribute it and/or modify it under the |
| 8 | + * terms of the 3-clause BSD license. You should have received a copy of the |
| 9 | + * 3-clause BSD license along with mlpack. If not, see |
| 10 | + * http://www.opensource.org/licenses/BSD-3-Clause for more information. |
| 11 | + */ |
| 12 | +#include <mlpack/core.hpp> |
| 13 | +#include <mlpack/core/data/split_data.hpp> |
| 14 | +#include <mlpack/core/data/save.hpp> |
| 15 | + |
| 16 | +#include <mlpack/methods/ann/ffn.hpp> |
| 17 | +#include <mlpack/methods/ann/layer/layer.hpp> |
| 18 | +#include <mlpack/methods/ann/init_rules/he_init.hpp> |
| 19 | +#include <mlpack/methods/ann/loss_functions/reconstruction_loss.hpp> |
| 20 | +#include <mlpack/methods/ann/loss_functions/mean_squared_error.hpp> |
| 21 | +#include <mlpack/methods/ann/dists/bernoulli_distribution.hpp> |
| 22 | + |
| 23 | +#include "vae_utils.hpp" |
| 24 | + |
| 25 | +using namespace mlpack; |
| 26 | +using namespace mlpack::ann; |
| 27 | + |
| 28 | +// Convenience typedef |
| 29 | +typedef FFN<ReconstructionLoss<arma::mat, |
| 30 | + arma::mat, |
| 31 | + BernoulliDistribution<arma::mat> >, |
| 32 | + HeInitialization> ReconModel; |
| 33 | + |
| 34 | +int main() |
| 35 | +{ |
| 36 | + // Whether to load training data. |
| 37 | + constexpr bool loadData = true; |
| 38 | + // The number of samples to generate. |
| 39 | + constexpr size_t nofSamples = 20; |
| 40 | + // Whether modelled on binary data. |
| 41 | + constexpr bool isBinary = false; |
| 42 | + // the latent size of the VAE model. |
| 43 | + constexpr size_t latentSize = 20; |
| 44 | + |
| 45 | + arma::mat fullData, train, validation; |
| 46 | + |
| 47 | + if (loadData) |
| 48 | + { |
| 49 | + data::Load("../data/mnist_train.csv", fullData, true, false); |
| 50 | + // Get rid of the header. |
| 51 | + fullData = |
| 52 | + fullData.submat(0, 1, fullData.n_rows - 1, fullData.n_cols -1); |
| 53 | + fullData /= 255.0; |
| 54 | + // Get rid of the labels. |
| 55 | + fullData = |
| 56 | + fullData.submat(1, 0, fullData.n_rows - 1, fullData.n_cols - 1); |
| 57 | + |
| 58 | + if (isBinary) |
| 59 | + { |
| 60 | + fullData = arma::conv_to<arma::mat>::from(arma::randu<arma::mat> |
| 61 | + (fullData.n_rows, fullData.n_cols) <= fullData); |
| 62 | + } |
| 63 | + else |
| 64 | + fullData = (fullData - 0.5) * 2; |
| 65 | + |
| 66 | + data::Split(fullData, validation, train, 0.8); |
| 67 | + } |
| 68 | + |
| 69 | + arma::arma_rng::set_seed_random(); |
| 70 | + |
| 71 | + // It doesn't matter what type of network we initialize, as we only need to |
| 72 | + // forward pass throught it and not initialize weights or take loss. |
| 73 | + FFN<> vaeModel; |
| 74 | + |
| 75 | + // Load the trained model. |
| 76 | + if (isBinary) |
| 77 | + { |
| 78 | + data::Load("./saved_models/vaeBinaryMS.xml", "vaeBinaryMS", vaeModel); |
| 79 | + vaeModel.Add<SigmoidLayer<> >(); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + data::Load("./saved_models/vaeCNN.bin", "vaeMS", vaeModel); |
| 84 | + } |
| 85 | + |
| 86 | + arma::mat gaussianSamples, outputDists, samples; |
| 87 | + |
| 88 | + /* |
| 89 | + * Sampling from the prior. |
| 90 | + */ |
| 91 | + gaussianSamples = arma::randn<arma::mat>(latentSize, nofSamples); |
| 92 | + |
| 93 | + // Forward pass only through the decoder(and Sigmod layer in case of binary). |
| 94 | + vaeModel.Forward(gaussianSamples, |
| 95 | + outputDists, |
| 96 | + 3 /* Index of the decoder */, |
| 97 | + 3 + (size_t)isBinary /* Index of the last layer */); |
| 98 | + |
| 99 | + GetSample(outputDists, samples, isBinary); |
| 100 | + // Save the prior samples as csv. |
| 101 | + data::Save("./samples_csv_files/samples_prior.csv", samples, false, false); |
| 102 | + |
| 103 | + /* |
| 104 | + * Sampling from the prior by varying all latent variables. |
| 105 | + */ |
| 106 | + arma::mat gaussianVaried; |
| 107 | + |
| 108 | + for (size_t i = 0; i < latentSize; i++) |
| 109 | + { |
| 110 | + gaussianSamples = arma::randn<arma::mat>(latentSize, 1); |
| 111 | + gaussianVaried = arma::zeros(latentSize, nofSamples); |
| 112 | + gaussianVaried.each_col() = gaussianSamples; |
| 113 | + |
| 114 | + for (size_t j = 0; j < nofSamples; j++) |
| 115 | + { |
| 116 | + gaussianVaried.col(j)(i) = -1.5 + j * (3.0 / nofSamples); |
| 117 | + } |
| 118 | + |
| 119 | + // Forward pass only through the decoder |
| 120 | + // (and Sigmod layer in case of binary). |
| 121 | + vaeModel.Forward(gaussianVaried, |
| 122 | + outputDists, |
| 123 | + 3 /* Index of the decoder */, |
| 124 | + 3 + (size_t)isBinary /* Index of the last layer */); |
| 125 | + |
| 126 | + GetSample(outputDists, samples, isBinary); |
| 127 | + // Save the prior samples as csv. |
| 128 | + data::Save( |
| 129 | + "./samples_csv_files/samples_prior_latent" + std::to_string(i) + ".csv", |
| 130 | + samples, |
| 131 | + false, |
| 132 | + false); |
| 133 | + } |
| 134 | + |
| 135 | + /* |
| 136 | + * Sampling from the prior by varying two latent variables in 2d. |
| 137 | + */ |
| 138 | + size_t latent1 = 3; // Latent variable to be varied vertically. |
| 139 | + size_t latent2 = 4; // Latent variable to be varied horizontally. |
| 140 | + |
| 141 | + for (size_t i = 0; i < nofSamples; i++) |
| 142 | + { |
| 143 | + gaussianVaried = arma::zeros(latentSize, nofSamples); |
| 144 | + |
| 145 | + for (size_t j = 0; j < nofSamples; j++) |
| 146 | + { |
| 147 | + // Set the vertical variable to a constant value for the outer loop. |
| 148 | + gaussianVaried.col(j)(latent1) = 1.5 - i * (3.0 / nofSamples); |
| 149 | + // Vary the horizontal variable from -1.5 to 1.5. |
| 150 | + gaussianVaried.col(j)(latent2) = -1.5 + j * (3.0 / nofSamples); |
| 151 | + } |
| 152 | + |
| 153 | + // Forward pass only through the decoder |
| 154 | + // (and Sigmod layer in case of binary). |
| 155 | + vaeModel.Forward(gaussianVaried, |
| 156 | + outputDists, |
| 157 | + 3 /* Index of the decoder */, |
| 158 | + 3 + (size_t)isBinary /* Index of the last layer */); |
| 159 | + |
| 160 | + GetSample(outputDists, samples, isBinary); |
| 161 | + // Save the prior samples as csv. |
| 162 | + data::Save("./samples_csv_files/samples_prior_latent_2d" + std::to_string(i) |
| 163 | + + ".csv", samples, false, false); |
| 164 | + } |
| 165 | + |
| 166 | + /* |
| 167 | + * Sampling from the posterior. |
| 168 | + */ |
| 169 | + if (loadData) |
| 170 | + { |
| 171 | + // Forward pass through the entire network given an input datapoint. |
| 172 | + vaeModel.Forward(validation.cols(0, 19), |
| 173 | + outputDists, |
| 174 | + 1 /* Index of the encoder */, |
| 175 | + 3 + (size_t)isBinary /* Index of the last layer */); |
| 176 | + |
| 177 | + GetSample(outputDists, samples, isBinary); |
| 178 | + // Save the posterior samples as csv. |
| 179 | + data::Save( |
| 180 | + "./samples_csv_files/samples_posterior.csv", |
| 181 | + samples, |
| 182 | + false, |
| 183 | + false); |
| 184 | + } |
| 185 | +} |
0 commit comments