What is TFRecord and why it’s useful for image classification?

Yiling Liu
1 min readFeb 18, 2020

--

If we want to train multiple models based on the same dataset, instead of storing the image data raw, we can preprocess the data into TFRecords format, which can be reused to train different models.

Image → TFRecord → Model Training → Inference

How to create TFRecord from image?

There are 2 steps.

Step1: write the image to tf.Example type which contains the following attributions as features:

  • Image string
  • Image attributes: height, width, depth
  • Image label (0, 1, 2 etc, has to be integer)
tf_example = tf.train.Example(features=tf.train.Features(feature=feature))

Step2: Serialise the tf.Example object into string and store it in a file with tf.io.TFRecordWriter(record_file).

tf_example = 
with tf.io.TFRecordWriter(record_file) as writer:
for filename, label in image_labels.items():
image_string = open(filename, 'rb').read()
writer.write(tf_example.SerializeToString())

References

--

--

Yiling Liu
Yiling Liu

Written by Yiling Liu

Creative Technologist, ex-googler

No responses yet