MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
mnist_convnet.cpp
Go to the documentation of this file.
1 
16 /*
17 * @Author: kmrocki/tkornuta
18 * @Date: 2016-03-10 09:43:05
19 */
20 
21 #include <logger/Log.hpp>
22 #include <logger/ConsoleOutput.hpp>
23 using namespace mic::logger;
24 
25 #include <iomanip>
26 
27 #include <importers/MNISTMatrixImporter.hpp>
28 #include <encoders/MatrixXfMatrixXfEncoder.hpp>
29 #include <encoders/UIntMatrixXfEncoder.hpp>
30 
32 
33 using namespace mic::types;
34 // Using multi layer neural networks
35 using namespace mic::mlnn;
36 using namespace mic::mlnn::convolution;
37 
38 int main() {
39  // Task parameters.
40  size_t epochs = 100;
41  size_t batch_size = 1;
42 
43  // Set console output.
44  ConsoleOutput* co = new ConsoleOutput();
45  LOGGER->addOutput(co);
46 
47  //[60000, 784]
48  // Load the MNIST training...
49  mic::importers::MNISTMatrixImporter<float> training;
50  // Manually set paths. DEPRICATED! Used here only for simplification of the test.
51  training.setDataFilename("../data/mnist/train-images.idx3-ubyte");
52  training.setLabelsFilename("../data/mnist/train-labels.idx1-ubyte");
53  training.setBatchSize(batch_size);
54 
55  if (!training.importData())
56  return -1;
57 
58  // ... and test datasets.
59  mic::importers::MNISTMatrixImporter<float> test;
60  // Manually set paths. DEPRICATED! Used here only for simplification of the test.
61  test.setDataFilename("../data/mnist/t10k-images.idx3-ubyte");
62  test.setLabelsFilename("../data/mnist/t10k-labels.idx1-ubyte");
63  test.setBatchSize(batch_size);
64 
65  if (!test.importData())
66  return -1;
67 
68  // Initialize the encoders.
69  mic::encoders::MatrixXfMatrixXfEncoder mnist_encoder(28, 28);
70  mic::encoders::UIntMatrixXfEncoder label_encoder(10);
71 
72  // Create a convolutional neural network.
73  // 20 Epochs
74  // Train : 99.99 %
75  // Test : 99.61 %
76 
77  // Neural net.
79 
80  // Convolution 1
82  nn.pushLayer(new mic::mlnn::convolution::Convolution<float>(26, 26, 1, 16, 3, 1));
83  nn.pushLayer(new ELU<float>(24, 24, 16));
85 
86  // Convolution 2
87  nn.pushLayer(new mic::mlnn::convolution::Convolution<float>(12, 12, 16, 32, 3, 1));
88  nn.pushLayer(new ELU<float>(10, 10, 32));
90 
91  // Linear + dropout
92  nn.pushLayer(new Linear<float>(5, 5, 32, 100, 1, 1));
93  nn.pushLayer(new ELU<float>(100, 1, 1));
94  nn.pushLayer(new Dropout<float>(100, 0.5f));
95 
96  // Softmax
97  nn.pushLayer(new Linear<float>(100, 10));
98  nn.pushLayer(new Softmax<float>(10));
99  if (!nn.verify())
100  exit(-1);
101 
102  // Set batch size.
103  nn.resizeBatch(batch_size);
104 
105  // Change optimization function from default GradientDescent to Adam.
107 
108  // Set training parameters.
109  double learning_rate = 1e-4;
110  double weight_decay = 1e-5;
111  size_t iterations = training.size() / batch_size;
112 
113  MatrixXfPtr encoded_batch, encoded_targets;
114  // For all epochs.
115  for (size_t e = 0; e < epochs; e++) {
116  LOG(LSTATUS) << "Epoch " << e + 1 << ": starting the training of neural network...";
117  // Perform the training.
118  for (size_t ii = 0; ii < iterations; ii++) {
119  std::cout<< "[" << std::setw(4) << ii << "/" << std::setw(4) << iterations << "] ";
120 
121  // Get random batch [784 x batch_size].
122  MNISTBatch<float> rand_batch = training.getRandomBatch();
123  encoded_batch = mnist_encoder.encodeBatch(rand_batch.data());
124  encoded_targets = label_encoder.encodeBatch(rand_batch.labels());
125 
126  // Train network with batch.
127  float loss = nn.train (encoded_batch, encoded_targets, learning_rate, weight_decay);
128  std::cout << " loss = " << loss << std::endl;
129  }//: for iteration
130 
131  // Save results to file.
132  nn.save("mnist_conv");
133 
134  LOG(LSTATUS) << "Training finished";
135 
136  // Check performance on the test dataset.
137  LOG(LSTATUS) << "Calculating performance for test dataset...";
138  size_t correct = 0;
139  test.setNextSampleIndex(0);
140  while(!test.isLastBatch()) {
141 
142  // Get next batch [784 x batch_size].
143  MNISTBatch<float> next_batch = test.getNextBatch();
144  encoded_batch = mnist_encoder.encodeBatch(next_batch.data());
145  encoded_targets = label_encoder.encodeBatch(next_batch.labels());
146 
147  // Test network response.
148  correct += nn.test(encoded_batch, encoded_targets);
149 
150  }
151  double test_acc = (double)correct / (double)(test.size());
152  LOG(LINFO) << "Test accuracy : " << std::setprecision(3) << 100.0 * test_acc << " %";
153 
154  // Check performance on the training dataset.
155  LOG(LSTATUS) << "Calculating performance for the training dataset...";
156  correct = 0;
157  training.setNextSampleIndex(0);
158  while(!training.isLastBatch()) {
159 
160  // Get next batch [784 x batch_size].
161  MNISTBatch<float> next_batch = training.getNextBatch();
162  encoded_batch = mnist_encoder.encodeBatch(next_batch.data());
163  encoded_targets = label_encoder.encodeBatch(next_batch.labels());
164 
165  // Test network response.
166  correct += nn.test(encoded_batch, encoded_targets);
167 
168  }
169  double train_acc = (double)correct / (double)(training.size());
170  LOG(LINFO) << "Trainin accuracy : " << std::setprecision(3) << 100.0 * train_acc << " %";
171 
172  }//: for epoch
173 
174 }
Class implementing the layer with Exponential Linear Unit (ELU). http://arxiv.org/pdf/1511.07289v5.pdf.
Definition: ELU.hpp:39
Layer performing max pooling.
Definition: MaxPooling.hpp:39
mic::encoders::UIntMatrixXfEncoder * label_encoder
Label 2 matrix encoder (1 hot).
Class implementing cropping operation - crops the size of image (matrix) by a margin of n pixels on e...
Definition: Cropping.hpp:38
Droput layer - a layer used for the regularization of neural network by randomly dropping neurons dur...
Definition: Dropout.hpp:39
int main()
eT test(mic::types::MatrixPtr< eT > encoded_batch_, mic::types::MatrixPtr< eT > encoded_targets_)
eT train(mic::types::MatrixPtr< eT > encoded_batch_, mic::types::MatrixPtr< eT > encoded_targets_, eT learning_rate_, eT decay_=0.0f)
const size_t batch_size
Adam - adaptive moment estimation.
Definition: Adam.hpp:39
mic::encoders::ColMatrixEncoder< double > * mnist_encoder
MNIST matrix encoder.