问题描述
在用skimage的函数计算两张图片的SSIM时:
from skimage.metrics import structural_similarity as SSIM
我想在模型训练过程中计算验证集GT和pre图片的SSIM,对输入SSIM函数的图像shape已转换为 (B,H,W,C)
:
SSIM((data_val[1].numpy().transpose(0, 2, 3, 1)*255).astype(np.uint8),
(pred_rgb.detach().cpu().numpy().transpose(0, 2, 3, 1)*255).astype(np.uint8), multichannel=True))
此时已经设置 multichannel=True
,但是在验证集evaluation的过程中依旧报错:
ValueError: win_size exceeds image extent. If the input is a multichannel (color) image, set multichannel=True.
出现原因
进到 structural_similarity
源码,发现win_size默认大小为7:
if win_size is None:
if gaussian_weights:
r = int(truncate * sigma + 0.5)
win_size = 2 * r + 1
else:
win_size = 7
win_size大小要大于图片的shape,否则的话就会raise ValueError:
if np.any((np.asarray(im1.shape) - win_size) < 0):
raise ValueError(
"win_size exceeds image extent. If the input is a multichannel "
"(color) image, set multichannel=True.")
本人验证集50张图片, batch size=16
,在计算SSIM时传进去的数组尺寸为 (B,H,W,C)
,即使再设置 multichannel=True
后也需要 im.shape>win_size
,因此当验证集按照bs=16,取到最后一个batch时输入尺寸为 (2,H,W,C)
,此时 B<win_size< code>如果像我这样按照一个batch去计算SSIM,当出现一个batch中数量小于win_size时则会出现上诉错误!</win_size<>
解决方法
欢迎讨论与补充。
使用较新版本的scikit-image这个库计算两图片ssim时,报Warning:
FutureWarning: multichannel
is a deprecated argument name for structural_similarity
. It will be removed in version 1.0. Please use channel_axis
instead.
我目前使用的是0.19.版本的 scikit-image
,根据提示,在1.0.版本之后将会移除参数 structural_similarity
,将其替换成 channel_axis
参数,其中,对 channel_axis
的描述为:
channel_axis : int or None, optional
If None, the image is assumed to be a grayscale (single channel) image.
Otherwise, this parameter indicates which axis of the array corresponds
to channels.
翻译一下就是这个参数指定的是数组里面通道数的axis,为None的话默认是计算灰度图像的ssim。
因此,对三通道数组进行计算SSIM时,例如数组维度为 (H, W, C)
,此时指定 channel_axis=-1
即可,不用再指定 multichannel=True
。
Original: https://blog.csdn.net/xwyljt/article/details/124408257
Author: 圆圆的阿司匹林大药片
Title: 用skimage里的函数计算彩色图像SSIM时,明明设置multichannel=True却还是报错ValueError: win_size exceeds image extent.
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/632730/
转载文章受原作者版权保护。转载请注明原作者出处!