@@ -113,19 +113,50 @@ def normalized_mean_square_error(output, target):
113113
114114 Parameters
115115 ----------
116- output : 2D or 4D tensor.
117- target : 2D or 4D tensor.
116+ output : 2D, 3D or 4D tensor i.e. [batch_size, n_feature], [batch_size, w, h] or [batch_size, w, h, c] .
117+ target : 2D, 3D or 4D tensor.
118118 """
119119 with tf .name_scope ("mean_squared_error_loss" ):
120120 if output .get_shape ().ndims == 2 : # [batch_size, n_feature]
121121 nmse_a = tf .sqrt (tf .reduce_sum (tf .squared_difference (output , target ), axis = 1 ))
122122 nmse_b = tf .sqrt (tf .reduce_sum (tf .square (target ), axis = 1 ))
123+ elif output .get_shape ().ndims == 3 : # [batch_size, w, h]
124+ nmse_a = tf .sqrt (tf .reduce_sum (tf .squared_difference (output , target ), axis = [1 ,2 ]))
125+ nmse_b = tf .sqrt (tf .reduce_sum (tf .square (target ), axis = [1 ,2 ]))
123126 elif output .get_shape ().ndims == 4 : # [batch_size, w, h, c]
124127 nmse_a = tf .sqrt (tf .reduce_sum (tf .squared_difference (output , target ), axis = [1 ,2 ,3 ]))
125128 nmse_b = tf .sqrt (tf .reduce_sum (tf .square (target ), axis = [1 ,2 ,3 ]))
126129 nmse = tf .reduce_mean (nmse_a / nmse_b )
127130 return nmse
128131
132+ def absolute_difference_error (output , target , is_mean = False ):
133+ """ Return the TensorFlow expression of absolute difference error (L1) of two batch of data.
134+
135+ Parameters
136+ ----------
137+ output : 2D, 3D or 4D tensor i.e. [batch_size, n_feature], [batch_size, w, h] or [batch_size, w, h, c].
138+ target : 2D, 3D or 4D tensor.
139+ is_mean : boolean, if True, use ``tf.reduce_mean`` to compute the loss of one data, otherwise, use ``tf.reduce_sum`` (default).
140+ """
141+ with tf .name_scope ("mean_squared_error_loss" ):
142+ if output .get_shape ().ndims == 2 : # [batch_size, n_feature]
143+ if is_mean :
144+ loss = tf .reduce_mean (tf .reduce_mean (tf .abs (output - target ), 1 ))
145+ else :
146+ loss = tf .reduce_mean (tf .reduce_sum (tf .abs (output - target ), 1 ))
147+ elif output .get_shape ().ndims == 3 : # [batch_size, w, h]
148+ if is_mean :
149+ loss = tf .reduce_mean (tf .reduce_mean (tf .abs (output - target ), [1 , 2 ]))
150+ else :
151+ loss = tf .reduce_mean (tf .reduce_sum (tf .abs (output - target ), [1 , 2 ]))
152+ elif output .get_shape ().ndims == 4 : # [batch_size, w, h, c]
153+ if is_mean :
154+ loss = tf .reduce_mean (tf .reduce_mean (tf .abs (output - target ), [1 , 2 , 3 ]))
155+ else :
156+ loss = tf .reduce_mean (tf .reduce_sum (tf .abs (output - target ), [1 , 2 , 3 ]))
157+ else :
158+ raise Exception ("Unknow dimension" )
159+ return loss
129160
130161
131162def dice_coe (output , target , loss_type = 'jaccard' , axis = [1 ,2 ,3 ], smooth = 1e-5 ):
0 commit comments