SSIM(Structural Similarity),结构相似性,是一种衡量两幅图像相似度的指标。常用于衡量生成模型的图像生成能力。
记录tensorflow下评估源图像与生成图像SSIM的code。有三个要点:
1.解决输入图像的img.shape不存在问题,需要代码最前面加入tf.enable_eager_execution()
2.value = SSIM_LOSS(img1, img2)函数输入格式问题,必须先通过resize将两图像大小,维度保持一致
3.img维度问题:img1/img2均需为4维张量,故用tf.expand_dims(img2, axis=0)扩充了维度。
img1 = tf.image.decode_image(tf.io.read_file('./test_imgs/IR5_ds.bmp'), channels=0)是灰度/bmp格式图像的参数,若为rgb,channels应设置为3。
import tensorflow as tf
#tf.compat.v1.disable_eager_execution()
import numpy as np
def _tf_fspecial_gauss(size, sigma):
"""Function to mimic the 'fspecial' gaussian MATLAB function
"""
x_data, y_data = np.mgrid[-size // 2 + 1:size // 2 + 1, -size // 2 + 1:size // 2 + 1]
x_data = np.expand_dims(x_data, axis = -1)
x_data = np.expand_dims(x_data, axis = -1)
y_data = np.expand_dims(y_data, axis = -1)
y_data = np.expand_dims(y_data, axis = -1)
x = tf.constant(x_data, dtype = tf.float32)
y = tf.constant(y_data, dtype = tf.float32)
g = tf.exp(-((x ** 2 + y ** 2) / (2.0 * sigma ** 2)))
return g / tf.reduce_sum(g)
def SSIM_LOSS(img1, img2, size = 11, sigma = 1.5):
window = _tf_fspecial_gauss(size, sigma) # window shape [size, size]
K1 = 0.01
K2 = 0.03
L = 1 # depth of image (255 in case the image has a different scale)
C1 = (K1 * L) ** 2
C2 = (K2 * L) ** 2
mu1 = tf.nn.conv2d(img1, window, strides = [1, 1, 1, 1], padding = 'VALID')
mu2 = tf.nn.conv2d(img2, window, strides = [1, 1, 1, 1], padding = 'VALID')
mu1_sq = mu1 * mu1
mu2_sq = mu2 * mu2
mu1_mu2 = mu1 * mu2
sigma1_sq = tf.nn.conv2d(img1 * img1, window, strides = [1, 1, 1, 1], padding = 'VALID') - mu1_sq
sigma2_sq = tf.nn.conv2d(img2 * img2, window, strides = [1, 1, 1, 1], padding = 'VALID') - mu2_sq
sigma12 = tf.nn.conv2d(img1 * img2, window, strides = [1, 1, 1, 1], padding = 'VALID') - mu1_mu2
value = (2.0 * sigma12 + C2) / (sigma1_sq + sigma2_sq + C2)
value = tf.reduce_mean(value)
return value
def main():
tf.enable_eager_execution()
img1 = tf.image.decode_image(tf.io.read_file('./generated_imgs/XXX.bmp'), channels=0)
img2 = tf.image.decode_image(tf.io.read_file('./test_imgs/XXX.bmp'), channels=0) # assuming your image is RGB
print(img1.shape)
img2_height, img2_width = img2.shape[0], img2.shape[1] ## img1 当前的形状是 [268, 360, 1]
img1 = tf.image.resize(img1, [img2_height, img2_width], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
img1 = tf.image.convert_image_dtype(img1, tf.float32)
img2 = tf.image.convert_image_dtype(img2, tf.float32)
img1_shape = img1.shape
# 使用 tf.expand_dims 增加一个额外的维度,表示批处理大小为 1
img1 = tf.expand_dims(img1, axis=0)
assert img1.shape == [1] + img1_shape
img2_shape = img2.shape
img2 = tf.expand_dims(img2, axis=0)
assert img2.shape == [1] + img2_shape
value = SSIM_LOSS(img1, img2)
print('hi')
print(type(value))
with tf.Session():
print(value.numpy())
#print(value.eval())
#print('SSIM_Value:', num.numpy())
if __name__ == '__main__':
main()