25 #include <opencv2/opencv.hpp> 32 #include "caffe2/core/init.h" 33 #include "caffe2/proto/caffe2_pb.h" 34 #include "caffe2/core/logging.h" 35 #include "leveldb/db.h" 36 #include "leveldb/write_batch.h" 38 C10_DEFINE_string(input_db_name,
"",
"The input image file name.");
39 C10_DEFINE_string(output_db_name,
"",
"The output training leveldb name.");
40 C10_DEFINE_bool(color,
true,
"If set, load images in color.");
44 "If FLAGS_raw is set, scale all the images' shorter edge to the given " 46 C10_DEFINE_bool(warp,
false,
"If warp is set, warp the images to square.");
51 using std::unique_ptr;
53 void ConvertToRawDataset(
54 const string& input_db_name,
const string& output_db_name) {
56 std::unique_ptr<leveldb::DB> input_db;
57 LOG(INFO) <<
"Opening input leveldb " << input_db_name;
59 leveldb::Options options;
60 options.create_if_missing =
false;
62 leveldb::Status status = leveldb::DB::Open(
63 options, input_db_name, &db_temp);
64 CAFFE_ENFORCE(status.ok(),
"Failed to open leveldb ", input_db_name,
".");
65 input_db.reset(db_temp);
69 std::unique_ptr<leveldb::DB> output_db;
70 std::unique_ptr<leveldb::WriteBatch> batch;
71 LOG(INFO) <<
"Opening leveldb " << output_db_name;
73 leveldb::Options options;
74 options.error_if_exists =
true;
75 options.create_if_missing =
true;
76 options.write_buffer_size = 268435456;
78 leveldb::Status status = leveldb::DB::Open(
79 options, output_db_name, &db_temp);
82 "Failed to open leveldb ",
84 ". Is it already existing?");
85 output_db.reset(db_temp);
87 batch.reset(
new leveldb::WriteBatch());
89 TensorProtos input_protos;
90 TensorProtos output_protos;
91 TensorProto* data = output_protos.add_protos();
92 TensorProto* label = output_protos.add_protos();
93 data->set_data_type(TensorProto::BYTE);
101 unique_ptr<leveldb::Iterator> iter;
102 iter.reset(input_db->NewIterator(leveldb::ReadOptions()));
105 for (; iter->Valid(); iter->Next()) {
106 CAFFE_ENFORCE(input_protos.ParseFromString(iter->value().ToString()));
107 label->CopyFrom(input_protos.protos(1));
108 const string& encoded_image = input_protos.protos(0).string_data(0);
109 int encoded_size = encoded_image.size();
110 cv::Mat img = cv::imdecode(
112 1, &encoded_size, CV_8UC1, const_cast<char*>(encoded_image.data())),
113 FLAGS_color ? cv::IMREAD_COLOR : cv::IMREAD_GRAYSCALE);
115 int scaled_width, scaled_height;
117 scaled_width = FLAGS_scale;
118 scaled_height = FLAGS_scale;
119 }
else if (img.rows > img.cols) {
120 scaled_width = FLAGS_scale;
121 scaled_height =
static_cast<float>(img.rows) * FLAGS_scale / img.cols;
123 scaled_height = FLAGS_scale;
124 scaled_width =
static_cast<float>(img.cols) * FLAGS_scale / img.rows;
126 cv::resize(img, resized_img, cv::Size(scaled_width, scaled_height), 0, 0,
128 data->set_dims(0, scaled_height);
129 data->set_dims(1, scaled_width);
130 DCHECK(resized_img.isContinuous());
133 scaled_height * scaled_width * (FLAGS_color ? 3 : 1));
134 output_protos.SerializeToString(&value);
136 batch->Put(iter->key(), value);
137 if (++count % 1000 == 0) {
138 output_db->Write(leveldb::WriteOptions(), batch.get());
139 batch.reset(
new leveldb::WriteBatch());
140 LOG(INFO) <<
"Processed " << count <<
" files.";
144 if (count % 1000 != 0) {
145 output_db->Write(leveldb::WriteOptions(), batch.get());
147 LOG(INFO) <<
"Processed a total of " << count <<
" files.";
153 int main(
int argc,
char** argv) {
155 caffe2::ConvertToRawDataset(FLAGS_input_db_name, FLAGS_output_db_name);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
bool GlobalInit(int *pargc, char ***pargv)
Initialize the global environment of caffe2.