DaNNet
dnn_misc.h
Go to the documentation of this file.
1 // Copyright 2019 Claes Rolen (www.rolensystems.com)
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 namespace dnn
17 {
23 
24 
29 std::string version_info(void)
30 {
31  return std::to_string(DNN_VERSION_MAJOR)+"."+std::to_string(DNN_VERSION_MINOR)+"."+std::to_string(DNN_VERSION_PATCH);
32 }
33 
41 template<typename T>
42 void pol2cart(const T phi,const T r, T& x, T& y)
43 {
44  x = r*cos(phi);
45  y = r*sin(phi);
46 }
54 template<typename T>
55 void pol2cart(const arma::Mat<T> phi,const arma::Mat<T> r, arma::Mat<T>& x, arma::Mat<T>& y)
56 {
57  x = r%cos(phi);
58  y = r%sin(phi);
59 }
60 
68 template<typename T>
69 void cart2pol(const T x, const T y, T& phi, T& r)
70 {
71  phi = atan2(y,x);
72  r = sqrt(x*x+y*y);
73 }
74 
82 template<typename T>
83 void cart2pol(const arma::Mat<T> x, const arma::Mat<T> y, arma::Mat<T>& phi, arma::Mat<T>& r)
84 {
85  phi = atan2(y,x);
86  r = sqrt(x%x+y%y);
87 }
88 
89 
97 arma_inline arma::Mat<DNN_Dtype> row2col(const arma::Mat<DNN_Dtype>& A)
98 {
99  arma::Mat<DNN_Dtype> B=arma::vectorise(A,1);
100  return B.t();
101 }
102 
110 arma_inline arma::Mat<DNN_Dtype> col2col(const arma::Mat<DNN_Dtype>& A)
111 {
112  return arma::vectorise(A);
113 }
114 
125 arma_inline arma::Mat<DNN_Dtype> mat2mat(const arma::Mat<DNN_Dtype>& A, const arma::uword rows,const arma::uword cols)
126 {
127  arma::Mat<DNN_Dtype> B(A.memptr(), rows, cols);
128  return B;
129 }
130 
142 arma_inline arma::Cube<DNN_Dtype> mat2cube(const arma::Mat<DNN_Dtype>& A, const arma::uword rows,const arma::uword cols, const arma::uword slices)
143 {
144  arma::Cube<DNN_Dtype> B(A.memptr(), rows, cols, slices);
145  return B;
146 }
147 
158 arma_inline arma::Mat<DNN_Dtype> cube2mat(arma::Cube<DNN_Dtype>& A,const arma::uword rows,const arma::uword cols)
159 {
160  arma::Mat<DNN_Dtype> B(A.memptr(),rows,cols);
161  return B;
162 }
163 
164 
179 void gen_spiral(arma::Mat<DNN_Dtype> &X,arma::Mat<DNN_Dtype> &T, arma::uword N, arma::uword K,
180  const double ph_var = 0.1, const double r_min = 0.1,
181  const double ph_max = 7)
182 {
183  X.zeros(2,N);
184  T.zeros(K,N);
185  arma::uvec Tk = arma::randi<arma::uvec>(N,arma::distr_param(1,(int)K));
186  for(arma::uword n=0; n<N; n++)
187  {
188  double x=0,y=0;
189  double r = r_min+ arma::randu()*(1-r_min);
190  double ph = r*ph_max+ ph_var*arma::randn()+ Tk(n)*2.0*arma::datum::pi/K;
191  pol2cart(ph,r,x,y);
192  T(Tk(n)-1,n) = static_cast<DNN_Dtype>(1.0);
193  X(0,n) = static_cast<DNN_Dtype>(x);
194  X(1,n) = static_cast<DNN_Dtype>(y);
195  }
196 }
197 
198 #ifdef SIGPACK_H
199 void scat_plot(arma::Mat<DNN_Dtype>& _X,arma::Mat<DNN_Dtype>& _T,int f1,int f2)
200 {
201  sp::gplot gp0;
202  std::ostringstream tmp_s;
203  double xmin,xmax,ymin,ymax;
204  xmin = _X.row(f1).min();
205  xmax = _X.row(f1).max();
206  ymin = _X.row(f2).min();
207  ymax = _X.row(f2).max();
208 
209  gp0.window("Plot", 10, 10, 600, 600);
210  gp0.xlim(xmin,xmax);
211  gp0.ylim(ymin,ymax);
212 
213  for(arma::uword c = 0; c<_T.n_rows; c++)
214  {
215  arma::Mat<DNN_Dtype> XC = _X.cols(arma::find(_T.row(c)>0.9));
216  arma::Row<DNN_Dtype> x = XC.row(f1);
217  arma::Row<DNN_Dtype> y = XC.row(f2);
218  gp0.plot_add(x,y," ","points ");
219  }
220  gp0.plot_show();
221 }
222 #endif // SIGPACK_H
223 
226 uint32_t swap_endian(uint32_t i)
227 {
228  uint32_t rev_i = 0;
229  rev_i += (i & 0xff) << 24;
230  rev_i += ((i >> 8) & 0xff) << 16;
231  rev_i += ((i >> 16) & 0xff) << 8;
232  rev_i += ((i >> 24) & 0xff);
233  return rev_i;
234 }
235 
246 bool read_idx3(std::string fname, arma::Cube<DNN_Dtype>& img, const double frac=1.0)
247 {
248  uint32_t magic_number = 0;
249  uint32_t number_of_images = 0;
250  uint32_t number_of_rows = 0;
251  uint32_t number_of_cols = 0;
252 
253  std::ifstream ifs;
254  ifs.open(fname.c_str(), std::ios::binary);
255  if (!ifs.good())
256  {
257  std::cout << "Could not open " << fname << std::endl;
258  return false;
259  }
260  else
261  {
262  // Read Header IDX3
263  ifs.seekg(0, std::ios::beg);
264  ifs.read(reinterpret_cast<char*>(&magic_number), sizeof(magic_number));
265  magic_number = swap_endian(magic_number);
266  ifs.read(reinterpret_cast< char*>(&number_of_images), sizeof(number_of_images));
267  number_of_images = swap_endian(number_of_images);
268  ifs.read(reinterpret_cast<char*>(&number_of_rows), sizeof(number_of_rows));
269  number_of_rows = swap_endian(number_of_rows);
270  ifs.read(reinterpret_cast<char*>(&number_of_cols), sizeof(number_of_cols));
271  number_of_cols = swap_endian(number_of_cols);
272 
273  arma::uword red_nr = static_cast<arma::uword>(floor(frac*number_of_images));
274  // Create cube of images
275  img.set_size(number_of_rows, number_of_cols, red_nr);
276 
277  // Fill with data
278  for (arma::uword i = 0; i < red_nr; ++i)
279  {
280  for (arma::uword r = 0; r < number_of_rows; ++r)
281  {
282  for (arma::uword c = 0; c < number_of_cols; ++c)
283  {
284  unsigned char tmp = 0;
285  ifs.read((char*)&tmp, sizeof(tmp));
286  img(r, c, i) = (DNN_Dtype)tmp;
287  }
288  }
289  }
290  }
291  ifs.close();
292  return true;
293 }
294 
305 bool read_idx1(std::string fname, arma::ivec& label, const double frac=1.0)
306 {
307  std::ifstream ifs;
308  ifs.open(fname.c_str(), std::ifstream::binary);
309  if (!ifs.good())
310  {
311  std::cout << "Could not open " << fname << std::endl;
312  return false;
313  }
314  else
315  {
316  uint32_t magic_number = 0;
317  uint32_t number_of_items = 0;
318 
319  // Read Header IDX1
320  ifs.read(reinterpret_cast<char*>(&magic_number), 4);
321  magic_number = swap_endian(magic_number);
322  ifs.read(reinterpret_cast<char*>(&number_of_items),4);
323  number_of_items = swap_endian(number_of_items);
324 
325 
326  arma::uword red_nr = static_cast<arma::uword>(floor(frac*number_of_items));
327  // Create vec of labels
328  label.set_size(red_nr);
329  for (arma::uword i = 0; i < red_nr; ++i)
330  {
331  unsigned char tmp = 0;
332  ifs.read((char*)&tmp, sizeof(tmp));
333  label(i) = (unsigned int)tmp;
334  }
335  }
336  ifs.close();
337  return true;
338 }
339 
352 void read_MNIST(std::string fname_X, std::string fname_T, arma::Mat<DNN_Dtype>& X, arma::Mat<DNN_Dtype>& T,double frac=1.0)
353 {
354  arma::Cube<DNN_Dtype> img;
355  arma::ivec label;
356 
357  read_idx3(fname_X, img,frac);
358  read_idx1(fname_T, label,frac);
359 
360  X.set_size(label.n_elem,img.n_elem_slice);
361  X.zeros();
362  T.set_size(label.n_elem,10);
363  T.zeros();
364  for(arma::uword n=0; n<label.n_elem; n++)
365  {
366  T(n,label(n)) = 1; // Map 0..9 to matrix
367  arma::Mat<DNN_Dtype> xx = arma::vectorise(img.slice(n)).t();
368  X.row(n) = xx/255;
369  }
370  // cout << "Size= " << arma::size(img) << endl;
371  arma::inplace_trans(X);
372  arma::inplace_trans(T);
373 }
374 
382 void progress_bar(const std::string str, double p)
383 {
384  const int length = 20;
385  const int update = 100/length;
386  if(int(p*100)%update==0)
387  {
388  std::cout << str << " [";
389  int pos = static_cast<int>(length * p);
390  for (int i = 0; i < length; ++i)
391  {
392  if (i < pos)
393  std::cout << "=";
394  else if (i == pos)
395  std::cout << ">";
396  else
397  std::cout << " ";
398  }
399  std::cout << "] " << int(p*100)+update << " %\r";
400  std::cout.flush();
401  }
402 }
403 
412 void progress_bar(const std::string str, arma::uword num, arma::uword maxnum)
413 {
414  progress_bar(str,double(num)/maxnum);
415 }
416 
423 {
424 // Erase line
425 #ifdef _WIN32
426  std::cout << "\r \r";
427 #else
428  std::cout << "\33[2K";
429 #endif
430 }
432 } // End namespace dnn
void cart2pol(const T x, const T y, T &phi, T &r)
Cartesian to polar convertion.
Definition: dnn_misc.h:69
arma_inline arma::Mat< DNN_Dtype > row2col(const arma::Mat< DNN_Dtype > &A)
Flattens a matrix row-wise to a one column matrix.
Definition: dnn_misc.h:97
void remove_progress_bar(void)
Clears progress bar.
Definition: dnn_misc.h:422
#define DNN_VERSION_MAJOR
Definition: dnn.h:24
arma_inline arma::Mat< DNN_Dtype > mat2mat(const arma::Mat< DNN_Dtype > &A, const arma::uword rows, const arma::uword cols)
Reshapes a matrix.
Definition: dnn_misc.h:125
#define DNN_VERSION_PATCH
Definition: dnn.h:26
arma_inline arma::Mat< DNN_Dtype > cube2mat(arma::Cube< DNN_Dtype > &A, const arma::uword rows, const arma::uword cols)
Converts a cube to a matrix.
Definition: dnn_misc.h:158
void gen_spiral(arma::Mat< DNN_Dtype > &X, arma::Mat< DNN_Dtype > &T, arma::uword N, arma::uword K, const double ph_var=0.1, const double r_min=0.1, const double ph_max=7)
Dataset generator: a rotating spiral with K classes.
Definition: dnn_misc.h:179
float DNN_Dtype
Data type used in the network (float or double)
Definition: dnn.h:28
bool read_idx1(std::string fname, arma::ivec &label, const double frac=1.0)
Reads a IDX1 file.
Definition: dnn_misc.h:305
#define DNN_VERSION_MINOR
Definition: dnn.h:25
arma_inline arma::Cube< DNN_Dtype > mat2cube(const arma::Mat< DNN_Dtype > &A, const arma::uword rows, const arma::uword cols, const arma::uword slices)
Converts a matrix to a cube.
Definition: dnn_misc.h:142
uint32_t swap_endian(uint32_t i)
Endian converter for IDX1 and IDX3 file.
Definition: dnn_misc.h:226
Definition: dnn.h:22
std::string version_info(void)
Generate DaNNet version string.
Definition: dnn_misc.h:29
void progress_bar(const std::string str, double p)
Console progress bar.
Definition: dnn_misc.h:382
void read_MNIST(std::string fname_X, std::string fname_T, arma::Mat< DNN_Dtype > &X, arma::Mat< DNN_Dtype > &T, double frac=1.0)
Reads a MNIST digit dataset.
Definition: dnn_misc.h:352
arma_inline arma::Mat< DNN_Dtype > col2col(const arma::Mat< DNN_Dtype > &A)
Flattens a matrix col-wise to a one column matrix.
Definition: dnn_misc.h:110
void pol2cart(const T phi, const T r, T &x, T &y)
Polar to cartesian convertion.
Definition: dnn_misc.h:42
bool read_idx3(std::string fname, arma::Cube< DNN_Dtype > &img, const double frac=1.0)
Reads a IDX3 file.
Definition: dnn_misc.h:246