与Keras一起使用SSIM丢失功能 [英] Use SSIM loss function with Keras
本文介绍了与Keras一起使用SSIM丢失功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用Sewar的SSIM作为损失函数,以便比较模型的图像.
I need to use the SSIM from Sewar as a loss function in order to compare images for my model.
尝试编译模型时出现错误.我导入函数并像这样编译模型:
I am getting errors when I try to compile my model. I import the function and compile the model like this:
from sewar.full_ref import ssim
...
model.compile('ssim', optimizer=my_optimizer, metrics=[ssim])
我明白了:
File "/media/merry/merry32/train.py", line 19, in train
model.compile(loss='ssim', optimizer=opt, metrics=[ssim])
File "/home/merry/anaconda3/envs/merry_env/lib/python3.7/site-packages/keras/engine/training.py", line 451, in compile
handle_metrics(output_metrics)
File "/home/merry/anaconda3/envs/merry_env/lib/python3.7/site-packages/keras/engine/training.py", line 420, in handle_metrics
mask=masks[i])
File "/home/merry/anaconda3/envs/merry_env/lib/python3.7/site-packages/keras/engine/training_utils.py", line 404, in weighted
score_array = fn(y_true, y_pred)
File "/home/merry/anaconda3/envs/merry_env/lib/python3.7/site-packages/sewar/full_ref.py", line 143, in ssim
MAX = np.iinfo(GT.dtype).max
File "/home/merry/anaconda3/envs/merry_env/lib/python3.7/site-packages/numpy/core/getlimits.py", line 506, in __init__
raise ValueError("Invalid integer data type %r." % (self.kind,))
ValueError: Invalid integer data type 'O'.
我也可以这样写:
model.compile(ssim(), optimizer=my_optimizer, metrics=[ssim()])
但随后出现此错误(显然):
But then I get this error (obviously):
TypeError: ssim() missing 2 required positional arguments: 'GT' and 'P'
我只是想做与mean_sqeared_error相同的操作,但是要使用SSIM,就像这样(它不需要传递参数即可完美运行):
I just wanted to do the same I was doing with mean_sqeared_error but with SSIM, like this (which works perfectly with no need of passing parameters to it):
model.compile('mean_squared_error', optimizer=my_optimizer, metrics=['mse'])
关于如何使用此函数进行编译的任何想法?
Any idea on how should I use this function to compile?
推荐答案
- 您可以使用
tf.image.ssim
计算两个图像之间的SSIM索引. - 由于训练是针对一批图像进行的,我们将使用该批次中所有图像的SSIM值的平均值作为损失值
- 我们的模型将返回一张图像(基于所使用的CNN层,该图像的大小会再次基于输入和预期输出图像的尺寸).
- You can use
tf.image.ssim
to compute SSIM index between two images. - Since training happens on batch of images we will use the mean of SSIM values of all the images in the batch as the loss value
- Our model will return an image (of some size based on the CNN layers used which is again based on input and expected output image dimensions).
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
import numpy as np
import tensorflow as tf
# Loss functtion
def ssim_loss(y_true, y_pred):
return tf.reduce_mean(tf.image.ssim(y_true, y_pred, 2.0))
# Model: Input Image size: 32X32X1 output Image size: 28X28X1
# check model.summary
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(32,32,1)))
model.add(Conv2D(1, kernel_size=(3, 3),
activation='relu'))
model.compile(optimizer='adam', loss=ssim_loss, metrics=[ssim_loss, 'accuracy'])
# Train
model.fit(np.random.randn(10,32,32,1), np.random.randn(10,28,28,1))
这篇关于与Keras一起使用SSIM丢失功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文