Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GANs mnist #172

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mnist_gan/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.ipynb_checkpoints
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
saved_models
mnist_gan
mnist_gan_generate
mnist_gan_generate.o
mnist_gan.o
.vscode
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 37 additions & 0 deletions mnist_gan/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

TARGET := mnist_gan_generate
SRC := mnist_gan_generate.cpp
LIBS_NAME := armadillo mlpack

CXX := g++
CXXFLAGS += -std=c++11 -Wall -Wextra -O3 -DNDEBUG
# Use these CXXFLAGS instead if you want to compile with debugging symbols and
# without optimizations.
# CXXFLAGS += -std=c++11 -Wall -Wextra -g -O0
LDFLAGS += -fopenmp
LDFLAGS += -lboost_serialization
LDFLAGS += -larmadillo
LDFLAGS += -L. # /path to mlpack library if installed locally.
# path: mlpack/build/lib.
# Add header directories for any includes that aren't on the
# default compiler search path.
INCLFLAGS := -I /home/viole/mlpac/build/include/
CXXFLAGS += $(INCLFLAGS)

OBJS := $(SRC:.cpp=.o)
LIBS := $(addprefix -l,$(LIBS_NAME))
CLEAN_LIST := $(TARGET) $(OBJS)

# default rule
default: all

$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) $(OBJS) -o $(TARGET) $(LDFLAGS) $(LIBS)

.PHONY: all
all: $(TARGET)

.PHONY: clean
clean:
@echo CLEAN $(CLEAN_LIST)
@rm -f $(CLEAN_LIST)
Binary file not shown.
214 changes: 214 additions & 0 deletions mnist_gan/mnist_gan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
#include <mlpack/core.hpp>
#include <mlpack/core/data/split_data.hpp>

#include <mlpack/methods/ann/init_rules/gaussian_init.hpp>
#include <mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp>
#include <mlpack/methods/ann/gan/gan.hpp>
#include <mlpack/methods/ann/layer/layer.hpp>
#include <mlpack/methods/softmax_regression/softmax_regression.hpp>

#include <ensmallen.hpp>

using namespace mlpack;
using namespace mlpack::data;
using namespace mlpack::ann;
using namespace mlpack::math;
using namespace mlpack::regression;
using namespace std::placeholders;


