示例#1
0
    	/**
    	 * Evaluate the given image to see its probability in the given category.
    	 */
    	float _getProbability(const cv::Mat& image, const int category)
    	{
    		this->initCaffeNet();	//Initialize caffe

				// Initialize test network
				shared_ptr<Net<float> > caffe_test_net = shared_ptr<Net<float> >( new Net<float>(Params::image::model_definition));

				// Get the trained model
				caffe_test_net->CopyTrainedLayersFrom(Params::image::pretrained_model);

				// Run ForwardPrefilled
				float loss;

				// Add images and labels manually to the ImageDataLayer
				vector<int> labels(10, 0);
				vector<cv::Mat> images;

				// Add images to the list
				if (Params::image::use_crops)
				{
					// Ten crops have been stored in the vector
					_createTenCrops(image, images);
				}
				else
				{
					images.push_back(image);
				}

				// Classify images
				const shared_ptr<ImageDataLayer<float> > image_data_layer =
						boost::static_pointer_cast<ImageDataLayer<float> >(
								caffe_test_net->layer_by_name("data"));

				image_data_layer->AddImagesAndLabels(images, labels);

				const vector<Blob<float>*>& result = caffe_test_net->ForwardPrefilled(&loss);

				// Get the highest layer of Softmax
				const float* softmax = result[1]->cpu_data();

				// If use 10 crops, we have to average the predictions of 10 crops
				if (Params::image::use_crops)
				{
					vector<double> values;

					// Average the predictions of evaluating 10 crops
					for(int i = 0; i < Params::image::num_categories; ++i)
					{
						boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::mean> > avg;

						for(int j = 0; j < 10 * Params::image::num_categories; j += Params::image::num_categories)
						{
							avg(softmax[i + j]);
						}

						double mean = boost::accumulators::mean(avg);

						values.push_back(mean);
					}

	    		return values[category];
				}
				// If use only 1 crop
				else
				{
					return softmax[category];
				}

    	}