void CRForest::growATree(const int treeNum) { // positive, negative dataset std::vector<CPosDataset> posSet(0); std::vector<CNegDataset> negSet(0); // positive, negative patch std::vector<CPosPatch> posPatch(0); std::vector<CNegPatch> negPatch(0); char buffer[256]; std::cout << "tree number " << treeNum << std::endl; // initialize random seed boost::mt19937 gen( treeNum * static_cast<unsigned long>(time(NULL)) ); //boost::timer t; //loadTrainPosFile(conf, posSet);//, gen); loadTrainObjFile(conf,posSet); CClassDatabase tempClassDatabase; // extract pos features and register classDatabase for(int i = 0; i < posSet.size(); ++i) { //std::cout << i << std::endl; //std::cout << posSet.at(i).rgb << std::endl; // if(posSet.at(i).loadImage(conf) == -1 && conf.learningMode != 2){ // exit(-1); // } //posSet.at(i).extractFeatures(conf); //std::cout << posSet.size() << std::endl; tempClassDatabase.add(posSet.at(i).getParam()->getClassName(),cv::Size(),0); } // std::vector<CPosDataset> tempPosSet(0); // int currentClass = treeNum % tempClassDatabase.vNode.size(); //std::cout << "okashiina" << std::endl; // for(int i = 0; i < posSet.size(); ++i){ // if(tempClassDatabase.search(posSet.at(i).getClassName()) == currentClass){ // tempPosSet.push_back(posSet.at(i)); // //std::cout << "teketeke" << std::endl; // }else{ // negSet.push_back(convertPosToNeg2(posSet.at(i))); // //std::cout << "negneg" << std::endl; // } // } //posSet = tempPosSet; loadTrainNegFile(conf, negSet); std::cout << "dataset loaded" << std::endl; // initialize class database //classDatabase.clear(); std::cout << "generating appearance from 3D model!" << std::endl; // extract pos features and register classDatabase for(int i = 0; i < posSet.size(); ++i) { //std::cout << i << std::endl; //std::cout << posSet.at(i).rgb << std::endl; //#pragma omp critical posSet.at(i).loadImage(conf, posSet.at(i).getModelPath(), posSet.at(i).getParam()); // if(imgload == -1 && conf.learningMode != 2){ // std::cout << "can't load image files" << std::endl; // exit(-1); // } posSet.at(i).extractFeatures(conf); //std::cout << posSet.at(i).img.at(1)->type() << std::endl; //std::cout << "detayo" << std::endl; //std::cout << posSet.size() << std::endl; classDatabase.add(posSet.at(i).getParam()->getClassName(),posSet.at(i).img.at(0)->size(),0); pBar(i,posSet.size(),50); } std::cout << std::endl; classDatabase.show(); // extract neg features for(int i = 0; i < negSet.size(); ++i) { // if(negSet.at(i).getModel() != NULL) // negSet.at(i).loadImage(conf, negSet.at(i).getModelPath(), posSet.at(i)) negSet.at(i).loadImage(conf); negSet.at(i).extractFeatures(conf); } CRTree *tree = new CRTree(conf.min_sample, conf.max_depth, classDatabase.vNode.size(),this->classDatabase); std::cout << "tree created" << std::endl; extractPosPatches(posSet,posPatch,conf,treeNum,this->classDatabase); extractNegPatches(negSet,negPatch,conf); std::cout << "extracted pathes" << std::endl; std::vector<int> patchClassNum(classDatabase.vNode.size(), 0); for(int j = 0; j < posPatch.size(); ++j) patchClassNum.at(classDatabase.search(posPatch.at(j).getClassName()))++; // grow tree //vTrees.at(treeNum)->growTree(vPatches, 0,0, (float)(vPatches.at(0).size()) / ((float)(vPatches.at(0).size()) + (float)(vPatches.at(1).size())), conf, gen, patchClassNum); //#pragma omp parallel tree->growTree(posPatch,negPatch, 0,0, ((float)posPatch.size() / (float)(posPatch.size() + negPatch.size())), conf, patchClassNum); // cv::namedWindow("test"); // cv::imshow("test", *posSet.at(0).feature.at(3)); // cv::waitKey(0); // cv::destroyAllWindows(); // save tree sprintf(buffer, "%s%03d.txt", conf.treepath.c_str(), treeNum + conf.off_tree); std::cout << "tree file name is " << buffer << std::endl; tree->saveTree(buffer); // save class database sprintf(buffer, "%s%s%03d.txt", conf.treepath.c_str(), conf.classDatabaseName.c_str(), treeNum + conf.off_tree); std::cout << "write tree data" << std::endl; classDatabase.write(buffer); //double time = t.elapsed(); //std::cout << "tree " << treeNum << " calicuration time is " << time << std::endl; sprintf(buffer, "%s%03d_timeResult.txt",conf.treepath.c_str(), treeNum + conf.off_tree); std::fstream lerningResult(buffer, std::ios::out); if(lerningResult.fail()) { std::cout << "can't write result" << std::endl; } lerningResult << time << std::endl; lerningResult.close(); delete tree; posPatch.clear(); negPatch.clear(); // for(int i = 0; i< posSet.size(); ++i){ // cv::namedWindow("test"); // cv::imshow("test", *posSet.at(i).img.at(0)); // cv::waitKey(0); // cv::destroyWindow("test"); // } // for(int i = 0; i < posSet.size(); ++i){ // posSet.at(i).releaseImage(); // } // for(int i = 0; i< posSet.size(); ++i){ // cv::namedWindow("test"); // cv::imshow("test", *posSet.at(i).feature.at(0)); // cv::waitKey(0); // cv::destroyWindow("test"); // } // for(int i = 0; i < posSet.size(); ++i){ // posSet.at(i).releaseFeatures(); // } posSet.clear(); negSet.clear(); }
void loadTrainPosFile(CConfig conf, std::vector<CPosDataset> &posSet) { std::string traindatafilepath = conf.trainpath + PATH_SEP + conf.traindatafile; int n_folders; int n_files; std::vector<std::string> trainimagefolder; std::vector<CPosDataset> tempDataSet; std::string trainDataListPath; int dataSetNum; CClassDatabase database; cv::Point tempPoint; nCk nck; posSet.clear(); //read train data folder list std::ifstream in(traindatafilepath.c_str()); if(!in.is_open()){ std::cout << "train data floder list is not found!" << std::endl; exit(1); } // read train pos folder in >> n_folders; std::cout << "number of training data folders : " << n_folders << std::endl; trainimagefolder.resize(n_folders); for(int i = 0;i < n_folders; ++i) in >> trainimagefolder.at(i); // close train pos data folder list in.close(); std::cout << "train folders lists : " << std::endl; //read train file name and grand truth from file tempDataSet.clear(); for(int i = 0;i < n_folders; ++i){ trainDataListPath = conf.trainpath + PATH_SEP + trainimagefolder.at(i) + PATH_SEP + conf.traindatalist; std::cout << trainDataListPath << std::endl; std::string imageFilePath = conf.trainpath + PATH_SEP + trainimagefolder.at(i) + PATH_SEP; std::ifstream trainDataList(trainDataListPath.c_str()); if(!trainDataList.is_open()){ std::cout << "can't read " << trainDataListPath << std::endl; exit(1); } trainDataList >> n_files; for(int j = 0;j < n_files; ++j){ CPosDataset posTemp; std::string nameTemp; // posTemp.angles.clear(); // posTemp.centerPoint.clear(); //read file names trainDataList >> nameTemp; posTemp.setRgbImagePath(imageFilePath + nameTemp); trainDataList >> nameTemp; posTemp.setDepthImagePath(imageFilePath + nameTemp); trainDataList >> nameTemp;// dummy //read class name std::string tempClassName; trainDataList >> tempClassName; //temp.className.push_back(tempClassName); posTemp.setClassName(tempClassName); // read image size cv::Mat tempImage = cv::imread(posTemp.getRgbImagePath(),3); cv::Size tempSize = cv::Size(tempImage.cols, tempImage.rows); // set center point tempPoint = cv::Point(tempImage.cols / 2, tempImage.rows / 2); posTemp.setCenterPoint(tempPoint); // registre class param to class database database.add(posTemp.getParam()->getClassName(), tempSize, 0); //read angle grand truth double tempAngle; trainDataList >> tempAngle; posTemp.setAngle(tempAngle); tempDataSet.push_back(posTemp); } trainDataList.close(); } dataSetNum = tempDataSet.size(); int dataOffset = 0; database.show(); for(int j = 0;j < database.vNode.size(); j++){ std::set<int> chosenData = nck.generate(database.vNode.at(j).instances, conf.imagePerTree); std::set<int>::iterator ite = chosenData.begin(); while(ite != chosenData.end()){ posSet.push_back(tempDataSet.at(*ite + dataOffset)); ite++; } dataOffset += database.vNode.at(j).instances; } // std::cout << "show chosen dataset" << std::endl; // for(int i = 0; i < dataSet.size(); ++i){ // dataSet.at(i).showDataset(); // } }
void loadTrainFile(CConfig conf, std::vector<CDataset> &dataSet) { dataSet.resize(0); std::string traindatafilepath = conf.trainpath + PATH_SEP + conf.traindatafile; int n_folders; int n_files; std::vector<std::string> trainimagefolder; CDataset temp; std::vector<CDataset> tempDataSet; std::string trainDataListPath; int dataSetNum; CClassDatabase database; cv::Point tempPoint; nCk nck; //read train data folder list std::ifstream in(traindatafilepath.c_str()); if(!in.is_open()){ std::cout << "train data floder list is not found!" << std::endl; exit(1); } //std::cout << "kokomade " << std::endl; // read folder number in >> n_folders; std::cout << "number of training data folder: " << n_folders << std::endl; trainimagefolder.resize(n_folders); // read train folder name for(int i = 0;i < n_folders; ++i) in >> trainimagefolder.at(i); std::cout << "train image foloder num is " << trainimagefolder.size() << std::endl; in.close(); //read train file name and grand truth from file tempDataSet.clear(); for(int i = 0;i < n_folders; ++i){ trainDataListPath = conf.trainpath + PATH_SEP + trainimagefolder.at(i) + PATH_SEP + conf.traindatalist; std::cout << trainDataListPath << std::endl; temp.imageFilePath = conf.trainpath + PATH_SEP + trainimagefolder.at(i) + PATH_SEP; std::ifstream trainDataList(trainDataListPath.c_str()); if(!trainDataList.is_open()){ std::cout << "can't read " << trainDataListPath << std::endl; exit(0); } trainDataList >> n_files; for(int j = 0;j < n_files; ++j){ //read file names trainDataList >> temp.rgbImageName; trainDataList >> temp.depthImageName; trainDataList >> temp.maskImageName; //read bounding box //trainDataList >> temp.bBox.x; //trainDataList >> temp.bBox.y; //trainDataList >> temp.bBox.width; //trainDataList >> temp.bBox.height; temp.centerPoint.clear(); //read center point //trainDataList >> tempPoint.x;//temp.centerPoint.x; //trainDataList >> tempPoint.y; //temp.centerPoint.push_back(tempPoint); //read class name trainDataList >> temp.className; database.add(temp.className); //read angle grand truth trainDataList >> temp.angle; //show read data *for debug //temp.showDataset(); tempDataSet.push_back(temp); } trainDataList.close(); } dataSetNum = tempDataSet.size(); int dataOffset = 0; database.show(); for(int j = 0;j < database.vNode.size(); j++){ std::set<int> chosenData = nck.generate(database.vNode.at(j).instances, conf.imagePerTree); std::set<int>::iterator ite = chosenData.begin(); while(ite != chosenData.end()){ dataSet.push_back(tempDataSet.at(*ite + dataOffset)); ite++; } dataOffset += database.vNode.at(j).instances; } //in.close(); }