我需要使用Sewar的SSIM作为损失函数,以便比较模型的图像。
尝试编译模型时出现错误。我导入函数并像这样编译模型:
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()])
但是然后我得到了这个错误(显然):
TypeError: ssim() missing 2 required positional arguments: 'GT' and 'P'
我只是想做与mean_sqeared_error相同的操作,但要使用SSIM,就像这样(它完美地工作,不需要将参数传递给它):
model.compile('mean_squared_error', optimizer=my_optimizer, metrics=['mse'])
关于我应该如何使用此函数进行编译的任何想法?
答案 0 :(得分:2)
Keras具有SSIM的实现。您可以像这样使用它:
def SSIMLoss(y_true, y_pred):
return 1 - tf.reduce_mean(tf.image.ssim(y_true, y_pred, 1.0))
self.model.compile(optimizer=sgd, loss=SSIMLoss)
答案 1 :(得分:0)
您需要创建自己的自定义损失函数才能使用外部损失。 但是,必须使用Tensorflow的张量和不是数值或矩阵来适应这些损失,所以这不是那么简单。
我建议您看看如何编写自定义损失函数,其中有很多不错的教程,例如this one。
答案 2 :(得分:0)
tf.image.ssim
计算两个图像之间的SSIM索引。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))