MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
MNISTPatchReconstructionApplication.cpp
Go to the documentation of this file.
1 
40 
41 
42 
43 namespace mic {
44 namespace application {
45 
50 void RegisterApplication (void) {
52 }
53 
54 } /* namespace application */
55 
56 namespace applications {
57 
58 MNISTPatchReconstructionApplication::MNISTPatchReconstructionApplication(std::string node_name_) : OpenGLContinuousLearningApplication(node_name_),
59  mlnn_filename("mlnn_filename", "mlnn.txt"),
60  mlnn_save("mlnn_save", false),
61  mlnn_load("mlnn_load", false)
62  {
63  // Register properties - so their values can be overridden (read from the configuration file).
64  registerProperty(mlnn_filename);
65  registerProperty(mlnn_save);
66  registerProperty(mlnn_load);
67 
68  LOG(LINFO) << "Properties registered";
69 
70  // Create importers.
71  training_dataset_importer = new mic::importers::MNISTPatchImporter("mnist_training_dataset_importer");
72  test_dataset_importer = new mic::importers::MNISTPatchImporter("mnist_test_dataset_importer");
73 
74 }
75 
78  delete(test_dataset_importer);
79  // Delete visualization windows.
80  delete (w2d_input);
81  delete (w2d_reconstruction);
82  delete (w_chart);
83 }
84 
86 
87  // Initialize GLUT! :]
88  VGL_MANAGER->initializeGLUT(argc, argv);
89 
90  // Create two visualization windows
91  w2d_input = new WindowMatrix2D("Input matrix", 0, 0, 256, 256);
92  w2d_reconstruction = new WindowMatrix2D("Reconstructed matrix", 320, 0, 256, 256);
93 
94  collector_ptr = std::make_shared < mic::utils::DataCollector<std::string, float> >( );
95  // Add containers to collector.
96  collector_ptr->createContainer("training_loss", mic::types::color_rgba(0, 0, 255, 180));
97  collector_ptr->createContainer("test_loss", mic::types::color_rgba(0, 255, 0, 180));
98 
99  // Create the visualization windows - must be created in the same, main thread :]
100  w_chart = new WindowCollectorChart<float>("MNISTPatchReconstruction", 0, 310, 512, 256);
101  w_chart->setDataCollectorPtr(collector_ptr);
102 
103 }
104 
106  LOG(LTRACE) << "MNISTClassificationSoftmaxApplication::initializePropertyDependentVariables";
107 
108  // Get patch size.
109  patch_size = training_dataset_importer->getPatchSize();
110 
111  // Allocate memory for images.
112  input_image = std::make_shared<mic::types::MatrixXf >(patch_size, patch_size);
113  reconstructed_image = std::make_shared<mic::types::MatrixXf >(patch_size, patch_size);
114 
115  // Set displayed matrix pointers.
116  w2d_input->setMatrixPointerSynchronized(input_image);
117  w2d_reconstruction->setMatrixPointerSynchronized(reconstructed_image);
118 
119  // Load datasets.
120  if (!training_dataset_importer->importData())
121  return;
122 
123  if (!test_dataset_importer->importData())
124  return;
125 
126  // Try to load neural network from file.
127  if ((mlnn_load) && (neural_net.load(mlnn_filename))) {
128  // Do nothing ;)
129  } else {
130  // Create a simple autoencoder.
131  neural_net.pushLayer(new Linear<float>(patch_size*patch_size, 20));
133 
134 /* neural_net.pushLayer(new Linear<float>(20, 5));
135  neural_net.pushLayer(new ReLU<float>(5));
136 
137  neural_net.pushLayer(new Linear<float>(5, 20));
138  neural_net.pushLayer(new ReLU<float>(20));*/
139 
140  neural_net.pushLayer(new Linear<float>(20, patch_size*patch_size));
141  neural_net.pushLayer(new ReLU<float>(patch_size*patch_size));
143 
144  LOG(LINFO) << "Generated new neural network";
145  }//: else
146 
147 }
148 
149 
151 
152  // Random select sample from training dataset.
153  mic::types::MNISTSample<float> sample = training_dataset_importer->getRandomSample();
154 
155  // Copy sample data to input matrix - for visualization.
156  (*input_image) = (*sample.data());
157  //std::cout << " input: \n" << *(input_image) << std::endl;
158 
159  // Encode sample data....
160  mic::types::MatrixXfPtr encoded_patch (new mic::types::MatrixXf(*sample.data()));
161  // ... i.e. reshape it.
162  encoded_patch->resize(patch_size*patch_size, 1);
163 
164  // Train the autoencoder.
165  float loss = neural_net.train (encoded_patch, encoded_patch, 0.005);
166  //std::cout << loss << std::endl;
167  collector_ptr->addDataToContainer("training_loss", loss);
168 
169  // Get reconstruction.
170  mic::types::MatrixXfPtr encoded_reconstruction = neural_net.getPredictions();
171 
172  (*reconstructed_image) = (*encoded_reconstruction);
173  reconstructed_image->resize(patch_size, patch_size);
174  return true;
175 }
176 
177 
179  // Random select sample from test dataset.
180  mic::types::MNISTSample<float> sample = test_dataset_importer->getRandomSample();
181 
182  // Copy sample data to input matrix - for visualization.
183  (*input_image) = (*sample.data());
184 
185  // Encode sample data....
186  mic::types::MatrixXfPtr encoded_patch (new mic::types::MatrixXf(*sample.data()));
187  // ... i.e. reshape it.
188  encoded_patch->resize(patch_size*patch_size, 1);
189 
190  // Train the autoencoder.
191  float loss = neural_net.test (encoded_patch, encoded_patch);
192 
193  // Get reconstruction.
194  mic::types::MatrixXfPtr decoded_reconstruction = neural_net.getPredictions();
195 
196  (*reconstructed_image) = (*decoded_reconstruction);
197  reconstructed_image->resize(patch_size, patch_size);
198 
199  // Collect statistics.
200  collector_ptr->addDataToContainer("test_loss", loss);
201 
202 }
203 
204 
206  // Average the sums.
207  /*classification_cost_sum /= (float)number_of_averaged_test_measures;
208  correct_classification_factor_sum /= (float)number_of_averaged_test_measures;
209 
210  LOG(LINFO)<< "Iteration = " << iteration << " classification_cost_sum = " << classification_cost_sum;
211 
212  // Add data to chart window.
213 
214  // Reset partial sums.
215  classification_cost_sum = 0;
216  correct_classification_factor_sum = 0;
217  */
218 
219  //collector_ptr->addDataToContainer("reconstruction", classification_cost_sum);
220  //classification_cost_sum += .001;
221 
222  LOG(LINFO)<< "Iteration = " << iteration;
223 
224  // Save nn to file.
225  if (mlnn_save)
227 }
228 
229 
230 
231 } /* namespace applications */
232 } /* namespace mic */
mic::types::MatrixXfPtr reconstructed_image
Reconstructed image/matrix.
mic::importers::MNISTPatchImporter * training_dataset_importer
Importer responsible for loading training dataset.
mic::importers::MNISTPatchImporter * test_dataset_importer
Importer responsible for loading testing dataset.
WindowCollectorChart< float > * w_chart
Window for displaying chart with statistics.
Class implementing a simple MNIST patch reconstruction with multi-layer neural net.
MNISTPatchReconstructionApplication(std::string node_name_="mnist_patch_autoencoder_reconstruction")
WindowMatrix2D * w2d_input
Window for displaying the input image.
mic::types::MatrixPtr< eT > getPredictions()
void RegisterApplication(void)
Registers application.
eT test(mic::types::MatrixPtr< eT > encoded_batch_, mic::types::MatrixPtr< eT > encoded_targets_)
mic::configuration::Property< bool > mlnn_load
Property: flag denoting whether the nn should be loaded from a file (at the initialization of the tas...
mic::configuration::Property< std::string > mlnn_filename
Property: name of the file to which the neural network will be serialized (or deserialized from)...
mic::configuration::Property< bool > mlnn_save
Property: flag denoting whether the nn should be saved to a file (after every episode end)...
BackpropagationNeuralNetwork< float > neural_net
Multi-layer neural network.
eT train(mic::types::MatrixPtr< eT > encoded_batch_, mic::types::MatrixPtr< eT > encoded_targets_, eT learning_rate_, eT decay_=0.0f)
WindowMatrix2D * w2d_reconstruction
Window for displaying the reconstructed image.
mic::utils::DataCollectorPtr< std::string, float > collector_ptr
Data collector.