Skip to content

Instantly share code, notes, and snippets.

@deepwilson
Created February 19, 2021 05:07
Show Gist options
  • Save deepwilson/3e604739fd35660172c54026fa171aba to your computer and use it in GitHub Desktop.
Save deepwilson/3e604739fd35660172c54026fa171aba to your computer and use it in GitHub Desktop.
from tqdm.notebook import tqdm
import os
# TF Records
dest_path = './tf_records_of_images.tfrecords'
file_writer = tf.io.TFRecordWriter(dest_path)
# source dir
source_dir = '/path/to/images//*'
import glob
filenames = glob.glob(source_dir)
print("[INFO] Number of files", len(filenames))
# from index 1 we can access each image along with the label
for path in tqdm(filenames, total=len(filenames)):
with tf.io.gfile.GFile(path, 'rb') as fid:
img = fid.read()
label = int(os.path.dirname(path).split('/')[-1])
record_bytes = tf.train.Example(features=tf.train.Features
(feature={
"image": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img])),
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
}
)
).SerializeToString()
file_writer.write(record_bytes)
file_writer.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment