-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
85 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import numpy as np | ||
import tensorflow.keras.backend as K | ||
import tensorflow as tf | ||
from tensorflow.keras.losses import binary_crossentropy | ||
from core.metrics import dice_coef | ||
|
||
|
||
def IoU(y_true, y_pred, eps=1e-6): | ||
if np.max(y_true) == 0.: | ||
return IoU(1-y_true, 1-y_pred) ## empty image; calc IoU of zeros | ||
def dice_p_bce(y_true, y_pred): | ||
y_true = tf.cast(y_true, dtype=tf.float32) | ||
y_pred = tf.cast(y_pred, dtype=tf.float32) | ||
|
||
intersection = K.sum(y_true * y_pred, axis=[1, 2, 3]) | ||
union = K.sum(y_true, axis=[1, 2, 3]) + K.sum(y_pred, axis=[1, 2, 3]) - intersection | ||
return -K.mean( (intersection + eps) / (union + eps), axis=0) | ||
return 1e-3 * binary_crossentropy(y_true, y_pred) - dice_coef(y_true, y_pred) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
import tensorflow as tf | ||
import tensorflow.keras.backend as K | ||
|
||
|
||
def dice(y_true, y_pred): | ||
y_true = tf.cast(y_true, tf.float32) | ||
y_pred = tf.math.sigmoid(y_pred) | ||
numerator = 2 * tf.reduce_sum(y_true * y_pred) | ||
denominator = tf.reduce_sum(y_true + y_pred) | ||
return numerator / denominator | ||
def dice_coef(y_true, y_pred, smooth=1): | ||
y_true = tf.cast(y_true, dtype=tf.float32) | ||
y_pred = tf.cast(y_pred, dtype=tf.float32) | ||
|
||
intersection = K.sum(y_true * y_pred, axis=[1, 2, 3]) | ||
union = K.sum(y_true, axis=[1, 2, 3]) + K.sum(y_pred, axis=[1, 2, 3]) | ||
return K.mean((2. * intersection + smooth) / (union + smooth), axis=0) | ||
|
||
|
||
def POD(y_true, y_pred): | ||
y_true = tf.cast(y_true, dtype=tf.float32) | ||
y_pred = tf.cast(y_pred, dtype=tf.float32) | ||
|
||
y_true_pos = K.flatten(y_true) | ||
y_pred_pos = K.flatten(y_pred) | ||
true_pos = K.sum(y_true_pos * y_pred_pos) | ||
false_neg = K.sum(y_true_pos * (1 - y_pred_pos)) | ||
return true_pos / (true_pos + false_neg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from tensorflow.keras.optimizers import AdamW | ||
from core.data.preprocessing import SemanticSegmentationDataGenerator | ||
from core.losses import dice_p_bce | ||
from core.metrics import POD, dice_coef | ||
|
||
|
||
def train(model, img_dir, config, img_scaling, callbacks, train_df, valid_df, transform): | ||
batch_size = config['batch_size'] | ||
model.compile(optimizer=AdamW(learning_rate=config['learning_rate']), loss=dice_p_bce, | ||
metrics=['binary_accuracy', dice_coef, POD], run_eagerly=True) | ||
|
||
train_data_generator = SemanticSegmentationDataGenerator( | ||
train_df, img_dir, batch_size, img_scaling, config['do_augmentation'], transform | ||
) | ||
val_data_generator = SemanticSegmentationDataGenerator(valid_df, img_dir, batch_size, img_scaling) | ||
loss_history = model.fit( | ||
train_data_generator, | ||
steps_per_epoch=len(train_data_generator) // train_data_generator.batch_size, | ||
epochs=config['epochs'], | ||
validation_data=val_data_generator, | ||
validation_steps=len(val_data_generator) // val_data_generator.batch_size, | ||
callbacks=callbacks, | ||
verbose=1, | ||
workers=1, | ||
) | ||
|
||
return model, loss_history |