How to read images with different size in a TFRecord file
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
TFRecord can store images of different sizes without any problem, but your input pipeline has to decode them correctly and then decide how to batch them. The main trap is assuming that all images have one fixed shape at parse time. In practice, you usually store encoded image bytes, decode each example individually, and only resize or pad after decoding.
Store Encoded Bytes, Not Raw Fixed-Shape Tensors
The usual pattern is to serialize each image as encoded bytes, plus optional metadata such as label, height, and width.
Example writer:
The variable-sized part is stored safely inside image_bytes. That is what makes heterogeneous image shapes practical in one TFRecord file.
Parse and Decode Each Example First
When reading, parse the serialized example and decode the image bytes:
At this stage, TensorFlow knows the tensor rank but not a fixed height and width shared by the whole dataset. That is expected.
Resize or Pad Before Regular Batching
Regular batch() requires compatible tensor shapes. If your images are different sizes, you usually do one of these:
- resize all images to a common target
- pad them to the largest shape in a batch
- keep them unbatched for a custom consumer
The simplest path is resize:
That is the standard training pipeline approach because most models require a fixed input size anyway.
Use padded_batch When You Need Variable Original Shapes
If you want to preserve more of the original size information, padded_batch is a better fit:
This pads each image in the batch to a common height and width for that batch only. It is useful when resizing would damage the task or when the downstream model supports masking or variable spatial dimensions.
Keep Shape Decisions Close to the Model
The right strategy depends on the model and task:
- classification usually resizes to a fixed input shape
- detection and segmentation may prefer padding or smarter aspect-ratio handling
- archival or inspection pipelines may keep original shapes as long as possible
Do not force one shape policy into the TFRecord design itself. The TFRecord should store decodable examples cleanly; the input pipeline should decide how to normalize them for the model.
Common Pitfalls
- Storing raw pixel tensors and assuming all examples can share one fixed shape forever.
- Trying to use ordinary
batch()before resizing or padding variable-sized images. - Forgetting that
decode_jpegreturns dynamic spatial dimensions until later pipeline steps constrain them. - Resizing blindly without checking whether the task actually tolerates geometric distortion.
- Treating TFRecord as the place to solve batching strategy instead of the dataset pipeline.
Summary
- TFRecord can store images of different sizes by keeping them as encoded bytes.
- Parse and decode each example first, then decide how to normalize shapes.
- Use resizing for fixed-shape model input pipelines.
- Use
padded_batchwhen preserving variable dimensions is more important. - Keep storage format and batching strategy as separate decisions in the data pipeline.

