-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloss.py
18 lines (15 loc) · 929 Bytes
/
loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import tensorflow as tf
from tensorflow.keras import backend as K
def iou_coef_MC_for_loss(y_true, y_pred):
smooth = 1 # Smoothing factor to prevent division by zero
threshold = 0.5 # Threshold value for binary classification
y_true = tf.cast(y_true, tf.float32) # Convert true labels to float32
y_pred = tf.cast(y_pred, tf.float32) # Convert predicted labels to float32
intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2]) # Calculate intersection
union = K.sum(y_true,[1,2])+K.sum(y_pred,[1,2])-intersection # Calculate union
iou = K.mean((intersection + smooth) / (union + smooth)) # Compute IoU score
return iou
def iou_loss_MC(y_true, y_pred):
y_true = tf.cast(y_true, tf.float32) # Convert true labels to float32
y_pred = tf.cast(y_pred, tf.float32) # Convert predicted labels to float32
return 1-iou_coef_MC_for_loss(y_true, y_pred)