# https://github.com/busyboxs/SSD-Tensorflow/blob/master/datasets/pascalvoc_to_tfrecords.py#L210 with tf.python_io.TFRecordWriter(tf_filename) as tfrecord_writer:
以下代码对应3种类型的特征方法
# https://github.com/busyboxs/SSD-Tensorflow/blob/master/datasets/dataset_utils.py#L30 defint64_feature(value): """Wrapper for inserting int64 features into Example proto. """ ifnotisinstance(value, list): value = [value] return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
deffloat_feature(value): """Wrapper for inserting float features into Example proto. """ ifnotisinstance(value, list): value = [value] return tf.train.Feature(float_list=tf.train.FloatList(value=value))
defbytes_feature(value): """Wrapper for inserting bytes features into Example proto. """ ifnotisinstance(value, list): value = [value] return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))
以下程序主要是读取图像并解析相应的xml文件获取标注
https://github.com/busyboxs/SSD-Tensorflow/blob/master/datasets/pascalvoc_to_tfrecords.py#L70 def_process_image(directory, name): """Process a image and annotation file. Args: filename: string, path to an image file e.g., '/path/to/example.JPG'. coder: instance of ImageCoder to provide TensorFlow image coding utils. Returns: image_buffer: string, JPEG encoding of RGB image. height: integer, image height in pixels. width: integer, image width in pixels. """ # Read the image file. filename = directory + DIRECTORY_IMAGES + name + '.jpg' image_data = tf.gfile.FastGFile(filename, 'r').read()
# Read the XML annotation file. filename = os.path.join(directory, DIRECTORY_ANNOTATIONS, name + '.xml') tree = ET.parse(filename) root = tree.getroot()
if obj.find('difficult'): difficult.append(int(obj.find('difficult').text)) else: difficult.append(0) if obj.find('truncated'): truncated.append(int(obj.find('truncated').text)) else: truncated.append(0)
# https://github.com/busyboxs/SSD-Tensorflow/blob/master/datasets/pascalvoc_to_tfrecords.py#L124 def_convert_to_example(image_data, labels, labels_text, bboxes, shape, difficult, truncated): """Build an Example proto for an image example. Args: image_data: string, JPEG encoding of RGB image; labels: list of integers, identifier for the ground truth; labels_text: list of strings, human-readable labels; bboxes: list of bounding boxes; each box is a list of integers; specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong to the same label as the image label. shape: 3 integers, image shapes in pixels. Returns: Example proto """ xmin = [] ymin = [] xmax = [] ymax = [] for b in bboxes: assertlen(b) == 4 # pylint: disable=expression-not-assigned [l.append(point) for l, point inzip([ymin, xmin, ymax, xmax], b)] # pylint: enable=expression-not-assigned
# https://github.com/tensorflow/tensorflow/blob/r1.10/tensorflow/contrib/slim/python/slim/data/tfexample_decoder.py#L455 classTFExampleDecoder(data_decoder.DataDecoder): """A decoder for TensorFlow Examples. Decoding Example proto buffers is comprised of two stages: (1) Example parsing and (2) tensor manipulation. In the first stage, the tf.parse_example function is called with a list of FixedLenFeatures and SparseLenFeatures. These instances tell TF how to parse the example. The output of this stage is a set of tensors. In the second stage, the resulting tensors are manipulated to provide the requested 'item' tensors. To perform this decoding operation, an ExampleDecoder is given a list of ItemHandlers. Each ItemHandler indicates the set of features for stage 1 and contains the instructions for post_processing its tensors for stage 2. """
def__init__(self, keys_to_features, items_to_handlers): """Constructs the decoder. Args: keys_to_features: a dictionary from TF-Example keys to either tf.VarLenFeature or tf.FixedLenFeature instances. See tensorflow's parsing_ops.py. items_to_handlers: a dictionary from items (strings) to ItemHandler instances. Note that the ItemHandler's are provided the keys that they use to return the final item Tensors. """ self._keys_to_features = keys_to_features self._items_to_handlers = items_to_handlers
deflist_items(self): """See base class.""" returnlist(self._items_to_handlers.keys())
defdecode(self, serialized_example, items=None): """Decodes the given serialized TF-example. Args: serialized_example: a serialized TF-example tensor. items: the list of items to decode. These must be a subset of the item keys in self._items_to_handlers. If `items` is left as None, then all of the items in self._items_to_handlers are decoded. Returns: the decoded items, a list of tensor. """ example = parsing_ops.parse_single_example(serialized_example, self._keys_to_features)
# Reshape non-sparse elements just once, adding the reshape ops in # deterministic order. for k insorted(self._keys_to_features): v = self._keys_to_features[k] ifisinstance(v, parsing_ops.FixedLenFeature): example[k] = array_ops.reshape(example[k], v.shape)
# https://github.com/tensorflow/models/blob/master/research/deeplab/datasets/build_data.py#L105 def_int64_list_feature(values): """Returns a TF-Feature of int64_list. Args: values: A scalar or list of values. Returns: A TF-Feature. """ ifnotisinstance(values, collections.Iterable): values = [values]
def_bytes_list_feature(values): """Returns a TF-Feature of bytes. Args: values: A string. Returns: A TF-Feature. """ defnorm2bytes(value): return value.encode() ifisinstance(value, str) and six.PY3 else value