add feature blob option for feature extraction

pull/276/head
Wangyida 9 years ago
parent b831fc3bad
commit 4483d00c26
  1. 8
      modules/cnn_3dobj/README.md
  2. 4
      modules/cnn_3dobj/include/opencv2/cnn_3dobj.hpp
  3. 10
      modules/cnn_3dobj/samples/classifyIMG_demo.cpp
  4. 10
      modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp
  5. 12
      modules/cnn_3dobj/src/cnn_classification.cpp

@ -37,19 +37,19 @@ $ make
#Demo1:
###Imagas generation from different pose, 4 models are used, there will be 276 images in all which each class contains 69 iamges
```
$ ./sphereview_test -ite_depth=2 -plymodel=../3Dmodel/ape.ply -imagedir=../data/images_all/ -labeldir=../data/label_all.txt -num_class=4 -label_class=0
$ ./sphereview_test -plymodel=../3Dmodel/ape.ply -label_class=0
```
###press q to start
```
$ ./sphereview_test -ite_depth=2 -plymodel=../3Dmodel/ant.ply -imagedir=../data/images_all/ -labeldir=../data/label_all.txt -num_class=4 -label_class=1
$ ./sphereview_test -plymodel=../3Dmodel/ant.ply -label_class=1
```
###press q to start
```
$ ./sphereview_test -ite_depth=2 -plymodel=../3Dmodel/cow.ply -imagedir=../data/images_all/ -labeldir=../data/label_all.txt -num_class=4 -label_class=2
$ ./sphereview_test -plymodel=../3Dmodel/cow.ply -label_class=2
```
###press q to start
```
$ ./sphereview_test -ite_depth=2 -plymodel=../3Dmodel/plane.ply -imagedir=../data/images_all/ -labeldir=../data/label_all.txt -num_class=4 -label_class=3
$ ./sphereview_test -plymodel=../3Dmodel/plane.ply -label_class=3
```
###press q to start, when all images are created in images_all folder as a collection of images for network tranining and feature extraction, then proceed on.
###After this demo, the binary files of images and labels will be stored as 'binary_image' and 'binary_label' in current path, you should copy them into the leveldb folder in Caffe triplet training, for example: copy these 2 files in <caffe_source_directory>/data/linemod and rename them as 'binary_image_train', 'binary_image_test' and 'binary_label_train', 'binary_label_train'.

@ -160,10 +160,10 @@ class CV_EXPORTS_W Classification
void GetLabellist(const std::vector<string>& name_gallery);
/** @brief Get the label of the gallery images for result displaying in prediction.
*/
std::vector<std::pair<string, float> > Classify(const cv::Mat& reference, const cv::Mat& img, int N, bool mean_substract = false);
std::vector<std::pair<string, float> > Classify(const cv::Mat& reference, const cv::Mat& target, int N);
/** @brief Make a classification.
*/
void FeatureExtract(InputArray inputimg, OutputArray feature, bool mean_subtract);
void FeatureExtract(InputArray inputimg, OutputArray feature, bool mean_subtract, std::string feature_blob);
/** @brief Extract a single featrue of one image.
*/
std::vector<int> Argmax(const std::vector<float>& v, int N);

