1

For a image segmentation problem, I need to write a custom loss function. I am getting below said error.

Code Base: https://www.tensorflow.org/tutorials/images/segmentation

Last layer: Conv2DTrans (128,128,2) [Note that in my case it is only 2 values]

       def call(self, y_true, y_pred):
              ytrue = ytrue.numpy()
              .....

Error:

AttributeError: 'Tensor' object has no attribute 'numpy'

I tried py_function and numpy_function but both return the same error and also

      with tf.compat.v1.Session() as sess:
        for i,j in enumerate(sess.run(y_true),sess.run(y_pred)):
Current Model Layers:


Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_82 (InputLayer)           [(None, 128, 128, 3) 0                                            
__________________________________________________________________________________________________
model_80 (Model)                [(None, 64, 64, 96), 1841984     input_82[0][0]                   
__________________________________________________________________________________________________
sequential_160 (Sequential)     (None, 8, 8, 512)    1476608     model_80[1][4]                   
__________________________________________________________________________________________________
concatenate_160 (Concatenate)   (None, 8, 8, 1088)   0           sequential_160[0][0]             
                                                                 model_80[1][3]                   
__________________________________________________________________________________________________
sequential_161 (Sequential)     (None, 16, 16, 256)  2507776     concatenate_160[0][0]            
__________________________________________________________________________________________________
concatenate_161 (Concatenate)   (None, 16, 16, 448)  0           sequential_161[0][0]             
                                                                 model_80[1][2]                   
__________________________________________________________________________________________________
sequential_162 (Sequential)     (None, 32, 32, 128)  516608      concatenate_161[0][0]            
__________________________________________________________________________________________________
concatenate_162 (Concatenate)   (None, 32, 32, 272)  0           sequential_162[0][0]             
                                                                 model_80[1][1]                   
__________________________________________________________________________________________________
sequential_163 (Sequential)     (None, 64, 64, 64)   156928      concatenate_162[0][0]            
__________________________________________________________________________________________________
concatenate_163 (Concatenate)   (None, 64, 64, 160)  0           sequential_163[0][0]             
                                                                 model_80[1][0]                   
__________________________________________________________________________________________________
conv2d_transpose_204 (Conv2DTra (None, 128, 128, 2)  2882        concatenate_163[0][0]            
==================================================================================================

I need a numpy array to focus only more on 1's and not on zeroes. Now the metric and accuracy are overwhelmed by the presence of lot of zeros.

def tumor_loss(y_true,y_pred):
  y_true = y_true.reshape((SHAPE,SHAPE))
  y_pred = y_pred.reshape((SHAPE,SHAPE))

  y_true_ind = np.where(y_true ==1)[1]
  y_pred_ind = np.where(y_pred==1)[1]

  if np.array_equal(y_true_ind,y_pred_ind):
    return 0

  if y_true_ind.shape[0] > y_pred_ind.shape[0]:
    return y_true_ind.shape[0] - np.setdiff1d(y_true_ind,y_pred_ind).shape[0]
  else:
    return y_true_ind.shape[0] - np.setdiff1d(y_pred_ind,y_true_ind).shape[0]

Padmaraj Bhat
  • 136
  • 2
  • 10
  • Are you trying to write a custom loss or a custom metric? https://stackoverflow.com/a/48281534/8342910 A loss function needs to be differentiable, and your function isn't. – Viktoriya Malyasova Nov 02 '19 at 09:49
  • @seed My loss expression here is similar to mean absolute error (MAE). Mean is of course is what I have to compute at the end. Please correct me if it is not so.. I am finding difficulty in processing of y_pred and y_true !!!! – Padmaraj Bhat Nov 02 '19 at 10:19
  • MAE is differentiable everywhere except zero. Your loss function only takes integer values. Its derivative will be either zero or not defined. – Viktoriya Malyasova Nov 02 '19 at 15:00
  • interesting...I dig up more about the loss function... However, here is the I am facing Programming challenge ...pls help me in this regard – Padmaraj Bhat Nov 03 '19 at 00:43

1 Answers1

0

If you are running on tf version >= 2.0, try using

model.compile(loss=custom_loss, optimizer='adam', run_eagerly=True)

if you are using Keras api.

Vlade
  • 89
  • 1
  • 7