Commit db396b9f authored by Wangyida's avatar Wangyida

add data transmission functions in DataTrans Class

parent 391596a1
......@@ -45,15 +45,24 @@ the use of this software, even if advised of the possibility of such damage.
#ifndef __OPENCV_CNN_3DOBJ_HPP__
#define __OPENCV_CNN_3DOBJ_HPP__
#ifdef __cplusplus
#include <glog/logging.h>
#include <leveldb/db.h>
#include <caffe/proto/caffe.pb.h>
#include <opencv2/calib3d.hpp>
#include <opencv2/viz/vizcore.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
//#include <opencv2/imgproc/imgproc.hpp>
#include <string>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <string>
#include <set>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
using std::string;
/** @defgroup cnn_3dobj CNN based on Caffe aimming at 3D object recognition and pose estimation
*/
......@@ -113,8 +122,21 @@ class CV_EXPORTS_W IcoSphere
*/
};
//! @}
class CV_EXPORTS_W DataTrans
{
private:
std::set<string> all_class_name;
std::map<string,int> class2id;
public:
DataTrans();
CV_WRAP void list_dir(const char *path,std::vector<string>& files,bool r);
CV_WRAP string get_classname(string path);
CV_WRAP int get_labelid(string fileName);
CV_WRAP void loadimg(string path,char* buffer,bool is_color);
CV_WRAP void convert(string imgdir,string outputdb,string attachdir,int channel,int width,int height);
};
//! @}
}}
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -7,3 +7,7 @@ set(SOURCES sphereview_3dobj_demo.cpp)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(sphereview_test ${SOURCES})
target_link_libraries(sphereview_test ${OpenCV_LIBS})
set(SOURCES2 images2db_demo.cpp)
add_executable(images2db_test ${SOURCES2})
target_link_libraries(images2db_test ${OpenCV_LIBS})
#include <stdio.h> // for snprintf
#include <string>
#include <vector>
#include "boost/algorithm/string.hpp"
#include "google/protobuf/text_format.h"
#define CPU_ONLY
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/net.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp"
#include "caffe/util/io.hpp"
#include "caffe/vision_layers.hpp"
using caffe::Blob;
using caffe::Caffe;
using caffe::Datum;
using caffe::Net;
using boost::shared_ptr;
using std::string;
namespace db = caffe::db;
template<typename Dtype>
int feature_extraction_pipeline(int argc, char** argv);
int main(int argc, char** argv) {
return feature_extraction_pipeline<float>(argc, argv);
// return feature_extraction_pipeline<double>(argc, argv);
}
template<typename Dtype>
int feature_extraction_pipeline(int argc, char** argv) {
::google::InitGoogleLogging(argv[0]);
const int num_required_args = 7;
/*if (argc < num_required_args) {
LOG(ERROR)<<
"This program takes in a trained network and an input data layer, and then"
" extract features of the input data produced by the net.\n"
"Usage: extract_features pretrained_net_param"
" feature_extraction_proto_file extract_feature_blob_name1[,name2,...]"
" save_feature_dataset_name1[,name2,...] num_mini_batches db_type"
" [CPU/GPU] [DEVICE_ID=0]\n"
"Note: you can extract multiple features in one pass by specifying"
" multiple feature blob names and dataset names seperated by ','."
" The names cannot contain white space characters and the number of blobs"
" and datasets must be equal.";
return 1;
}*/
int arg_pos = num_required_args;
arg_pos = num_required_args;
//if (argc > arg_pos && strcmp(argv[arg_pos], "GPU") == 0) {
if (argc > arg_pos && strcmp("CPU", "GPU") == 0) {
LOG(ERROR)<< "Using GPU";
int device_id = 0;
if (argc > arg_pos + 1) {
device_id = atoi(argv[arg_pos + 1]);
CHECK_GE(device_id, 0);
}
LOG(ERROR) << "Using Device_id=" << device_id;
Caffe::SetDevice(device_id);
Caffe::set_mode(Caffe::GPU);
} else {
LOG(ERROR) << "Using CPU";
Caffe::set_mode(Caffe::CPU);
}
arg_pos = 0; // the name of the executable
//std::string pretrained_binary_proto(argv[++arg_pos]);
std::string pretrained_binary_proto("/home/wangyida/Desktop/caffe/examples/triplet/3d_triplet_iter_200.caffemodel");
// Expected prototxt contains at least one data layer such as
// the layer data_layer_name and one feature blob such as the
// fc7 top blob to extract features.
/*
layers {
name: "data_layer_name"
type: DATA
data_param {
source: "/path/to/your/images/to/extract/feature/images_leveldb"
mean_file: "/path/to/your/image_mean.binaryproto"
batch_size: 128
crop_size: 227
mirror: false
}
top: "data_blob_name"
top: "label_blob_name"
}
layers {
name: "drop7"
type: DROPOUT
dropout_param {
dropout_ratio: 0.5
}
bottom: "fc7"
top: "fc7"
}
*/
//std::string feature_extraction_proto(argv[++arg_pos]);
std::string feature_extraction_proto("/home/wangyida/Desktop/caffe/examples/triplet/3d_triplet.prototxt");
boost::shared_ptr<Net<Dtype> > feature_extraction_net(
new Net<Dtype>(feature_extraction_proto, caffe::TEST));
feature_extraction_net->CopyTrainedLayersFrom(pretrained_binary_proto);
//std::string extract_feature_blob_names(argv[++arg_pos]);
std::string extract_feature_blob_names("feat");
std::vector<std::string> blob_names;
boost::split(blob_names, extract_feature_blob_names, boost::is_any_of(","));
//std::string save_feature_dataset_names(argv[++arg_pos]);
std::string save_feature_dataset_names("/home/wangyida/Desktop/caffe/examples/triplet/feature_extracted");
std::vector<std::string> dataset_names;
boost::split(dataset_names, save_feature_dataset_names,
boost::is_any_of(","));
CHECK_EQ(blob_names.size(), dataset_names.size()) <<
" the number of blob names and dataset names must be equal";
size_t num_features = blob_names.size();
for (size_t i = 0; i < num_features; i++) {
CHECK(feature_extraction_net->has_blob(blob_names[i]))
<< "Unknown feature blob name " << blob_names[i]
<< " in the network " << feature_extraction_proto;
}
//int num_mini_batches = atoi(argv[++arg_pos]);
int num_mini_batches = atoi("6");
/*std::vector<shared_ptr<db::DB> > feature_dbs;
std::vector<shared_ptr<db::Transaction> > txns;
const char* db_type = argv[++arg_pos];
for (size_t i = 0; i < num_features; ++i) {
LOG(INFO)<< "Opening dataset " << dataset_names[i];
shared_ptr<db::DB> db(db::GetDB(db_type));
db->Open(dataset_names.at(i), db::NEW);
feature_dbs.push_back(db);
shared_ptr<db::Transaction> txn(db->NewTransaction());
txns.push_back(txn);
}*/
std::vector<FILE*> files;
for (size_t i = 0; i < num_features; ++i)
{
LOG(INFO) << "Opening file " << dataset_names[i];
FILE * temp = NULL;
temp = fopen(dataset_names[i].c_str(), "wb");
files.push_back(temp);
}
LOG(ERROR)<< "Extacting Features";
//bool header_flag = true;
Datum datum;
//const int kMaxKeyStrLength = 100;
//char key_str[kMaxKeyStrLength];
std::vector<Blob<float>*> input_vec;
std::vector<int> image_indices(num_features, 0);
for (int batch_index = 0; batch_index < num_mini_batches; ++batch_index) {
feature_extraction_net->Forward(input_vec);
for (int i = 0; i < num_features; ++i) {
const boost::shared_ptr<Blob<Dtype> > feature_blob = feature_extraction_net
->blob_by_name(blob_names[i]);
int batch_size = feature_blob->num();
int dim_features = feature_blob->count() / batch_size;
if (batch_index == 0)
{
int fea_num = batch_size*num_mini_batches;
fwrite(&dim_features, sizeof(int), 1, files[i]);
fwrite(&fea_num, sizeof(int), 1, files[i]);
//bool header_flag = false;
}
const Dtype* feature_blob_data;
for (int n = 0; n < batch_size; ++n) {
feature_blob_data = feature_blob->cpu_data() +
feature_blob->offset(n);
fwrite(feature_blob_data, sizeof(Dtype), dim_features, files[i]);
++image_indices[i];
if (image_indices[i] % 1000 == 0) {
LOG(ERROR)<< "Extracted features of " << image_indices[i] <<
" query images for feature blob " << blob_names[i];
}
} // for (int n = 0; n < batch_size; ++n)
} // for (int i = 0; i < num_features; ++i)
} // for (int batch_index = 0; batch_index < num_mini_batches; ++batch_index)
// write the last batch
for (int i = 0; i < num_features; ++i) {
/* if (image_indices[i] % 1000 != 0) {
txns.at(i)->Commit();
}
LOG(ERROR)<< "Extracted features of " << image_indices[i] <<
" query images for feature blob " << blob_names[i];
feature_dbs.at(i)->Close();*/
fclose(files[i]);
}
LOG(ERROR)<< "Successfully extracted the features!";
return 0;
}
#include <google/protobuf/text_format.h>
#include <glog/logging.h>
#include <leveldb/db.h>
#include <stdint.h>
#include <fstream> // NOLINT(readability/streams)
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include "caffe/proto/caffe.pb.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc.hpp>
using std::string;
using namespace std;
set<string> all_class_name;
map<string,int> class2id;
void list_dir(const char *path,vector<string>& files,bool r = false)
{
DIR *pDir;
struct dirent *ent;
char childpath[512];
pDir = opendir(path);
memset(childpath, 0, sizeof(childpath));
while ((ent = readdir(pDir)) != NULL)
{
if (ent->d_type & DT_DIR)
{
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
{
continue;
}
if(r)
{
sprintf(childpath, "%s/%s", path, ent->d_name);
list_dir(childpath,files);
}
}
else
{
files.push_back(ent->d_name);
}
}
sort(files.begin(),files.end());
}
string get_classname(string path)
{
int index = path.find_last_of('_');
return path.substr(0, index);
}
int get_labelid(string fileName)
{
string class_name_tmp = get_classname(fileName);
all_class_name.insert(class_name_tmp);
map<string,int>::iterator name_iter_tmp = class2id.find(class_name_tmp);
if (name_iter_tmp == class2id.end())
{
int id = class2id.size();
class2id.insert(name_iter_tmp, std::make_pair(class_name_tmp, id));
return id;
}
else
{
return name_iter_tmp->second;
}
}
void loadimg(string path,char* buffer)
{
cv::Mat img = cv::imread(path, CV_LOAD_IMAGE_COLOR);
string val;
int rows = img.rows;
int cols = img.cols;
int pos=0;
for (int c = 0; c < 3; c++)
{
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
buffer[pos++]=img.at<cv::Vec3b>(row,col)[c];
}
}
}
}
void convert(string imgdir,string outputdb,string attachdir,int channel,int width,int height)
{
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
options.error_if_exists = true;
caffe::Datum datum;
datum.set_channels(channel);
datum.set_height(height);
datum.set_width(width);
int image_size = channel*width*height;
char buffer[image_size];
string value;
CHECK(leveldb::DB::Open(options, outputdb, &db).ok());
vector<string> filenames;
list_dir(imgdir.c_str(),filenames);
string img_log = attachdir+"image_filename";
ofstream writefile(img_log.c_str());
for(int i=0;i<filenames.size();i++)
{
string path= imgdir;
path.append(filenames[i]);
loadimg(path,buffer);
int labelid = get_labelid(filenames[i]);
datum.set_label(labelid);
datum.set_data(buffer,image_size);
datum.SerializeToString(&value);
snprintf(buffer, image_size, "%05d", i);
printf("\nclassid:%d classname:%s abspath:%s",labelid,get_classname(filenames[i]).c_str(),path.c_str());
db->Put(leveldb::WriteOptions(),string(buffer),value);
//printf("%d %s\n",i,fileNames[i].c_str());
assert(writefile.is_open());
writefile<<i<<" "<<filenames[i]<<"\n";
}
delete db;
writefile.close();
img_log = attachdir+"image_classname";
writefile.open(img_log.c_str());
set<string>::iterator iter = all_class_name.begin();
while(iter != all_class_name.end())
{
assert(writefile.is_open());
writefile<<(*iter)<<"\n";
//printf("%s\n",(*iter).c_str());
iter++;
}
writefile.close();
}
int main(int argc, char** argv)
{
if (argc < 6)
{
LOG(ERROR) << "convert_imagedata src_dir dst_dir attach_dir channel width height";
return 0;
}
//./convert_imagedata.bin /home/linger/imdata/collarTest/ /home/linger/linger/testfile/dbtest/ /home/linger/linger/testfile/test_attachment/ 3 250 250
// ./convert_imagedata.bin /home/linger/imdata/collar_train/ /home/linger/linger/testfile/crop_train_db/ /home/linger/linger/testfile/crop_train_attachment/ 3 50 50
google::InitGoogleLogging(argv[0]);
string src_dir = argv[1];
string src_dst = argv[2];
string attach_dir = argv[3];
int channel = atoi(argv[4]);
int width = atoi(argv[5]);
int height = atoi(argv[6]);
//for test
/*
src_dir = "/home/linger/imdata/collarTest/";
src_dst = "/home/linger/linger/testfile/dbtest/";
attach_dir = "/home/linger/linger/testfile/";
channel = 3;
width = 250;
height = 250;
*/
convert(src_dir,src_dst,attach_dir,channel,width,height);
}
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2009, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <opencv2/cnn_3dobj.hpp>
using namespace cv;
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.}"
"{src_dir | | Source direction of the images ready for being converted to leveldb dataset.}"
"{src_dst | | Aim direction of the converted to leveldb dataset. }"
"{attach_dir | | Path for saving additional files which describe the transmission results. }"
"{channel | 1 | Channel of the images. }"
"{width | 64 | Width of images}"
"{height | 64 | Height of images}";
cv::CommandLineParser parser(argc, argv, keys);
parser.about("Demo for Sphere View data generation");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
string src_dir = parser.get<string>("src_dir");
string src_dst = parser.get<string>("src_dst");
string attach_dir = parser.get<string>("attach_dir");
int channel = parser.get<int>("channel");
int width = parser.get<int>("width");
int height = parser.get<int>("height");
cv::cnn_3dobj::DataTrans transTemp();
transTemp.convert(src_dir,src_dst,attach_dir,channel,width,height);
}
......@@ -112,9 +112,12 @@ int main(int argc, char *argv[]){
if (camera_pov)
myWindow.setViewerPose(cam_pose);
char* temp = new char;
sprintf (temp,"%d",pose);
sprintf (temp,"%d",label_class);
string filename = temp;
filename += "_";
filename = imagedir + filename;
sprintf (temp,"%d",pose);
filename += temp;
filename += ".png";
myWindow.saveScreenshot(filename);
ViewSphere.writeBinaryfile(filename, binaryPath, headerPath,(int)campos.size()*num_class, label_class);
......
#include "precomp.hpp"
using std::string;
using namespace std;
namespace cv
{
namespace cnn_3dobj
{
DataTrans::DataTrans()
{
};
void DataTrans::list_dir(const char *path,vector<string>& files,bool r)
{
DIR *pDir;
struct dirent *ent;
char childpath[512];
pDir = opendir(path);
memset(childpath, 0, sizeof(childpath));
while ((ent = readdir(pDir)) != NULL)
{
if (ent->d_type & DT_DIR)
{
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
{
continue;
}
if(r)
{
sprintf(childpath, "%s/%s", path, ent->d_name);
list_dir(childpath,files,false);
}
}
else
{
files.push_back(ent->d_name);
}
}
sort(files.begin(),files.end());
};
string DataTrans::get_classname(string path)
{
int index = path.find_last_of('_');
return path.substr(0, index);
}
int DataTrans::get_labelid(string fileName)
{
string class_name_tmp = get_classname(fileName);
all_class_name.insert(class_name_tmp);
map<string,int>::iterator name_iter_tmp = class2id.find(class_name_tmp);
if (name_iter_tmp == class2id.end())
{
int id = class2id.size();
class2id.insert(name_iter_tmp, std::make_pair(class_name_tmp, id));
return id;
}
else
{
return name_iter_tmp->second;
}
}
void DataTrans::loadimg(string path,char* buffer,const bool is_color)
{
cv::Mat img = cv::imread(path, is_color);
string val;
int rows = img.rows;
int cols = img.cols;
int pos=0;
int channel;
if (is_color == 0)
{
channel = 1;
}else{
channel = 3;
}
for (int c = 0; c < channel; c++)
{
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
buffer[pos++]=img.at<cv::Vec3b>(row,col)[c];
}
}
}
};
void DataTrans::convert(string imgdir,string outputdb,string attachdir,int channel,int width,int height)
{
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
options.error_if_exists = true;
caffe::Datum datum;
datum.set_channels(channel);
datum.set_height(height);
datum.set_width(width);
int image_size = channel*width*height;
char buffer[image_size];
string value;
CHECK(leveldb::DB::Open(options, outputdb, &db).ok());
vector<string> filenames;
list_dir(imgdir.c_str(),filenames, false);
string img_log = attachdir+"image_filename";
ofstream writefile(img_log.c_str());
for(int i=0;i<(int)filenames.size();i++)
{
string path= imgdir;
path.append(filenames[i]);
loadimg(path,buffer,false);
int labelid = get_labelid(filenames[i]);
datum.set_label(labelid);
datum.set_data(buffer,image_size);
datum.SerializeToString(&value);
snprintf(buffer, image_size, "%05d", i);
printf("\nclassid:%d classname:%s abspath:%s",labelid,get_classname(filenames[i]).c_str(),path.c_str());
db->Put(leveldb::WriteOptions(),string(buffer),value);
//printf("%d %s\n",i,fileNames[i].c_str());
assert(writefile.is_open());
writefile<<i<<" "<<filenames[i]<<"\n";
}
delete db;
writefile.close();
img_log = attachdir+"image_classname";
writefile.open(img_log.c_str());
set<string>::iterator iter = all_class_name.begin();
while(iter != all_class_name.end())
{
assert(writefile.is_open());
writefile<<(*iter)<<"\n";
//printf("%s\n",(*iter).c_str());
iter++;
}
writefile.close();
};
}}
......@@ -6,7 +6,6 @@ namespace cv
{
namespace cnn_3dobj
{
IcoSphere::IcoSphere(float radius_in, int depth_in)
{
......@@ -50,7 +49,7 @@ namespace cnn_3dobj
for(int i=0; i < (int)CameraPos.size(); i++) {
cout << CameraPos.at(i).x <<' '<< CameraPos.at(i).y << ' ' << CameraPos.at(i).z << endl;
}
}
};
void IcoSphere::norm(float v[])
{
......@@ -65,7 +64,7 @@ namespace cnn_3dobj
for (int i = 0; i < 3; ++i) {
v[i] /= ((float)len);
}
}
};
void IcoSphere::add(float v[])
{
......@@ -78,7 +77,7 @@ namespace cnn_3dobj
}
temp_Campos.x = temp->at(0);temp_Campos.y = temp->at(1);temp_Campos.z = temp->at(2);
CameraPos.push_back(temp_Campos);
}
};
void IcoSphere::subdivide(float v1[], float v2[], float v3[], int depth)
{
......@@ -111,13 +110,13 @@ namespace cnn_3dobj
subdivide(v2, v23, v12, depth - 1);
subdivide(v3, v31, v23, depth - 1);
subdivide(v12, v23, v31, depth - 1);
}
};
uint32_t IcoSphere::swap_endian(uint32_t val)
{
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
return (val << 16) | (val >> 16);
}
};
cv::Point3d IcoSphere::getCenter(cv::Mat cloud)
{
......@@ -133,7 +132,7 @@ namespace cnn_3dobj
dataout.y = dataout.y/cloud.cols;
dataout.z = dataout.z/cloud.cols;
return dataout;
}
};
float IcoSphere::getRadius(cv::Mat cloud, cv::Point3d center)
{
......
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