How to display custom images in TensorBoard using Keras?
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
TensorBoard can display more than scalar metrics. Logging custom images during training helps you inspect data quality, augmentation behavior, and model outputs in a visual workflow. In Keras, the clean approach is to write image summaries from a callback.
Set Up TensorBoard Logging in Keras
Start with a log directory and the standard TensorBoard callback for scalars. Then add a custom callback for images.
Image summaries require tensors shaped as batch, height, width, channels. Values should be in a valid display range, usually zero to one for float inputs.
Custom Callback for Image Summaries
The callback below logs a fixed validation batch each epoch and writes model predictions as images.
Attach both callbacks to model training:
Then run TensorBoard and open the Images tab.
Logging Grids and Preprocessing Views
For debugging preprocessing, log intermediate tensors such as normalized images, augmented samples, or segmentation masks. If masks are single channel, ensure shape still includes channel dimension.
This makes it easy to spot pipeline issues such as wrong channel order, over aggressive augmentation, or unexpected normalization ranges.
Practical Workflow Tips
Use a fixed sample batch for epoch to epoch comparison, plus occasional random batches for coverage. Keep logged image counts modest to avoid huge event files. A few representative samples are usually enough.
Use separate tags for inputs, labels, and predictions so visual comparisons are clear. For segmentation or detection tasks, consider overlay rendering in preprocessing code before logging.
If training runs on remote machines, store logs in stable paths and sync them for local TensorBoard inspection.
For long experiments, split logs by run id and hyperparameter tag so image streams stay comparable. A clear directory convention such as model name, date, and seed makes debugging much easier when several experiments run in parallel.
It is also useful to log occasional failure cases separately, for example images with highest reconstruction error or misclassified samples. Reviewing these targeted images can reveal dataset issues that scalar loss curves cannot show.
Common Pitfalls
A common pitfall is logging tensors with wrong shape, such as missing batch dimension. tf.summary.image expects four dimensions.
Another issue is logging values outside display range. Tensors with large raw values can appear black or washed out. Normalize or clip before summary write.
Developers also log too many images every step, which inflates event files and slows training. Log at epoch boundaries or every few hundred steps.
Finally, using random samples only can make trend comparison difficult. Include a fixed reference batch to track qualitative improvements consistently.
Summary
- Use a custom Keras callback to write image summaries during training.
- Log tensors in batch height width channel shape with valid value ranges.
- Separate tags for inputs and predictions improve debugging clarity.
- Keep image logging frequency moderate to control log size.
- Combine fixed and random samples for both trend and coverage checks.
Related reading
- How to display Runtime Statistics in Tensorboard using Estimator API in a distributed environment
- How to display the average of multiple runs on tensorboard
- How to display training progress bar in tensorflow?
- How to do a column sum in Tensorflow?
- How to Do a Simple CLI Query for a Saved Estimator Model?
- How to do batching in Tensorflow Serving?
- How to do matrix-scalar multiplication in TensorFlow?
- How to do multi-class image classification in keras?
.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.