Predicting a single image with Keras' ImageDataGenerator
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
Single image inference in Keras looks simple, but accuracy depends on preprocessing consistency more than model call syntax. Most incorrect predictions come from wrong shape, wrong normalization, or wrong label mapping. ImageDataGenerator can be used for one image, but direct preprocessing is often clearer for production inference.
Match Training Preprocessing Exactly
Before writing inference code, confirm what preprocessing was used during training:
- image size
- color mode
- scaling strategy
- model specific preprocessing function
If training used rescale=1.0/255.0, do the same in inference. If training used an application specific function such as preprocess_input, reuse that exact function.
Direct Single Image Prediction Pattern
For one image, direct array preparation is explicit and easy to debug.
The batch dimension is required, even for one sample.
Using ImageDataGenerator for One Image
You can still use ImageDataGenerator if you want parity with an existing generator pipeline.
For strict single image use cases, direct array code is usually easier to reason about. Generators shine more when processing many images.
Convert Model Output to Human Labels
Prediction array interpretation depends on the final activation.
Softmax multi class example:
Sigmoid binary example:
Wrong output interpretation can look like a model bug even when the model is fine.
Build a Reusable Inference Helper
A helper function prevents subtle preprocessing drift between scripts and services.
Keep helper versioned with the model artifact. If model preprocessing changes, update helper and bump model metadata together.
Evaluate Confidence and Top Candidates
Top one prediction is often not enough for diagnostics. Inspect top three probabilities to understand uncertainty.
For user facing features, define confidence thresholds. Low confidence results may need fallback behavior or manual review.
Common Pitfalls
A common mistake is forgetting np.expand_dims, which causes shape mismatch at runtime.
Another frequent issue is preprocessing mismatch. Using divide by 255 during inference when training used model specific preprocess_input can significantly reduce accuracy.
Teams also accidentally keep augmentation enabled during inference when reusing a training generator configuration. Augmentation should be disabled for deterministic prediction.
Finally, class index mapping can drift. Save label order from training and load that mapping for inference.
Summary
- Single image prediction works best when preprocessing matches training exactly.
- '
ImageDataGeneratorcan handle one image, but direct array preprocessing is simpler.' - Always include batch dimension before calling
model.predict. - Interpret outputs according to final activation and saved class mapping.
- Most inference errors are data pipeline mismatches, not model architecture issues.
Related reading
- predicting class for new data using neuralnet
- Predicting the next word using the LSTM ptb model tensorflow example
- Prediction from model saved with tf.estimator.Estimator in Tensorflow
- Preload whole dataset on gpu for training Keras model
- Predicting next word using the language model tensorflow example
- Predicting probabilities in classfier tensorflow
- Predicting a users next action based on current day and time
- Predicting how long an scikit-learn classification will take to run
System design interview problems on this topic
- Design an LLM Inference Service: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough
- Design ChatGPT: solution and walkthrough
- Design an E-commerce Recommendation System: solution and walkthrough
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.