How to plot grid of images in tensorboard?
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 images directly with tf.summary.image, but if you want a true grid, you need to arrange the batch into one tiled image first. That usually means reshaping and concatenating the image tensor so several samples appear in a single canvas. Once you have that tiled tensor, you log it as one image summary.
What TensorBoard Expects
tf.summary.image expects a tensor shaped like:
where:
- '
Nis the number of images' - '
His height' - '
Wis width' - '
Cis channels, typically1,3, or4'
If you pass a batch directly, TensorBoard will show multiple separate images up to max_outputs. That is useful, but it is not the same as a manually arranged grid.
Build a Grid Tensor First
A common approach is to tile rows * cols images into one larger image.
The returned tensor has shape [1, big_height, big_width, channels], which is exactly what tf.summary.image needs for logging one composite image.
Log the Grid to TensorBoard
Then run:
Open TensorBoard and look under the Images tab.
Normalization and Data Range
TensorBoard expects image values in a reasonable display range. If your images are floating-point tensors, a [0, 1] range is usually the safest choice.
For example:
If your data comes in [-1, 1], rescale it first:
Otherwise the grid may appear washed out, too dark, or inconsistent.
Use It During Training
A common pattern is to log image grids periodically inside a training loop so you can inspect:
- input batches
- model reconstructions
- generated images
- segmentation predictions
Example inside an epoch loop:
This gives you a timeline of visual changes across training steps.
A Note on Alternatives
If you already use PyTorch or Matplotlib-style tooling, you may know helper utilities that create grids automatically. In TensorFlow-only workflows, the explicit tensor-reshaping approach is often simplest because it avoids extra dependencies and keeps the data inside the TensorFlow pipeline.
That also makes it easier to log model outputs without converting back and forth between formats.
Common Pitfalls
The most common mistake is assuming max_outputs=16 automatically creates a 4 by 4 grid. It does not. It logs up to 16 separate images.
Another mistake is passing tensors with the wrong shape, such as [H, W, C] instead of [N, H, W, C]. tf.summary.image expects a batch dimension.
Developers also often forget to normalize image values, which makes the logged result look wrong even when the tensor layout is correct.
Finally, if the grid appears scrambled, check the reshape and transpose order. A wrong transpose usually causes row and column mixing.
Summary
- '
tf.summary.imagelogs batches of images, but a real grid requires tiling first.' - Build the grid by reshaping, transposing, and flattening the image batch into one larger image.
- Log the tiled tensor as a single image summary.
- Normalize image values so TensorBoard displays them correctly.
- Use the same pattern to visualize inputs, reconstructions, or generated samples during training.
Related reading
- how to plot the tensorflow neural network object
- How to predict from saved model in Keras ?
- How to predict input image using trained model in Keras?
- how to predict my own image using cnn in keras after training on MNIST dataset
- How to predict a function/table using Keras?
- How to predict a simple sequence using seq2seq from tensorflow?
- How to Plot PR-Curve Over 10 folds of Cross Validation in Scikit-Learn
- How to plot ROC curves for every cross-validations using Caret
.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.