Skip to content

Instantly share code, notes, and snippets.

@TopHatCroat
Last active March 14, 2018 08:25
Show Gist options
  • Save TopHatCroat/38302cc7fe31ab994d688d3623d2c5df to your computer and use it in GitHub Desktop.
Save TopHatCroat/38302cc7fe31ab994d688d3623d2c5df to your computer and use it in GitHub Desktop.
tfrecord.py
import os
import io
import re
import random
import tensorflow as tf
from PIL import Image
from object_detection.utils import dataset_util
flags = tf.app.flags
flags.DEFINE_string('input_path', '', 'Path to images input directory')
flags.DEFINE_string('classes', '', 'Space separated list of classes')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
FLAGS = flags.FLAGS
VALID_EXTENSIONS = [".jpg", ".jpeg", ".jpe", ".png"]
label_map = {}
testing_validation_factor = 0.8
def load_images_paths(dir):
result = []
for f in os.listdir(dir):
ext = os.path.splitext(f)[1]
if ext.lower() not in VALID_EXTENSIONS:
continue
result.append(os.path.join(dir, f))
return result
def create_tf_example(img_path, class_text, class_id):
with tf.gfile.GFile(img_path, 'rb') as fid:
image_file = fid.read()
encoded_image = io.BytesIO(image_file)
image = Image.open(encoded_image)
width, height = image.size
image_name_and_ext = img_path.split("/")[-1].split(".")
filename = image_name_and_ext[0].encode('utf8')
# image_format = b'jpg'
image_format = image_name_and_ext[1].encode('utf8')
xmins = [0]
xmaxs = [1]
ymins = [0]
ymaxs = [1]
tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(filename),
'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(image_file),
'image/format': dataset_util.bytes_feature(image_format),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature([class_text.encode('utf8')]),
'image/object/class/label': dataset_util.int64_list_feature([int(class_id)]),
}))
return tf_example
def load_label_map():
with open("./label_map.pbtxt", "r") as file:
file_content = file.read().replace("\'", "").split(sep=None)
file_content = "".join(file_content)
id_re = re.compile("id:(\d+)")
name_re = re.compile("name:(\w+)")
for i in file_content.split("item")[1:]:
item_id = id_re.search(i).group(1)
item_name = name_re.search(i).group(1)
label_map.update({item_name: item_id})
def id_for_class_name(c):
if len(label_map) == 0:
load_label_map()
return label_map.get(c)
def main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
validation_writer = tf.python_io.TFRecordWriter("valid_" + FLAGS.output_path)
classes = ["table", "chair"]
for c in classes:
images = load_images_paths(os.path.join(FLAGS.input_path, c))
random.shuffle(images)
for index, i in enumerate(images):
example = create_tf_example(i, c, id_for_class_name(c))
if index / len(images) > testing_validation_factor:
validation_writer.write(example.SerializeToString())
else:
writer.write(example.SerializeToString())
writer.close()
validation_writer.close()
if __name__ == '__main__':
tf.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment