简体   繁体   English

如何在 keras 中为损失函数实现 ssim?

[英]how do I implement ssim for loss function in keras?

I need SSIM as a loss function in my network, but my network has 2 outputs.我需要 SSIM 作为我网络中的损失函数,但我的网络有 2 个输出。 I need to use SSIM for first output and cross-entropy for the next.我需要将 SSIM 用于第一个输出,并为下一个输出使用cross-entropy The loss function is a combination of them.损失函数是它们的组合。 However, I need to have a higher SSIM and lower cross-entropy , so I think the combination of them isn't true.但是,我需要更高的 SSIM 和更低的cross-entropy ,所以我认为它们的组合是不正确的。 Another problem is that I could not find an implementation of SSIM in keras.另一个问题是我在 keras 中找不到 SSIM 的实现。

Tensorflow has tf.image.ssim , but it accepts the image and I do not think I can use it in loss function, right? Tensorflow 有tf.image.ssim ,但它接受图像,我认为我不能在损失函数中使用它,对吧? Could you please tell me what should I do?你能告诉我我该怎么办吗? I am a beginner in keras and deep learning and I do not know how can I make SSIM as a custom loss function in keras.我是 keras 和深度学习的初学者,我不知道如何将 SSIM 作为 keras 中的自定义损失函数。

Have a look here for SSIM loss in Keras. 在此处查看Keras中的SSIM丢失。 You can write a custom loss function and create SSIM loss for one prediction and cross-entropy for another. 您可以编写自定义损失函数,并为一种预测创建SSIM损失,为另一种预测创建交叉熵。 You can return a weighted sum of the two losses as the final loss. 您可以返回两个损失的加权和作为最终损失。

other choice would be ssim_loss = 1 - tf.reduce_mean(tf.image.ssim(target, output, max_val=self.max_val))其他选择是ssim_loss = 1 - tf.reduce_mean(tf.image.ssim(target, output, max_val=self.max_val))

then combine_loss = mae (or mse) + ssim_loss In this way, you are minimizing both of them.然后combine_loss = mae (or mse) + ssim_loss这样,你就最小化了它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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