int main()
{
size_t dNumKernels = 32;
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
size_t discriminatorPreTrain = 5;
size_t batchSize = 5;
size_t noiseDim = 100;
size_t generatorUpdateStep = 1;
size_t numSamples = 10;
size_t cycles = 10;
size_t numEpoches = 25;
double stepSize = 0.0003;
double trainRatio = 0.8;
double eps = 1e-8;
double tolerance = 1e-5;
bool shuffle = true;
double multiplier = 10;
int datasetMaxCols = 10;

std::cout << std::boolalpha
<< " batchSize = " << batchSize << std::endl
<< " generatorUpdateStep = " << generatorUpdateStep << std::endl
<< " noiseDim = " << noiseDim << std::endl
<< " numSamples = " << numSamples << std::endl
<< " stepSize = " << stepSize << std::endl
<< " numEpochs = " << numEpoches << std::endl
<< " shuffle = " << shuffle << std::endl;

swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved

arma::mat mnistDataset;
mnistDataset.load("./dataset/mnist_first250_training_4s_and_9s.arm");

std::cout << arma::size(mnistDataset) << std::endl;
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved

mnistDataset = mnistDataset.cols(0, datasetMaxCols-1);
size_t numIterations = mnistDataset.n_cols * numEpoches;
numIterations /= batchSize;

std::cout << "MnistDataset No. of rows: " << mnistDataset.n_rows << std::endl;

/**
* @brief Model Architecture:
*
* Discriminator:
* 28x28x1-----------> conv (32 filters of size 5x5,
* stride = 1, padding = 2)----------> 28x28x32
* 28x28x32----------> ReLU -----------------------------> 28x28x32
* 28x28x32----------> Mean pooling ---------------------> 14x14x32
* 14x14x32----------> conv (64 filters of size 5x5,
* stride = 1, padding = 2)------> 14x14x64
* 14x14x64----------> ReLU -----------------------------> 14x14x64
* 14x14x64----------> Mean pooling ---------------------> 7x7x64
* 7x7x64------------> Linear Layer ---------------------> 1024
* 1024--------------> ReLU -----------------------------> 1024
* 1024 -------------> Linear ---------------------------> 1
*
*
* Generator:
* noiseDim---------> Linear ---------------------------> 3136
* 3136 ------------> BatchNormalizaton ----------------> 3136
* 3136 ------------> ReLu Layer -----------------------> 3136
* 56x56x1 ---------> conv(1 filter of size 3x3,
* stride = 2, padding = 1)----> 28x28x(noiseDim/2)
* 28x28x(noiseDim/2)----> BatchNormalizaton -----------> 28x28x(noiseDim/2)
* 28x28x(noiseDim/2)----> ReLu Layer-------------------> 28x28x(noiseDim/2)
* 28x28x(noiseDim/2) ----> BilinearInterpolation ------> 56x56x(noiseDim/2)
* 56x56x(noiseDim/2) -----> conv((noiseDim/2) filters
* of size 3x3,stride = 2,
* padding = 1)----------> 28x28x(noiseDim/4)
* 28x28x(noiseDim/4) ----->BatchNormalization----------> 28x28x(noiseDim/4)
* 28x28x(noiseDim/4) ------> ReLu Layer ---------------> 28x28x(noiseDim/4)
* 28x28x(noiseDim/4) ------> BilinearInterpolation ----> 56x56x(noiseDim/4)
* 56x56x(noiseDim/4) ------> conv((noiseDim/4) filters
* of size 3x3, stride = 2,
* padding = 1)-------> 28x28x1
* 28x28x1 ----------> tanh layer ----------------------> 28x28x1
*
*
* Note: Output of a Convolution layer = [(W-K+2P)/S + 1]
* where, W : Size of input volume
* K : Kernel size
* P : Padding
* S : Stride
*/

swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved

// Creating the Discriminator network.
FFN<SigmoidCrossEntropyError<> > discriminator;
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
discriminator.Add<Convolution<> >(1, // Number of input activation maps
dNumKernels, // Number of output activation maps
5, // Filter width
5, // Filter height
1, // Stride along width
1, // Stride along height
2, // Padding width
2, // Padding height
28, // Input widht
28); // Input height
// Adding first ReLU
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
discriminator.Add<ReLULayer<> >();
// Adding mean pooling layer
discriminator.Add<MeanPooling<> >(2, 2, 2, 2);
// Adding second convolution layer
discriminator.Add<Convolution<> >(dNumKernels, 2 * dNumKernels, 5, 5, 1, 1,
2, 2, 14, 14);
// Adding second ReLU
discriminator.Add<ReLULayer<> >();
// Adding second mean pooling layer
discriminator.Add<MeanPooling<> >(2, 2, 2, 2);
// Adding linear layer
discriminator.Add<Linear<> >(7 * 7 * 2 * dNumKernels, 1024);
// Adding third ReLU
discriminator.Add<ReLULayer<> >();
// Adding final layer
discriminator.Add<Linear<> >(1024, 1);

swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved

// Creating the Generator network
FFN<SigmoidCrossEntropyError<> > generator;
generator.Add<Linear<> >(noiseDim, 3136);
generator.Add<BatchNorm<> >(3136);
generator.Add<ReLULayer<> >();
generator.Add<Convolution<> >(1, // Number of input activation maps
noiseDim / 2, // Number of output activation maps
3, // Filter width
3, // Filter height
2, // Stride along width
2, // Stride along height
1, // Padding width
1, // Padding height
56, // input width
56); // input height
// Adding first batch normalization layer
generator.Add<BatchNorm<> >(39200);
// Adding first ReLU
generator.Add<ReLULayer<> >();
// Adding a bilinear interpolation layer
generator.Add<BilinearInterpolation<> >(28, 28, 56, 56, noiseDim / 2);
// Adding second convolution layer
generator.Add<Convolution<> >(noiseDim / 2, noiseDim / 4, 3, 3, 2, 2, 1, 1,
56, 56);
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
// Adding second batch normalization layer
generator.Add<BatchNorm<> >(19600);
// Adding second ReLU
generator.Add<ReLULayer<> >();
// Adding second bilinear interpolation layer
generator.Add<BilinearInterpolation<> >(28, 28, 56, 56, noiseDim / 4);
// Adding third convolution layer
generator.Add<Convolution<> >(noiseDim / 4, 1, 3, 3, 2, 2, 1, 1, 56, 56);
// Adding final tanh layer
generator.Add<TanHLayer<> >();

// Creating GAN.
GaussianInitialization gaussian(0, 1);
ens::Adam optimizer(stepSize, // Step size of optimizer.
batchSize, // Batch size.
0.9, // Exponential decay rate for first moment estimates.
0.999, // Exponential decay rate for weighted norm estimates.
eps, // Value used to initialize the mean squared gradient parameter.
numIterations, // iterPerCycle// Maximum number of iterations.
tolerance, // Tolerance.
shuffle); // Shuffle.
std::function<double()> noiseFunction = [] () {
return math::RandNormal(0, 1);};
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
GAN<FFN<SigmoidCrossEntropyError<> >, GaussianInitialization,
std::function<double()> > gan(generator, discriminator,
gaussian, noiseFunction, noiseDim, batchSize, generatorUpdateStep,
discriminatorPreTrain, multiplier);
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved

std::cout << "Training ... " << std::endl;

const clock_t beginTime = clock();
// Cycles for monitoring training progress.
for( int i = 0; i < cycles; i++)
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
{
// Training the neural network. For first iteration, weights are random,
// thus using current values as starting point.
gan.Train(mnistDataset, //trainDataset
optimizer,
ens::PrintLoss(),
ens::ProgressBar(),
ens::Report());

optimizer.ResetPolicy() = false;
std::cout << " Model Performance " <<
gan.Evaluate(gan.Parameters(), // Parameters of the network.
i, // Index of current input.
batchSize); // Batch size.
Copy link
Member

@kartikdutt18 kartikdutt18 Jul 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For saving periodically, you can use https://github.com/mlpack/models/blob/master/ensmallen_utils/periodic_save.hpp
You can download the file and use it temporarily for testing / forming the notebook.

}

std::cout << " Time taken to train -> " << float(clock()-beginTime) / CLOCKS_PER_SEC << "seconds" << std::endl;

// Let's save the model.
data::Save("./saved_models/ganMnist_25epochs.bin", "ganMnist", gan);
std::cout << "Model saved in mnist_gan/saved_models." << std::endl;
}
116 changes: 116 additions & 0 deletions mnist_gan/mnist_gan_generate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <mlpack/core.hpp>

