|
| 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 | +r"""Utilities for creating TFRecords of TF examples for the Open Images dataset. |
| 16 | +""" |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import tensorflow as tf |
| 22 | + |
| 23 | +from object_detection.core import standard_fields |
| 24 | +from object_detection.utils import dataset_util |
| 25 | + |
| 26 | + |
| 27 | +def tf_example_from_annotations_data_frame(annotations_data_frame, label_map, |
| 28 | + encoded_image): |
| 29 | + """Populates a TF Example message with image annotations from a data frame. |
| 30 | +
|
| 31 | + Args: |
| 32 | + annotations_data_frame: Data frame containing the annotations for a single |
| 33 | + image. |
| 34 | + label_map: String to integer label map. |
| 35 | + encoded_image: The encoded image string |
| 36 | +
|
| 37 | + Returns: |
| 38 | + The populated TF Example, if the label of at least one object is present in |
| 39 | + label_map. Otherwise, returns None. |
| 40 | + """ |
| 41 | + |
| 42 | + filtered_data_frame = annotations_data_frame[ |
| 43 | + annotations_data_frame.LabelName.isin(label_map)] |
| 44 | + |
| 45 | + image_id = annotations_data_frame.ImageID.iloc[0] |
| 46 | + |
| 47 | + feature_map = { |
| 48 | + standard_fields.TfExampleFields.object_bbox_ymin: |
| 49 | + dataset_util.float_list_feature(filtered_data_frame.YMin.as_matrix()), |
| 50 | + standard_fields.TfExampleFields.object_bbox_xmin: |
| 51 | + dataset_util.float_list_feature(filtered_data_frame.XMin.as_matrix()), |
| 52 | + standard_fields.TfExampleFields.object_bbox_ymax: |
| 53 | + dataset_util.float_list_feature(filtered_data_frame.YMax.as_matrix()), |
| 54 | + standard_fields.TfExampleFields.object_bbox_xmax: |
| 55 | + dataset_util.float_list_feature(filtered_data_frame.XMax.as_matrix()), |
| 56 | + standard_fields.TfExampleFields.object_class_text: |
| 57 | + dataset_util.bytes_list_feature( |
| 58 | + filtered_data_frame.LabelName.as_matrix()), |
| 59 | + standard_fields.TfExampleFields.object_class_label: |
| 60 | + dataset_util.int64_list_feature( |
| 61 | + filtered_data_frame.LabelName.map(lambda x: label_map[x]) |
| 62 | + .as_matrix()), |
| 63 | + standard_fields.TfExampleFields.filename: |
| 64 | + dataset_util.bytes_feature('{}.jpg'.format(image_id)), |
| 65 | + standard_fields.TfExampleFields.source_id: |
| 66 | + dataset_util.bytes_feature(image_id), |
| 67 | + standard_fields.TfExampleFields.image_encoded: |
| 68 | + dataset_util.bytes_feature(encoded_image), |
| 69 | + } |
| 70 | + |
| 71 | + if 'IsGroupOf' in filtered_data_frame.columns: |
| 72 | + feature_map[standard_fields.TfExampleFields. |
| 73 | + object_group_of] = dataset_util.int64_list_feature( |
| 74 | + filtered_data_frame.IsGroupOf.as_matrix().astype(int)) |
| 75 | + if 'IsOccluded' in filtered_data_frame.columns: |
| 76 | + feature_map[standard_fields.TfExampleFields. |
| 77 | + object_occluded] = dataset_util.int64_list_feature( |
| 78 | + filtered_data_frame.IsOccluded.as_matrix().astype(int)) |
| 79 | + if 'IsTruncated' in filtered_data_frame.columns: |
| 80 | + feature_map[standard_fields.TfExampleFields. |
| 81 | + object_truncated] = dataset_util.int64_list_feature( |
| 82 | + filtered_data_frame.IsTruncated.as_matrix().astype(int)) |
| 83 | + if 'IsDepiction' in filtered_data_frame.columns: |
| 84 | + feature_map[standard_fields.TfExampleFields. |
| 85 | + object_depiction] = dataset_util.int64_list_feature( |
| 86 | + filtered_data_frame.IsDepiction.as_matrix().astype(int)) |
| 87 | + |
| 88 | + return tf.train.Example(features=tf.train.Features(feature=feature_map)) |
| 89 | + |
| 90 | + |
| 91 | +def open_sharded_output_tfrecords(exit_stack, base_path, num_shards): |
| 92 | + """Opens all TFRecord shards for writing and adds them to an exit stack. |
| 93 | +
|
| 94 | + Args: |
| 95 | + exit_stack: A context2.ExitStack used to automatically closed the TFRecords |
| 96 | + opened in this function. |
| 97 | + base_path: The base path for all shards |
| 98 | + num_shards: The number of shards |
| 99 | +
|
| 100 | + Returns: |
| 101 | + The list of opened TFRecords. Position k in the list corresponds to shard k. |
| 102 | + """ |
| 103 | + tf_record_output_filenames = [ |
| 104 | + '{}-{:05d}-of-{:05d}'.format(base_path, idx, num_shards) |
| 105 | + for idx in xrange(num_shards) |
| 106 | + ] |
| 107 | + |
| 108 | + tfrecords = [ |
| 109 | + exit_stack.enter_context(tf.python_io.TFRecordWriter(file_name)) |
| 110 | + for file_name in tf_record_output_filenames |
| 111 | + ] |
| 112 | + |
| 113 | + return tfrecords |
0 commit comments