void testVuFunctions() { MatrixData testMat(10,10,CV_32FC1); MatrixData testMat2(10,10,CV_32FC1); MatrixData buffer(10,10,CV_32FC1); MatrixSize windowSize; windowSize.height = 5; windowSize.width = 5; testMat.setTo(Scalar(1)); testMat.at<float>(5,5) = 0; namedWindow("Before"); imshow("Before",testMat); waitKey(0); vuErodeFloat(&testMat,windowSize, &buffer, &testMat2); namedWindow("After"); namedWindow("Results"); namedWindow("Buffer"); imshow("Before",testMat); imshow("After",testMat); imshow("Results",testMat2); imshow("Buffer",buffer); cout <<"TestMat:\n" << endl; printFloatMatrixToConsole(&testMat); cout << "\n\n" << endl; cout <<"TestMat2\n:" << endl; printFloatMatrixToConsole(&testMat2); cout << "\n\n" << endl; cout << "Matrix Results\n" << testMat2 << endl; waitKey(0); vuDilateFloat(&testMat2,windowSize, &buffer, &testMat); imshow("Before",testMat2); imshow("After",testMat2); imshow("Results",testMat); imshow("Buffer",buffer); cout <<"TestMat:\n" << endl; printFloatMatrixToConsole(&testMat2); cout << "\n\n" << endl; cout <<"TestMat2\n:" << endl; printFloatMatrixToConsole(&testMat); cout << "\n\n" << endl; cout << "Matrix Results\n" << testMat << endl; waitKey(0); testMat.create(10,10,CV_16SC1); testMat2.create(10,10,CV_16SC1); buffer.create(10,10,CV_16SC1); vuErodeInt(&testMat,windowSize, &buffer, &testMat2); MatrixData queryDataIn(5,3,CV_32FC1); MatrixData trainDataIn(10,3,CV_32FC1); MatrixData rows; vector<FeatureMatch> matchResults; int numberOfMatches = 15; matchResults.resize(queryDataIn.rows * numberOfMatches); for(int i = 0; i< trainDataIn.rows;i++) { rows= trainDataIn.row(i); rows.setTo(Scalar(i)); } for(int i = 0; i< queryDataIn.rows;i++) { rows= queryDataIn.row(i); rows.setTo(Scalar(i)); } rows = trainDataIn.row(9); rows.setTo(Scalar(1)); vuTopNMatchesFloat(&queryDataIn, &trainDataIn, numberOfMatches,&matchResults[0]); for(int i = 0; i < queryDataIn.rows; i ++) { cout << "Query Feature vector " << i << ":"<<endl; vuPrintFeatureVectorToConsole(&queryDataIn, i); for(int j = 0; j< numberOfMatches; j++) { vuPrintFeatureMatchToConsole(&matchResults[i*numberOfMatches + j], &queryDataIn, &trainDataIn); } } vector< vector<DMatch > > testMatches; converFeatureMatchToDMatch(&matchResults[0], queryDataIn.rows, numberOfMatches,testMatches); for(int i = 0; i < (int)testMatches.size(); i ++) { cout << "DMatch Query Feature vector " << i << ":"<<endl; for(int j = 0; j< (int)testMatches[i].size(); j++) { printf("\t Dist: %g, queryId %d -> train Id %d\n",testMatches[i][j].distance,testMatches[i][j].queryIdx,testMatches[i][j].trainIdx); } } waitKey(0); }
int main(int argc, char *argv[]) { /* Overall structure: * - Load data into memory, from CSV files, using Boost::tokenizer * - Set up neural network scaffolds for genetic evolution * - Evolve network on training data, until criteria met or user input * - Output resulting network structure and weights * - Test on both training data and test data, and output */ /*** *** A0 Process command-line options ***/ /* Process command-line options * Using boost::program_options */ // Defaults uint16_t m_threads = 2; string m_traindata = ""; string m_testdata = ""; string m_genomeFile = ""; bool m_testGenome = false; int m_seed = 0; uint32_t m_genmax = 1; uint16_t m_maxPop = 150; string m_speciationAlgo = "preserveSpecies"; bool m_seedGenome = false; bool m_printPopulation = false; string m_printPopulationFile = ""; string m_printSpeciesStackFile = ""; string m_printSpeciesSizeFile = ""; string m_printFitnessFile = ""; bool m_printSuperChampions = false; float m_weightPerturbStdev = FLT_MAX; uint32_t m_killStagnated = 0; uint32_t m_refocusStagnated = 0; uint16_t m_targetSpecies = 0; // Non-CLI-configurable options float m_survival_threshold = 0.20; float m_compat_threshold = 3.00; // List of command line options boost::program_options::options_description cliOptDesc("Options"); cliOptDesc.add_options() ("threads", boost::program_options::value<uint16_t>(), "number of threads to run concurrently") ("train-data", boost::program_options::value<string>(), "location of training data CSV") ("test-data", boost::program_options::value<string>(), "location of test data CSV") ("genome-file", boost::program_options::value<string>(), "load a genome from a CSV file") ("seed-genome", "uses the loaded genome as the first seed") ("test-genome", "runs the loaded genome on the databases") ("seed", boost::program_options::value<int>(), "random number generator seed") ("generations", boost::program_options::value<uint32_t>(), "number of generations to perform") ("population-size", boost::program_options::value<uint16_t>(), "maximum population size") ("compat-excess", boost::program_options::value<float>(), "compatibility weight c1") ("compat-disjoint", boost::program_options::value<float>(), "compatibility weight c2") ("compat-weight", boost::program_options::value<float>(), "compatibility weight c3") ("perturb-stdev", boost::program_options::value<float>(), "standard deviation of gaussian perturb weights") ("speciation-algo", boost::program_options::value<string>(), "sets the algorithm used for speciation") ("limit-growth", "limits initial growth to 2*size(species)") ("kill-stagnated", boost::program_options::value<uint32_t>(), "removes species that stagnate after N generations") ("refocus-stagnated", boost::program_options::value<uint32_t>(), "refocuses species that stagnate after N generations") ("target-species", boost::program_options::value<uint16_t>(), "targets N species with a self-adjusting threshold") ("print-population", "print population statistics") ("print-population-file", boost::program_options::value<string>(), "print population statistics to a file") ("print-speciesstack-file", boost::program_options::value<string>(), "print graph of species size to a file") ("print-speciessize-file", boost::program_options::value<string>(), "print CSV-formatted sizes of species per generation") ("print-fitness-file", boost::program_options::value<string>(), "print best fitness to a file") ("print-super-champions", "print every super champion to a file") ("debug", boost::program_options::value<uint16_t>(), "enable debug mode") ("help", "give this help list") ; // Parse options boost::program_options::variables_map varMap; store(parse_command_line(argc, argv, cliOptDesc), varMap); notify(varMap); if(argc==1) { cout << cliOptDesc; return 1; } // Process options if (varMap.count("debug")) gm_debug = varMap["debug"].as<uint16_t>(); if (varMap.count("threads")) m_threads = varMap["threads"].as<uint16_t>(); if (varMap.count("train-data")) m_traindata = varMap["train-data"].as<string>(); if (varMap.count("test-data")) m_testdata = varMap["test-data"].as<string>(); if (varMap.count("genome-file")) m_genomeFile = varMap["genome-file"].as<string>(); if (varMap.count("test-genome")) m_testGenome = true; if (varMap.count("seed")) m_seed = varMap["seed"].as<int>(); if (varMap.count("generations")) m_genmax = varMap["generations"].as<uint32_t>(); if (varMap.count("population-size")) m_maxPop = varMap["population-size"].as<uint16_t>(); if (varMap.count("compat-excess")) gm_compat_excess = varMap["compat-excess"].as<float>(); if (varMap.count("compat-disjoint")) gm_compat_disjoint = varMap["compat-disjoint"].as<float>(); if (varMap.count("compat-weight")) gm_compat_weight = varMap["compat-weight"].as<float>(); if (varMap.count("speciation-algo")) m_speciationAlgo = varMap["speciation-algo"].as<string>(); if (varMap.count("perturb-stdev")) m_weightPerturbStdev = varMap["perturb-stdev"].as<float>(); if (varMap.count("limit-growth")) gm_limitInitialGrowth = true; if (varMap.count("kill-stagnated")) m_killStagnated = varMap["kill-stagnated"].as<uint32_t>(); if (varMap.count("refocus-stagnated")) m_refocusStagnated = varMap["refocus-stagnated"].as<uint32_t>(); if (varMap.count("target-species")) m_targetSpecies = varMap["target-species"].as<uint16_t>(); if (varMap.count("seed-genome")) m_seedGenome = true; if (varMap.count("print-population")) m_printPopulation = true; if (varMap.count("print-population-file")) m_printPopulationFile = varMap["print-population-file"].as<string>(); if (varMap.count("print-speciesstack-file")) m_printSpeciesStackFile = varMap["print-speciesstack-file"].as<string>(); if (varMap.count("print-speciessize-file")) m_printSpeciesSizeFile = varMap["print-speciessize-file"].as<string>(); if (varMap.count("print-fitness-file")) m_printFitnessFile = varMap["print-fitness-file"].as<string>(); if (varMap.count("print-super-champions")) m_printSuperChampions = true; if (varMap.count("help")) { cout << cliOptDesc; return 1; } if(m_refocusStagnated>m_killStagnated) {cout << "ERROR --refocus-stagnated must be inferior to --kill-stagnated."; exit(1); } if(m_seedGenome and m_genomeFile.empty()) {cout << "ERROR --seed-genome requires --genome-file."; exit(1); } if( (m_threads<1) or (m_threads>32) ) {cout << "ERROR --threads must be between 1 and 32."; exit(1); } if(m_speciationAlgo == "bestCompat") cout << "INFO\tSelected 'bestCompat' speciation algorithm.\n"; else if(m_speciationAlgo == "firstCompat") cout << "INFO\tSelected 'firstCompat' speciation algorithm.\n"; else if(m_speciationAlgo == "preserveSpecies") cout << "INFO\tSelected 'preserveSpecies' speciation algorithm.\n"; /*** *** A1 Load training data ***/ // Database to store training data vector<DataEntry> TrainingDB; if(m_traindata.empty()) { cout << "ERROR\tPlease specify a file with training data." << endl; exit(1); } else { cout << "INFO\tLoading training data on " << m_traindata << " into memory... " << flush; ifstream trainDataIn( m_traindata.c_str() ); if (!trainDataIn.is_open()) { cout << "\nERROR\tFailed to open file." << endl; exit(1); } // Setup boost::tokenizer vector<string> fields; string line; while (getline(trainDataIn,line)) { // Tokenize the line into 'fields' boost::tokenizer< boost::escaped_list_separator<char> > tok(line); fields.assign(tok.begin(),tok.end()); // Create an entry from the tokenized string vector // DataEntry constructor automatically converts strings to the correct types DataEntry entry(fields); TrainingDB.push_back(entry); } trainDataIn.close(); cout << "done." << endl; cout << "INFO\tLoaded " << TrainingDB.size() << " training entries into memory." << endl; // Sort TrainingData by NodeID, then Time. sort(TrainingDB.begin(), TrainingDB.end(), sortIdThenTime); } /*** *** A2 Load test data ***/ // Database to store training data vector<DataEntry> TestDB; // Check if a test data file was specified in the options. if(m_testdata.empty()) { cout << "INFO\tNo test data provided, evaluation to be performed on training data only." << endl; } else { // Load data cout << "INFO\tLoading test data on " << m_testdata << " into memory... " << flush; ifstream testDataIn( m_testdata.c_str() ); if (!testDataIn.is_open()) { cout << "\nERROR\tFailed to open file." << endl; exit(1); } // Setup boost::tokenizer vector<string> fields; string line; while (getline(testDataIn,line)) { // Tokenize the line into 'fields' boost::tokenizer< boost::escaped_list_separator<char> > tok(line); fields.assign(tok.begin(),tok.end()); // Create an entry from the tokenized string vector // DataEntry constructor automatically converts strings to the correct types DataEntry entry(fields); TestDB.push_back(entry); } testDataIn.close(); cout << "done." << endl; cout << "INFO\tLoaded " << TestDB.size() << " testing entries into memory." << endl; // Sort TestDB by NodeID, then Time. sort(TestDB.begin(), TestDB.end(), sortIdThenTime); } /*** *** A3a Load genome from file ***/ Genome genomeFile; if(!m_genomeFile.empty()) { // Load data cout << "INFO\tLoading genome from " << m_genomeFile << " into memory... " << flush; ifstream genomeIn( m_genomeFile.c_str() ); if (!genomeIn.is_open()) { cout << "\nERROR\tFailed to open file." << endl; exit(1); } // Setup boost::tokenizer vector<string> fields; string line; while (getline(genomeIn,line)) { // Tokenize the line into 'fields' boost::tokenizer< boost::escaped_list_separator<char> > tok(line); fields.assign(tok.begin(),tok.end()); if(fields[0] == "id") { stringstream ss; ss << hex << fields[1]; ss >> genomeFile.id; } else if(fields[0] == "node")