From 7e71fc4a31c4aff2894e5d09947b355fb1a2947e Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 12:31:06 +0530 Subject: [PATCH 01/12] Update .gitignore --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ebf1ca1b7..f9e3453de 100755 --- a/.gitignore +++ b/.gitignore @@ -105,4 +105,8 @@ ENV/ .ropeproject # VSCode settings -.vscode/ \ No newline at end of file +.vscode/ +.idea/image-segmentation-keras.iml +.idea/inspectionProfiles/profiles_settings.xml +.idea/modules.xml +.idea/inspectionProfiles/Project_Default.xml From f1d3f8d2175f5d8e16895475dc3601439cfbcbdf Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 12:51:29 +0530 Subject: [PATCH 02/12] Update README.md Added the usage of loss function --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c31213855..ef6ccbbea 100644 --- a/README.md +++ b/README.md @@ -170,10 +170,14 @@ from keras_segmentation.models.unet import vgg_unet model = vgg_unet(n_classes=51 , input_height=416, input_width=608 ) +def custom_fun(): + #some loss function + pass + model.train( train_images = "dataset1/images_prepped_train/", train_annotations = "dataset1/annotations_prepped_train/", - checkpoints_path = "/tmp/vgg_unet_1" , epochs=5 + checkpoints_path = "/tmp/vgg_unet_1" , epochs=5, loss_fun = custom_fun() #dice or soft_dice ) out = model.predict_segmentation( @@ -186,9 +190,14 @@ plt.imshow(out) # evaluating the model print(model.evaluate_segmentation( inp_images_dir="dataset1/images_prepped_test/" , annotations_dir="dataset1/annotations_prepped_test/" ) ) - ``` +### Custom loss function +#### Available loss function: + +- Dice loss (usage: `loss_fun = dice`) +- Soft Dice Loss (usage: `loss_fun = soft_dice`) +- and others available in [Losses](https://keras.io/api/losses/) ## Usage via command line You can also use the tool just using command line @@ -229,6 +238,7 @@ python -m keras_segmentation train \ --input_height=320 \ --input_width=640 \ --model_name="vgg_unet" + --loss_fun= "dice" or "soft_dice" ``` Choose model_name from the table above From 68968ff7add610af324ad208a50e7f650c75fcf8 Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 13:39:13 +0530 Subject: [PATCH 03/12] Update train.py Added the implementation of dice coefficient, dice loss, soft dice loss --- keras_segmentation/train.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/keras_segmentation/train.py b/keras_segmentation/train.py index 6306f719d..b1a479f99 100755 --- a/keras_segmentation/train.py +++ b/keras_segmentation/train.py @@ -7,9 +7,40 @@ from keras.callbacks import Callback from tensorflow.keras.callbacks import ModelCheckpoint import tensorflow as tf +from tensorflow.keras import backend as K import glob import sys + +def dice_coef(y_true, y_pred): + """ + Function to calculate dice coefficient + + Parameters + ---------- + y_true : numpy array of actual masks + y_pred : numpy array of predicted masks + + Returns + ------- + dice coefficient + """ + 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 dice_coef_loss(y_true, y_pred): + return 1-dice_coef(y_true, y_pred) + +def soft_dice_loss(y_true, y_pred, axis=(1, 2, 3)): + dice_numerator = 2. * K.sum(y_true * y_pred, axis=axis) + 0.00001 + dice_denominator = K.sum(y_true ** 2, axis=axis) + K.sum(y_pred ** 2, axis=axis) + 0.00001 + dice_loss = 1 - K.mean((dice_numerator) / (dice_denominator)) + return dice_loss + + def find_latest_checkpoint(checkpoints_path, fail_safe=True): # This is legacy code, there should always be a "checkpoint" file in your directory From b41a5eb3f2a29a07ca659372b7e30e6883069c74 Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 13:41:25 +0530 Subject: [PATCH 04/12] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ef6ccbbea..22cc81103 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ def custom_fun(): model.train( train_images = "dataset1/images_prepped_train/", train_annotations = "dataset1/annotations_prepped_train/", - checkpoints_path = "/tmp/vgg_unet_1" , epochs=5, loss_fun = custom_fun() #dice or soft_dice + checkpoints_path = "/tmp/vgg_unet_1" , epochs=5, loss_fun = custom_fun() #dice or soft_dice, default = 'categorical_crossentropy' ) out = model.predict_segmentation( @@ -238,7 +238,7 @@ python -m keras_segmentation train \ --input_height=320 \ --input_width=640 \ --model_name="vgg_unet" - --loss_fun= "dice" or "soft_dice" + --loss_fun= "dice" or "soft_dice" #default = 'categorical_crossentropy' ``` Choose model_name from the table above From 09cb3f8f520071a69f4355a0f59299273a9e27e2 Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 14:02:06 +0530 Subject: [PATCH 05/12] Updated the metrics Added the Jaccard Index metrics --- keras_segmentation/metrics.py | 21 +++++++++++++++++++++ keras_segmentation/train.py | 26 ++++---------------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/keras_segmentation/metrics.py b/keras_segmentation/metrics.py index 0162dd9ce..ea1bfc750 100644 --- a/keras_segmentation/metrics.py +++ b/keras_segmentation/metrics.py @@ -1,4 +1,5 @@ import numpy as np +from tensorflow.keras import backend as K EPS = 1e-12 @@ -11,3 +12,23 @@ def get_iou(gt, pr, n_classes): iou = float(intersection)/(union + EPS) class_wise[cl] = iou return class_wise + +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 + EPS) / (K.sum(y_true_f) + K.sum(y_pred_f) + EPS) + +# def dice_coef(y_true, y_pre, smooth=1): +# # Dice Coefficient is 2 * the Area of Overlap divided by the total number of pixels in both images +# y_true = np.asarray(y_true, 'bool') +# y_pre = np.asarray(y_pre, 'bool') +# inter = np.sum(y_true * y_pre) +# uni = np.sum(y_true) + np.sum(y_pre) +# +# return (2 * inter + smooth) / (uni + smooth) + + +def jacard_index(dice): + return dice / (2 - dice) diff --git a/keras_segmentation/train.py b/keras_segmentation/train.py index b1a479f99..c2d1ec36c 100755 --- a/keras_segmentation/train.py +++ b/keras_segmentation/train.py @@ -10,26 +10,7 @@ from tensorflow.keras import backend as K import glob import sys - - -def dice_coef(y_true, y_pred): - """ - Function to calculate dice coefficient - - Parameters - ---------- - y_true : numpy array of actual masks - y_pred : numpy array of predicted masks - - Returns - ------- - dice coefficient - """ - 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()) - +from metrics import dice_coef def dice_coef_loss(y_true, y_pred): return 1-dice_coef(y_true, y_pred) @@ -109,6 +90,7 @@ def train(model, gen_use_multiprocessing=False, ignore_zero_class=False, optimizer_name='adam', + loss_fun ='categorical_crossentropy', do_augment=False, augmentation_name="aug_all", callbacks=None, @@ -145,11 +127,11 @@ def train(model, if ignore_zero_class: loss_k = masked_categorical_crossentropy else: - loss_k = 'categorical_crossentropy' + loss_k = loss_fun #'categorical_crossentropy' model.compile(loss=loss_k, optimizer=optimizer_name, - metrics=['accuracy']) + metrics=['accuracy',dice_coef]) if checkpoints_path is not None: config_file = checkpoints_path + "_config.json" From bfaec91c807747c846f0fc688bd259c3dcf34c13 Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 14:12:55 +0530 Subject: [PATCH 06/12] Update train.py --- keras_segmentation/train.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/keras_segmentation/train.py b/keras_segmentation/train.py index c2d1ec36c..2f00bbcec 100755 --- a/keras_segmentation/train.py +++ b/keras_segmentation/train.py @@ -10,7 +10,14 @@ from tensorflow.keras import backend as K import glob import sys -from metrics import dice_coef + + +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 dice_coef_loss(y_true, y_pred): return 1-dice_coef(y_true, y_pred) From 342cd37ca0aa3e3217b70d1297d73789df23c2a0 Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 14:20:58 +0530 Subject: [PATCH 07/12] Update pretrained.py --- keras_segmentation/pretrained.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/keras_segmentation/pretrained.py b/keras_segmentation/pretrained.py index 647e8cd9f..e52d7ceb8 100644 --- a/keras_segmentation/pretrained.py +++ b/keras_segmentation/pretrained.py @@ -1,4 +1,5 @@ -import keras +# import keras +import tensorflow.keras as keras from .models.all_models import model_from_name From 991f9f26046d6313d29eaf4cc62811ef596d61d6 Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 14:29:43 +0530 Subject: [PATCH 08/12] updated the tf and keras import module --- keras_segmentation/models/mobilenet.py | 4 ++-- keras_segmentation/models/resnet50.py | 2 +- keras_segmentation/models/vgg16.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/keras_segmentation/models/mobilenet.py b/keras_segmentation/models/mobilenet.py index f8a34f5d7..2d70af4f0 100644 --- a/keras_segmentation/models/mobilenet.py +++ b/keras_segmentation/models/mobilenet.py @@ -1,7 +1,7 @@ from keras.models import * from keras.layers import * -import keras.backend as K -import keras +import tensorflow.keras.backend as K +import tensorflow.keras as keras from .config import IMAGE_ORDERING diff --git a/keras_segmentation/models/resnet50.py b/keras_segmentation/models/resnet50.py index 5bbb3368f..ee13ed8c4 100644 --- a/keras_segmentation/models/resnet50.py +++ b/keras_segmentation/models/resnet50.py @@ -1,4 +1,4 @@ -import keras +import tensorflow.keras as keras from keras.models import * from keras.layers import * from keras import layers diff --git a/keras_segmentation/models/vgg16.py b/keras_segmentation/models/vgg16.py index 922682121..79a763960 100644 --- a/keras_segmentation/models/vgg16.py +++ b/keras_segmentation/models/vgg16.py @@ -1,4 +1,4 @@ -import keras +import tensorflow.keras as keras from keras.models import * from keras.layers import * From 85ca9dd7c7561a9b8b37bf92e3d22c47e03efcf9 Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 14:38:18 +0530 Subject: [PATCH 09/12] Update model_compression.py --- keras_segmentation/model_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras_segmentation/model_compression.py b/keras_segmentation/model_compression.py index a461aa7c4..cb96ee28a 100644 --- a/keras_segmentation/model_compression.py +++ b/keras_segmentation/model_compression.py @@ -1,4 +1,4 @@ -import keras +import tensorflow.keras as keras import tensorflow as tf from tqdm import tqdm From 93327d50c8a9d30311d0bd5ac0f32bcafb0ff232 Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Tue, 31 Aug 2021 14:40:01 +0530 Subject: [PATCH 10/12] Update model_compression.py --- keras_segmentation/model_compression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras_segmentation/model_compression.py b/keras_segmentation/model_compression.py index cb96ee28a..cb58d0e52 100644 --- a/keras_segmentation/model_compression.py +++ b/keras_segmentation/model_compression.py @@ -11,7 +11,7 @@ from .data_utils.data_loader import image_segmentation_generator from .train import CheckpointsCallback -from keras.models import Model +from tensorflow.keras.models import Model From 13450361fca7aab9bb3a939d867b9c4d3b5990f4 Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Wed, 1 Dec 2021 16:16:56 +0530 Subject: [PATCH 11/12] fixed tenforflow and keras import issue Update import keras to import tensorflow.keras --- keras_segmentation/models/_pspnet_2.py | 8 ++++---- keras_segmentation/models/basic_models.py | 6 +++--- keras_segmentation/models/fcn.py | 4 ++-- keras_segmentation/models/mobilenet.py | 4 ++-- keras_segmentation/models/model_utils.py | 6 +++--- keras_segmentation/models/pspnet.py | 8 ++++---- keras_segmentation/models/resnet50.py | 6 +++--- keras_segmentation/models/segnet.py | 4 ++-- keras_segmentation/models/unet.py | 4 ++-- keras_segmentation/models/vgg16.py | 4 ++-- 10 files changed, 27 insertions(+), 27 deletions(-) diff --git a/keras_segmentation/models/_pspnet_2.py b/keras_segmentation/models/_pspnet_2.py index 145ab99b2..dad61cd9b 100644 --- a/keras_segmentation/models/_pspnet_2.py +++ b/keras_segmentation/models/_pspnet_2.py @@ -2,11 +2,11 @@ from math import ceil from sys import exit -from keras import layers -from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D -from keras.layers import BatchNormalization, Activation,\ +from tensorflow.keras import layers +from tensorflow.keras.layers import Conv2D, MaxPooling2D, AveragePooling2D +from tensorflow.keras.layers import BatchNormalization, Activation,\ Input, Dropout, ZeroPadding2D -from keras.layers.merge import Concatenate, Add +from tensorflow.keras.layers.merge import Concatenate, Add import tensorflow as tf from .config import IMAGE_ORDERING diff --git a/keras_segmentation/models/basic_models.py b/keras_segmentation/models/basic_models.py index a9405b91a..2bca106ce 100644 --- a/keras_segmentation/models/basic_models.py +++ b/keras_segmentation/models/basic_models.py @@ -1,6 +1,6 @@ -from keras.models import * -from keras.layers import * -import keras.backend as K +from tensorflow.keras.models import * +from tensorflow.keras.layers import * +import tensorflow.keras.backend as K from .config import IMAGE_ORDERING diff --git a/keras_segmentation/models/fcn.py b/keras_segmentation/models/fcn.py index 6b2f347d6..de417d3b9 100644 --- a/keras_segmentation/models/fcn.py +++ b/keras_segmentation/models/fcn.py @@ -1,5 +1,5 @@ -from keras.models import * -from keras.layers import * +from tensorflow.keras.models import * +from tensorflow.keras.layers import * from .config import IMAGE_ORDERING from .model_utils import get_segmentation_model diff --git a/keras_segmentation/models/mobilenet.py b/keras_segmentation/models/mobilenet.py index 2d70af4f0..f9a618d0c 100644 --- a/keras_segmentation/models/mobilenet.py +++ b/keras_segmentation/models/mobilenet.py @@ -1,5 +1,5 @@ -from keras.models import * -from keras.layers import * +from tensorflow.keras.models import * +from tensorflow.keras.layers import * import tensorflow.keras.backend as K import tensorflow.keras as keras diff --git a/keras_segmentation/models/model_utils.py b/keras_segmentation/models/model_utils.py index 8232f5e03..367f93143 100644 --- a/keras_segmentation/models/model_utils.py +++ b/keras_segmentation/models/model_utils.py @@ -1,8 +1,8 @@ from types import MethodType -from keras.models import * -from keras.layers import * -import keras.backend as K +from tensorflow.keras.models import * +from tensorflow.keras.layers import * +import tensorflow.keras.backend as K from tqdm import tqdm from .config import IMAGE_ORDERING diff --git a/keras_segmentation/models/pspnet.py b/keras_segmentation/models/pspnet.py index a0c82c158..a347a95f1 100755 --- a/keras_segmentation/models/pspnet.py +++ b/keras_segmentation/models/pspnet.py @@ -1,8 +1,8 @@ import numpy as np -import keras -from keras.models import * -from keras.layers import * -import keras.backend as K +import tensorflow.keras as keras +from tensorflow.keras.models import * +from tensorflow.keras.layers import * +import tensorflow.keras.backend as K from .config import IMAGE_ORDERING from .model_utils import get_segmentation_model, resize_image diff --git a/keras_segmentation/models/resnet50.py b/keras_segmentation/models/resnet50.py index ee13ed8c4..4ce21fbe3 100644 --- a/keras_segmentation/models/resnet50.py +++ b/keras_segmentation/models/resnet50.py @@ -1,7 +1,7 @@ import tensorflow.keras as keras -from keras.models import * -from keras.layers import * -from keras import layers +from tensorflow.keras.models import * +from tensorflow.keras.layers import * +from tensorflow.keras import layers # Source: # https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py diff --git a/keras_segmentation/models/segnet.py b/keras_segmentation/models/segnet.py index 6a9ad431e..142a74892 100755 --- a/keras_segmentation/models/segnet.py +++ b/keras_segmentation/models/segnet.py @@ -1,5 +1,5 @@ -from keras.models import * -from keras.layers import * +from tensorflow.keras.models import * +from tensorflow.keras.layers import * from .config import IMAGE_ORDERING from .model_utils import get_segmentation_model diff --git a/keras_segmentation/models/unet.py b/keras_segmentation/models/unet.py index ba5d5e3be..1ee16f5a6 100755 --- a/keras_segmentation/models/unet.py +++ b/keras_segmentation/models/unet.py @@ -1,5 +1,5 @@ -from keras.models import * -from keras.layers import * +from tensorflow.keras.models import * +from tensorflow.keras.layers import * from .config import IMAGE_ORDERING from .model_utils import get_segmentation_model diff --git a/keras_segmentation/models/vgg16.py b/keras_segmentation/models/vgg16.py index 79a763960..25198c27a 100644 --- a/keras_segmentation/models/vgg16.py +++ b/keras_segmentation/models/vgg16.py @@ -1,6 +1,6 @@ import tensorflow.keras as keras -from keras.models import * -from keras.layers import * +from tensorflow.keras.models import * +from tensorflow.keras.layers import * from .config import IMAGE_ORDERING From 9d27c752b9d40f9730ec7f4de6fae03bf320b9dc Mon Sep 17 00:00:00 2001 From: Sushant Agarwal <41518238+sushantag9@users.noreply.github.com> Date: Sat, 1 Jan 2022 20:30:59 +0530 Subject: [PATCH 12/12] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 22cc81103..95e46b553 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Downloads](https://pepy.tech/badge/keras-segmentation)](https://pepy.tech/project/keras-segmentation) [![Build Status](https://travis-ci.org/divamgupta/image-segmentation-keras.png)](https://travis-ci.org/divamgupta/image-segmentation-keras) [![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](http://perso.crans.org/besson/LICENSE.html) -[![Twitter](https://img.shields.io/twitter/url.svg?label=Follow%20%40divamgupta&style=social&url=https%3A%2F%2Ftwitter.com%2Fdivamgupta)](https://twitter.com/divamgupta) +[![Twitter](https://img.shields.io/twitter/url.svg?label=Follow%20%40sushantag9&style=social&url=https%3A%2F%2Ftwitter.com%2Fdivamgupta)](https://twitter.com/sushantag9)