在我的模型中,隐藏层的输出(即“已编码”)具有两个通道(例如,形状:[none,128、128、2])。我希望在损耗函数的这两个通道之间添加SSIM:
损耗= ssim(输入,输出)+ theta * ssim(已编码(通道1),已编码(通道2))。
我该如何实施?以下是我的模型的体系结构。
def structural_similarity_index(y_true, y_pred):
loss = 1 - tf.image.ssim(y_true, y_pred, max_val=1.0)
return loss
def mymodel():
input_img = Input(shape=(256, 256, 1))
# encoder
x = Conv2D(4, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
encoded = Conv2D(2, (3, 3), activation='relu', padding='same', name='encoder')(x)
# decoder
x = Conv2D(4, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer = 'adadelta', loss = structural_similarity_index)
autoencoder.summary()
return autoencoder
我试图定义一个'loss_warper'函数,如下所示,但是它没有用。这就是我添加此损失函数的方式:
autoencoder.add_loss(loss_wrapper(encoded[:,:,:,0],encoded[:,:,:,1])(input_img, decoded))
'loss_warper'函数:
def loss_wrapper(CH1, CH2):
def structural_similarity_index(y_true, y_pred):
regweight = 0.01
loss = 1 - tf.image.ssim(y_true, y_pred, max_val=1.0)
loss = loss + regweight*(1-tf.image.ssim(CH1, CH2, max_val=1.0))
return loss
return structural_similarity_index
错误消息:
File "E:/Autoencoder.py", line 160, in trainprocess
validation_data= (x_validate, x_validate))
...
ValueError: ('Error when checking model target: expected no data, but got:', array([...]...[...]))
有人知道如何实现吗?非常感谢您的帮助!