Blame projects/neural/trainer.cpp

e865c9
e865c9
e865c9
#include <ctime></ctime>
e865c9
e865c9
#include "layer.all.inc.cpp"
b579b3
#include "test.all.inc.cpp"
e865c9
#include "train.digit.inc.cpp"
b579b3
#include "train.image.inc.cpp"
b579b3
#include "train.cx4.inc.cpp"
e4740d
#include "benchmark.inc.cpp"
e4740d
#include "benchmark.segment.inc.cpp"
e865c9
e865c9
e865c9
bool runTests() {
e865c9
 if (!AllTest::test()) return false;
e865c9
 return printf("success\n"), true;
e865c9
}
e865c9
e865c9
b579b3
bool trainDigits() {
b579b3
  #define FILENAME "data/output/weights-digit.bin"
e865c9
b579b3
  printf("create neural network\n");
b579b3
  Layer l(                    nullptr, Layout(28, 28) );
b579b3
  (new LayerSimple<funcsigmoidexp>( l, Layout(256)   ))->filename = FILENAME "1";</funcsigmoidexp>
b579b3
  (new LayerSimple<funcsigmoidexp>( l, Layout(64)    ))->filename = FILENAME "2";</funcsigmoidexp>
b579b3
  (new LayerSimple<funcsigmoidexp>( l, Layout(10)    ))->filename = FILENAME "3";</funcsigmoidexp>
b579b3
  l.sumStat().print();
e865c9
  
b579b3
  #undef FILENAME
b579b3
b579b3
  printf("load training data\n");
b579b3
  TrainerDigit t;
b579b3
  if (!t.loadSymbolMap("data/symbols-data.bin")) return 1; // 28x28
b579b3
b579b3
  printf("try load previously saved network\n"); l.load();
b579b3
  t.configure(l, 0.5, 8, 70000, 0, 0, 0.00001).run();
b579b3
  
b579b3
  return true;
b579b3
}
b579b3
b579b3
b579b3
bool trainDigitsConv() {
8e5348
  #define FILENAME "data/output/weights-digit-conv.bin"
e865c9
e865c9
  printf("create neural network\n");
8e5348
  Layer l(nullptr, Layout(28, 28));
b579b3
  Layer *ll[10] = {};
b579b3
  ll[1] = new LayerConvShared<funcrelu>(l, Layout(12, 12, 6), Kernel(4, 2, 0)); ll[1]->filename = FILENAME "1";</funcrelu>
b579b3
  ll[2] = new LayerConvShared<funcrelu>(l, Layout(4, 4, 12), Kernel(4, 2, 0)); ll[2]->filename = FILENAME "2";</funcrelu>
b579b3
  ll[3] = new LayerSimple<funcsigmoidexp>(l, Layout(64)); ll[3]->filename = FILENAME "3";</funcsigmoidexp>
b579b3
  ll[4] = new LayerSimple<funcsigmoidexp>(l, Layout(10)); ll[4]->filename = FILENAME "4";</funcsigmoidexp>
e865c9
b579b3
  
b579b3
  #undef FILENAME
e865c9
  l.sumStat().print();
e865c9
e865c9
  printf("load training data\n");
e865c9
  TrainerDigit t;
8e5348
  if (!t.loadSymbolMap("data/symbols-data.bin")) return 1; // 28x28
e865c9
b579b3
  printf("try load previously saved network\n"); l.load();
e865c9
b579b3
  //ll[1]->skipTrain = true;
b579b3
  //ll[2]->skipTrain = true;
b579b3
  
b579b3
  t.configure(l, 0.01, 8, 70000, 0, 0, 0.00001).run();
8e5348
  //t.configure(l, 0.5, 8, 70000, 0, 0, 0.00001).run();
b579b3
  
b579b3
  return true;
b579b3
}
b579b3
b579b3
b579b3
bool trainImage() {
b579b3
  #define FILENAME "data/output/weights-image.bin"
b579b3
b579b3
  printf("create neural network\n");
b579b3
  Layer l(nullptr, Layout(128, 128, 3));
b579b3
  Layer *ll[20] = {};
b579b3
  ll[ 1] = new LayerConvShared<funcrelu>(l, Layout(63, 63, 24), Kernel(4, 2, 0)); ll[1]->filename = FILENAME "1";</funcrelu>
b579b3
  ll[ 2] = new LayerConvShared<funcrelu>(l, Layout(29, 29, 48), Kernel(5, 2, 0)); ll[2]->filename = FILENAME "2";</funcrelu>
b579b3
  //ll[ 3] = new LayerConvShared<funcrelu>(l, Layout(14, 14, 24), Kernel(4, 2, 0)); ll[3]->filename = FILENAME "3";</funcrelu>
b579b3
  //ll[ 4] = new LayerConvShared<funcrelu>(l, Layout( 6,  6, 48), Kernel(4, 2, 0)); ll[4]->filename = FILENAME "4";</funcrelu>
b579b3
  //ll[ 5] = new LayerConvShared<funcrelu>(l, Layout( 2,  2, 96), Kernel(4, 2, 0)); ll[5]->filename = FILENAME "5";</funcrelu>
b579b3
  //ll[ 6] = new LayerDeconvShared<funcrelu>(l, Layout(  6,  6, 48), Kernel(4, 2, 0), ll[5]->weights);</funcrelu>
b579b3
  //ll[ 7] = new LayerDeconvShared<funcrelu>(l, Layout( 14, 14, 24), Kernel(4, 2, 0), ll[4]->weights);</funcrelu>
b579b3
  //ll[ 8] = new LayerDeconvShared<funcrelu>(l, Layout( 30, 30, 12), Kernel(4, 2, 0), ll[3]->weights);</funcrelu>
b579b3
  ll[ 9] = new LayerDeconvShared<funcrelu>(l, Layout( 63, 63, 24), Kernel(5, 2, 0), ll[2]->weights);</funcrelu>
b579b3
  ll[10] = new LayerDeconvShared<funcrelu>(l, Layout(128, 128, 3), Kernel(4, 2, 0), ll[1]->weights);</funcrelu>
b579b3
b579b3
  l.sumStat().print();
e865c9
b579b3
  printf("try load previously saved network\n"); l.load();
b579b3
b579b3
  ll[1]->skipTrain = true;
b579b3
  ll[10]->skipTrain = true;
b579b3
b579b3
  
b579b3
  TrainerImage t;
b579b3
  t.pad = 16;
b579b3
  t.datafile = "data/img128-data.bin";
b579b3
  t.outfile = FILENAME ".test";
b579b3
b579b3
  t.configure(l, 0.00001, 8, 1000, 0, 0, 0.00001).run();
b579b3
  
b579b3
  #undef FILENAME
b579b3
  return true;
b579b3
}
b579b3
b579b3
b579b3
bool trainCx4() {
b579b3
  #define FILENAME "data/output/weights-cx4.bin"
b579b3
b579b3
  printf("create neural network\n");
b579b3
  Layer l(nullptr, Layout(512, 512, 3).expandXY(2));
b579b3
  Layer *fl[20] = { &l };
b579b3
  int cnt = 1;
b579b3
  fl[cnt] = new LayerConvShared<funcrelu>(l, Layout(257, 257, 24).expandXY(3), Kernel(4, 2, -2)); fl[cnt]->filename = FILENAME "1"; ++cnt;</funcrelu>
b579b3
  fl[cnt] = new LayerConvShared<funcrelu>(l, Layout(130, 130, 48), Kernel(4, 2, -2)); fl[cnt]->filename = FILENAME "2"; ++cnt;</funcrelu>
e4740d
  //fl[cnt] = new LayerConvShared<funcrelu>(l, Layout( 66,  66, 96), Kernel(4, 2, -2)); fl[cnt]->filename = FILENAME "3"; ++cnt;</funcrelu>
b579b3
  //fl[cnt] = new LayerConvShared<funcrelu>(l, Layout( 6,  6, 48), Kernel(4, 2,  0)); fl[cnt]->filename = FILENAME "4"; ++cnt;</funcrelu>
b579b3
  //fl[cnt] = new LayerConvShared<funcrelu>(l, Layout( 2,  2, 96), Kernel(4, 2,  0)); fl[cnt]->filename = FILENAME "5"; ++cnt;</funcrelu>
b579b3
  for(int i = cnt-1; i > 0; --i) {
b579b3
    Layer *bl = new LayerDeconvShared<funcrelu>(l, fl[i-1]->layout, dynamic_cast<layerconvshared<funcrelu>*>(fl[i])->kernel, fl[i]->weights);</layerconvshared<funcrelu></funcrelu>
b579b3
    if (i < cnt-1) fl[i]->skipTrain = bl->skipTrain = true;
b579b3
  }
b579b3
b579b3
  l.sumStat().print();
b579b3
b579b3
  printf("try load previously saved network\n"); l.load();
b579b3
b579b3
  SegmentCx4 s(fl[cnt-2]->layout.getD(), fl[cnt-1]->layout.getD(), fl[cnt-1]->weights);
b579b3
  s.filename = fl[cnt-1]->filename;
b579b3
b579b3
  TrainerCx4 t;
b579b3
  t.layerFull        = &l;
b579b3
  t.layerPre         = cnt > 2 ? fl[cnt-2] : nullptr;
b579b3
  t.segment          = &s;
b579b3
  t.ratio            = 0.000001;
b579b3
  t.threadsCount     = 8;
b579b3
  t.measuresPerBlock = 1000;
e4740d
  t.trainsPerBlock   = 100000;
b579b3
  t.loadImagesCount  = 100;
e4740d
  t.blocksPerLoading = 1;
b579b3
  t.qmin             = 0.00001;
b579b3
  t.infile           = "data/img512-data.bin";
b579b3
  t.outfile          = FILENAME ".test";
b579b3
  
b579b3
  t.run();
b579b3
  
b579b3
  #undef FILENAME
b579b3
  return true;
b579b3
}
b579b3
b579b3
b579b3
int main() {
b579b3
  srand(time(NULL));
b579b3
e4740d
  //while(1) BenchmarkSegment::run(8);
e4740d
  //while(1) Benchmark().run(8);
b579b3
  //return !runTests();
b579b3
  //return !trainDigits();
b579b3
  //return !trainDigitsConv();
b579b3
  //return !trainImage();
b579b3
  return !trainCx4();
e865c9
}
e865c9