Last active
August 3, 2020 22:43
-
-
Save Dref360/a48feaecfdb9e0609c6a02590fd1f91b to your computer and use it in GitHub Desktop.
Difference of stuctural similarity using Tensorflow and keras. Works ONLY on tf >= 0.11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras.backend as K | |
import tensorflow as tf | |
class Model: | |
def __init__(self,batch_size): | |
self.batch_size = batch_size | |
def loss_DSSIS_tf11(self, y_true, y_pred): | |
"""Need tf0.11rc to work""" | |
y_true = tf.reshape(y_true, [self.batch_size] + get_shape(y_pred)[1:]) | |
y_pred = tf.reshape(y_pred, [self.batch_size] + get_shape(y_pred)[1:]) | |
y_true = tf.transpose(y_true, [0, 2, 3, 1]) | |
y_pred = tf.transpose(y_pred, [0, 2, 3, 1]) | |
patches_true = tf.extract_image_patches(y_true, [1, 5, 5, 1], [1, 2, 2, 1], [1, 1, 1, 1], "SAME") | |
patches_pred = tf.extract_image_patches(y_pred, [1, 5, 5, 1], [1, 2, 2, 1], [1, 1, 1, 1], "SAME") | |
u_true = K.mean(patches_true, axis=3) | |
u_pred = K.mean(patches_pred, axis=3) | |
var_true = K.var(patches_true, axis=3) | |
var_pred = K.var(patches_pred, axis=3) | |
std_true = K.sqrt(var_true) | |
std_pred = K.sqrt(var_pred) | |
c1 = 0.01 ** 2 | |
c2 = 0.03 ** 2 | |
ssim = (2 * u_true * u_pred + c1) * (2 * std_pred * std_true + c2) | |
denom = (u_true ** 2 + u_pred ** 2 + c1) * (var_pred + var_true + c2) | |
ssim /= denom | |
ssim = tf.select(tf.is_nan(ssim), K.zeros_like(ssim), ssim) | |
return K.mean(((1.0 - ssim) / 2)) |
Aspie96
commented
Oct 15, 2019
via email
Ok, thank you very much!
Il giorno mar 15 ott 2019 alle ore 16:17 Frédéric Branchaud-Charron <
notifications@github.com> ha scritto:
… You can probably use
https://www.tensorflow.org/api_docs/python/tf/image/ssim
In any case, this has been merged to keras-contrib a while ago.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/a48feaecfdb9e0609c6a02590fd1f91b?email_source=notifications&email_token=ADJ3F5PP7T6PAQTUOZU2HF3QOXGHVA5CNFSM4H7N7DB2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF2QOU#gistcomment-3055850>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADJ3F5JCXFP7MWEEH3ZMSLDQOXGHVANCNFSM4H7N7DBQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment