diff --git a/modules/cnn_3dobj/README.md b/modules/cnn_3dobj/README.md index bc41071b1..2867ad4ef 100644 --- a/modules/cnn_3dobj/README.md +++ b/modules/cnn_3dobj/README.md @@ -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 /data/linemod and rename them as 'binary_image_train', 'binary_image_test' and 'binary_label_train', 'binary_label_train'. diff --git a/modules/cnn_3dobj/include/opencv2/cnn_3dobj.hpp b/modules/cnn_3dobj/include/opencv2/cnn_3dobj.hpp index 2e65e30b9..1b3186c14 100644 --- a/modules/cnn_3dobj/include/opencv2/cnn_3dobj.hpp +++ b/modules/cnn_3dobj/include/opencv2/cnn_3dobj.hpp @@ -160,10 +160,10 @@ class CV_EXPORTS_W Classification void GetLabellist(const std::vector& name_gallery); /** @brief Get the label of the gallery images for result displaying in prediction. */ - std::vector > Classify(const cv::Mat& reference, const cv::Mat& img, int N, bool mean_substract = false); + std::vector > 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 Argmax(const std::vector& v, int N); diff --git a/modules/cnn_3dobj/samples/classifyIMG_demo.cpp b/modules/cnn_3dobj/samples/classifyIMG_demo.cpp index c286a4c95..c17bb275f 100644 --- a/modules/cnn_3dobj/samples/classifyIMG_demo.cpp +++ b/modules/cnn_3dobj/samples/classifyIMG_demo.cpp @@ -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("network_forIMG"); string mean_file = parser.get("mean_file"); string target_img = parser.get("target_img"); + string feature_blob = parser.get("feature_blob"); int num_candidate = parser.get("num_candidate"); string device = parser.get("device"); int dev_id = parser.get("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) { diff --git a/modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp b/modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp index 907f8946d..0718128be 100644 --- a/modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp +++ b/modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp @@ -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"); diff --git a/modules/cnn_3dobj/src/cnn_classification.cpp b/modules/cnn_3dobj/src/cnn_classification.cpp index 2e0c483cc..029f3aa74 100644 --- a/modules/cnn_3dobj/src/cnn_classification.cpp +++ b/modules/cnn_3dobj/src/cnn_classification.cpp @@ -81,15 +81,13 @@ namespace cnn_3dobj }; //Return the top N predictions. - std::vector > Classification::Classify(const cv::Mat& reference, const cv::Mat& img, int N, bool mean_substract) + std::vector > Classification::Classify(const cv::Mat& reference, const cv::Mat& target, int N) { - cv::Mat feature; - Classification::FeatureExtract(img, feature, mean_substract); std::vector 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* 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* output_layer = net_->output_blobs()[0]; + Blob* 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 featureVec = std::vector(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* output_layer = net_->output_blobs()[0]; + Blob* 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 featureVec = std::vector(begin, end);