forked from saky-semicolon/Retinal-Layer-Segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraining Strategy.py
More file actions
30 lines (23 loc) · 1.07 KB
/
Training Strategy.py
File metadata and controls
30 lines (23 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""## 4. Training Strategy
### Defining Custom Loss & Metrics
📌 **Description:**
Defines evaluation metrics to assess model performance.
✅ **Metrics Implemented:**
- **Dice Coefficient** → Measures segmentation overlap.
- **Jaccard Coefficient** → Measures intersection over union (IoU).
- **Custom Loss Function** → Uses categorical cross-entropy + Dice loss for better segmentation accuracy.
"""
# Dice and Jaccard coefficients
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + K.epsilon()) / (K.sum(y_true_f) + K.sum(y_pred_f) + K.epsilon())
def jaccard_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return intersection / (K.sum(y_true_f) + K.sum(y_pred_f) - intersection + K.epsilon())
# Custom loss function
def customized_loss(y_true, y_pred):
return tf.keras.losses.categorical_crossentropy(y_true, y_pred) + 0.5 * (1 - dice_coef(y_true, y_pred))