jpdefrutos commited on
Commit
8fb4d8e
·
1 Parent(s): fd0e2dc

Implemented NCC loss function

Browse files
.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
DeepDeformationMapRegistration/losses.py CHANGED
@@ -2,6 +2,7 @@ import tensorflow as tf
2
  from scipy.ndimage import generate_binary_structure
3
 
4
  from DeepDeformationMapRegistration.utils.operators import soft_threshold
 
5
 
6
 
7
  class HausdorffDistanceErosion:
@@ -46,3 +47,25 @@ class HausdorffDistanceErosion:
46
 
47
  return batched_dist
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from scipy.ndimage import generate_binary_structure
3
 
4
  from DeepDeformationMapRegistration.utils.operators import soft_threshold
5
+ from DeepDeformationMapRegistration.utils.constants import EPS_tf
6
 
7
 
8
  class HausdorffDistanceErosion:
 
47
 
48
  return batched_dist
49
 
50
+
51
+ class NCC:
52
+ def __init__(self, in_shape, eps=EPS_tf):
53
+ self.__shape_size = tf.cast(tf.reduce_prod(in_shape), tf.float32)
54
+ self.__eps = eps
55
+
56
+ def ncc(self, y_true, y_pred):
57
+ f_yt = tf.reshape(y_true, [-1])
58
+ f_yp = tf.reshape(y_pred, [-1])
59
+ mean_yt = tf.reduce_mean(f_yt)
60
+ mean_yp = tf.reduce_mean(f_yp)
61
+ std_yt = tf.math.reduce_std(f_yt)
62
+ std_yp = tf.math.reduce_std(f_yp)
63
+
64
+ n_f_yt = f_yt - mean_yt
65
+ n_f_yp = f_yp - mean_yp
66
+ numerator = tf.reduce_sum(n_f_yt * n_f_yp)
67
+ denominator = std_yt * std_yp * self.__shape_size + self.__eps
68
+ return tf.math.divide_no_nan(numerator, denominator)
69
+
70
+ def loss(self, y_true, y_pred):
71
+ return tf.map_fn(lambda x: 1 - self.ncc(x[0], x[1]), (y_true, y_pred), tf.float32)