Blob Blame Raw

#include <ctime>
#include <cstdlib>
#include <cstdio>

#include <chrono>
#include <algorithm>

#include "nnlayer3.mt.inc.cpp"
#include "tga.inc.cpp"


long long timeUs() {
  static std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
  return (long long)std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now() - begin ).count();
}


void imgTrain(Layer &l, const char *datafile, int size, const char *outfile, int blockSize, int blocksCount, Real trainRatio, int threads) {
  Layer &fl = l.front();
  Layer &bl = l.back();

  assert(!l.prev);
  assert(datafile);
  assert(size > 0);
  assert(fl.countNeurons() == size);
  assert(bl.countNeurons() == size);

  assert(blockSize > 0);
  assert(blocksCount > 0);
  assert(trainRatio > 0);
  assert(threads > 0);

  FILE *f = fopen(datafile, "rb");
  if (!f)
    { printf("cannot open file: %s\n", datafile); return; }
  fseeko64(f, 0, SEEK_END);
  long long fsize = ftello64(f);
  int xCount = (int)(fsize/size);
  if (xCount <= 0)
    { printf("no tests in file: %s\n", datafile); return; }

  printf("allocate %lld bytes for tests\n", ((long long)blockSize + 1)*size);

  int *block = new int[blockSize*2];
  int *shuffle = block + blockSize;
  unsigned char *blockData = new unsigned char[(blockSize + 1)*size];
  unsigned char *blockResData = blockData + blockSize*size;
  bool err = false;

  for(int j = 0; j < blockSize; ++j)
    shuffle[j] = j;

  TrainMT tmt;
  tmt.layer = &fl;
  tmt.dataX = blockData;
  tmt.dataY = blockData;
  tmt.strideX = tmt.strideY = size;
  tmt.shuffle = shuffle;
  tmt.count = blockSize;
  tmt.trainRatio = trainRatio;

  printf("training %d (%d x %d blocks), tests: %d, ratio: %f:\n", blocksCount*blockSize, blocksCount, blockSize, xCount, trainRatio);

  long long t0 = timeUs();
  for(int i = 0; i < blocksCount; ++i) {
    for(int j = 0; j < blockSize; ++j) {
      block[j] = rand()%xCount;
      std::swap(shuffle[j], shuffle[rand()%blockSize]);
    }
    std::sort(block, block + blockSize);

    for(int j = 0; j < blockSize; ++j) {
      fseeko64(f, block[j]*(long long)size, SEEK_SET);
      if (!fread(blockData + j*size, size, 1, f))
        { printf("cannot read data from file: %s\n", datafile); err = true; break; }
    }
    if (err) break;

    //printf("  next data block loaded\n");

    long long t = timeUs();
    double res = tmt.train(threads);
    long long dt = timeUs() - t;

    if (outfile && !fl.saveAll(outfile))
      { printf("cannot save neural network weights to file: %s\n", outfile); err = true; break; }

    unsigned char *data = blockResData;
    for(Neuron *ibn = bl.neurons, *e = ibn + size; ibn < e; ++ibn, ++data) {
      Real v = (ibn->v - 0.25)*2;
      *data = v < 0 ? 0u : v > 1 ? 255u : (unsigned char)(v * 255.999);
    }
    tgaSave("data/output/sampleX.tga", blockData + shuffle[blockSize-1]*size, 256, 256, 3);
    tgaSave("data/output/sampleY.tga", blockResData, 256, 256, 3);

    long long t1 = timeUs();
    long long dt0 = t1 - t0;
    t0 = t1;

    printf("%4d: total: %6d, avg result: %f, time: %f + %f = %f\n", i+1, (i+1)*blockSize, res, (dt0-dt)*0.000001, dt*0.000001, dt0*0.000001);

  }

  delete[] block;
  delete[] blockData;

  printf("finished\n");
}


int main() {
  srand(time(NULL));

  const char *datafile = "data/img256-data.bin";
  const char *outfile = "data/output/img256-weights.bin";

  printf("create neural network\n");

  Layer l(nullptr, 256, 256, 3);
  //new Layer(&l, 128, 128, 3,  6);
  //new Layer(&l,  64,  64, 3,  8);
  //new Layer(&l,  32,  32, 3, 11);
  //new Layer(&l,  16,  16, 4, 16);
  //new Layer(&l,  16,  16, 4, 16);
  //new Layer(&l,  16,  16, 4, 16);
  //new Layer(&l,  32,  32, 3, 11);
  //new Layer(&l,  64,  64, 3,  8);
  //new Layer(&l, 128, 128, 3,  6);
  new Layer(&l, 256, 256, 3,  4);
  new Layer(&l, 256, 256, 3,  4);
  new Layer(&l, 256, 256, 3,  4);

  printf("  neurons: %d, links %d, memSize: %llu\n", l.totalNeurons(), l.totalLinks(), (unsigned long long)l.totalMemSize());

  if (outfile) {
    printf("try load previously saved network\n");
    l.loadAll(outfile);
  }

  printf("train\n");
  imgTrain(l, datafile, l.countNeurons(), outfile, 1000, 10000, 0.1, 4);

  return 0;
}