SSIM
MS-SSIM
TensorFlow
image quality assessment
deep learning

SSIM / MS-SSIM for TensorFlow

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Structural Similarity Index Measure, or SSIM, is a perceptual image similarity metric that is often more useful than plain pixel error for image generation tasks. TensorFlow exposes both single-scale SSIM and multi-scale SSIM, making it straightforward to use them as evaluation metrics or even as part of a training loss.

What SSIM measures

SSIM compares two images by looking at luminance, contrast, and structure rather than only raw pixel difference. In practice, that means two images can have a modest mean squared error but still look very similar to a person, and SSIM tends to reflect that better than MSE.

In TensorFlow, the two key APIs are:

  • 'tf.image.ssim'
  • 'tf.image.ssim_multiscale'

Both functions expect images with shape [batch, height, width, channels] or [height, width, channels]. The values can be integer or floating point, but max_val must match the range you are using.

Using tf.image.ssim

If your images are normalized to the range from 0.0 to 1.0, pass max_val=1.0. If they are uint8 images in the range from 0 to 255, pass max_val=255.

python
1import tensorflow as tf
2
3# Two RGB images in the range [0.0, 1.0]
4img1 = tf.random.uniform((1, 128, 128, 3), minval=0.0, maxval=1.0)
5img2 = tf.random.uniform((1, 128, 128, 3), minval=0.0, maxval=1.0)
6
7score = tf.image.ssim(img1, img2, max_val=1.0)
8print(score.numpy())

The return value is one score per image pair in the batch. A higher score means the images are more similar, with 1.0 representing identical images.

This is a common metric for super-resolution, denoising, compression, and autoencoder evaluation.

What MS-SSIM adds

Regular SSIM measures similarity at one scale. tf.image.ssim_multiscale evaluates the images across multiple downsampled resolutions, which makes it more robust for larger structural differences and multi-resolution details.

python
1import tensorflow as tf
2
3img1 = tf.random.uniform((2, 256, 256, 1), minval=0.0, maxval=1.0)
4img2 = tf.random.uniform((2, 256, 256, 1), minval=0.0, maxval=1.0)
5
6ms_score = tf.image.ssim_multiscale(img1, img2, max_val=1.0)
7print(ms_score.numpy())

MS-SSIM is often preferred in image generation papers because it captures coarse structure and finer details better than a single-scale comparison.

Using SSIM as a loss

Because higher SSIM is better, a simple training loss is 1.0 - ssim. This is common when training image-to-image models.

python
1import tensorflow as tf
2
3def ssim_loss(y_true, y_pred):
4    ssim = tf.image.ssim(y_true, y_pred, max_val=1.0)
5    return 1.0 - tf.reduce_mean(ssim)
6
7model = tf.keras.Sequential([
8    tf.keras.layers.Input(shape=(64, 64, 3)),
9    tf.keras.layers.Conv2D(16, 3, padding="same", activation="relu"),
10    tf.keras.layers.Conv2D(3, 3, padding="same", activation="sigmoid"),
11])
12
13model.compile(optimizer="adam", loss=ssim_loss)

A lot of teams combine SSIM with pixel losses instead of using it alone:

python
1def combined_loss(y_true, y_pred):
2    l1 = tf.reduce_mean(tf.abs(y_true - y_pred))
3    ssim_term = 1.0 - tf.reduce_mean(tf.image.ssim(y_true, y_pred, max_val=1.0))
4    return 0.8 * l1 + 0.2 * ssim_term

That combination usually stabilizes training because pixel loss preserves exact values while SSIM rewards structural similarity.

Choosing between SSIM and MS-SSIM

Use SSIM when you want a simple perceptual metric that is cheap to compute and easy to interpret. Use MS-SSIM when image structure across scales matters more than raw speed.

For small benchmark experiments, SSIM is often enough. For super-resolution, deblurring, or learned compression, MS-SSIM is common because it tracks perceptual quality more closely.

Common Pitfalls

  • Passing the wrong max_val. This is the most common reason for nonsensical scores.
  • Feeding tensors in the wrong shape. TensorFlow expects channels last by default.
  • Using images outside the expected value range after preprocessing or augmentation.
  • Treating SSIM as a perfect proxy for human judgment. It is useful, but it is still a heuristic.
  • Using 1 - SSIM as the only loss and expecting stable convergence for every architecture and dataset.

Summary

  • TensorFlow provides tf.image.ssim and tf.image.ssim_multiscale for perceptual image comparison.
  • SSIM measures structural similarity more usefully than plain pixel error in many vision tasks.
  • MS-SSIM extends the idea across multiple image scales.
  • 'max_val must match the numeric range of your tensors.'
  • A common training pattern is to use 1 - SSIM or combine SSIM with L1 or L2 loss.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.