#include <mlpack/core/data/split_data.hpp>
#include <mlpack/core/data/save.hpp>


#include <mlpack/methods/ann/init_rules/gaussian_init.hpp>
#include <mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp>
#include <mlpack/methods/ann/gan/gan.hpp>
#include <mlpack/methods/ann/layer/layer.hpp>
#include <mlpack/methods/softmax_regression/softmax_regression.hpp>

#include <ensmallen.hpp>

using namespace mlpack;
using namespace mlpack::ann;

int main()
{
size_t discriminatorPreTrain = 5;
size_t batchSize = 5;
size_t noiseDim = 100;
size_t generatorUpdateStep = 1;
size_t numSamples = 10;
double multiplier = 10;
bool loadData = false;

arma::mat trainData,inputData, validData;
trainData.load("./dataset/mnist_first250_training_4s_and_9s.arm");

// If you want to load other mnist data, then uncomment the below lines in the "if" statement to remove and prepare the data for your test.
// if(loadData)
// {

// inputData.load("File Path");

// // Removing the headers.
// inputData = inputData.submat(0, 1, inputData.n_rows - 1, inputData.n_cols - 1);
// inputData /= 255.0; // Note that if you are bringing all the values to 0-1, then in the output csv, you have to multiply all values by 255.0

// // Removing the labels.
// inputData = inputData.submat(1, 0, inputData.n_rows - 1, inputData.n_cols - 1);

// inputData = (inputData - 0.5) * 2;

// data::Split(inputData, trainData, validData, 0.8);
// }

arma::arma_rng::set_seed_random();

// Define noise function.
std::function<double ()> noiseFunction = [](){ return math::Random(-8, 8) +
math::RandNormal(0, 1) * 0.01;};
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved

// Define generator.
FFN<SigmoidCrossEntropyError<> > generator;

// Define discriminator.
FFN<SigmoidCrossEntropyError<> > discriminator;

// Define GaussinaInitialization.
GaussianInitialization gaussian(0,1);

// Define GAN class.
GAN<FFN<SigmoidCrossEntropyError<> >, GaussianInitialization,
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
std::function<double()> > gan(generator, discriminator,
gaussian, noiseFunction, noiseDim, batchSize, generatorUpdateStep,
discriminatorPreTrain, multiplier);

// Load the saved model.
data::Load("./saved_models/ganMnist_25epochs.bin", "ganMnist", gan);

/*--------------Sampling-----------------------------------------*/

std::cout << "Sampling...." << std::endl;

// Noise matrix.
arma::mat noise(noiseDim, batchSize);

// Dimensions of the image.
size_t dim = std::sqrt(trainData.n_rows);

// Matrix to store the generated data.
arma::mat generatedData(2 * dim, dim * numSamples);


for (size_t i = 0; i < numSamples; ++i)
{
arma::mat samples;
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved

// Create random noise using noise function.
noise.imbue( [&]() { return noiseFunction(); } );
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved

// Pass noise through generator and store output in samples.
gan.Generator().Forward(noise, samples);

// Reshape and Transpose the samples output.
samples.reshape(dim, dim);
samples = samples.t();

// Store the output sample in a dimxdim grid in final output matrix.
generatedData.submat(0, i * dim, dim - 1, i * dim + dim - 1) = samples;

// Add the image from original train data to compare.
samples = trainData.col(math::RandInt(0, trainData.n_cols));
samples.reshape(dim, dim);
samples = samples.t();
generatedData.submat(dim,
i * dim, 2 * dim - 1, i * dim + dim - 1) = samples;
}
// Save the output as csv.
data::Save("./samples_csv_files/sample.csv", generatedData, false, false);

std::cout << "Output generated!" << std::endl;

}
swaingotnochill marked this conversation as resolved.
Show resolved Hide resolved
Loading