How can I add labels to TensorBoard Images?
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 image tensors very well, but it does not provide a built-in caption field for each image tile in the image grid. If you want labels to appear with the images, the usual solutions are to draw the label onto the image before logging it or to log the labels separately with tf.summary.text.
Understand What tf.summary.image Actually Logs
The tf.summary.image API expects a batch of image tensors with shape batch x height x width x channels. It stores the image data in the event file and TensorBoard renders those images later, but it does not attach a per-image text caption in the way a plotting library might.
That means two different goals require two different approaches:
- If you want visible text on top of the image, modify the pixels before logging.
- If you want the labels available beside the image summary, log a matching text summary.
Knowing this limitation early prevents a lot of time spent looking for a non-existent caption argument.
Draw Labels Onto the Image Before Logging
The most direct way to show labels in TensorBoard is to annotate the images first. The example below uses Pillow to draw a label strip on each image, then logs the annotated batch with TensorBoard.
This approach works well for predicted classes, filenames, confidence scores, or dataset split markers because the information is literally embedded into the image TensorBoard renders.
Log Text Labels Beside the Image Summary
If you do not want to modify the image pixels, log a text summary alongside the image summary. The text will appear in TensorBoard's text dashboard rather than inside the image tile, but it is often good enough for debugging.
This is useful when you want the raw image to remain untouched, such as in medical imaging or when exact pixel values matter.
A practical pattern is to log both:
- annotated images for quick visual review
- text summaries for machine-readable labels
That combination makes it much easier to compare predictions with ground truth during experiments.
Match Images and Labels Carefully
The hard part is not the logging call itself. It is keeping label order aligned with the image batch. If the image batch is shuffled, augmented, or batched dynamically, the label list must go through the same pipeline.
For example, if you log model predictions during validation, make sure the labels you draw are generated from the same batch and in the same order:
Once you have predicted_labels, you can pass them into the annotation function shown earlier and log the resulting images.
Common Pitfalls
- Expecting
tf.summary.imageto support a per-image caption field. It does not, so labels must be embedded or logged separately. - Forgetting to keep labels in the same order as the image batch. A mismatch makes debugging harder, not easier.
- Drawing text on float images without converting the value range first. Many image libraries expect integer pixel values.
- Logging too many large images every training step. TensorBoard event files can grow quickly and slow down the workflow.
- Using very small images and long labels. The text becomes unreadable unless you reserve space or enlarge the image.
Summary
- TensorBoard image summaries do not provide native per-tile captions.
- To show labels inside the image view, draw the text onto the image before calling
tf.summary.image. - To keep the original pixels unchanged, log matching labels with
tf.summary.text. - Make sure image order and label order stay synchronized throughout the pipeline.
- Keep image logging selective so experiment logs remain usable and reasonably small.
Related reading
- How can I assign a class_weight in Keras in a simple way?
- How can I clear a model created with Keras and Tensorflowas backend?
- How can I combine ImageDataGenerator with TensorFlow datasets in TF2?
- How can I compute element-wise conditionals on batches in TensorFlow?
- How can I build libtensorflow.so for the Tensorflow Rust bindings without SSE?
- How can I change the shape of a variable in TensorFlow?
- How can I apply reinforcement learning to continuous action spaces?
- How can I apply reinforcement learning to continuous action spaces?
.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.