Commit dd50d5bc authored by Wangyida's avatar Wangyida

modify samples for feature storing option

parent a81322a8
...@@ -72,7 +72,6 @@ the use of this software, even if advised of the possibility of such damage. ...@@ -72,7 +72,6 @@ the use of this software, even if advised of the possibility of such damage.
#include "opencv2/highgui.hpp" #include "opencv2/highgui.hpp"
#include "opencv2/highgui/highgui_c.h" #include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc.hpp" #include "opencv2/imgproc.hpp"
using std::string;
using caffe::Blob; using caffe::Blob;
using caffe::Caffe; using caffe::Caffe;
using caffe::Datum; using caffe::Datum;
...@@ -185,7 +184,7 @@ namespace cnn_3dobj ...@@ -185,7 +184,7 @@ namespace cnn_3dobj
/** @brief Suit the position of bytes in 4 byte data structure for particular system. /** @brief Suit the position of bytes in 4 byte data structure for particular system.
*/ */
CV_WRAP static uint32_t swapEndian(uint32_t val); CV_WRAP static int swapEndian(int val);
/** @brief Create header in binary files collecting the image data and label. /** @brief Create header in binary files collecting the image data and label.
@param num_item Number of items. @param num_item Number of items.
...@@ -206,7 +205,7 @@ namespace cnn_3dobj ...@@ -206,7 +205,7 @@ namespace cnn_3dobj
@param z Pose label of Z. @param z Pose label of Z.
@param isrgb Option for choice of using RGB images or not. @param isrgb Option for choice of using RGB images or not.
*/ */
CV_WRAP static void writeBinaryfile(string filenameImg, const char* binaryPath, const char* headerPath, int num_item, int label_class, int x, int y, int z, int isrgb); CV_WRAP static void writeBinaryfile(String filenameImg, const char* binaryPath, const char* headerPath, int num_item, int label_class, int x, int y, int z, int isrgb);
}; };
/** @brief Caffe based 3D images descriptor. /** @brief Caffe based 3D images descriptor.
...@@ -223,13 +222,13 @@ namespace cnn_3dobj ...@@ -223,13 +222,13 @@ namespace cnn_3dobj
bool net_set; bool net_set;
int net_ready; int net_ready;
cv::Mat mean_; cv::Mat mean_;
string deviceType; String deviceType;
int deviceId; int deviceId;
/** @brief Load the mean file in binaryproto format if it is needed. /** @brief Load the mean file in binaryproto format if it is needed.
@param mean_file Path of mean file which stores the mean of training images, it is usually generated by Caffe tool. @param mean_file Path of mean file which stores the mean of training images, it is usually generated by Caffe tool.
*/ */
void setMean(const string& mean_file); void setMean(const String& mean_file);
/** @brief Wrap the input layer of the network in separate cv::Mat objects(one per channel). /** @brief Wrap the input layer of the network in separate cv::Mat objects(one per channel).
This way we save one memcpy operation and we don't need to rely on cudaMemcpy2D. This way we save one memcpy operation and we don't need to rely on cudaMemcpy2D.
...@@ -246,11 +245,11 @@ namespace cnn_3dobj ...@@ -246,11 +245,11 @@ namespace cnn_3dobj
@param device_type CPU or GPU. @param device_type CPU or GPU.
@param device_id ID of GPU. @param device_id ID of GPU.
*/ */
descriptorExtractor(const string& device_type, int device_id = 0); descriptorExtractor(const String& device_type, int device_id = 0);
/** @brief Get device type information for feature extraction. /** @brief Get device type information for feature extraction.
*/ */
string getDeviceType(); String getDeviceType();
/** @brief Get device ID information for feature extraction. /** @brief Get device ID information for feature extraction.
*/ */
...@@ -260,7 +259,7 @@ namespace cnn_3dobj ...@@ -260,7 +259,7 @@ namespace cnn_3dobj
Useful to change device without the need to reload the net. Useful to change device without the need to reload the net.
@param device_type CPU or GPU. @param device_type CPU or GPU.
*/ */
void setDeviceType(const string& device_type); void setDeviceType(const String& device_type);
/** @brief Set device ID information for feature extraction. /** @brief Set device ID information for feature extraction.
Useful to change device without the need to reload the net. Only used for GPU. Useful to change device without the need to reload the net. Only used for GPU.
...@@ -274,7 +273,7 @@ namespace cnn_3dobj ...@@ -274,7 +273,7 @@ namespace cnn_3dobj
@param trained_file Path of prototxt which defining the structure of CNN. @param trained_file Path of prototxt which defining the structure of CNN.
@param mean_file Path of mean file(option). @param mean_file Path of mean file(option).
*/ */
void loadNet(const string& model_file, const string& trained_file, const string& mean_file = ""); void loadNet(const String& model_file, const String& trained_file, const String& mean_file = "");
/** @brief Extract features from a single image or from a vector of images. /** @brief Extract features from a single image or from a vector of images.
If loadNet was not called before, this method invocation will fail. If loadNet was not called before, this method invocation will fail.
...@@ -282,7 +281,7 @@ namespace cnn_3dobj ...@@ -282,7 +281,7 @@ namespace cnn_3dobj
@param feature Output features. @param feature Output features.
@param feature_blob Layer which the feature is extracted from. @param feature_blob Layer which the feature is extracted from.
*/ */
void extract(InputArrayOfArrays inputimg, OutputArray feature, std::string feature_blob); void extract(InputArrayOfArrays inputimg, OutputArray feature, String feature_blob);
}; };
//! @} //! @}
} }
......
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/01.ply -label_class=1 -label_item=1 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/aeroplane_pascal/ -semisphere=0 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/02.ply -label_class=1 -label_item=2 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/aeroplane_pascal/ -semisphere=0 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/03.ply -label_class=1 -label_item=3 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/aeroplane_pascal/ -semisphere=0 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/04.ply -label_class=1 -label_item=4 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/aeroplane_pascal/ -semisphere=0 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/05.ply -label_class=1 -label_item=5 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/aeroplane_pascal/ -semisphere=0 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/06.ply -label_class=1 -label_item=6 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/aeroplane_pascal/ -semisphere=0 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/07.ply -label_class=1 -label_item=7 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/aeroplane_pascal/ -semisphere=0 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/08.ply -label_class=1 -label_item=8 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/aeroplane_pascal/ -semisphere=0 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/01.ply -label_class=2 -label_item=1 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bicycle_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/02.ply -label_class=2 -label_item=2 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bicycle_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/03.ply -label_class=2 -label_item=3 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bicycle_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/04.ply -label_class=2 -label_item=4 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bicycle_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/05.ply -label_class=2 -label_item=5 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bicycle_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/06.ply -label_class=2 -label_item=6 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bicycle_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/01.ply -label_class=3 -label_item=1 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/boat_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/02.ply -label_class=3 -label_item=2 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/boat_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/03.ply -label_class=3 -label_item=3 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/boat_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/04.ply -label_class=3 -label_item=4 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/boat_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/05.ply -label_class=3 -label_item=5 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/boat_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/06.ply -label_class=3 -label_item=6 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/boat_pascal/ -z_range=0.6 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/01.ply -label_class=5 -label_item=1 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bus_pascal/ -z_range=0.2 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/02.ply -label_class=5 -label_item=2 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bus_pascal/ -z_range=0.2 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/03.ply -label_class=5 -label_item=3 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bus_pascal/ -z_range=0.2 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/04.ply -label_class=5 -label_item=4 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bus_pascal/ -z_range=0.2 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/05.ply -label_class=5 -label_item=5 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bus_pascal/ -z_range=0.2 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/06.ply -label_class=5 -label_item=6 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/bus_pascal/ -z_range=0.2 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/01.ply -label_class=6 -label_item=1 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/02.ply -label_class=6 -label_item=2 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/03.ply -label_class=6 -label_item=3 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/04.ply -label_class=6 -label_item=4 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/05.ply -label_class=6 -label_item=5 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/06.ply -label_class=6 -label_item=6 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/07.ply -label_class=6 -label_item=7 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/08.ply -label_class=6 -label_item=8 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/09.ply -label_class=6 -label_item=9 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/10.ply -label_class=6 -label_item=10 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/car_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/01.ply -label_class=9 -label_item=1 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/motorbike_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/02.ply -label_class=9 -label_item=2 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/motorbike_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/03.ply -label_class=9 -label_item=3 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/motorbike_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/04.ply -label_class=9 -label_item=4 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/motorbike_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/05.ply -label_class=9 -label_item=5 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/motorbike_pascal/ -z_range=0.5 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/train/01.ply -label_class=11 -label_item=1 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/train_pascal/ -z_range=0.2 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/train/02.ply -label_class=11 -label_item=2 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/train_pascal/ -z_range=0.2 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/train/03.ply -label_class=11 -label_item=3 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/train_pascal/ -z_range=0.2 -front_view=1
./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/train/04.ply -label_class=11 -label_item=4 -bakgrdir=/Users/yidawang/Documents/database/backgrd_pascal/train_pascal/ -z_range=0.2 -front_view=1
\ No newline at end of file
...@@ -48,7 +48,7 @@ using namespace cv::cnn_3dobj; ...@@ -48,7 +48,7 @@ using namespace cv::cnn_3dobj;
* @function listDir * @function listDir
* @brief Making all files names under a directory into a list * @brief Making all files names under a directory into a list
*/ */
void listDir(const char *path, std::vector<string>& files, bool r) void listDir(const char *path, std::vector<String>& files, bool r)
{ {
DIR *pDir; DIR *pDir;
struct dirent *ent; struct dirent *ent;
...@@ -59,11 +59,11 @@ void listDir(const char *path, std::vector<string>& files, bool r) ...@@ -59,11 +59,11 @@ void listDir(const char *path, std::vector<string>& files, bool r)
{ {
if (ent->d_type & DT_DIR) if (ent->d_type & DT_DIR)
{ {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0 || strcmp(ent->d_name, ".DS_Store") == 0)
{ {
continue; continue;
} }
if(r) if (r)
{ {
sprintf(childpath, "%s/%s", path, ent->d_name); sprintf(childpath, "%s/%s", path, ent->d_name);
listDir(childpath,files,false); listDir(childpath,files,false);
...@@ -71,18 +71,40 @@ void listDir(const char *path, std::vector<string>& files, bool r) ...@@ -71,18 +71,40 @@ void listDir(const char *path, std::vector<string>& files, bool r)
} }
else else
{ {
files.push_back(ent->d_name); if (strcmp(ent->d_name, ".DS_Store") != 0)
files.push_back(ent->d_name);
} }
} }
sort(files.begin(),files.end()); sort(files.begin(),files.end());
}; };
/**
* @function featureWrite
* @brief Writing features of gallery images into binary files
*/
int featureWrite(const Mat &features, const String &fname)
{
ofstream ouF;
ouF.open(fname.c_str(), std::ofstream::binary);
if (!ouF)
{
cerr << "failed to open the file : " << fname << endl;
return 0;
}
for (int r = 0; r < features.rows; r++)
{
ouF.write(reinterpret_cast<const char*>(features.ptr(r)), features.cols*features.elemSize());
}
ouF.close();
return 1;
}
/** /**
* @function main * @function main
*/ */
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
const String keys = "{help | | This sample will extract featrues from reference images and target image for classification. You can add a mean_file if there little variance in data such as human faces, otherwise it is not so useful}" const String keys = "{help | | This sample will extract features from reference images and target image for classification. You can add a mean_file if there little variance in data such as human faces, otherwise it is not so useful}"
"{src_dir | ../data/images_all/ | Source direction of the images ready for being used for extract feature as gallery.}" "{src_dir | ../data/images_all/ | Source direction of the images ready for being used for extract feature as gallery.}"
"{caffemodel | ../../testdata/cv/3d_triplet_iter_30000.caffemodel | caffe model for feature exrtaction.}" "{caffemodel | ../../testdata/cv/3d_triplet_iter_30000.caffemodel | caffe model for feature exrtaction.}"
"{network_forIMG | ../../testdata/cv/3d_triplet_testIMG.prototxt | Network definition file used for extracting feature from a single image and making a classification}" "{network_forIMG | ../../testdata/cv/3d_triplet_testIMG.prototxt | Network definition file used for extracting feature from a single image and making a classification}"
...@@ -91,7 +113,8 @@ int main(int argc, char** argv) ...@@ -91,7 +113,8 @@ int main(int argc, char** argv)
"{feature_blob | feat | Name of layer which will represent as the feature, in this network, ip1 or feat is well.}" "{feature_blob | feat | Name of layer which will represent as the feature, in this network, ip1 or feat is well.}"
"{num_candidate | 15 | Number of candidates in gallery as the prediction result.}" "{num_candidate | 15 | Number of candidates in gallery as the prediction result.}"
"{device | CPU | Device type: CPU or GPU}" "{device | CPU | Device type: CPU or GPU}"
"{dev_id | 0 | Device id}"; "{dev_id | 0 | Device id}"
"{gallery_out | 0 | Option on output binary features on gallery images}";
/* get parameters from comand line */ /* get parameters from comand line */
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
parser.about("Feature extraction and classification"); parser.about("Feature extraction and classification");
...@@ -100,15 +123,16 @@ int main(int argc, char** argv) ...@@ -100,15 +123,16 @@ int main(int argc, char** argv)
parser.printMessage(); parser.printMessage();
return 0; return 0;
} }
string src_dir = parser.get<string>("src_dir"); String src_dir = parser.get<String>("src_dir");
string caffemodel = parser.get<string>("caffemodel"); String caffemodel = parser.get<String>("caffemodel");
string network_forIMG = parser.get<string>("network_forIMG"); String network_forIMG = parser.get<String>("network_forIMG");
string mean_file = parser.get<string>("mean_file"); String mean_file = parser.get<String>("mean_file");
string target_img = parser.get<string>("target_img"); String target_img = parser.get<String>("target_img");
string feature_blob = parser.get<string>("feature_blob"); String feature_blob = parser.get<String>("feature_blob");
int num_candidate = parser.get<int>("num_candidate"); int num_candidate = parser.get<int>("num_candidate");
string device = parser.get<string>("device"); String device = parser.get<String>("device");
int dev_id = parser.get<int>("dev_id"); int dev_id = parser.get<int>("dev_id");
int gallery_out = parser.get<int>("gallery_out");
/* Initialize a net work with Device */ /* Initialize a net work with Device */
cv::cnn_3dobj::descriptorExtractor descriptor(device); cv::cnn_3dobj::descriptorExtractor descriptor(device);
std::cout << "Using" << descriptor.getDeviceType() << std::endl; std::cout << "Using" << descriptor.getDeviceType() << std::endl;
...@@ -117,9 +141,16 @@ int main(int argc, char** argv) ...@@ -117,9 +141,16 @@ int main(int argc, char** argv)
descriptor.loadNet(network_forIMG, caffemodel); descriptor.loadNet(network_forIMG, caffemodel);
else else
descriptor.loadNet(network_forIMG, caffemodel, mean_file); descriptor.loadNet(network_forIMG, caffemodel, mean_file);
std::vector<string> name_gallery; std::vector<String> name_gallery;
/* List the file names under a given path */ /* List the file names under a given path */
listDir(src_dir.c_str(), name_gallery, false); listDir(src_dir.c_str(), name_gallery, false);
if (gallery_out)
{
ofstream namelist_out("gallelist.txt");
/* Writing name of the reference images. */
for (unsigned int i = 0; i < name_gallery.size(); i++)
namelist_out << name_gallery.at(i) << endl;
}
for (unsigned int i = 0; i < name_gallery.size(); i++) for (unsigned int i = 0; i < name_gallery.size(); i++)
{ {
name_gallery[i] = src_dir + name_gallery[i]; name_gallery[i] = src_dir + name_gallery[i];
...@@ -128,31 +159,43 @@ int main(int argc, char** argv) ...@@ -128,31 +159,43 @@ int main(int argc, char** argv)
cv::Mat feature_reference; cv::Mat feature_reference;
for (unsigned int i = 0; i < name_gallery.size(); i++) for (unsigned int i = 0; i < name_gallery.size(); i++)
{ {
img_gallery.push_back(cv::imread(name_gallery[i], -1)); img_gallery.push_back(cv::imread(name_gallery[i]));
} }
/* Extract feature from a set of images */ /* Extract feature from a set of images */
descriptor.extract(img_gallery, feature_reference, feature_blob); descriptor.extract(img_gallery, feature_reference, feature_blob);
std::cout << std::endl << "---------- Prediction for " << target_img << " ----------" << std::endl; if (gallery_out)
cv::Mat img = cv::imread(target_img, -1); {
std::cout << std::endl << "---------- Features of gallery images ----------" << std::endl; std::cout << std::endl << "---------- Features of gallery images ----------" << std::endl;
std::vector<std::pair<string, float> > prediction; /* Print features of the reference images. */
/* Print features of the reference images. */ for (unsigned int i = 0; i < feature_reference.rows; i++)
for (unsigned int i = 0; i < feature_reference.rows; i++) std::cout << feature_reference.row(i) << endl;
std::cout << feature_reference.row(i) << endl; std::cout << std::endl << "---------- Saving features of gallery images into feature.bin ----------" << std::endl;
cv::Mat feature_test; featureWrite(feature_reference, "feature.bin");
descriptor.extract(img, feature_test, feature_blob); }
/* Initialize a matcher which using L2 distance. */ else
cv::BFMatcher matcher(NORM_L2);
std::vector<std::vector<cv::DMatch> > matches;
/* Have a KNN match on the target and reference images. */
matcher.knnMatch(feature_test, feature_reference, matches, num_candidate);
/* Print feature of the target image waiting to be classified. */
std::cout << std::endl << "---------- Features of target image: " << target_img << "----------" << endl << feature_test << std::endl;
/* Print the top N prediction. */
std::cout << std::endl << "---------- Prediction result(Distance - File Name in Gallery) ----------" << std::endl;
for (size_t i = 0; i < matches[0].size(); ++i)
{ {
std::cout << i << " - " << std::fixed << std::setprecision(2) << name_gallery[matches[0][i].trainIdx] << " - \"" << matches[0][i].distance << "\"" << std::endl; std::cout << std::endl << "---------- Prediction for " << target_img << " ----------" << std::endl;
cv::Mat img = cv::imread(target_img);
std::cout << std::endl << "---------- Features of gallery images ----------" << std::endl;
std::vector<std::pair<String, float> > prediction;
/* Print features of the reference images. */
for (unsigned int i = 0; i < feature_reference.rows; i++)
std::cout << feature_reference.row(i) << endl;
cv::Mat feature_test;
descriptor.extract(img, feature_test, feature_blob);
/* Initialize a matcher which using L2 distance. */
cv::BFMatcher matcher(NORM_L2);
std::vector<std::vector<cv::DMatch> > matches;
/* Have a KNN match on the target and reference images. */
matcher.knnMatch(feature_test, feature_reference, matches, num_candidate);
/* Print feature of the target image waiting to be classified. */
std::cout << std::endl << "---------- Features of target image: " << target_img << "----------" << endl << feature_test << std::endl;
/* Print the top N prediction. */
std::cout << std::endl << "---------- Prediction result(Distance - File Name in Gallery) ----------" << std::endl;
for (size_t i = 0; i < matches[0].size(); ++i)
{
std::cout << i << " - " << std::fixed << std::setprecision(2) << name_gallery[matches[0][i].trainIdx] << " - \"" << matches[0][i].distance << "\"" << std::endl;
}
} }
return 0; return 0;
} }
\ No newline at end of file
...@@ -64,18 +64,18 @@ int main(int argc, char** argv) ...@@ -64,18 +64,18 @@ int main(int argc, char** argv)
parser.printMessage(); parser.printMessage();
return 0; return 0;
} }
string caffemodel = parser.get<string>("caffemodel"); String caffemodel = parser.get<String>("caffemodel");
string network_forIMG = parser.get<string>("network_forIMG"); String network_forIMG = parser.get<String>("network_forIMG");
string mean_file = parser.get<string>("mean_file"); String mean_file = parser.get<String>("mean_file");
string target_img = parser.get<string>("target_img"); String target_img = parser.get<String>("target_img");
string ref_img1 = parser.get<string>("ref_img1"); String ref_img1 = parser.get<String>("ref_img1");
string ref_img2 = parser.get<string>("ref_img2"); String ref_img2 = parser.get<String>("ref_img2");
string ref_img3 = parser.get<string>("ref_img3"); String ref_img3 = parser.get<String>("ref_img3");
string feature_blob = parser.get<string>("feature_blob"); String feature_blob = parser.get<String>("feature_blob");
string device = parser.get<string>("device"); String device = parser.get<String>("device");
int dev_id = parser.get<int>("dev_id"); int dev_id = parser.get<int>("dev_id");
std::vector<string> ref_img; std::vector<String> ref_img;
/* Sample which is most closest in pose to reference image /* Sample which is most closest in pose to reference image
*and also the same class. *and also the same class.
*/ */
......
...@@ -12,7 +12,7 @@ using namespace cv::cnn_3dobj; ...@@ -12,7 +12,7 @@ using namespace cv::cnn_3dobj;
* @function listDir * @function listDir
* @brief Making all files names under a directory into a list * @brief Making all files names under a directory into a list
*/ */
void listDir(const char *path, std::vector<string>& files, bool r) void listDir(const char *path, std::vector<String>& files, bool r)
{ {
DIR *pDir; DIR *pDir;
struct dirent *ent; struct dirent *ent;
...@@ -89,22 +89,22 @@ int main(int argc, char **argv) ...@@ -89,22 +89,22 @@ int main(int argc, char **argv)
parser.printMessage(); parser.printMessage();
return 0; return 0;
} }
string src_dir = parser.get<string>("src_dir"); String src_dir = parser.get<String>("src_dir");
string caffemodellist = parser.get<string>("caffemodellist"); String caffemodellist = parser.get<String>("caffemodellist");
string network_forIMG = parser.get<string>("network_forIMG"); String network_forIMG = parser.get<String>("network_forIMG");
string mean_file = parser.get<string>("mean_file"); String mean_file = parser.get<String>("mean_file");
string target_img1 = parser.get<string>("target_img1"); String target_img1 = parser.get<String>("target_img1");
string target_img2 = parser.get<string>("target_img2"); String target_img2 = parser.get<String>("target_img2");
string target_img3 = parser.get<string>("target_img3"); String target_img3 = parser.get<String>("target_img3");
string target_img4 = parser.get<string>("target_img4"); String target_img4 = parser.get<String>("target_img4");
string target_img5 = parser.get<string>("target_img5"); String target_img5 = parser.get<String>("target_img5");
string target_img6 = parser.get<string>("target_img6"); String target_img6 = parser.get<String>("target_img6");
string feature_blob = parser.get<string>("feature_blob"); String feature_blob = parser.get<String>("feature_blob");
int num_candidate = parser.get<int>("num_candidate"); int num_candidate = parser.get<int>("num_candidate");
string device = parser.get<string>("device"); String device = parser.get<String>("device");
int dev_id = parser.get<int>("dev_id"); int dev_id = parser.get<int>("dev_id");
ifstream namelist_model(caffemodellist.c_str(), ios::in); ifstream namelist_model(caffemodellist.c_str(), ios::in);
vector<string> caffemodel; vector<String> caffemodel;
char *buf = new char[512]; char *buf = new char[512];
int number_model = 0; int number_model = 0;
while (!namelist_model.eof()) while (!namelist_model.eof())
...@@ -114,7 +114,7 @@ int main(int argc, char **argv) ...@@ -114,7 +114,7 @@ int main(int argc, char **argv)
number_model++; number_model++;
} }
/* List the file names under a given path */ /* List the file names under a given path */
std::vector<string> name_gallery; std::vector<String> name_gallery;
listDir(src_dir.c_str(), name_gallery, false); listDir(src_dir.c_str(), name_gallery, false);
for (unsigned int i = 0; i < name_gallery.size(); i++) for (unsigned int i = 0; i < name_gallery.size(); i++)
{ {
...@@ -229,7 +229,7 @@ int main(int argc, char **argv) ...@@ -229,7 +229,7 @@ int main(int argc, char **argv)
int count_pre, num_rotate, max_rotate; int count_pre, num_rotate, max_rotate;
String titlename, Hint, Pred("prediction: "); String titlename, Hint, Pred("prediction: ");
vector<viz::WImageOverlay> imagepredict; vector<viz::WImageOverlay> imagepredict;
string widgename[24] = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"}; String widgename[24] = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
vector<Mat> slide; vector<Mat> slide;
slide.push_back(imread("1.png")); slide.push_back(imread("1.png"));
slide.push_back(imread("2.png")); slide.push_back(imread("2.png"));
......
./classify_test --src_dir=/Users/yidawang/Downloads/PASCAL3D+_release1.1/ImageCollection/ --network_forIMG=/Users/yidawang/Documents/buildboat/caffe/examples/triplet/pascal_triplet.prototxt --caffemodel=/Users/yidawang/Documents/buildboat/caffe/examples/triplet/pascal_triplet_iter_10000.caffemodel --gallery_out=1
\ No newline at end of file
#include "precomp.hpp" #include "precomp.hpp"
using namespace caffe; using namespace caffe;
using std::string;
namespace cv namespace cv
{ {
namespace cnn_3dobj namespace cnn_3dobj
{ {
descriptorExtractor::descriptorExtractor(const string& device_type, int device_id) descriptorExtractor::descriptorExtractor(const String& device_type, int device_id)
{ {
net_ready = 0; net_ready = 0;
if (strcmp(device_type.c_str(), "CPU") == 0 || strcmp(device_type.c_str(), "GPU") == 0) if (strcmp(device_type.c_str(), "CPU") == 0 || strcmp(device_type.c_str(), "GPU") == 0)
...@@ -34,9 +33,9 @@ namespace cnn_3dobj ...@@ -34,9 +33,9 @@ namespace cnn_3dobj
} }
}; };
string descriptorExtractor::getDeviceType() String descriptorExtractor::getDeviceType()
{ {
string device_info_out; String device_info_out;
device_info_out = deviceType; device_info_out = deviceType;
return device_info_out; return device_info_out;
}; };
...@@ -48,7 +47,7 @@ namespace cnn_3dobj ...@@ -48,7 +47,7 @@ namespace cnn_3dobj
return device_info_out; return device_info_out;
}; };
void descriptorExtractor::setDeviceType(const string& device_type) void descriptorExtractor::setDeviceType(const String& device_type)
{ {
if (strcmp(device_type.c_str(), "CPU") == 0 || strcmp(device_type.c_str(), "GPU") == 0) if (strcmp(device_type.c_str(), "CPU") == 0 || strcmp(device_type.c_str(), "GPU") == 0)
{ {
...@@ -85,7 +84,7 @@ namespace cnn_3dobj ...@@ -85,7 +84,7 @@ namespace cnn_3dobj
} }
}; };
void descriptorExtractor::loadNet(const string& model_file, const string& trained_file, const string& mean_file) void descriptorExtractor::loadNet(const String& model_file, const String& trained_file, const String& mean_file)
{ {
if (net_set) if (net_set)
{ {
...@@ -119,7 +118,7 @@ namespace cnn_3dobj ...@@ -119,7 +118,7 @@ namespace cnn_3dobj
}; };
/* Load the mean file in binaryproto format. */ /* Load the mean file in binaryproto format. */
void descriptorExtractor::setMean(const string& mean_file) void descriptorExtractor::setMean(const String& mean_file)
{ {
BlobProto blob_proto; BlobProto blob_proto;
ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto); ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
...@@ -147,7 +146,7 @@ namespace cnn_3dobj ...@@ -147,7 +146,7 @@ namespace cnn_3dobj
mean_ = cv::Mat(input_geometry, mean.type(), channel_mean); mean_ = cv::Mat(input_geometry, mean.type(), channel_mean);
}; };
void descriptorExtractor::extract(InputArrayOfArrays inputimg, OutputArray feature, std::string feature_blob) void descriptorExtractor::extract(InputArrayOfArrays inputimg, OutputArray feature, String feature_blob)
{ {
if (net_ready) if (net_ready)
{ {
......
...@@ -108,7 +108,7 @@ namespace cnn_3dobj ...@@ -108,7 +108,7 @@ namespace cnn_3dobj
subdivide(v12, v23, v31, depth - 1); subdivide(v12, v23, v31, depth - 1);
}; };
uint32_t icoSphere::swapEndian(uint32_t val) int icoSphere::swapEndian(int val)
{ {
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
return (val << 16) | (val >> 16); return (val << 16) | (val >> 16);
...@@ -175,7 +175,7 @@ namespace cnn_3dobj ...@@ -175,7 +175,7 @@ namespace cnn_3dobj
headerLabel.close(); headerLabel.close();
}; };
void icoSphere::writeBinaryfile(string filenameImg, const char* binaryPath, const char* headerPath, int num_item, int label_class, int x, int y, int z, int isrgb) void icoSphere::writeBinaryfile(String filenameImg, const char* binaryPath, const char* headerPath, int num_item, int label_class, int x, int y, int z, int isrgb)
{ {
cv::Mat ImgforBin = cv::imread(filenameImg, isrgb); cv::Mat ImgforBin = cv::imread(filenameImg, isrgb);
char* A0 = (char*)malloc(1024); char* A0 = (char*)malloc(1024);
......
...@@ -26,13 +26,13 @@ CV_CNN_Feature_Test::CV_CNN_Feature_Test() ...@@ -26,13 +26,13 @@ CV_CNN_Feature_Test::CV_CNN_Feature_Test()
*/ */
void CV_CNN_Feature_Test::run(int) void CV_CNN_Feature_Test::run(int)
{ {
string caffemodel = std::string(ts->get_data_path()) + "3d_triplet_iter_30000.caffemodel"; String caffemodel = String(ts->get_data_path()) + "3d_triplet_iter_30000.caffemodel";
string network_forIMG = cvtest::TS::ptr()->get_data_path() + "3d_triplet_testIMG.prototxt"; String network_forIMG = cvtest::TS::ptr()->get_data_path() + "3d_triplet_testIMG.prototxt";
string mean_file = "no"; String mean_file = "no";
std::vector<string> ref_img; std::vector<String> ref_img;
string target_img = std::string(ts->get_data_path()) + "1_8.png"; String target_img = String(ts->get_data_path()) + "1_8.png";
string feature_blob = "feat"; String feature_blob = "feat";
string device = "CPU"; String device = "CPU";
int dev_id = 0; int dev_id = 0;
cv::Mat img_base = cv::imread(target_img, -1); cv::Mat img_base = cv::imread(target_img, -1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment