3737
3838from dataset import tfrecord_util
3939
40-
4140flags .DEFINE_string ('data_dir' , '' , 'Root directory to raw PASCAL VOC dataset.' )
4241flags .DEFINE_string ('set' , 'train' , 'Convert training set, validation set or '
4342 'merged set.' )
5756YEARS = ['VOC2007' , 'VOC2012' , 'merged' ]
5857
5958pascal_label_map_dict = {
60- 'background' : 0 , 'aeroplane' : 1 , 'bicycle' : 2 , 'bird' : 3 , 'boat' : 4 ,
61- 'bottle' : 5 , 'bus' : 6 , 'car' : 7 , 'cat' : 8 , 'chair' : 9 , 'cow' : 10 ,
62- 'diningtable' : 11 , 'dog' : 12 , 'horse' : 13 , 'motorbike' : 14 , 'person' : 15 ,
63- 'pottedplant' : 16 , 'sheep' : 17 , 'sofa' : 18 , 'train' : 19 , 'tvmonitor' : 20 ,
59+ 'background' : 0 ,
60+ 'aeroplane' : 1 ,
61+ 'bicycle' : 2 ,
62+ 'bird' : 3 ,
63+ 'boat' : 4 ,
64+ 'bottle' : 5 ,
65+ 'bus' : 6 ,
66+ 'car' : 7 ,
67+ 'cat' : 8 ,
68+ 'chair' : 9 ,
69+ 'cow' : 10 ,
70+ 'diningtable' : 11 ,
71+ 'dog' : 12 ,
72+ 'horse' : 13 ,
73+ 'motorbike' : 14 ,
74+ 'person' : 15 ,
75+ 'pottedplant' : 16 ,
76+ 'sheep' : 17 ,
77+ 'sofa' : 18 ,
78+ 'train' : 19 ,
79+ 'tvmonitor' : 20 ,
6480}
6581
66-
6782GLOBAL_IMG_ID = 0 # global image id.
6883GLOBAL_ANN_ID = 0 # global annotation id.
6984
@@ -101,14 +116,14 @@ def dict_to_tf_example(data,
101116 by the raw data.
102117
103118 Args:
104- data: dict holding PASCAL XML fields for a single image (obtained by
105- running tfrecord_util.recursive_parse_xml_to_dict)
119+ data: dict holding PASCAL XML fields for a single image (obtained by running
120+ tfrecord_util.recursive_parse_xml_to_dict)
106121 dataset_directory: Path to root directory holding PASCAL dataset
107122 label_map_dict: A map from string label names to integers ids.
108123 ignore_difficult_instances: Whether to skip difficult instances in the
109124 dataset (default: False).
110- image_subdirectory: String specifying subdirectory within the
111- PASCAL dataset directory holding the actual image data.
125+ image_subdirectory: String specifying subdirectory within the PASCAL dataset
126+ directory holding the actual image data.
112127 ann_json_dict: annotation json dictionary.
113128
114129 Returns:
@@ -184,26 +199,42 @@ def dict_to_tf_example(data,
184199 }
185200 ann_json_dict ['annotations' ].append (ann )
186201
187- example = tf .train .Example (features = tf .train .Features (feature = {
188- 'image/height' : tfrecord_util .int64_feature (height ),
189- 'image/width' : tfrecord_util .int64_feature (width ),
190- 'image/filename' : tfrecord_util .bytes_feature (
191- data ['filename' ].encode ('utf8' )),
192- 'image/source_id' : tfrecord_util .bytes_feature (
193- str (image_id ).encode ('utf8' )),
194- 'image/key/sha256' : tfrecord_util .bytes_feature (key .encode ('utf8' )),
195- 'image/encoded' : tfrecord_util .bytes_feature (encoded_jpg ),
196- 'image/format' : tfrecord_util .bytes_feature ('jpeg' .encode ('utf8' )),
197- 'image/object/bbox/xmin' : tfrecord_util .float_list_feature (xmin ),
198- 'image/object/bbox/xmax' : tfrecord_util .float_list_feature (xmax ),
199- 'image/object/bbox/ymin' : tfrecord_util .float_list_feature (ymin ),
200- 'image/object/bbox/ymax' : tfrecord_util .float_list_feature (ymax ),
201- 'image/object/class/text' : tfrecord_util .bytes_list_feature (classes_text ),
202- 'image/object/class/label' : tfrecord_util .int64_list_feature (classes ),
203- 'image/object/difficult' : tfrecord_util .int64_list_feature (difficult_obj ),
204- 'image/object/truncated' : tfrecord_util .int64_list_feature (truncated ),
205- 'image/object/view' : tfrecord_util .bytes_list_feature (poses ),
206- }))
202+ example = tf .train .Example (
203+ features = tf .train .Features (
204+ feature = {
205+ 'image/height' :
206+ tfrecord_util .int64_feature (height ),
207+ 'image/width' :
208+ tfrecord_util .int64_feature (width ),
209+ 'image/filename' :
210+ tfrecord_util .bytes_feature (data ['filename' ].encode ('utf8' )),
211+ 'image/source_id' :
212+ tfrecord_util .bytes_feature (str (image_id ).encode ('utf8' )),
213+ 'image/key/sha256' :
214+ tfrecord_util .bytes_feature (key .encode ('utf8' )),
215+ 'image/encoded' :
216+ tfrecord_util .bytes_feature (encoded_jpg ),
217+ 'image/format' :
218+ tfrecord_util .bytes_feature ('jpeg' .encode ('utf8' )),
219+ 'image/object/bbox/xmin' :
220+ tfrecord_util .float_list_feature (xmin ),
221+ 'image/object/bbox/xmax' :
222+ tfrecord_util .float_list_feature (xmax ),
223+ 'image/object/bbox/ymin' :
224+ tfrecord_util .float_list_feature (ymin ),
225+ 'image/object/bbox/ymax' :
226+ tfrecord_util .float_list_feature (ymax ),
227+ 'image/object/class/text' :
228+ tfrecord_util .bytes_list_feature (classes_text ),
229+ 'image/object/class/label' :
230+ tfrecord_util .int64_list_feature (classes ),
231+ 'image/object/difficult' :
232+ tfrecord_util .int64_list_feature (difficult_obj ),
233+ 'image/object/truncated' :
234+ tfrecord_util .int64_list_feature (truncated ),
235+ 'image/object/view' :
236+ tfrecord_util .bytes_list_feature (poses ),
237+ }))
207238 return example
208239
209240
@@ -222,8 +253,8 @@ def main(_):
222253
223254 logging .info ('writing to output path: %s' , FLAGS .output_path )
224255 writers = [
225- tf .python_io .TFRecordWriter (
226- FLAGS . output_path + '-%05d-of-%05d.tfrecord' % (i , FLAGS .num_shards ))
256+ tf .python_io .TFRecordWriter (FLAGS . output_path + '-%05d-of-%05d.tfrecord' %
257+ (i , FLAGS .num_shards ))
227258 for i in range (FLAGS .num_shards )
228259 ]
229260
@@ -260,9 +291,12 @@ def main(_):
260291 xml = etree .fromstring (xml_str )
261292 data = tfrecord_util .recursive_parse_xml_to_dict (xml )['annotation' ]
262293
263- tf_example = dict_to_tf_example (data , FLAGS .data_dir , label_map_dict ,
264- FLAGS .ignore_difficult_instances ,
265- ann_json_dict = ann_json_dict )
294+ tf_example = dict_to_tf_example (
295+ data ,
296+ FLAGS .data_dir ,
297+ label_map_dict ,
298+ FLAGS .ignore_difficult_instances ,
299+ ann_json_dict = ann_json_dict )
266300 writers [idx % FLAGS .num_shards ].write (tf_example .SerializeToString ())
267301
268302 for writer in writers :
0 commit comments