SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
helper_functions.hpp
Go to the documentation of this file.
1 /*
2  * SNABSuite -- Spiking Neural Architecture Benchmark Suite
3  * Copyright (C) 2019 Christoph Ostrau
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 #pragma once
19 #include <assert.h>
20 
21 #include <chrono>
22 #include <cmath>
23 #include <cypress/cypress.hpp>
24 #include <fstream>
25 #include <string>
26 #include <utility> //std::pair
27 
28 namespace mnist_helper {
29 using namespace cypress;
30 
31 typedef std::pair<std::vector<std::vector<Real>>, std::vector<uint16_t>>
33 typedef std::pair<std::vector<std::vector<std::vector<Real>>>,
34  std::vector<uint16_t>>
36 typedef std::vector<std::vector<std::vector<std::vector<Real>>>>
40  std::vector<size_t> input_sizes;
41  std::vector<size_t> output_sizes;
42  size_t stride;
43  size_t padding;
44 };
46 struct POOLING_LAYER {
47  std::vector<size_t> input_sizes;
48  std::vector<size_t> output_sizes;
49  std::vector<size_t> size;
50  size_t stride;
51 };
63 MNIST_DATA loadMnistData(const size_t num_data, const std::string path);
64 
71 void print_image(const std::vector<Real> &img, size_t wrap);
72 
83 std::vector<std::vector<std::vector<Real>>> image_to_rate(
84  const std::vector<std::vector<Real>> &images, const Real duration,
85  const Real max_freq, size_t num_images, bool poisson = true);
86 
98 SPIKING_MNIST mnist_to_spike(const MNIST_DATA &mnist_data, const Real duration,
99  const Real max_freq, size_t num_images,
100  bool poisson = true);
101 
115 std::vector<MNIST_DATA> create_batches(const SPIKING_MNIST &mnist_data,
116  const size_t batch_size, Real duration,
117  Real pause, const bool shuffle = false,
118  unsigned seed = 0);
119 
127 cypress::Population<SpikeSourceArray> create_spike_source(
128  Network &netw, const MNIST_DATA &spikes);
129 
137 cypress::Population<SpikeSourceArray> &update_spike_source(
138  cypress::Population<SpikeSourceArray> &source, const MNIST_DATA &spikes);
139 
148 Json read_network(std::string path, bool msgpack = true);
149 
156 template <typename T>
157 Real max_weight(const T &json)
158 {
159  Real max = 0.0;
160  for (size_t i = 0; i < json.size(); i++) {
161  for (size_t j = 0; j < json[i].size(); j++) {
162  Real tmp = Real(json[i][j]);
163  if (tmp > max) {
164  max = tmp;
165  }
166  }
167  }
168  return max;
169 }
170 
177 template <typename T>
178 Real max_weight(const Matrix<T> &mat)
179 {
180  Real max = 0.0;
181  for (size_t i = 0; i < mat.size(); i++) {
182  Real tmp = Real(mat[i]);
183  if (tmp > max) {
184  max = tmp;
185  }
186  }
187  return max;
188 }
189 
196 template <typename T>
197 Real min_weight(const T &json)
198 {
199  Real min = 0.0;
200  for (size_t i = 0; i < json.size(); i++) {
201  for (size_t j = 0; j < json[i].size(); j++) {
202  Real tmp = Real(json[i][j]);
203  if (tmp < min) {
204  min = tmp;
205  }
206  }
207  }
208  return min;
209 }
210 
217 template <typename T>
218 Real min_weight(const Matrix<T> &json)
219 {
220  Real min = 0.0;
221  for (size_t i = 0; i < json.size(); i++) {
222  Real tmp = Real(json[i]);
223  if (tmp < min) {
224  min = tmp;
225  }
226  }
227  return min;
228 }
229 
236 template <typename T>
237 Real max_weight_abs(const T &json)
238 {
239  Real max = 0.0;
240  for (size_t i = 0; i < json.size(); i++) {
241  for (size_t j = 0; j < json[i].size(); j++) {
242  Real tmp = fabs(Real(json[i][j]));
243  if (tmp > max) {
244  max = tmp;
245  }
246  }
247  }
248  return max;
249 }
250 
257 template <typename T>
258 Real max_weight_abs(const Matrix<T> &json)
259 {
260  Real max = 0.0;
261  for (size_t i = 0; i < json.size(); i++) {
262  Real tmp = fabs(Real(json[i]));
263  if (tmp > max) {
264  max = tmp;
265  }
266  }
267  return max;
268 }
269 
278 std::vector<LocalConnection> dense_weights_to_conn(const Matrix<Real> &mat,
279  Real scale, Real delay);
280 
289 std::vector<LocalConnection> conv_weights_to_conn(const mnist_helper::CONVOLUTION_LAYER &layer,
290  Real scale, Real delay);
291 
302 std::vector<std::vector<LocalConnection>> pool_to_conn(
303  const mnist_helper::POOLING_LAYER &layer,Real max_pool_weight,
304  Real pool_inhib_weight, Real delay, Real pool_delay);
305 
317 std::vector<uint16_t> spikes_to_labels(const PopulationBase &pop, Real duration,
318  Real pause, size_t batch_size);
330 void conv_spikes_per_kernel(const std::string& filename, const PopulationBase& pop,
331  Real duration, Real pause, size_t batch_size, Real norm = 0.0);
332 
345 std::vector<std::vector<Real>> spikes_to_rates(const PopulationBase pop,
346  Real duration, Real pause,
347  size_t batch_size,
348  Real norm = 0.0);
349 
358 size_t compare_labels(std::vector<uint16_t> &label, std::vector<uint16_t> &res);
359 
369 std::vector<Real> av_pooling_image(std::vector<Real> &image, size_t height,
370  size_t width, size_t pooling_size);
371 
378 MNIST_DATA scale_mnist(MNIST_DATA &data, size_t pooling_size = 3);
379 
392 SPIKING_MNIST read_data_to_spike(const size_t num_images, bool train_data,
393  const Real duration, const Real max_freq,
394  bool poisson = true, bool scale_down = false);
395 
404 std::vector<LocalConnection> conns_from_mat(
405  const cypress::Matrix<Real> &weights, Real delay, Real scale_factor = 0.0);
406 
416 void update_conns_from_mat(const std::vector<cypress::Matrix<Real>> &weights,
417  Network &netw, Real delay = 1.0,
418  Real scale_factor = 0.0);
419 } // namespace mnist_helper
std::vector< size_t > output_sizes
std::vector< std::vector< std::vector< Real > > > image_to_rate(const std::vector< std::vector< Real >> &images, const Real duration, const Real max_freq, size_t num_images, bool poisson=true)
Converts a vector of images to a rate based representation.
std::vector< MNIST_DATA > create_batches(const SPIKING_MNIST &mnist_data, const size_t batch_size, Real duration, Real pause, const bool shuffle=false, unsigned seed=0)
Creates batches of spikes representing the MNIST data.
std::vector< std::vector< LocalConnection > > pool_to_conn(const mnist_helper::POOLING_LAYER &layer, Real max_pool_weight, Real pool_inhib_weight, Real delay, Real pool_delay)
std::vector< Real > av_pooling_image(std::vector< Real > &image, size_t height, size_t width, size_t pooling_size)
Downscale an image by average pooling.
std::vector< LocalConnection > dense_weights_to_conn(const Matrix< Real > &mat, Real scale, Real delay)
Convert a dense layer to list of Local Connections.
Real max_weight_abs(const T &json)
Calculate the max absolute weight.
std::vector< LocalConnection > conv_weights_to_conn(const mnist_helper::CONVOLUTION_LAYER &layer, Real scale, Real delay)
Converts a conv layer to list of Local Connections.
MNIST_DATA scale_mnist(MNIST_DATA &data, size_t pooling_size=3)
downscale the complete MNIST dataset
Json read_network(std::string path, bool msgpack=true)
Read in the network file from json of msgpack. The Repo provides a script which creates compatible fi...
std::vector< LocalConnection > conns_from_mat(const cypress::Matrix< Real > &weights, Real delay, Real scale_factor=0.0)
Generate connection from given weight matrix.
void print_image(const std::vector< Real > &img, size_t wrap)
Prints image to std::cout.
Real min_weight(const T &json)
Calculate the min weight.
std::vector< std::vector< Real > > spikes_to_rates(const PopulationBase pop, Real duration, Real pause, size_t batch_size, Real norm=0.0)
Converts the simulation results into values between 0 and 1.
SPIKING_MNIST read_data_to_spike(const size_t num_images, bool train_data, const Real duration, const Real max_freq, bool poisson=true, bool scale_down=false)
Reads in MNIST test or train data.
std::pair< std::vector< std::vector< Real > >, std::vector< uint16_t > > MNIST_DATA
std::vector< size_t > output_sizes
cypress::Population< SpikeSourceArray > create_spike_source(Network &netw, const MNIST_DATA &spikes)
Creates Spike sources in network from spikes.
std::pair< std::vector< std::vector< std::vector< Real > > >, std::vector< uint16_t > > SPIKING_MNIST
void conv_spikes_per_kernel(const std::string &filename, const PopulationBase &pop, Real duration, Real pause, size_t batch_size, Real norm=0.0)
Saves the spikes of a convolution layer in a file.
std::vector< size_t > input_sizes
size_t compare_labels(std::vector< uint16_t > &label, std::vector< uint16_t > &res)
Compare original labels with simulation labels, return number of correct labels.
cypress::Population< SpikeSourceArray > & update_spike_source(cypress::Population< SpikeSourceArray > &source, const MNIST_DATA &spikes)
Update Spike sources in network from spikes.
std::vector< size_t > size
void update_conns_from_mat(const std::vector< cypress::Matrix< Real >> &weights, Network &netw, Real delay=1.0, Real scale_factor=0.0)
Updates the connector is a given network with the weights provided.
std::vector< uint16_t > spikes_to_labels(const PopulationBase &pop, Real duration, Real pause, size_t batch_size)
Converts the simulation results into label data.
Real max_weight(const T &json)
Calculate the max weight, ignore negative values.
std::vector< std::vector< std::vector< std::vector< Real > > > > CONVOLUTION_FILTER
SPIKING_MNIST mnist_to_spike(const MNIST_DATA &mnist_data, const Real duration, const Real max_freq, size_t num_images, bool poisson=true)
Converts the full MNIST dataset to a spiking MNIST dataset.
MNIST_DATA loadMnistData(const size_t num_data, const std::string path)
Read in MNIST data from files.
std::vector< size_t > input_sizes