void GenSOM::train(const multi_img &input, ProgressObserver *po) { std::cout << "Start feeding" << std::endl; // matrices hold shuffled sequences of the input for number of iterations int maxIter = config.maxIter; cv::Mat_<int> shuffledY(1, maxIter); cv::Mat_<int> shuffledX(1, maxIter); // generate random sequence of the input x,y range cv::RNG rng(config.seed); rng.fill(shuffledY, cv::RNG::UNIFORM, cv::Scalar(0), cv::Scalar(input.height)); rng.fill(shuffledX, cv::RNG::UNIFORM, cv::Scalar(0), cv::Scalar(input.width)); // output percentage unsigned int hundred = std::max<unsigned int>(maxIter/100, 100); int percent = 1; if (config.verbosity > 0) { std::cout << " 0 %"; std::cout.flush(); } long sumOfUpdates = 0; // starting training (notifier needed by OpenCL impl.) notifyTrainingStart(); cv::MatConstIterator_<int> itY = shuffledY.begin(); cv::MatConstIterator_<int> itX = shuffledX.begin(); for (int curIter = 0; curIter < maxIter; ++curIter, ++itX, ++itY) { // feed one sample const multi_img::Pixel &vec = input(*itY, *itX); sumOfUpdates += trainSingle(vec, curIter, maxIter); // print progress (and maybe exit) if ((config.verbosity > 0 || po) && (config.maxIter > 100) && ((curIter % hundred) == 0) && (percent < 100)) { // send progress updates to observer or stdout if (po) { bool cont = po->update(curIter / (float)config.maxIter); if (!cont) { std::cerr << "Aborting training" << std::endl; return; } } else { std::cout << "\r " << (percent < 10 ? " " : "") << percent << " %"; std::cout.flush(); } if (config.verbosity >= 2) { std::cout << " Feed #" << curIter << ", avg. updates per iteration in the last " << (maxIter / hundred) << " iterations: " << ((double)sumOfUpdates / hundred) << std::endl; sumOfUpdates = 0; } // print som each 20% if (config.verbosity >= 3 && (percent % 20) == 0) { // TODO /*multi_img somimg = som->export_2d(); std::ostringstream filename, filename2; filename << "debug_som_"; filename << std::setfill('0') << std::setw(2); filename << (ctr / (config.maxIter / 20)); somimg.write_out(filename.str()); filename2 << filename.str(); filename << "-rgb.png"; cv::imwrite(filename.str(), somimg.bgr() * 255.f); if (!updateMap.empty()) { cv::Mat3f upBGR(updateMap.rows, updateMap.cols); cv::cvtColor(updateMap, upBGR, CV_GRAY2BGR); for (cv::Mat3f::iterator it = upBGR.begin(); it != upBGR.end();++it) if (*it == cv::Vec3f(0.f)) *it = cv::Vec3f(0.f, 0.f, 1.f); filename2 << "-updates.png"; cv::imwrite(filename2.str(), upBGR * 255.f); }*/ } percent++; } } if (config.verbosity > 0) std::cout << "\r100 %" <<std::endl; // finished training (notifier needed by OpenCL impl.) notifyTrainingEnd(); std::cout <<"# Feeding done" <<std::endl; if (!config.somFile.empty()) { std::cout << "# writing SOM in binary format to \"" << config.somFile << "\"" << std::endl; saveFile(config.somFile); } if (config.verbosity > 2) { cv::imwrite("debug_som.png", bgr(input.meta, input.maxval)*255.f); } }
void ViTrainer::trainSingle(ViNeuralNetwork *network) { setNetwork(network); trainSingle(); }