diff --git a/EasyCL b/EasyCL index b9023cdf..ef17a3e1 160000 --- a/EasyCL +++ b/EasyCL @@ -1 +1 @@ -Subproject commit b9023cdf25c10524ad06227275d3756bbe0c0ed2 +Subproject commit ef17a3e1b3d70b7ba89f94682500bc2d95ecb273 diff --git a/clMathLibraries/clBLAS b/clMathLibraries/clBLAS index 34f83bf5..6df2f999 160000 --- a/clMathLibraries/clBLAS +++ b/clMathLibraries/clBLAS @@ -1 +1 @@ -Subproject commit 34f83bf5a4a71bbcb23a68f449bcde5bdc08baa7 +Subproject commit 6df2f999a80789efee3376e8fffeb26d891b464c diff --git a/prototyping/clconvolve-fixedweights.cpp b/prototyping/clconvolve-fixedweights.cpp index c989378f..433ddf64 100644 --- a/prototyping/clconvolve-fixedweights.cpp +++ b/prototyping/clconvolve-fixedweights.cpp @@ -57,11 +57,11 @@ class Config { // [[[cog // cog.outl('// generated using cog:') // for astring in strings: - // cog.outl( 'string ' + astring + ';') + // cog.outl('string ' + astring + ';') // for anint in ints: - // cog.outl( 'int ' + anint + ';') + // cog.outl('int ' + anint + ';') // for name in floats: - // cog.outl( 'float ' + name + ';') + // cog.outl('float ' + name + ';') // ]]] // generated using cog: string dataDir; @@ -84,11 +84,11 @@ class Config { // [[[cog // cog.outl('// generated using cog:') // for astring in strings: - // cog.outl( astring + ' = "";') + // cog.outl(astring + ' = "";') // for anint in ints: - // cog.outl( anint + ' = 0;') + // cog.outl(anint + ' = 0;') // for name in floats: - // cog.outl( name + ' = 0.0f;') + // cog.outl(name + ' = 0.0f;') // ]]] // generated using cog: dataDir = ""; @@ -128,15 +128,15 @@ class Config { } }; -void sampleWeights( NeuralNet *net ) { - for( int layerId = 0; layerId < net->getNumLayers(); layerId++ ) { +void sampleWeights(NeuralNet *net) { + for(int layerId = 0; layerId < net->getNumLayers(); layerId++) { Layer *layer = net->getLayer(layerId); - FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >( layer ); - ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >( layer ); - if( fc != 0 ) { + FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >(layer); + ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >(layer); + if(fc != 0) { conv = fc->convolutionalLayer; } - if( conv == 0 ) { + if(conv == 0) { continue; } @@ -149,11 +149,11 @@ void sampleWeights( NeuralNet *net ) { int filterSize = dim.filterSize; initrand.seed(0); - for( int i = 0; i < 10; i++ ) { - int thisrand = abs( (int)initrand() ); - int seq = thisrand % ( numFilters * inputPlanes * filterSize * filterSize ); - int planefilter = seq / ( filterSize * filterSize ); - int rowcol = seq % ( filterSize * filterSize ); + for(int i = 0; i < 10; i++) { + int thisrand = abs((int)initrand()); + int seq = thisrand % (numFilters * inputPlanes * filterSize * filterSize); + int planefilter = seq / (filterSize * filterSize); + int rowcol = seq % (filterSize * filterSize); int filter = planefilter / inputPlanes; int inputPlane = planefilter % inputPlanes; int row = rowcol / filterSize; @@ -180,24 +180,24 @@ void go(Config config) { int testAllocateN = 0; // int totalLinearSize; - GenericLoader::getDimensions((config.dataDir + "/" + config.trainFile).c_str(), &Ntrain, &numPlanes, &imageSize ); + GenericLoader::getDimensions((config.dataDir + "/" + config.trainFile).c_str(), &Ntrain, &numPlanes, &imageSize); Ntrain = config.numTrain == -1 ? Ntrain : config.numTrain; // long allocateSize = (long)Ntrain * numPlanes * imageSize * imageSize; cout << "Ntrain " << Ntrain << " numPlanes " << numPlanes << " imageSize " << imageSize << endl; trainAllocateN = Ntrain; trainData = new float[ (long)trainAllocateN * numPlanes * imageSize * imageSize ]; trainLabels = new int[trainAllocateN]; - if( Ntrain > 0 ) { - GenericLoader::load((config.dataDir + "/" + config.trainFile).c_str(), trainData, trainLabels, 0, Ntrain ); + if(Ntrain > 0) { + GenericLoader::load((config.dataDir + "/" + config.trainFile).c_str(), trainData, trainLabels, 0, Ntrain); } - GenericLoader::getDimensions((config.dataDir + "/" + config.validateFile).c_str(), &Ntest, &numPlanes, &imageSize ); + GenericLoader::getDimensions((config.dataDir + "/" + config.validateFile).c_str(), &Ntest, &numPlanes, &imageSize); Ntest = config.numTest == -1 ? Ntest : config.numTest; testAllocateN = Ntest; testData = new float[ (long)testAllocateN * numPlanes * imageSize * imageSize ]; testLabels = new int[testAllocateN]; - if( Ntest > 0 ) { - GenericLoader::load((config.dataDir + "/" + config.validateFile).c_str(), testData, testLabels, 0, Ntest ); + if(Ntest > 0) { + GenericLoader::load((config.dataDir + "/" + config.validateFile).c_str(), testData, testLabels, 0, Ntest); } timer.timeCheck("after load images"); @@ -206,15 +206,15 @@ void go(Config config) { float translate; float scale; int normalizationExamples = config.normalizationExamples > Ntrain ? Ntrain : config.normalizationExamples; - if( config.normalization == "stddev" ) { + if(config.normalization == "stddev") { float mean, stdDev; - NormalizationHelper::getMeanAndStdDev( trainData, normalizationExamples * inputCubeSize, &mean, &stdDev ); + NormalizationHelper::getMeanAndStdDev(trainData, normalizationExamples * inputCubeSize, &mean, &stdDev); cout << " image stats mean " << mean << " stdDev " << stdDev << endl; translate = - mean; scale = 1.0f / stdDev / config.normalizationNumStds; - } else if( config.normalization == "maxmin" ) { + } else if(config.normalization == "maxmin") { float mean, stdDev; - NormalizationHelper::getMinMax( trainData, normalizationExamples * inputCubeSize, &mean, &stdDev ); + NormalizationHelper::getMinMax(trainData, normalizationExamples * inputCubeSize, &mean, &stdDev); translate = - mean; scale = 1.0f / stdDev; } else { @@ -228,48 +228,48 @@ void go(Config config) { // const int numToTrain = Ntrain; // const int batchSize = config.batchSize; EasyCL *cl = new EasyCL(); - NeuralNet *net = new NeuralNet( cl ); + NeuralNet *net = new NeuralNet(cl); // net->inputMaker()->numPlanes(numPlanes)->imageSize(imageSize)->insert(); - net->addLayer( InputLayerMaker::instance()->numPlanes(numPlanes)->imageSize(imageSize) ); - net->addLayer( NormalizationLayerMaker::instance()->translate(translate)->scale(scale) ); - if( !NetdefToNet::createNetFromNetdef( net, config.netDef ) ) { + net->addLayer(InputLayerMaker::instance()->numPlanes(numPlanes)->imageSize(imageSize)); + net->addLayer(NormalizationLayerMaker::instance()->translate(translate)->scale(scale)); + if(!NetdefToNet::createNetFromNetdef(net, config.netDef)) { return; } net->print(); - for( int i = 1; i < net->getNumLayers() - 1; i++ ) { + for(int i = 1; i < net->getNumLayers() - 1; i++) { Layer *layer = net->getLayer(i); FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >(layer); ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >(layer); - if( fc != 0 ) { + if(fc != 0) { conv = fc->convolutionalLayer; } - if( conv == 0 ) { + if(conv == 0) { continue; } initrand.seed(0); int weightsSize = conv->getWeightsSize(); //int weightsSize = layer->getPersistSize(); - if( weightsSize > 0 ) { + if(weightsSize > 0) { cout << "weightsSize " << weightsSize << endl; float *weights = new float[weightsSize]; - for( int j = 0; j < weightsSize; j++ ) { + for(int j = 0; j < weightsSize; j++) { int thisrand = (int)initrand(); - float thisweight = ( thisrand % 100000 ) / 1000000.0f; + float thisweight = (thisrand % 100000) / 1000000.0f; weights[j] = thisweight; } - conv->initWeights( weights ); + conv->initWeights(weights); } - if( conv->dim.biased ) { + if(conv->dim.biased) { initrand.seed(0); int biasedSize = conv->getBiasSize(); float *biasWeights = new float[biasedSize]; - for( int j = 0; j < biasedSize; j++ ) { + for(int j = 0; j < biasedSize; j++) { int thisrand = (int)initrand(); - float thisweight = ( thisrand % 100000 ) / 1000000.0f; + float thisweight = (thisrand % 100000) / 1000000.0f; biasWeights[j] = thisweight; //biasWeights[j] = 0; } - conv->initBias( biasWeights ); + conv->initBias(biasWeights); } } @@ -284,45 +284,45 @@ void go(Config config) { // float restartLoss = 0; timer.timeCheck("before learning start"); - if( config.dumpTimings ) { - StatefulTimer::dump( true ); + if(config.dumpTimings) { + StatefulTimer::dump(true); } StatefulTimer::timeCheck("START"); - SGD *sgd = SGD::instance( cl, config.learningRate, 0.0f ); + SGD *sgd = SGD::instance(cl, config.learningRate, 0.0f); Trainable *trainable = net; - NetLearner netLearner( + NetLearner netLearner( sgd, trainable, Ntrain, trainData, trainLabels, Ntest, testData, testLabels, - config.batchSize ); - netLearner.setSchedule( config.numEpochs, afterRestart ? restartEpoch : 1 ); -// netLearner.setBatchSize( config.batchSize ); - netLearner.setDumpTimings( config.dumpTimings ); -// netLearner.learn( config.learningRate, 1.0f ); + config.batchSize); + netLearner.setSchedule(config.numEpochs, afterRestart ? restartEpoch : 1); +// netLearner.setBatchSize(config.batchSize); + netLearner.setDumpTimings(config.dumpTimings); +// netLearner.learn(config.learningRate, 1.0f); cout << "forward output" << endl; - for( int layerId = 0; layerId < net->getNumLayers(); layerId++ ) { + for(int layerId = 0; layerId < net->getNumLayers(); layerId++) { Layer *layer = net->getLayer(layerId); - FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >( layer ); - ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >( layer ); - PoolingLayer *pool = dynamic_cast< PoolingLayer * >( layer ); - SoftMaxLayer *softMax = dynamic_cast< SoftMaxLayer * >( layer ); - if( fc != 0 ) { + FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >(layer); + ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >(layer); + PoolingLayer *pool = dynamic_cast< PoolingLayer * >(layer); + SoftMaxLayer *softMax = dynamic_cast< SoftMaxLayer * >(layer); + if(fc != 0) { conv = fc->convolutionalLayer; } int planes = 0; int imageSize = 0; - if( conv != 0 ) { + if(conv != 0) { cout << "convolutional (or conv based, ie fc)" << endl; planes = conv->dim.numFilters; imageSize = conv->dim.outputSize; // continue; - } else if( pool != 0 ) { + } else if(pool != 0) { cout << "pooling" << endl; planes = pool->numPlanes; imageSize = pool->outputSize; - } else if( softMax != 0 ) { + } else if(softMax != 0) { cout << "softmax" << endl; planes = softMax->numPlanes; imageSize = softMax->imageSize; @@ -332,16 +332,16 @@ void go(Config config) { cout << "layer " << layerId << endl; // conv->getOutput(); float const*output = layer->getOutput(); -// for( int i = 0; i < 3; i++ ) { +// for(int i = 0; i < 3; i++) { // cout << conv->getOutput()[i] << endl; // } initrand.seed(0); // LayerDimensions &dim = conv->dim; - for( int i = 0; i < 10; i++ ) { - int thisrand = abs( (int)initrand() ); - int seq = thisrand % ( planes * imageSize * imageSize ); - int outPlane = seq / ( imageSize * imageSize ); - int rowcol = seq % ( imageSize * imageSize ); + for(int i = 0; i < 10; i++) { + int thisrand = abs((int)initrand()); + int seq = thisrand % (planes * imageSize * imageSize); + int outPlane = seq / (imageSize * imageSize); + int rowcol = seq % (imageSize * imageSize); int row = rowcol / imageSize; int col = rowcol % imageSize; cout << "out[" << outPlane << "," << row << "," << col << "]=" << output[ seq ] << endl; @@ -352,14 +352,14 @@ void go(Config config) { sampleWeights(net); cout << "backprop output" << endl; - for( int layerId = net->getNumLayers() - 1; layerId >= 0; layerId-- ) { + for(int layerId = net->getNumLayers() - 1; layerId >= 0; layerId--) { Layer *layer = net->getLayer(layerId); - FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >( layer ); - ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >( layer ); - if( fc != 0 ) { + FullyConnectedLayer *fc = dynamic_cast< FullyConnectedLayer * >(layer); + ConvolutionalLayer *conv = dynamic_cast< ConvolutionalLayer * >(layer); + if(fc != 0) { conv = fc->convolutionalLayer; } - if( conv == 0 ) { + if(conv == 0) { continue; } @@ -367,10 +367,10 @@ void go(Config config) { float const*weights = conv->getWeights(); float const*biases = conv->getBias(); int weightsSize = conv->getWeightsSize() / conv->dim.numFilters; - for( int i = 0; i < weightsSize; i++ ) { + for(int i = 0; i < weightsSize; i++) { cout << " weight " << i << " " << weights[i] << endl; } - for( int i = 0; i < 3; i++ ) { + for(int i = 0; i < 3; i++) { cout << " bias " << i << " " << biases[i] << endl; } } @@ -380,32 +380,32 @@ void go(Config config) { delete net; delete cl; - if( trainData != 0 ) { + if(trainData != 0) { delete[] trainData; } - if( testData != 0 ) { + if(testData != 0) { delete[] testData; } - if( testLabels != 0 ) { + if(testLabels != 0) { delete[] testLabels; } - if( trainLabels != 0 ) { + if(trainLabels != 0) { delete[] trainLabels; } } -void printUsage( char *argv[], Config config ) { +void printUsage(char *argv[], Config config) { cout << "Usage: " << argv[0] << " [key]=[value] [[key]=[value]] ..." << endl; cout << endl; cout << "Possible key=value pairs:" << endl; // [[[cog // cog.outl('// generated using cog:') // for name in strings: - // cog.outl( 'cout << " ' + name.lower() + '=[' + descriptions[name.lower()] + '] (" << config.' + name + ' << ")" << endl;') + // cog.outl('cout << " ' + name.lower() + '=[' + descriptions[name.lower()] + '] (" << config.' + name + ' << ")" << endl;') // for name in ints: - // cog.outl( 'cout << " ' + name.lower() + '=[' + descriptions[name.lower()] + '] (" << config.' + name + ' << ")" << endl;') + // cog.outl('cout << " ' + name.lower() + '=[' + descriptions[name.lower()] + '] (" << config.' + name + ' << ")" << endl;') // for name in floats: - // cog.outl( 'cout << " ' + name.lower() + '=[' + descriptions[name.lower()] + '] (" << config.' + name + ' << ")" << endl;') + // cog.outl('cout << " ' + name.lower() + '=[' + descriptions[name.lower()] + '] (" << config.' + name + ' << ")" << endl;') // ]]] // generated using cog: cout << " datadir=[directory to search for train and validate files] (" << config.dataDir << ")" << endl; @@ -425,14 +425,14 @@ void printUsage( char *argv[], Config config ) { // [[[end]]] } -int main( int argc, char *argv[] ) { +int main(int argc, char *argv[]) { Config config; - if( argc == 2 && ( string(argv[1]) == "--help" || string(argv[1]) == "--?" || string(argv[1]) == "-?" || string(argv[1]) == "-h" ) ) { - printUsage( argv, config ); + if(argc == 2 && (string(argv[1]) == "--help" || string(argv[1]) == "--?" || string(argv[1]) == "-?" || string(argv[1]) == "-h")) { + printUsage(argv, config); } - for( int i = 1; i < argc; i++ ) { - vector splitkeyval = split( argv[i], "=" ); - if( splitkeyval.size() != 2 ) { + for(int i = 1; i < argc; i++) { + vector splitkeyval = split(argv[i], "="); + if(splitkeyval.size() != 2) { cout << "Usage: " << argv[0] << " [key]=[value] [[key]=[value]] ..." << endl; exit(1); } else { @@ -441,69 +441,69 @@ int main( int argc, char *argv[] ) { // cout << "key [" << key << "]" << endl; // [[[cog // cog.outl('// generated using cog:') - // cog.outl('if( false ) {') + // cog.outl('if(false) {') // for name in strings: - // cog.outl( '} else if( key == "' + name.lower() + '" ) {') - // cog.outl( ' config.' + name + ' = value;') + // cog.outl('} else if(key == "' + name.lower() + '") {') + // cog.outl(' config.' + name + ' = value;') // for name in ints: - // cog.outl( '} else if( key == "' + name.lower() + '" ) {') - // cog.outl( ' config.' + name + ' = atoi( value );') + // cog.outl('} else if(key == "' + name.lower() + '") {') + // cog.outl(' config.' + name + ' = atoi(value);') // for name in floats: - // cog.outl( '} else if( key == "' + name.lower() + '" ) {') - // cog.outl( ' config.' + name + ' = atof( value );') + // cog.outl('} else if(key == "' + name.lower() + '") {') + // cog.outl(' config.' + name + ' = atof(value);') // ]]] // generated using cog: - if( false ) { - } else if( key == "datadir" ) { + if(false) { + } else if(key == "datadir") { config.dataDir = value; - } else if( key == "trainfile" ) { + } else if(key == "trainfile") { config.trainFile = value; - } else if( key == "validatefile" ) { + } else if(key == "validatefile") { config.validateFile = value; - } else if( key == "netdef" ) { + } else if(key == "netdef") { config.netDef = value; - } else if( key == "normalization" ) { + } else if(key == "normalization") { config.normalization = value; - } else if( key == "dataset" ) { + } else if(key == "dataset") { config.dataset = value; - } else if( key == "numtrain" ) { - config.numTrain = atoi( value ); - } else if( key == "numtest" ) { - config.numTest = atoi( value ); - } else if( key == "batchsize" ) { - config.batchSize = atoi( value ); - } else if( key == "numepochs" ) { - config.numEpochs = atoi( value ); - } else if( key == "dumptimings" ) { - config.dumpTimings = atoi( value ); - } else if( key == "normalizationexamples" ) { - config.normalizationExamples = atoi( value ); - } else if( key == "learningrate" ) { - config.learningRate = atof( value ); - } else if( key == "normalizationnumstds" ) { - config.normalizationNumStds = atof( value ); + } else if(key == "numtrain") { + config.numTrain = atoi(value); + } else if(key == "numtest") { + config.numTest = atoi(value); + } else if(key == "batchsize") { + config.batchSize = atoi(value); + } else if(key == "numepochs") { + config.numEpochs = atoi(value); + } else if(key == "dumptimings") { + config.dumpTimings = atoi(value); + } else if(key == "normalizationexamples") { + config.normalizationExamples = atoi(value); + } else if(key == "learningrate") { + config.learningRate = atof(value); + } else if(key == "normalizationnumstds") { + config.normalizationNumStds = atof(value); // [[[end]]] } else { cout << endl; cout << "Error: key '" << key << "' not recognised" << endl; cout << endl; - printUsage( argv, config ); + printUsage(argv, config); cout << endl; return -1; } } } - string dataset = toLower( config.dataset ); - if( dataset != "" ) { - if( dataset == "mnist" ) { + string dataset = toLower(config.dataset); + if(dataset != "") { + if(dataset == "mnist") { config.dataDir = "../data/mnist"; config.trainFile = "train-dat.mat"; config.validateFile = "t10k-dat.mat"; - } else if( dataset == "norb" ) { + } else if(dataset == "norb") { config.dataDir = "../data/norb"; config.trainFile = "training-shuffled-dat.mat"; config.validateFile = "testing-sampled-dat.mat"; - } else if( dataset == "cifar10" ) { + } else if(dataset == "cifar10") { config.dataDir = "../data/cifar10"; config.trainFile = "train-dat.mat"; config.validateFile = "test-dat.mat"; @@ -517,8 +517,8 @@ int main( int argc, char *argv[] ) { cout << " validatefile: " << config.validateFile << ":" << endl; } try { - go( config ); - } catch( runtime_error e ) { + go(config); + } catch(runtime_error e) { cout << "Something went wrong: " << e.what() << endl; return -1; } diff --git a/prototyping/qlearning/ScenarioImage.cpp b/prototyping/qlearning/ScenarioImage.cpp index ba31291d..a30f9839 100644 --- a/prototyping/qlearning/ScenarioImage.cpp +++ b/prototyping/qlearning/ScenarioImage.cpp @@ -15,7 +15,7 @@ using namespace std; #undef VIRTUAL #define VIRTUAL -ScenarioImage::ScenarioImage( int size, bool appleMoves ) : +ScenarioImage::ScenarioImage(int size, bool appleMoves) : net(0), // this is simply used by the showQRepresentation method size(size), appleMoves(appleMoves) { @@ -31,7 +31,7 @@ ScenarioImage::ScenarioImage( int size, bool appleMoves ) : reset(); print(); } -void ScenarioImage::setNet( NeuralNet *net ) { +void ScenarioImage::setNet(NeuralNet *net) { this->net = net; } void ScenarioImage::printQRepresentation() { @@ -39,29 +39,29 @@ void ScenarioImage::printQRepresentation() { cout << "q directions:" << endl; int size = scenario->getPerceptionSize(); float *input = new float[ size * size * 2 ]; - arrayZero( input, size * size * 2 ); + arrayZero(input, size * size * 2); input[ scenario->appleY * size + scenario->appleX ] = 1; - for( int y = 0; y < size; y++ ) { + for(int y = 0; y < size; y++) { string thisLine = ""; - for( int x = 0; x < size; x++ ) { + for(int x = 0; x < size; x++) { float highestQ = 0; int bestAction = 0; input[ size * size + y * size + x ] = 1; - net->forward( input ); + net->forward(input); input[ size * size + y * size + x ] = 0; float const*output = net->getOutput(); - for( int action = 0; action < 4; action++ ) { + for(int action = 0; action < 4; action++) { float thisQ = output[action]; - if( action == 0 || thisQ > highestQ ) { + if(action == 0 || thisQ > highestQ) { highestQ = thisQ; bestAction = action; } } - if( bestAction == 0 ) { + if(bestAction == 0) { thisLine += ">"; - } else if( bestAction == 1 ) { + } else if(bestAction == 1) { thisLine += "<"; - } else if( bestAction == 2 ) { + } else if(bestAction == 2) { thisLine += "V"; } else { thisLine += "^"; @@ -73,12 +73,12 @@ void ScenarioImage::printQRepresentation() { } VIRTUAL void ScenarioImage::print() { - for( int y = 0; y < size; y++ ) { + for(int y = 0; y < size; y++) { string line = ""; - for( int x = 0; x < size; x++ ) { - if( x == posX && y == posY ) { + for(int x = 0; x < size; x++) { + if(x == posX && y == posY) { line += "X"; - } else if( x == appleX && y == appleY ) { + } else if(x == appleX && y == appleY) { line += "O"; } else { line += "."; @@ -92,11 +92,11 @@ VIRTUAL ScenarioImage::~ScenarioImage() { VIRTUAL int ScenarioImage::getNumActions() { return 4; } -VIRTUAL float ScenarioImage::act( int index ) { // returns reward +VIRTUAL float ScenarioImage::act(int index) { // returns reward numMoves++; int dx = 0; int dy = 0; - switch( index ) { + switch(index) { case 0: dx = 1; break; @@ -112,10 +112,10 @@ VIRTUAL float ScenarioImage::act( int index ) { // returns reward } int newX = posX + dx; int newY = posY + dy; - if( newX < 0 || newX >= size || newY < 0 || newY >= size ) { + if(newX < 0 || newX >= size || newY < 0 || newY >= size) { return -0.5f; } - if( newX == appleX && newY == appleY ) { + if(newX == appleX && newY == appleY) { finished = true; posX = newX; posY = newY; @@ -135,8 +135,8 @@ VIRTUAL int ScenarioImage::getPerceptionSize() { VIRTUAL int ScenarioImage::getPerceptionPlanes() { return 2; } -VIRTUAL void ScenarioImage::getPerception( float *perception ) { - for( int i = 0; i < size * size * 2; i++ ) { +VIRTUAL void ScenarioImage::getPerception(float *perception) { + for(int i = 0; i < size * size * 2; i++) { perception[i] = 0; } perception[appleY * size + appleX] = 1; @@ -146,12 +146,12 @@ VIRTUAL int ScenarioImage::getWorldSize() { return size; } VIRTUAL void ScenarioImage::reset() { - if( net != 0 ) { + if(net != 0) { this->print(); this->printQRepresentation(); cout << "game: " << game << " moves: " << numMoves << endl; } - if( appleMoves ) { + if(appleMoves) { appleX = myrand() % size; appleY = myrand() % size; } else { @@ -159,7 +159,7 @@ VIRTUAL void ScenarioImage::reset() { } finished = false; bool sampledOnce = false; - while( !sampledOnce || ( posX == appleX && posY == appleY ) ) { + while(!sampledOnce || (posX == appleX && posY == appleY)) { posX = myrand() % size; posY = myrand() % size; sampledOnce = true; diff --git a/test/testGenericLoader.cpp b/test/testGenericLoader.cpp index d6f0e28d..204aeda5 100644 --- a/test/testGenericLoader.cpp +++ b/test/testGenericLoader.cpp @@ -14,42 +14,42 @@ using namespace std; -void go( string trainFilepath, int startN, int numExamples ) { +void go(string trainFilepath, int startN, int numExamples) { int N; int numPlanes; int imageSize; // int totalSize; - GenericLoader::getDimensions(trainFilepath.c_str(), &N, &numPlanes, &imageSize ); + GenericLoader::getDimensions(trainFilepath.c_str(), &N, &numPlanes, &imageSize); cout << "N " << N << " numplanes " << numPlanes << " imageSize " << imageSize << endl; float *images = new float[ numExamples * numPlanes * imageSize * imageSize ]; int *labels = new int[ numExamples ]; - GenericLoader::load(trainFilepath.c_str(), images, labels, startN, numExamples ); + GenericLoader::load(trainFilepath.c_str(), images, labels, startN, numExamples); // float *images = new float[ N * numPlanes * imageSize * imageSize ]; -// for( int i = 0; i < N * numPlanes * imageSize * imageSize; i++ ) { +// for(int i = 0; i < N * numPlanes * imageSize * imageSize; i++) { // images[i] = imagesUchar[i]; // } float thismin; float thismax; - NormalizationHelper::getMinMax( images, numExamples * numPlanes * imageSize * imageSize, &thismin, &thismax ); + NormalizationHelper::getMinMax(images, numExamples * numPlanes * imageSize * imageSize, &thismin, &thismax); cout << "min: " << thismin << " max: " << thismax << endl; - ImagePng::writeImagesToPng( "testGenericLoader.png", images, numExamples * numPlanes, imageSize ); - for( int i = 0; i < numExamples; i++ ) { + ImagePng::writeImagesToPng("testGenericLoader.png", images, numExamples * numPlanes, imageSize); + for(int i = 0; i < numExamples; i++) { cout << "labels[" << i << "]=" << labels[i] << endl; } // float *translated = new float[N * numPlanes * imageSize * imageSize]; -// Translator::translate( n, numPlanes, imageSize, translateRows, translateCols, images, translated ); -// ImagePng::writeImagesToPng( "testTranslator-2.png", translated + n * numPlanes * imageSize * imageSize, numPlanes, imageSize ); +// Translator::translate(n, numPlanes, imageSize, translateRows, translateCols, images, translated); +// ImagePng::writeImagesToPng("testTranslator-2.png", translated + n * numPlanes * imageSize * imageSize, numPlanes, imageSize); } -int main( int argc, char *argv[] ) { - if( argc != 4 ) { +int main(int argc, char *argv[]) { + if(argc != 4) { cout << "Usage: [trainfilepath] [startn] [numexamples]" << endl; return -1; } - string trainFilepath = string( argv[1] ); - int startN = atoi( argv[2] ); - int numExamples = atoi( argv[3] ); - go( trainFilepath, startN, numExamples ); + string trainFilepath = string(argv[1]); + int startN = atoi(argv[2]); + int numExamples = atoi(argv[3]); + go(trainFilepath, startN, numExamples); return 0; } diff --git a/test/testPatchExtractor.cpp b/test/testPatchExtractor.cpp index 75307ebb..e75a8c07 100644 --- a/test/testPatchExtractor.cpp +++ b/test/testPatchExtractor.cpp @@ -7,35 +7,35 @@ using namespace std; -void go( string dataDir, string setName, int n, int patchSize, int patchRow, int patchCol ) { +void go(string dataDir, string setName, int n, int patchSize, int patchRow, int patchCol) { int N; int numPlanes; int imageSize; - unsigned char *imagesUchar = NorbLoader::loadImages( dataDir + "/" + setName + "-dat.mat", &N, &numPlanes, &imageSize, n + 1 ); + unsigned char *imagesUchar = NorbLoader::loadImages(dataDir + "/" + setName + "-dat.mat", &N, &numPlanes, &imageSize, n + 1); cout << "n " << n << " N " << N << endl; N = n + 1; float *images = new float[ N * numPlanes * imageSize * imageSize ]; - for( int i = 0; i < N * numPlanes * imageSize * imageSize; i++ ) { + for(int i = 0; i < N * numPlanes * imageSize * imageSize; i++) { images[i] = imagesUchar[i]; } - ImagePng::writeImagesToPng( "testPatchExtractor-1.png", images + n * numPlanes * imageSize * imageSize, numPlanes, imageSize ); + ImagePng::writeImagesToPng("testPatchExtractor-1.png", images + n * numPlanes * imageSize * imageSize, numPlanes, imageSize); float *patches = new float[N * numPlanes * patchSize * patchSize]; - PatchExtractor::extractPatch( n, numPlanes, imageSize, patchSize, patchRow, patchCol, images, patches ); - ImagePng::writeImagesToPng( "testPatchExtractor-2.png", patches + n * numPlanes * patchSize * patchSize, numPlanes, patchSize ); + PatchExtractor::extractPatch(n, numPlanes, imageSize, patchSize, patchRow, patchCol, images, patches); + ImagePng::writeImagesToPng("testPatchExtractor-2.png", patches + n * numPlanes * patchSize * patchSize, numPlanes, patchSize); } -int main( int argc, char *argv[] ) { - if( argc != 7 ) { +int main(int argc, char *argv[]) { + if(argc != 7) { cout << "Usage: [datadir] [setname] [n] [patchsize] [patchrow] [patchcol]" << endl; return -1; } - string dataDir = string( argv[1] ); - string setName = string( argv[2] ); - int n = atoi( argv[3] ); - int patchSize = atoi( argv[4] ); - int patchRow = atoi( argv[5] ); - int patchCol = atoi( argv[6] ); - go( dataDir, setName, n, patchSize, patchRow, patchCol ); + string dataDir = string(argv[1]); + string setName = string(argv[2]); + int n = atoi(argv[3]); + int patchSize = atoi(argv[4]); + int patchRow = atoi(argv[5]); + int patchCol = atoi(argv[6]); + go(dataDir, setName, n, patchSize, patchRow, patchCol); return 0; } diff --git a/test/testTranslator.cpp b/test/testTranslator.cpp index 15cd48b9..909cbaf2 100644 --- a/test/testTranslator.cpp +++ b/test/testTranslator.cpp @@ -7,38 +7,38 @@ using namespace std; -void go( string dataDir, string setName, int n, int translateRows, int translateCols ) { +void go(string dataDir, string setName, int n, int translateRows, int translateCols) { int N; int numPlanes; int imageSize; string filePath = dataDir + "/" + setName + "-dat.mat"; - NorbLoader::getDimensions( filePath, &N, &numPlanes, &imageSize ); + NorbLoader::getDimensions(filePath, &N, &numPlanes, &imageSize); N = n + 1; unsigned char *imagesUchar = new unsigned char[ N * numPlanes * imageSize * imageSize ]; int *labels = new int[ N ]; - NorbLoader::load( filePath, imagesUchar, labels, 0, N ); + NorbLoader::load(filePath, imagesUchar, labels, 0, N); cout << "n " << n << " N " << N << endl; float *images = new float[ N * numPlanes * imageSize * imageSize ]; - for( int i = 0; i < N * numPlanes * imageSize * imageSize; i++ ) { + for(int i = 0; i < N * numPlanes * imageSize * imageSize; i++) { images[i] = imagesUchar[i]; } - ImagePng::writeImagesToPng( "testTranslator-1.png", images + n * numPlanes * imageSize * imageSize, numPlanes, imageSize ); + ImagePng::writeImagesToPng("testTranslator-1.png", images + n * numPlanes * imageSize * imageSize, numPlanes, imageSize); float *translated = new float[N * numPlanes * imageSize * imageSize]; - Translator::translate( n, numPlanes, imageSize, translateRows, translateCols, images, translated ); - ImagePng::writeImagesToPng( "testTranslator-2.png", translated + n * numPlanes * imageSize * imageSize, numPlanes, imageSize ); + Translator::translate(n, numPlanes, imageSize, translateRows, translateCols, images, translated); + ImagePng::writeImagesToPng("testTranslator-2.png", translated + n * numPlanes * imageSize * imageSize, numPlanes, imageSize); } -int main( int argc, char *argv[] ) { - if( argc != 6 ) { +int main(int argc, char *argv[]) { + if(argc != 6) { cout << "Usage: [datadir] [setname] [n] [translaterows] [translatecols]" << endl; return -1; } - string dataDir = string( argv[1] ); - string setName = string( argv[2] ); - int n = atoi( argv[3] ); - int translateRows = atoi( argv[4] ); - int translateCols = atoi( argv[5] ); - go( dataDir, setName, n, translateRows, translateCols ); + string dataDir = string(argv[1]); + string setName = string(argv[2]); + int n = atoi(argv[3]); + int translateRows = atoi(argv[4]); + int translateCols = atoi(argv[5]); + go(dataDir, setName, n, translateRows, translateCols); return 0; }