void ClassMeasurer::measureIteration() { internal_error /= n_examples; if(binary_mode) file->write(&internal_error, sizeof(real), 1); else file->printf("%g\n", internal_error); file->flush(); if(calc_confusion_at_each_iter) printConfusionMatrix(); reset(); }
int annTrain::train(std::string imagesDir, int networkInputSize, float testRatio) { std::cout << "Reading training set..." << std::endl; uint64 start = ofGetElapsedTimeMillis(); std::vector<std::string> files = getFilesInDirectory(imagesDir); std::random_shuffle(files.begin(), files.end()); cv::Mat img; for (auto it = files.begin(); it != files.end(); ++it) { std::string filename = *it; //std::cout << "Reading image " << filename << "..." << std::endl; img = cv::imread(filename, 0); if (img.empty()) { std::cerr << "WARNING: Could not read image." << std::endl; continue; } std::string classname = getClassName(filename); cv::Mat descriptors = getDescriptors(img); processClassAndDesc(classname, descriptors); } std::cout << " Seconds : " << (ofGetElapsedTimeMillis() - start) / 1000.0 << std::endl; std::cout << "Creating vocabulary..." << std::endl; start = ofGetElapsedTimeMillis(); cv::Mat labels; cv::Mat vocabulary; // Use k-means to find k centroids (the words of our vocabulary) cv::kmeans(descriptorsSet, networkInputSize, labels, cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 10, 0.01), 1, cv::KMEANS_PP_CENTERS, vocabulary); // No need to keep it on memory anymore descriptorsSet.release(); std::cout << " Seconds : " << (ofGetElapsedTimeMillis() - start) / 1000.0 << std::endl; // Convert a set of local features for each image in a single descriptors // using the bag of words technique std::cout << "Getting histograms of visual words..." << std::endl; int* ptrLabels = (int*)(labels.data); int size = labels.rows * labels.cols; for (int i = 0; i < size; i++) { int label = *ptrLabels++; ImageData* data = descriptorsMetadata[i]; data->bowFeatures.at<float>(label)++; } // Filling matrixes to be used by the neural network std::cout << "Preparing neural network..." << std::endl; std::set<ImageData*> uniqueMetadata(descriptorsMetadata.begin(), descriptorsMetadata.end()); for (auto it = uniqueMetadata.begin(); it != uniqueMetadata.end(); ) { ImageData* data = *it; cv::Mat normalizedHist; cv::normalize(data->bowFeatures, normalizedHist, 0, data->bowFeatures.rows, cv::NORM_MINMAX, -1, cv::Mat()); trainSamples.push_back(normalizedHist); trainResponses.push_back(getClassCode(classes, data->classname)); delete *it; // clear memory it++; } descriptorsMetadata.clear(); // Training neural network std::cout << "Training neural network..." << std::endl; start = ofGetElapsedTimeMillis(); mlp = getTrainedNeuralNetwork(trainSamples, trainResponses); std::cout << " Seconds : " << (ofGetElapsedTimeMillis() - start) / 1000.0 << std::endl; // We can clear memory now trainSamples.release(); trainResponses.release(); // Train FLANN std::cout << "Training FLANN..." << std::endl; start = ofGetElapsedTimeMillis(); flann = cv::Ptr<cv::FlannBasedMatcher>(new cv::FlannBasedMatcher()); flann->add(vocabulary); flann->train(); std::cout << " Seconds : " << (ofGetElapsedTimeMillis() - start) / 1000.0 << std::endl; // Reading test set std::cout << "Reading test set..." << std::endl; start = ofGetElapsedTimeMillis(); readImagesToTest(files.begin() + (size_t)(files.size() * testRatio), files.end()); std::cout << " Seconds : " << (ofGetElapsedTimeMillis() - start) / 1000.0 << std::endl; // Get confusion matrix of the test set std::vector<std::vector<int> > confusionMatrix = getConfusionMatrix(); // how accurate is our model std::cout << "Confusion matrix " << std::endl; printConfusionMatrix(confusionMatrix, classes); std::cout << "Accuracy " << getAccuracy(confusionMatrix) << std::endl; // now save everything std::cout << "saving models" << std::endl; saveModels(vocabulary, classes); return 0; }