|
| 1 | +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +r"""Convert raw COCO dataset to TFRecord for object_detection. |
| 17 | +
|
| 18 | +Please note that this tool creates sharded output files. |
| 19 | +
|
| 20 | +Example usage: |
| 21 | + python create_coco_tf_record.py --logtostderr \ |
| 22 | + --train_image_dir="${TRAIN_IMAGE_DIR}" \ |
| 23 | + --test_image_dir="${TEST_IMAGE_DIR}" \ |
| 24 | + --train_annotations_file="${TRAIN_ANNOTATIONS_FILE}" \ |
| 25 | + --test_annotations_file="${TEST_ANNOTATIONS_FILE}" \ |
| 26 | + --output_dir="${OUTPUT_DIR}" |
| 27 | +""" |
| 28 | +from __future__ import absolute_import |
| 29 | +from __future__ import division |
| 30 | +from __future__ import print_function |
| 31 | + |
| 32 | +import hashlib |
| 33 | +import io |
| 34 | +import json |
| 35 | +import os |
| 36 | +import contextlib2 |
| 37 | +import numpy as np |
| 38 | +import PIL.Image |
| 39 | + |
| 40 | +from pycocotools import mask |
| 41 | + |
| 42 | +from tensorflow.python.framework.versions import VERSION |
| 43 | +if VERSION >= "2.0.0a0": |
| 44 | + import tensorflow.compat.v1 as tf |
| 45 | +else: |
| 46 | + import tensorflow as tf |
| 47 | + |
| 48 | +from object_detection.dataset_tools import tf_record_creation_util |
| 49 | +from object_detection.utils import dataset_util |
| 50 | +from object_detection.utils import label_map_util |
| 51 | + |
| 52 | + |
| 53 | +flags = tf.app.flags |
| 54 | +tf.flags.DEFINE_boolean('include_masks', True, |
| 55 | + 'Whether to include instance segmentations masks ' |
| 56 | + '(PNG encoded) in the result. default: True.') |
| 57 | +tf.flags.DEFINE_string('train_image_dir', '', |
| 58 | + 'Training image directory.') |
| 59 | +tf.flags.DEFINE_string('test_image_dir', '', |
| 60 | + 'Test image directory.') |
| 61 | +tf.flags.DEFINE_string('train_annotations_file', '', |
| 62 | + 'Training annotations JSON file.') |
| 63 | +tf.flags.DEFINE_string('test_annotations_file', '', |
| 64 | + 'Test-dev annotations JSON file.') |
| 65 | +tf.flags.DEFINE_string('output_dir', '/tmp/', 'Output data directory.') |
| 66 | + |
| 67 | +FLAGS = flags.FLAGS |
| 68 | + |
| 69 | +tf.logging.set_verbosity(tf.logging.INFO) |
| 70 | + |
| 71 | + |
| 72 | +def create_tf_example(image, |
| 73 | + annotations_list, |
| 74 | + image_dir, |
| 75 | + category_index, |
| 76 | + include_masks=False): |
| 77 | + """Converts image and annotations to a tf.Example proto. |
| 78 | +
|
| 79 | + Args: |
| 80 | + image: dict with keys: |
| 81 | + [u'license', u'file_name', u'coco_url', u'height', u'width', |
| 82 | + u'date_captured', u'flickr_url', u'id'] |
| 83 | + annotations_list: |
| 84 | + list of dicts with keys: |
| 85 | + [u'segmentation', u'area', u'iscrowd', u'image_id', |
| 86 | + u'bbox', u'category_id', u'id'] |
| 87 | + Notice that bounding box coordinates in the official COCO dataset are |
| 88 | + given as [x, y, width, height] tuples using absolute coordinates where |
| 89 | + x, y represent the top-left (0-indexed) corner. This function converts |
| 90 | + to the format expected by the Tensorflow Object Detection API (which is |
| 91 | + which is [ymin, xmin, ymax, xmax] with coordinates normalized relative |
| 92 | + to image size). |
| 93 | + image_dir: directory containing the image files. |
| 94 | + category_index: a dict containing COCO category information keyed |
| 95 | + by the 'id' field of each category. See the |
| 96 | + label_map_util.create_category_index function. |
| 97 | + include_masks: Whether to include instance segmentations masks |
| 98 | + (PNG encoded) in the result. default: False. |
| 99 | + Returns: |
| 100 | + example: The converted tf.Example |
| 101 | + num_annotations_skipped: Number of (invalid) annotations that were ignored. |
| 102 | +
|
| 103 | + Raises: |
| 104 | + ValueError: if the image pointed to by data['filename'] is not a valid JPEG |
| 105 | + """ |
| 106 | + image_height = image['height'] |
| 107 | + image_width = image['width'] |
| 108 | + filename = image['file_name'] |
| 109 | + image_id = image['id'] |
| 110 | + |
| 111 | + full_path = os.path.join(image_dir, filename) |
| 112 | + with tf.gfile.GFile(full_path, 'rb') as fid: |
| 113 | + encoded_jpg = fid.read() |
| 114 | + encoded_jpg_io = io.BytesIO(encoded_jpg) |
| 115 | + image = PIL.Image.open(encoded_jpg_io) |
| 116 | + key = hashlib.sha256(encoded_jpg).hexdigest() |
| 117 | + |
| 118 | + xmin = [] |
| 119 | + xmax = [] |
| 120 | + ymin = [] |
| 121 | + ymax = [] |
| 122 | + is_crowd = [] |
| 123 | + category_names = [] |
| 124 | + category_ids = [] |
| 125 | + area = [] |
| 126 | + encoded_mask_png = [] |
| 127 | + num_annotations_skipped = 0 |
| 128 | + for object_annotations in annotations_list: |
| 129 | + (x, y, width, height) = tuple(object_annotations['bbox']) |
| 130 | + if width <= 0 or height <= 0: |
| 131 | + num_annotations_skipped += 1 |
| 132 | + continue |
| 133 | + if x + width > image_width or y + height > image_height: |
| 134 | + num_annotations_skipped += 1 |
| 135 | + continue |
| 136 | + xmin.append(float(x) / image_width) |
| 137 | + xmax.append(float(x + width) / image_width) |
| 138 | + ymin.append(float(y) / image_height) |
| 139 | + ymax.append(float(y + height) / image_height) |
| 140 | + is_crowd.append(object_annotations['iscrowd']) |
| 141 | + category_id = int(object_annotations['category_id']) |
| 142 | + category_ids.append(category_id) |
| 143 | + category_names.append(category_index[category_id]['name'].encode('utf8')) |
| 144 | + area.append(object_annotations['area']) |
| 145 | + |
| 146 | + if include_masks: |
| 147 | + run_len_encoding = mask.frPyObjects(object_annotations['segmentation'], |
| 148 | + image_height, image_width) |
| 149 | + binary_mask = mask.decode(run_len_encoding) |
| 150 | + if not object_annotations['iscrowd']: |
| 151 | + binary_mask = np.amax(binary_mask, axis=2) |
| 152 | + pil_image = PIL.Image.fromarray(binary_mask) |
| 153 | + output_io = io.BytesIO() |
| 154 | + pil_image.save(output_io, format='PNG') |
| 155 | + encoded_mask_png.append(output_io.getvalue()) |
| 156 | + feature_dict = { |
| 157 | + 'image/height': |
| 158 | + dataset_util.int64_feature(image_height), |
| 159 | + 'image/width': |
| 160 | + dataset_util.int64_feature(image_width), |
| 161 | + 'image/filename': |
| 162 | + dataset_util.bytes_feature(filename.encode('utf8')), |
| 163 | + 'image/source_id': |
| 164 | + dataset_util.bytes_feature(str(image_id).encode('utf8')), |
| 165 | + 'image/key/sha256': |
| 166 | + dataset_util.bytes_feature(key.encode('utf8')), |
| 167 | + 'image/encoded': |
| 168 | + dataset_util.bytes_feature(encoded_jpg), |
| 169 | + 'image/format': |
| 170 | + dataset_util.bytes_feature('jpeg'.encode('utf8')), |
| 171 | + 'image/object/bbox/xmin': |
| 172 | + dataset_util.float_list_feature(xmin), |
| 173 | + 'image/object/bbox/xmax': |
| 174 | + dataset_util.float_list_feature(xmax), |
| 175 | + 'image/object/bbox/ymin': |
| 176 | + dataset_util.float_list_feature(ymin), |
| 177 | + 'image/object/bbox/ymax': |
| 178 | + dataset_util.float_list_feature(ymax), |
| 179 | + 'image/object/bbox/class/label': |
| 180 | + dataset_util.int64_list_feature(category_ids), |
| 181 | + 'image/object/class/text': |
| 182 | + dataset_util.bytes_list_feature(category_names), |
| 183 | + 'image/object/class/label': |
| 184 | + dataset_util.int64_list_feature(category_ids), |
| 185 | + 'image/object/is_crowd': |
| 186 | + dataset_util.int64_list_feature(is_crowd), |
| 187 | + 'image/object/area': |
| 188 | + dataset_util.float_list_feature(area), |
| 189 | + } |
| 190 | + if include_masks: |
| 191 | + feature_dict['image/object/mask'] = ( |
| 192 | + dataset_util.bytes_list_feature(encoded_mask_png)) |
| 193 | + example = tf.train.Example(features=tf.train.Features(feature=feature_dict)) |
| 194 | + return key, example, num_annotations_skipped |
| 195 | + |
| 196 | + |
| 197 | +def _create_tf_record_from_coco_annotations( |
| 198 | + annotations_file, image_dir, output_path, include_masks): |
| 199 | + """Loads COCO annotation json files and converts to tf.Record format. |
| 200 | +
|
| 201 | + Args: |
| 202 | + annotations_file: JSON file containing bounding box annotations. |
| 203 | + image_dir: Directory containing the image files. |
| 204 | + output_path: Path to output tf.Record file. |
| 205 | + include_masks: Whether to include instance segmentations masks |
| 206 | + (PNG encoded) in the result. default: False. |
| 207 | + """ |
| 208 | + with tf.gfile.GFile(annotations_file, 'r') as fid: |
| 209 | + output_tfrecords = tf.python_io.TFRecordWriter(output_path) |
| 210 | + groundtruth_data = json.load(fid) |
| 211 | + images = groundtruth_data['images'] |
| 212 | + category_index = label_map_util.create_category_index( |
| 213 | + groundtruth_data['categories']) |
| 214 | + |
| 215 | + annotations_index = {} |
| 216 | + if 'annotations' in groundtruth_data: |
| 217 | + tf.logging.info( |
| 218 | + 'Found groundtruth annotations. Building annotations index.') |
| 219 | + for annotation in groundtruth_data['annotations']: |
| 220 | + image_id = annotation['image_id'] |
| 221 | + if image_id not in annotations_index: |
| 222 | + annotations_index[image_id] = [] |
| 223 | + annotations_index[image_id].append(annotation) |
| 224 | + missing_annotation_count = 0 |
| 225 | + for image in images: |
| 226 | + image_id = image['id'] |
| 227 | + if image_id not in annotations_index: |
| 228 | + missing_annotation_count += 1 |
| 229 | + annotations_index[image_id] = [] |
| 230 | + tf.logging.info('%d images are missing annotations.', |
| 231 | + missing_annotation_count) |
| 232 | + |
| 233 | + total_num_annotations_skipped = 0 |
| 234 | + for idx, image in enumerate(images): |
| 235 | + if idx % 100 == 0: |
| 236 | + tf.logging.info('On image %d of %d', idx, len(images)) |
| 237 | + annotations_list = annotations_index[image['id']] |
| 238 | + _, tf_example, num_annotations_skipped = create_tf_example( |
| 239 | + image, annotations_list, image_dir, category_index, include_masks) |
| 240 | + total_num_annotations_skipped += num_annotations_skipped |
| 241 | + output_tfrecords.write(tf_example.SerializeToString()) |
| 242 | + tf.logging.info('Finished writing, skipped %d annotations.', |
| 243 | + total_num_annotations_skipped) |
| 244 | + |
| 245 | + |
| 246 | +def main(_): |
| 247 | + assert FLAGS.train_image_dir, '`train_image_dir` missing.' |
| 248 | + assert FLAGS.test_image_dir, '`test_image_dir` missing.' |
| 249 | + assert FLAGS.train_annotations_file, '`train_annotations_file` missing.' |
| 250 | + assert FLAGS.test_annotations_file, '`test_annotations_file` missing.' |
| 251 | + |
| 252 | + if not tf.gfile.IsDirectory(FLAGS.output_dir): |
| 253 | + tf.gfile.MakeDirs(FLAGS.output_dir) |
| 254 | + train_output_path = os.path.join(FLAGS.output_dir, 'train.record') |
| 255 | + testdev_output_path = os.path.join(FLAGS.output_dir, 'test.record') |
| 256 | + |
| 257 | + _create_tf_record_from_coco_annotations( |
| 258 | + FLAGS.train_annotations_file, |
| 259 | + FLAGS.train_image_dir, |
| 260 | + train_output_path, |
| 261 | + FLAGS.include_masks) |
| 262 | + _create_tf_record_from_coco_annotations( |
| 263 | + FLAGS.test_annotations_file, |
| 264 | + FLAGS.test_image_dir, |
| 265 | + testdev_output_path, |
| 266 | + FLAGS.include_masks) |
| 267 | + |
| 268 | + |
| 269 | +if __name__ == '__main__': |
| 270 | + tf.app.run() |
0 commit comments