Пример #1
0
PCA_Train_Result* PCANet_train(vector<cv::Mat>& InImg, PCANet* PcaNet, bool is_extract_feature){
	assert(PcaNet->NumFilters.size() == PcaNet->NumStages);
	
	PCA_Train_Result* train_result = new PCA_Train_Result;
	PCA_Out_Result* out_result = new PCA_Out_Result; 
	PCA_Out_Result* temp;

	out_result->OutImg = InImg;
	int img_length = InImg.size();
	for(int i=0; i<img_length; i++)
		out_result->OutImgIdx.push_back(i);

	int64 e1 = cv::getTickCount();
	int64 eo1, eo2, eb1, eb2;
	
	for(int s=0; s<PcaNet->NumStages; s++){
		eb1 = cv::getTickCount();
		cout << " Computing PCA filter bank and its outputs at stage " << s << "..." << endl;
		train_result->Filters.push_back(PCA_FilterBank(out_result->OutImg, PcaNet->PatchSize, PcaNet->NumFilters[s]));
		eb2 = cv::getTickCount();
		cout <<" stage"<<s<<" PCA_FilterBank time: "<<(eb2 - eb1)/ cv::getTickFrequency()<<endl;

		eo1 = cv::getTickCount();
		if(s != PcaNet->NumStages - 1){
			temp = PCA_output(out_result->OutImg, out_result->OutImgIdx, PcaNet->PatchSize, 
												PcaNet->NumFilters[s], train_result->Filters[s], omp_get_num_procs());
			delete out_result;
			out_result = temp;
		}
		eo2 = cv::getTickCount();
		cout <<" stage"<<s<<" output time: "<<(eo2 - eo1)/ cv::getTickFrequency()<<endl;
	}
	int64 e2 = cv::getTickCount();
	double time = (e2 - e1)/ cv::getTickFrequency();
	cout <<"\n totle FilterBank time: "<<time<<endl;

	InImg.clear();
	vector<cv::Mat>().swap(InImg);

	vector<cv::Mat> tempF;
	int end = PcaNet->NumStages - 1;
	int outIdx_length = out_result->OutImgIdx.size();

	if(is_extract_feature){

		vector<cv::Mat>::const_iterator first = out_result->OutImg.begin();
		vector<cv::Mat>::const_iterator last = out_result->OutImg.begin();
		
		vector<cv::Mat> features;
		Hashing_Result* hashing_r;

		int coreNum = omp_get_num_procs();//»ñµÃŽŠÀíÆ÷žöÊý
		e1 = cv::getTickCount();
# pragma omp parallel for default(none) num_threads(coreNum) private(temp, hashing_r) shared(features, out_result, PcaNet, first, last, outIdx_length, img_length, train_result, end)
		for(int i=0; i<img_length; i++){
			vector<cv::Mat> subInImg(first + i * PcaNet->NumFilters[end], last + (i + 1) * PcaNet->NumFilters[end]);
			vector<int> subIdx;
			/*for(int j=0; j<outIdx_length; j++){
				if(out_result->OutImgIdx[j] == i) subIdx.push_back(1);
				else subIdx.push_back(0);
			}*/
			for(int j=0; j< PcaNet->NumFilters[end]; j++)
				subIdx.push_back(j);
			
			temp = PCA_output(subInImg, subIdx, PcaNet->PatchSize, 
								PcaNet->NumFilters[end], train_result->Filters[end], 2);
			
			hashing_r = HashingHist(PcaNet, temp->OutImgIdx, temp->OutImg);	
			
#pragma omp critical 
{
			features.push_back(hashing_r->Features);
			train_result->feature_idx.push_back(out_result->OutImgIdx[i]);
}
			delete hashing_r;
			delete temp;
			subIdx.clear();
			vector<int>().swap(subIdx);
		}
		e2 = cv::getTickCount();
		time = (e2 - e1)/ cv::getTickFrequency();
		cout <<"\n hasing time: "<<time<<endl;
		
		//out_result->OutImg.clear();
		//vector<cv::Mat>().swap(out_result->OutImg);
		delete out_result;

		int size = features.size();
		if(size > 0){

			train_result->Features.create(0, features[0].cols, features[0].type());
			for(int i=0 ;i<size; i++){
				train_result->Features.push_back(features[i]);
			}

			/*
			train_result->Features = features[0];
			for(int i=1 ;i<size; i++){
				vconcat(train_result->Features, features[i], train_result->Features);
			}*/
		}

		features.clear();
		vector<cv::Mat>().swap(features);
	}
	
	//if(temp != NULL)
	//	delete temp;
	
	return train_result;
}
void Objectness::PCANet_pred(Mat img, vector<Mat> &testImg, vector<Rect> &bboxes, PCA_Train_Result* result, PCANet pcaNet){
	
	// cout << "\n ====== PCANet Testing ======= \n" << endl;	
	int testSIze = testImg.size();
	// cout << testSIze << endl;
	Hashing_Result* hashing_r;
	PCA_Out_Result *out;
	PCA_Out_Result *res;

	int coreNum = omp_get_num_procs();
	
#pragma omp parallel for default(none) num_threads(coreNum) private(out, res,hashing_r) shared(mySVM,libModel, pcaNet, testSIze, testImg, result, bboxes,img,color)
	for (int i = 0; i < testSIze; i++){
		out = new PCA_Out_Result;		
		out->OutImgIdx.push_back(0);
		out->OutImg.push_back(testImg[i]);
		PCA_output(res,out->OutImg, out->OutImgIdx, pcaNet.PatchSize,
			pcaNet.NumFilters[0], result->Filters[0], 2);
		for (int j = 1; j < pcaNet.NumFilters[1]; j++)
			res->OutImgIdx.push_back(j);
		delete out;
		out = new PCA_Out_Result;
		PCA_output(out, res->OutImg, res->OutImgIdx, pcaNet.PatchSize,
			pcaNet.NumFilters[1], result->Filters[1], 2);
		HashingHist(hashing_r,&pcaNet, out->OutImgIdx, out->OutImg);
		hashing_r->Features.convertTo(hashing_r->Features, CV_32F);
		delete res;
		delete out;

		// OpenCV SVM prediction 
		float pred =  mySVM.predict(hashing_r->Features/*, true*/);

		/*========== liblinear prediction =================*/
		//struct feature_node *svmVec;
		//svmVec = (struct feature_node *) malloc((hashing_r->Features.cols + 1)*sizeof(struct feature_node));
		//double *predictions = new double[hashing_r->Features.rows];
		//float *dataPtr = hashing_r->Features.ptr<float>(); // Get data from OpenCV Mat
		//double prob_est[2];  // Probability estimation
		//int r, c;
		//for (r = 0; r < hashing_r->Features.rows; r++)
		//{
		//	for (c = 0; c < hashing_r->Features.cols; c++)
		//	{
		//		svmVec[c].index = c + 1;  // Index starts from 1; Pre-computed kernel starts from 0
		//		svmVec[c].value = dataPtr[r*hashing_r->Features.cols + c];
		//	}
		//	svmVec[c].index = -1;   // End of line
		//}

		//double pred = predict(libModel, svmVec);
		//free(svmVec);
		//printf("pred: %f\n", pred);

		delete hashing_r;
		

#pragma omp critical 		
		if (pred < 3){
			//cout << imgnames[i] << endl;
			Rect box = bboxes[i]; 

			
			//printf("predict: %f\n", pred);

			/*========== save to patch =============*/
			//cv::Mat croppedImage;
			//img(box).copyTo(croppedImage);
			////imshow("asd", croppedImage);
			//char* patch_name = new char[30];
			//sprintf(patch_name, "%s%d%s", "patch/", k + 1, ".jpg");
			//imwrite(patch_name, croppedImage);
			//k++;
			
			rectangle(img, cvPoint(box.x, box.y), cvPoint(box.x + box.width, box.y + box.height), color, 1);

			// ========= add some text ============
			char	buf[50] = { 0 };
			if (pred == 1)
			sprintf_s(buf, 50, "right");
			else if (pred == 2)
			sprintf_s(buf, 50, "forward");
			else
			sprintf_s(buf, 50, "left");
			string   text = buf;
			int		  baseline = 0;
			Size	  textSize = getTextSize(text, fontFace, fontScale, 1, &baseline);
			Point2f   textOrg2(box.x, box.y + textSize.height + 2);
			putText(img, text, textOrg2, fontFace, fontScale, fontColor);
		}
		
	}
}