简体   繁体   中英

MS-SSIM in tensorflow

I want to use tf.image.ssim_multiscale for cacculate metric MS-SSIM and wrote this simple code. But it has error:

#(x_train.y_train),(x_test,y_test)=tf.keras.datasets.cifar10.load_data()
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
q1=train_images[0:10]
tf.image.ssim_multiscale(q1, q1, max_val=255)
InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true. Summarized data: 10, 8, 8, 3
11

Please refer my original answer here . The issue appears to be with assertion after spatial-dimension reduction. In _ssim_per_channel , the H and W of images is asserted against filter_size . Whereas in ssim_multiscale , downsampling is performed len(power_factors)-1 times.

Here are two workarounds:

  1. Make sure that filter_size is small enough to calculate ssim values for all the four spatial-scales(excluding first scale) after downsampling within ssim_multiscale . Contrarily, ensure both H and W of your image are big enough such that H/(2**4) and W/(2**4) >= filter_size .

  2. Since downsampling is performed len(power_factors)-1 times, you can also use lesser number of _MSSSIM_WEIGHTS or power_factors than default, which means H/(2**(len(power_factors)-1)) and W/(2**(len(power_factors)-1)) >= filter_size .

field1 = tf.random.uniform(shape=[8, 64, 64, 1], minval=0, maxval=1)
field2 = tf.random.uniform(shape=[8, 64, 64, 1], minval=0, maxval=1) 
#Use smaller filter_size
ms_ssim_score = tf.image.ssim_multiscale(img1=field1, img2=field2, max_val=1.0,
                                         filter_size=4)
#Or use lesser number of power_factors
ms_ssim_score = tf.image.ssim_multiscale(img1=field1, img2=field2, max_val=1.0,
                                         power_factors=(0.0448, 0.2856, 0.3001),
                                         filter_size=11)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM