9 #include <opencv2/opencv.hpp> 16 #include "caffe2/core/init.h" 17 #include "caffe2/proto/caffe2.pb.h" 18 #include "caffe2/core/logging.h" 19 #include "leveldb/db.h" 20 #include "leveldb/write_batch.h" 22 CAFFE2_DEFINE_string(input_db_name,
"",
"The input image file name.");
23 CAFFE2_DEFINE_string(output_db_name,
"",
"The output training leveldb name.");
24 CAFFE2_DEFINE_bool(color,
true,
"If set, load images in color.");
25 CAFFE2_DEFINE_int(scale, 256,
26 "If caffe2::FLAGS_raw is set, scale all the images' shorter edge to the given " 28 CAFFE2_DEFINE_bool(warp,
false,
"If warp is set, warp the images to square.");
34 using std::unique_ptr;
36 void ConvertToRawDataset(
37 const string& input_db_name,
const string& output_db_name) {
39 std::unique_ptr<leveldb::DB> input_db;
40 LOG(INFO) <<
"Opening input leveldb " << input_db_name;
42 leveldb::Options options;
43 options.create_if_missing =
false;
45 leveldb::Status status = leveldb::DB::Open(
46 options, input_db_name, &db_temp);
47 CAFFE_ENFORCE(status.ok(),
"Failed to open leveldb ", input_db_name,
".");
48 input_db.reset(db_temp);
52 std::unique_ptr<leveldb::DB> output_db;
53 std::unique_ptr<leveldb::WriteBatch> batch;
54 LOG(INFO) <<
"Opening leveldb " << output_db_name;
56 leveldb::Options options;
57 options.error_if_exists =
true;
58 options.create_if_missing =
true;
59 options.write_buffer_size = 268435456;
61 leveldb::Status status = leveldb::DB::Open(
62 options, output_db_name, &db_temp);
65 "Failed to open leveldb ",
67 ". Is it already existing?");
68 output_db.reset(db_temp);
70 batch.reset(
new leveldb::WriteBatch());
72 TensorProtos input_protos;
73 TensorProtos output_protos;
74 TensorProto* data = output_protos.add_protos();
75 TensorProto* label = output_protos.add_protos();
76 data->set_data_type(TensorProto::BYTE);
79 if (caffe2::FLAGS_color) {
84 unique_ptr<leveldb::Iterator> iter;
85 iter.reset(input_db->NewIterator(leveldb::ReadOptions()));
88 for (; iter->Valid(); iter->Next()) {
89 CAFFE_ENFORCE(input_protos.ParseFromString(iter->value().ToString()));
90 label->CopyFrom(input_protos.protos(1));
91 const string& encoded_image = input_protos.protos(0).string_data(0);
92 int encoded_size = encoded_image.size();
93 cv::Mat img = cv::imdecode(
94 cv::Mat(1, &encoded_size, CV_8UC1,
95 const_cast<char*>(encoded_image.data())),
96 caffe2::FLAGS_color ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE);
98 int scaled_width, scaled_height;
99 if (caffe2::FLAGS_warp) {
100 scaled_width = caffe2::FLAGS_scale;
101 scaled_height = caffe2::FLAGS_scale;
102 }
else if (img.rows > img.cols) {
103 scaled_width = caffe2::FLAGS_scale;
104 scaled_height =
static_cast<float>(img.rows) * caffe2::FLAGS_scale / img.cols;
106 scaled_height = caffe2::FLAGS_scale;
107 scaled_width =
static_cast<float>(img.cols) * caffe2::FLAGS_scale / img.rows;
109 cv::resize(img, resized_img, cv::Size(scaled_width, scaled_height), 0, 0,
111 data->set_dims(0, scaled_height);
112 data->set_dims(1, scaled_width);
113 DCHECK(resized_img.isContinuous());
114 data->set_byte_data(resized_img.ptr(),
115 scaled_height * scaled_width * (caffe2::FLAGS_color ? 3 : 1));
116 output_protos.SerializeToString(&value);
118 batch->Put(iter->key(), value);
119 if (++count % 1000 == 0) {
120 output_db->Write(leveldb::WriteOptions(), batch.get());
121 batch.reset(
new leveldb::WriteBatch());
122 LOG(INFO) <<
"Processed " << count <<
" files.";
126 if (count % 1000 != 0) {
127 output_db->Write(leveldb::WriteOptions(), batch.get());
129 LOG(INFO) <<
"Processed a total of " << count <<
" files.";
135 int main(
int argc,
char** argv) {
137 caffe2::ConvertToRawDataset(
138 caffe2::FLAGS_input_db_name, caffe2::FLAGS_output_db_name);
bool GlobalInit(int *pargc, char ***pargv)
Initialize the global environment of caffe2.
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...