16
16
17
17
// This script converts an image dataset to leveldb.
18
18
//
19
- // caffe2 ::FLAGS_input_folder is the root folder that holds all the images, and
20
- // caffe2 ::FLAGS_list_file should be a list of files as well as their labels, in the
21
- // format as
19
+ // c10 ::FLAGS_input_folder is the root folder that holds all the images, and
20
+ // c10 ::FLAGS_list_file should be a list of files as well as their labels, in
21
+ // the format as
22
22
// subfolder1/file1.JPEG 7
23
23
// ....
24
24
35
35
#include " leveldb/db.h"
36
36
#include " leveldb/write_batch.h"
37
37
38
- CAFFE2_DEFINE_string (input_db_name, " " , " The input image file name." );
39
- CAFFE2_DEFINE_string (output_db_name, " " , " The output training leveldb name." );
40
- CAFFE2_DEFINE_bool (color, true , " If set, load images in color." );
41
- CAFFE2_DEFINE_int (scale, 256 ,
42
- " If caffe2::FLAGS_raw is set, scale all the images' shorter edge to the given "
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." );
41
+ C10_DEFINE_int (
42
+ scale,
43
+ 256 ,
44
+ " If c10::FLAGS_raw is set, scale all the images' shorter edge to the given "
43
45
" value." );
44
- CAFFE2_DEFINE_bool (warp, false , " If warp is set, warp the images to square." );
45
-
46
+ C10_DEFINE_bool (warp, false , " If warp is set, warp the images to square." );
46
47
47
48
namespace caffe2 {
48
49
@@ -92,7 +93,7 @@ void ConvertToRawDataset(
92
93
data->set_data_type (TensorProto::BYTE);
93
94
data->add_dims (0 );
94
95
data->add_dims (0 );
95
- if (caffe2 ::FLAGS_color) {
96
+ if (c10 ::FLAGS_color) {
96
97
data->add_dims (3 );
97
98
}
98
99
string value;
@@ -107,28 +108,30 @@ void ConvertToRawDataset(
107
108
const string& encoded_image = input_protos.protos (0 ).string_data (0 );
108
109
int encoded_size = encoded_image.size ();
109
110
cv::Mat img = cv::imdecode (
110
- cv::Mat (1 , &encoded_size, CV_8UC1,
111
- const_cast <char *>(encoded_image.data ())),
112
- caffe2 ::FLAGS_color ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE);
111
+ cv::Mat (
112
+ 1 , &encoded_size, CV_8UC1, const_cast <char *>(encoded_image.data ())),
113
+ c10 ::FLAGS_color ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE);
113
114
cv::Mat resized_img;
114
115
int scaled_width, scaled_height;
115
- if (caffe2 ::FLAGS_warp) {
116
- scaled_width = caffe2 ::FLAGS_scale;
117
- scaled_height = caffe2 ::FLAGS_scale;
116
+ if (c10 ::FLAGS_warp) {
117
+ scaled_width = c10 ::FLAGS_scale;
118
+ scaled_height = c10 ::FLAGS_scale;
118
119
} else if (img.rows > img.cols ) {
119
- scaled_width = caffe2::FLAGS_scale;
120
- scaled_height = static_cast <float >(img.rows ) * caffe2::FLAGS_scale / img.cols ;
120
+ scaled_width = c10::FLAGS_scale;
121
+ scaled_height =
122
+ static_cast <float >(img.rows ) * c10::FLAGS_scale / img.cols ;
121
123
} else {
122
- scaled_height = caffe2 ::FLAGS_scale;
123
- scaled_width = static_cast <float >(img.cols ) * caffe2 ::FLAGS_scale / img.rows ;
124
+ scaled_height = c10 ::FLAGS_scale;
125
+ scaled_width = static_cast <float >(img.cols ) * c10 ::FLAGS_scale / img.rows ;
124
126
}
125
127
cv::resize (img, resized_img, cv::Size (scaled_width, scaled_height), 0 , 0 ,
126
128
cv::INTER_LINEAR);
127
129
data->set_dims (0 , scaled_height);
128
130
data->set_dims (1 , scaled_width);
129
131
DCHECK (resized_img.isContinuous ());
130
- data->set_byte_data (resized_img.ptr (),
131
- scaled_height * scaled_width * (caffe2::FLAGS_color ? 3 : 1 ));
132
+ data->set_byte_data (
133
+ resized_img.ptr (),
134
+ scaled_height * scaled_width * (c10::FLAGS_color ? 3 : 1 ));
132
135
output_protos.SerializeToString (&value);
133
136
// Put in db
134
137
batch->Put (iter->key (), value);
@@ -151,6 +154,6 @@ void ConvertToRawDataset(
151
154
int main (int argc, char ** argv) {
152
155
caffe2::GlobalInit (&argc, &argv);
153
156
caffe2::ConvertToRawDataset (
154
- caffe2 ::FLAGS_input_db_name, caffe2 ::FLAGS_output_db_name);
157
+ c10 ::FLAGS_input_db_name, c10 ::FLAGS_output_db_name);
155
158
return 0 ;
156
159
}
0 commit comments