nikhilroxtomar / Deep-Residual-Unet

ResUNet, a semantic segmentation model inspired by the deep residual learning and UNet. An architecture that take advantages from both(Residual and UNet) models.
https://idiotdeveloper.com/what-is-resunet/
221 stars 55 forks source link

Dice Loss TypeError #4

Open caginozkaya opened 1 year ago

caginozkaya commented 1 year ago

I am getting TypeError when I run the dice_coef_loss in model.fit:

TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_15776/2786157346.py in ----> 1 results = model.fit(X_train, Y_train, validation_split=0.1, batch_size=4, callbacks = model_checkpoint_callback, epochs=100)

C:\ProgramData\Anaconda3\envs\env1\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs) 65 except Exception as e: # pylint: disable=broad-except 66 filtered_tb = _process_traceback_frames(e.traceback) ---> 67 raise e.with_traceback(filtered_tb) from None 68 finally: 69 del filtered_tb

C:\ProgramData\Anaconda3\envs\env1\lib\site-packages\tensorflow\python\framework\func_graph.py in autograph_handler(*args, **kwargs) 1145 except Exception as e: # pylint:disable=broad-except 1146 if hasattr(e, "ag_error_metadata"): -> 1147 raise e.ag_error_metadata.to_exception(e) 1148 else: 1149 raise

TypeError: in user code:

File "C:\ProgramData\Anaconda3\envs\env1\lib\site-packages\keras\engine\training.py", line 1021, in train_function  *
    return step_function(self, iterator)
File "C:\../ipykernel_15776/709000421.py", line 11, in dice_coef_loss  *
    return float(1.0 - dice_coef(y_true, y_pred))
File "C:\..\Local\Temp/ipykernel_15776/2098934712.py", line 6, in dice_coef  *
    intersection = tf.reduce_sum(y_true_f * y_pred_f)

TypeError: Value passed to parameter 'x' has DataType bool not in list of allowed values: b
caginozkaya commented 1 year ago

Fixed. I think the problem is related to tf version.I have also added tf.cast. Here is the code:

smooth = 1.

def dice_coef(y_true, y_pred): y_true_f = tf.compat.v1.layers.flatten(y_true) y_pred_f = tf.compat.v1.layers.flatten(y_pred) intersection = tf.compat.v1.reduce_sum(y_true_f y_pred_f) return (2. intersection + smooth) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + smooth)

def dice_coef_loss(y_true, y_pred): y_true = tf.compat.v1.cast(y_true, tf.float32) pred = tf.compat.v1.cast(y_true, tf.float32) return 1.0 - dice_coef(y_true, y_pred)