Tensorflow Keras error Unknown image file format. One of JPEG, PNG, GIF, BMP required
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The error Unknown image file format. One of JPEG, PNG, GIF, BMP required means Keras tried to decode a file that is either unsupported, mislabeled, corrupt, or not actually an image at all. The failure often shows up when loading a directory of images, where one bad file poisons the whole pipeline.
In practice, the bug is usually not that TensorFlow forgot how to read JPEGs. It is that your dataset contains something unexpected such as a WebP file, a hidden system file, a text file with an image extension, or a damaged image.
Why Keras Raises This Error
Utilities such as tf.keras.utils.load_img and image dataset loaders expect image bytes in a supported format. They do not trust the filename alone; they inspect the content enough to decide whether the file can be decoded.
That means these examples can fail:
- '
photo.jpgthat is actually a PNG or text file,' - hidden files such as
.DS_Store, - unsupported formats such as TIFF or WebP in pipelines that expect only the classic formats,
- partially downloaded or corrupted image files.
The extension can look fine while the file contents are still invalid.
Reproduce the Problem in a Small Example
A minimal load looks like this:
If example.jpg is not a real decodable image of a supported type, Keras raises the format error immediately.
Validate Files Before Training
One practical way to clean a dataset is to scan the directory and verify images before handing them to TensorFlow. Pillow is useful for that:
This catches many common dataset issues before the training loop sees them.
Filter by Extension and by Content
Extension filtering is helpful but not sufficient:
This removes obvious non-image files, but it still does not guarantee the content is valid. A robust pipeline uses both checks:
- allowed extension,
- successful image verification.
That combination avoids many frustrating runtime crashes.
Common Real-World Causes
One of the most common causes is macOS metadata files such as .DS_Store appearing in image directories. Another is archive extraction leaving behind text files, thumbnails, or unsupported image formats mixed into the dataset.
It is also common to rename files without converting them. A TIFF renamed to .jpg still is not a JPEG, and Keras will not be fooled by the extension.
Common Pitfalls
- Trusting file extensions without validating the actual file contents.
- Assuming every file in an image folder is a usable training image.
- Ignoring hidden system files that dataset loaders may still encounter.
- Trying to debug model code when the real problem is dataset hygiene.
- Renaming unsupported image formats instead of actually converting them.
Summary
- This error usually means at least one input file is unsupported, corrupted, or not really an image.
- Keras checks decodability, not just the filename extension.
- Validate datasets before training, especially when loading from folders.
- Filtering by extension helps, but content verification is safer.
- Hidden files, bad downloads, and mislabeled formats are some of the most common causes.
Related reading
- Tensorflow L2 loss definition
- tensorflow lite conversion for LSTM Model
- Tensorflow Lite GPU support for python
- Tensorflow Lite GPU support for python
- TensorFlow keras model fit parameters steps_per_epoch and epochs behavior on train set
- Tensorflow Keras modify model variable from callback
- Tensorflow model for OCR
- Tensorflow Non-Maximum Suppression
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.