@ -41,10 +41,11 @@ int main(int argc, char** argv)
{
const String keys = "{help | | this demo will convert a set of images in a particular path into leveldb database for feature extraction using Caffe.}"
"{src_dir | ../data/images_all/ | Source direction of the images ready for being used for extract feature as gallery.}"
"{caffemodel | ../data/3d_triplet_iter_10000.caffemodel | caffe model for feature exrtaction.}"
"{caffemodel | ../data/3d_triplet_iter_20000.caffemodel | caffe model for feature exrtaction.}"
"{network_forIMG | ../data/3d_triplet_testIMG.prototxt | Network definition file used for extracting feature from a single image and making a classification}"
"{mean_file | ../data/images_mean/triplet_mean.binaryproto | The mean file generated by Caffe from all gallery images, this could be used for mean value substraction from all images.}"
"{target_img | ../data/images_all/3_13.png | Path of image waiting to be classified.}"
"{feature_blob | feat | Name of layer which will represent as the feature, in this network, ip1 or feat is well.}"
"{num_candidate | 6 | Number of candidates in gallery as the prediction result.}"
"{device | CPU | device}"
"{dev_id | 0 | dev_id}";
@ -60,6 +61,7 @@ int main(int argc, char** argv)
string network_forIMG = parser.get<string>("network_forIMG");
string mean_file = parser.get<string>("mean_file");
string target_img = parser.get<string>("target_img");
string feature_blob = parser.get<string>("feature_blob");
int num_candidate = parser.get<int>("num_candidate");
string device = parser.get<string>("device");
int dev_id = parser.get<int>("dev_id");
@ -77,7 +79,7 @@ int main(int argc, char** argv)
for (unsigned int i = 0; i < name_gallery.size(); i++) {
img_gallery.push_back(cv::imread(name_gallery[i], -1));
}
classifier.FeatureExtract(img_gallery, feature_reference, false);
classifier.FeatureExtract(img_gallery, feature_reference, false, feature_blob);
std::cout << std::endl << "---------- Prediction for "
<< target_img << " ----------" << std::endl;
@ -89,9 +91,9 @@ int main(int argc, char** argv)
for (unsigned int i = 0; i < feature_reference.rows; i++)
std::cout << feature_reference.row(i) << endl;
cv::Mat feature_test;
classifier.FeatureExtract(img, feature_test, false);
classifier.FeatureExtract(img, feature_test, false, feature_blob);
std::cout << std::endl << "---------- Featrue of target image: " << target_img << "----------" << endl << feature_test << std::endl;
prediction = classifier.Classify(feature_reference, img, num_candidate, false);
prediction = classifier.Classify(feature_reference, feature_test, num_candidate);
// Print the top N prediction.
std::cout << std::endl << "---------- Prediction result(Distance - File Name in Gallery) ----------" << std::endl;
for (size_t i = 0; i < prediction.size(); ++i) {

@ -41,11 +41,11 @@ using namespace std;
using namespace cv::cnn_3dobj;
int main(int argc, char *argv[]){
const String keys = "{help | | demo :$ ./sphereview_test -ite_depth=2 -plymodel=../3Dmodel/ape.ply -imagedir=../data/images_ape/ -labeldir=../data/label_ape.txt -num_class=4 -label_class=0, then press 'q' to run the demo for images generation when you see the gray background and a coordinate.}"
"{ite_depth | 1 | Iteration of sphere generation.}"
"{plymodel | ../ape.ply | path of the '.ply' file for image rendering. }"
"{imagedir | ../data/images_ape/ | path of the generated images for one particular .ply model. }"
"{labeldir | ../data/label_ape.txt | path of the generated images for one particular .ply model. }"
"{num_class | 2 | total number of classes of models}"
"{ite_depth | 2 | Iteration of sphere generation.}"
"{plymodel | ../3Dmodel/ape.ply | path of the '.ply' file for image rendering. }"
"{imagedir | ../data/images_all/ | path of the generated images for one particular .ply model. }"
"{labeldir | ../data/label_all.txt | path of the generated images for one particular .ply model. }"
"{num_class | 4 | total number of classes of models}"
"{label_class | 0 | class label of current .ply model}";
cv::CommandLineParser parser(argc, argv, keys);
parser.about("Demo for Sphere View data generation");

@ -81,15 +81,13 @@ namespace cnn_3dobj
};
//Return the top N predictions.
std::vector<std::pair<string, float> > Classification::Classify(const cv::Mat& reference, const cv::Mat& img, int N, bool mean_substract)
std::vector<std::pair<string, float> > Classification::Classify(const cv::Mat& reference, const cv::Mat& target, int N)
{
cv::Mat feature;
Classification::FeatureExtract(img, feature, mean_substract);
std::vector<float> output;
for (int i = 0; i < reference.rows; i++)
{
cv::Mat f1 = reference.row(i);
cv::Mat f2 = feature;
cv::Mat f2 = target;
cv::Mat output_temp = f1-f2;
output.push_back(cv::norm(output_temp));
}
@ -132,7 +130,7 @@ namespace cnn_3dobj
mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
};
void Classification::FeatureExtract(InputArray inputimg, OutputArray feature, bool mean_subtract)
void Classification::FeatureExtract(InputArray inputimg, OutputArray feature, bool mean_subtract, std::string featrue_blob)
{
Blob<float>* input_layer = net_->input_blobs()[0];
input_layer->Reshape(1, num_channels_,
@ -147,7 +145,7 @@ namespace cnn_3dobj
Preprocess(img, &input_channels, mean_subtract);
net_->ForwardPrefilled();
/* Copy the output layer to a std::vector */
Blob<float>* output_layer = net_->output_blobs()[0];
Blob<float>* output_layer = net_->blob_by_name(featrue_blob).get();
const float* begin = output_layer->cpu_data();
const float* end = begin + output_layer->channels();
std::vector<float> featureVec = std::vector<float>(begin, end);
@ -164,7 +162,7 @@ namespace cnn_3dobj
Preprocess(img[i], &input_channels, mean_subtract);
net_->ForwardPrefilled();
/* Copy the output layer to a std::vector */
Blob<float>* output_layer = net_->output_blobs()[0];
Blob<float>* output_layer = net_->blob_by_name(featrue_blob).get();
const float* begin = output_layer->cpu_data();
const float* end = begin + output_layer->channels();
std::vector<float> featureVec = std::vector<float>(begin, end);

Loading…
Cancel
Save