I was asked this on another forum but thought I'd post it here for anyone that is having trouble with TFRecords.
TensorFlow's Object Detection API can produce strange behavior if the labels in the TFRecord file do not align with the labels in your labels.pbtxt file. It will run, loss will likely decrease but the network will not produce good detections.
Also, I for one always get confused between X-Y, row-col space, and so I always like to double check to make sure that my annotations are actually annotating the right parts of the image.
The best way I've found to do this is by decoding the TFRecord and plotting it with TF tools. Here's some code below:
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from object_detection.utils import visualization_utils as vu
from object_detection.protos import string_int_label_map_pb2 as pb
from object_detection.data_decoders.tf_example_decoder import TfExampleDecoder as TfDecoder
from google.protobuf import text_format
def main(tfrecords_filename, label_map=None):
if label_map is not None:
label_map_proto = pb.StringIntLabelMap()
with tf.gfile.GFile(label_map,'r') as f:
text_format.Merge(f.read(), label_map_proto)
class_dict = {}
for entry in label_map_proto.item:
class_dict[entry.id] = {'name':entry.display_name}
sess = tf.Session()
decoder = TfDecoder(label_map_proto_file=label_map, use_display_name=False)
sess.run(tf.tables_initializer())
for record in tf.python_io.tf_record_iterator(tfrecords_filename):
example = decoder.decode(record)
host_example = sess.run(example)
scores = np.ones(host_example['groundtruth_boxes'].shape[0])
vu.visualize_boxes_and_labels_on_image_array(
host_example['image'],
host_example['groundtruth_boxes'],
host_example['groundtruth_classes'],
scores,
class_dict,
max_boxes_to_draw=None,
use_normalized_coordinates=True)
plt.imshow(host_example['image'])
plt.show()
The TFRecord format is a simple format for storing a sequence of binary records. Protocol buffers are a cross-platform, cross-language library for efficient serialization of structured data. Protocol messages are defined by . proto files, these are often the easiest way to understand a message type.
Creating TFRecord Files with Code Most often we have labeled data in PASCAL VOC XML or COCO JSON. Creating a TFRecord file from this data requires following a multistep process: (1) creating a TensorFlow Object Detection CSV (2) Using that TensorFlow Object Detection CSV to create TFRecord files.
Converting your data into TFRecord has many advantages, such as: More efficient storage: the TFRecord data can take up less space than the original data; it can also be partitioned into multiple files. Fast I/O: the TFRecord format can be read with parallel I/O operations, which is useful for TPUs or multiple hosts.
If you'd like to check bounding boxes/labels visually, you can check this TFRecord Viewer: https://github.com/sulc/tfrecord-viewer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With