"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

February 07, 2023

Segmentation Notes

If things work happy else its another next step towards solution :)

Residual Blocks


What is Dice Coeff and Dice Coeff Loss in segmentation explain with python code simple terms

Dice Coefficient (also known as the Sørensen–Dice coefficient) is a measure of similarity between two sets of data. It is commonly used in image segmentation to measure the accuracy of a segmentation algorithm.

The Dice coefficient is calculated as the ratio of the intersection of two sets of data to the union of the two sets. It is expressed as a value between 0 and 1, where 1 indicates a perfect match.

Dice Coefficient Loss is a loss function used in image segmentation tasks. It is used to measure the difference between the predicted segmentation and the ground truth segmentation. The loss is calculated as the negative of the Dice coefficient.

Code Examples Notes


#Lesson #1
#https://stackoverflow.com/questions/64792460/how-to-code-a-residual-block-using-two-layers-of-a-basic-cnn-algorithm-built-wit
#Sample Code
from tensorflow.keras import layers
def resblock(x, kernelsize, filters):
fx = layers.Conv2D(filters, kernelsize, activation='relu', padding='same')(x)
fx = layers.BatchNormalization()(fx)
fx = layers.Conv2D(filters, kernelsize, padding='same')(fx)
out = layers.Add()([x,fx])
out = layers.ReLU()(out)
out = layers.BatchNormalization()(out)
return out
#Lesson 2 Custom Dice function in segmentation
#https://github.com/DuFanXin/deep_residual_unet
#https://www.kaggle.com/code/bigironsphere/loss-function-library-keras-pytorch/notebook
import keras.backend as K
def dice_coef(y_true, y_pred):
smooth = 1.
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 + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
#Lesson 3
#When you load model with custom loss, set compile loss to load seamlessly
model2 = load_model('/content/gdrive/MyDrive/upgrad/Segmentation/resunetmodel_512.h5', compile=False)
#GPT Notes, Bonus Learning
#The Dice coefficient can be calculated in Python using the following code:
# Calculate Dice Coefficient
def dice_coeff(A, B):
# Calculate the intersection of A and B
intersection = len(A & B)
# Calculate the union of A and B
union = len(A) + len(B)
# Calculate the Dice Coefficient
return 2. * intersection / union
#The Dice Coefficient Loss can be calculated in Python using the following code:
# Calculate Dice Coefficient Loss
def dice_coeff_loss(A, B):
# Calculate the intersection of A and B
intersection = len(A & B)
# Calculate the union of A and B
union = len(A) + len(B)
# Calculate the Dice Coefficient Loss
return -2. * intersection / union
view raw segnotes.py hosted with ❤ by GitHub

Keep Exploring!!!

No comments: