Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with SSIM loss function in tensorflow for RGB images

I want to use SSIM metric as my loss function for the model I'm working on in tensorflow. SSIM should measure the similarity between my reconstructed output image of my denoising autoencoder and the input uncorrupted image (RGB).

As of what I understood, for using the SSIM metric in tensorflow, the images should be normalized to [0,1] or [0,255] and not [-1,1]. After converting my tensors to [0,1] and implementing SSIM as my loss function, the reconstructed image is black and white instead of a colorful RGB image.

tf.reduce_mean(tf.image.ssim(reconstructed, truth, 1.0))

My model is working fine with MSE (mean squared error), the reconstructed images are colorful (RGB).

using tf.losses.mean_squared_error(truth, reconstructed) the reconstructed image would be RGB image, while using SSIM would give me a one dimensional image.

Why using SSIM as loss function gives me different result than MSE (in terms of reconstructed image channels) in tensorflow?

like image 621
Yousif H Avatar asked Oct 14 '18 00:10

Yousif H


People also ask

Can SSIM be used as a loss function?

We can safely conclude that SSIM is an accurate way, at least better than MSE, to calculate how images can be similar. Therefore, it also makes sense to use SSIM as the Loss function during training of (convolutional) neural networks.

How do I find the SSIM of an image?

It's defined as r*(x, y) = σxy/σxσy when σxσy ≠ 0, 1 when both standard deviations are zero, and 0 when only one is zero. It has found use in analyzing human response to contrast-detail phantoms. SSIM has also been used on the gradient of images, making it "G-SSIM".

What is Psnr and SSIM?

Peak signal to noise ratio (PSNR) and structural index similarity (SSIM) are two measuring tools that are widely used in image quality assessment. Especially in the steganography image, these two measuring instruments are used to measure the quality of imperceptibility.

What is a good SSIM score?

The SSIM values ranges between 0 to 1, 1 means perfect match the reconstruct image with original one. Generally SSIM values 0.97, 0.98, 0.99 for good quallty recontruction techniques.


3 Answers

I was capable of solving the issue by changing the dynamic range of the images to 2.0, since I have images scaled between [-1, 1] by:

loss_rec = tf.reduce_mean(tf.image.ssim(truth, reconstructed, 2.0))

And since a better image quality is shown by a higher SSIM value, I had to minimize the negative of my loss function (SSIM) to optimize my model:

optimizer = tf.train.AdamOptimizer(learning_rate).minimize(-1 * loss_rec)

like image 159
Yousif H Avatar answered Oct 09 '22 17:10

Yousif H


SSIM is designed to only measure the difference between two luminance signals. The RGB images are converted to greyscale before measuring similarity. If that was fed back into the loss function, it wouldn't know if the image was losing color saturation because it wouldn't show up in the error metric. That's just a theory.

like image 37
James Stanard Avatar answered Oct 09 '22 17:10

James Stanard


The TensorFlow documentation says that no colorspace conversion is applied.

https://www.tensorflow.org/api_docs/python/tf/image/ssim

"Note: The true SSIM is only defined on grayscale. This function does not perform any colorspace transform. (If input is already YUV, then it will compute YUV SSIM average.)"

like image 1
ai2ys Avatar answered Oct 09 '22 16:10

ai2ys