-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'mlpack:master' into gan-mnist
- Loading branch information
Showing
16 changed files
with
3,424 additions
and
25 deletions.
There are no files selected for viewing
705 changes: 705 additions & 0 deletions
705
avocado_price_prediction_with_linear_regression/avocado_price_prediction_with_lr_cpp.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
979 changes: 979 additions & 0 deletions
979
avocado_price_prediction_with_linear_regression/avocado_price_prediction_with_lr_py.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
@file generate_images.py | ||
@author Atharva Khandait | ||
Generates jpg files from csv. | ||
mlpack is free software; you may redistribute it and/or modify it under the | ||
terms of the 3-clause BSD license. You should have received a copy of the | ||
3-clause BSD license along with mlpack. If not, see | ||
http://www.opensource.org/licenses/BSD-3-Clause for more information. | ||
""" | ||
|
||
from PIL import Image | ||
import numpy as np | ||
|
||
def ImagesFromCSV(filename, | ||
imgShape = (28, 28), | ||
destination = 'samples', | ||
saveIndividual = False): | ||
|
||
# Import the data into a numpy matrix. | ||
samples = np.genfromtxt(filename, delimiter = ',', dtype = np.uint8) | ||
|
||
# Reshape and save it as an image in the destination. | ||
tempImage = Image.fromarray(np.reshape(samples[:, 0], imgShape), 'L') | ||
if saveIndividual: | ||
tempImage.save(destination + '/sample0.jpg') | ||
|
||
# All the images will be concatenated to this for a combined image. | ||
allSamples = tempImage | ||
|
||
for i in range(1, samples.shape[1]): | ||
tempImage = np.reshape(samples[:, i], imgShape) | ||
|
||
allSamples = np.concatenate((allSamples, tempImage), axis = 1) | ||
|
||
tempImage = Image.fromarray(tempImage, 'L') | ||
if saveIndividual: | ||
tempImage.save(destination + '/sample' + str(i) + '.jpg') | ||
|
||
tempImage = allSamples | ||
allSamples = Image.fromarray(allSamples, 'L') | ||
allSamples.save(destination + '/allSamples' + '.jpg') | ||
|
||
print ('Samples saved in ' + destination + '/.') | ||
|
||
return tempImage | ||
|
||
# Save posterior samples. | ||
ImagesFromCSV('./samples_csv_files/samples_posterior.csv', destination = | ||
'samples_posterior') | ||
|
||
# Save prior samples with individual latent varying. | ||
latentSize = 10 | ||
allLatent = ImagesFromCSV('./samples_csv_files/samples_prior_latent0.csv', | ||
destination = 'samples_prior') | ||
|
||
for i in range(1, latentSize): | ||
allLatent = np.concatenate((allLatent, | ||
(ImagesFromCSV('./samples_csv_files/samples_prior_latent' + str(i) + '.csv', | ||
destination = 'samples_prior'))), axis = 0) | ||
|
||
saved = Image.fromarray(allLatent, 'L') | ||
saved.save('./samples_prior/allLatent.jpg') | ||
|
||
# Save prior samples with 2d latent varying. | ||
nofSamples = 20 | ||
allLatent = ImagesFromCSV('./samples_csv_files/samples_prior_latent_2d0.csv', | ||
destination = 'latent') | ||
|
||
for i in range(1, nofSamples): | ||
allLatent = np.concatenate((allLatent, | ||
(ImagesFromCSV('./samples_csv_files/samples_prior_latent_2d' + str(i) + | ||
'.csv', destination = 'samples_prior'))), axis = 0) | ||
|
||
saved = Image.fromarray(allLatent, 'L') | ||
saved.save('./samples_prior/2dLatent.jpg') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/** | ||
* @file vae_generate.cpp | ||
* @author Atharva Khandait | ||
* | ||
* Generate MNIST using trained VAE model. | ||
* | ||
* mlpack is free software; you may redistribute it and/or modify it under the | ||
* terms of the 3-clause BSD license. You should have received a copy of the | ||
* 3-clause BSD license along with mlpack. If not, see | ||
* http://www.opensource.org/licenses/BSD-3-Clause for more information. | ||
*/ | ||
#include <mlpack/core.hpp> | ||
#include <mlpack/core/data/split_data.hpp> | ||
#include <mlpack/core/data/save.hpp> | ||
|
||
#include <mlpack/methods/ann/ffn.hpp> | ||
#include <mlpack/methods/ann/layer/layer.hpp> | ||
#include <mlpack/methods/ann/init_rules/he_init.hpp> | ||
#include <mlpack/methods/ann/loss_functions/reconstruction_loss.hpp> | ||
#include <mlpack/methods/ann/loss_functions/mean_squared_error.hpp> | ||
#include <mlpack/methods/ann/dists/bernoulli_distribution.hpp> | ||
|
||
#include "vae_utils.hpp" | ||
|
||
using namespace mlpack; | ||
using namespace mlpack::ann; | ||
|
||
// Convenience typedef | ||
typedef FFN<ReconstructionLoss<arma::mat, | ||
arma::mat, | ||
BernoulliDistribution<arma::mat> >, | ||
HeInitialization> ReconModel; | ||
|
||
int main() | ||
{ | ||
// Whether to load training data. | ||
constexpr bool loadData = true; | ||
// The number of samples to generate. | ||
constexpr size_t nofSamples = 20; | ||
// Whether modelled on binary data. | ||
constexpr bool isBinary = false; | ||
// the latent size of the VAE model. | ||
constexpr size_t latentSize = 20; | ||
|
||
arma::mat fullData, train, validation; | ||
|
||
if (loadData) | ||
{ | ||
data::Load("../data/mnist_train.csv", fullData, true, false); | ||
// Get rid of the header. | ||
fullData = | ||
fullData.submat(0, 1, fullData.n_rows - 1, fullData.n_cols -1); | ||
fullData /= 255.0; | ||
// Get rid of the labels. | ||
fullData = | ||
fullData.submat(1, 0, fullData.n_rows - 1, fullData.n_cols - 1); | ||
|
||
if (isBinary) | ||
{ | ||
fullData = arma::conv_to<arma::mat>::from(arma::randu<arma::mat> | ||
(fullData.n_rows, fullData.n_cols) <= fullData); | ||
} | ||
else | ||
fullData = (fullData - 0.5) * 2; | ||
|
||
data::Split(fullData, validation, train, 0.8); | ||
} | ||
|
||
arma::arma_rng::set_seed_random(); | ||
|
||
// It doesn't matter what type of network we initialize, as we only need to | ||
// forward pass throught it and not initialize weights or take loss. | ||
FFN<> vaeModel; | ||
|
||
// Load the trained model. | ||
if (isBinary) | ||
{ | ||
data::Load("./saved_models/vaeBinaryMS.xml", "vaeBinaryMS", vaeModel); | ||
vaeModel.Add<SigmoidLayer<> >(); | ||
} | ||
else | ||
{ | ||
data::Load("./saved_models/vaeCNN.bin", "vaeMS", vaeModel); | ||
} | ||
|
||
arma::mat gaussianSamples, outputDists, samples; | ||
|
||
/* | ||
* Sampling from the prior. | ||
*/ | ||
gaussianSamples = arma::randn<arma::mat>(latentSize, nofSamples); | ||
|
||
// Forward pass only through the decoder(and Sigmod layer in case of binary). | ||
vaeModel.Forward(gaussianSamples, | ||
outputDists, | ||
3 /* Index of the decoder */, | ||
3 + (size_t)isBinary /* Index of the last layer */); | ||
|
||
GetSample(outputDists, samples, isBinary); | ||
// Save the prior samples as csv. | ||
data::Save("./samples_csv_files/samples_prior.csv", samples, false, false); | ||
|
||
/* | ||
* Sampling from the prior by varying all latent variables. | ||
*/ | ||
arma::mat gaussianVaried; | ||
|
||
for (size_t i = 0; i < latentSize; i++) | ||
{ | ||
gaussianSamples = arma::randn<arma::mat>(latentSize, 1); | ||
gaussianVaried = arma::zeros(latentSize, nofSamples); | ||
gaussianVaried.each_col() = gaussianSamples; | ||
|
||
for (size_t j = 0; j < nofSamples; j++) | ||
{ | ||
gaussianVaried.col(j)(i) = -1.5 + j * (3.0 / nofSamples); | ||
} | ||
|
||
// Forward pass only through the decoder | ||
// (and Sigmod layer in case of binary). | ||
vaeModel.Forward(gaussianVaried, | ||
outputDists, | ||
3 /* Index of the decoder */, | ||
3 + (size_t)isBinary /* Index of the last layer */); | ||
|
||
GetSample(outputDists, samples, isBinary); | ||
// Save the prior samples as csv. | ||
data::Save( | ||
"./samples_csv_files/samples_prior_latent" + std::to_string(i) + ".csv", | ||
samples, | ||
false, | ||
false); | ||
} | ||
|
||
/* | ||
* Sampling from the prior by varying two latent variables in 2d. | ||
*/ | ||
size_t latent1 = 3; // Latent variable to be varied vertically. | ||
size_t latent2 = 4; // Latent variable to be varied horizontally. | ||
|
||
for (size_t i = 0; i < nofSamples; i++) | ||
{ | ||
gaussianVaried = arma::zeros(latentSize, nofSamples); | ||
|
||
for (size_t j = 0; j < nofSamples; j++) | ||
{ | ||
// Set the vertical variable to a constant value for the outer loop. | ||
gaussianVaried.col(j)(latent1) = 1.5 - i * (3.0 / nofSamples); | ||
// Vary the horizontal variable from -1.5 to 1.5. | ||
gaussianVaried.col(j)(latent2) = -1.5 + j * (3.0 / nofSamples); | ||
} | ||
|
||
// Forward pass only through the decoder | ||
// (and Sigmod layer in case of binary). | ||
vaeModel.Forward(gaussianVaried, | ||
outputDists, | ||
3 /* Index of the decoder */, | ||
3 + (size_t)isBinary /* Index of the last layer */); | ||
|
||
GetSample(outputDists, samples, isBinary); | ||
// Save the prior samples as csv. | ||
data::Save("./samples_csv_files/samples_prior_latent_2d" + std::to_string(i) | ||
+ ".csv", samples, false, false); | ||
} | ||
|
||
/* | ||
* Sampling from the posterior. | ||
*/ | ||
if (loadData) | ||
{ | ||
// Forward pass through the entire network given an input datapoint. | ||
vaeModel.Forward(validation.cols(0, 19), | ||
outputDists, | ||
1 /* Index of the encoder */, | ||
3 + (size_t)isBinary /* Index of the last layer */); | ||
|
||
GetSample(outputDists, samples, isBinary); | ||
// Save the posterior samples as csv. | ||
data::Save( | ||
"./samples_csv_files/samples_posterior.csv", | ||
samples, | ||
false, | ||
false); | ||
} | ||
} |
Oops, something went wrong.