Brain Tumor Segmentation
Brain Tumor Segmentation
# Ignore Warnings
import warnings
warnings.filterwarnings("ignore")
modules loaded
1 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
for i in masks_paths:
images_paths.append(i.replace('_mask', ''))
return df
img_gen = ImageDataGenerator(**aug_dict)
msk_gen = ImageDataGenerator(**aug_dict)
2 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
""" Now UpConvolution / Decoder Leg will begin, so start with Conv2DTranspose
The gray arrows (in the above image) indicate the skip connections that concatenate the en
""" After every concatenation we again apply two consecutive regular convolutions so that
3 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
4 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
plt.axis('off')
plt.tight_layout()
plt.show()
def plot_training(hist):
'''
This function take training model and plot history of accuracy and losses with the best ep
'''
val_acc = hist.history['val_accuracy']
val_iou = hist.history['val_iou_coef']
val_dice = hist.history['val_dice_coef']
val_loss = hist.history['val_loss']
index_acc = np.argmax(val_acc)
acc_highest = val_acc[index_acc]
index_iou = np.argmax(iou_coef)
iou_highest = val_iou[index_iou]
index_dice = np.argmax(dice_coef)
dice_highest = val_dice[index_dice]
index_loss = np.argmin(val_loss)
val_lowest = val_loss[index_loss]
# Training Accuracy
plt.subplot(2, 2, 1)
plt.plot(Epochs tr_acc 'r' label= 'Training Accuracy')
5 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
# Training IoU
plt.subplot(2, 2, 2)
plt.plot(Epochs, tr_iou, 'r', label= 'Training IoU')
plt.plot(Epochs, val_iou, 'g', label= 'Validation IoU')
plt.scatter(index_iou + 1 , iou_highest, s= 150, c= 'blue', label= iou_label)
plt.title('Training and Validation IoU Coefficient')
plt.xlabel('Epochs')
plt.ylabel('IoU')
plt.legend()
# Training Dice
plt.subplot(2, 2, 3)
plt.plot(Epochs, tr_dice, 'r', label= 'Training Dice')
plt.plot(Epochs, val_dice, 'g', label= 'Validation Dice')
plt.scatter(index_dice + 1 , dice_highest, s= 150, c= 'blue', label= dice_label)
plt.title('Training and Validation Dice Coefficient')
plt.xlabel('Epochs')
plt.ylabel('Dice')
plt.legend()
# Training Loss
plt.subplot(2, 2, 4)
plt.plot(Epochs, tr_loss, 'r', label= 'Training loss')
plt.plot(Epochs, val_loss, 'g', label= 'Validation loss')
plt.scatter(index_loss + 1, val_lowest, s= 150, c= 'blue', label= loss_label)
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.tight_layout
plt.show()
Model Structure
data_dir = './archive/lgg-mri-segmentation/kaggle_3m'
df = create_df(data_dir)
train_df, valid_df, test_df = split_df(df)
6 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
tr_aug_dict = dict(rotation_range=0.2,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
zoom_range=0.05,
horizontal_flip=True,
fill_mode='nearest')
show_images(list(train_df['images_paths']), list(train_df['masks_paths']))
7 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
Unet Model
model = unet()
model.compile(Adamax(learning_rate= 0.001), loss= dice_loss, metrics= ['accuracy', iou_coef
model.summary()
Model training
epochs = 120
batch_size = 40
callbacks = [ModelCheckpoint('unet.hdf5', verbose=0, save_best_only=True)]
history = model.fit(train_gen,
steps_per_epoch=len(train_df) / batch_size,
epochs=epochs,
verbose=1,
callbacks=callbacks,
validation_data = valid_gen,
validation_steps=len(valid_df) / batch_size)
8 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
9 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
10 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
Model Evaluation
ts_length = len(test_df)
test_batch_size = max(sorted([ts_length // n for n in range(1, ts_length + 1) if ts_length%n =
test_steps = ts_length // test_batch_size
11 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
print('-' * 20)
Prediction
for _ in range(20):
index = np.random.randint(1, len(test_df.index))
img = cv2.imread(test_df['images_paths'].iloc[index])
img = cv2.resize(img, (256, 256))
img = img/255
img = img[np.newaxis, :, :, : ]
predicted_img = model.predict(img)
plt.figure(figsize=(12, 12))
plt.subplot(1, 3, 1)
plt.imshow(np.squeeze(img))
plt.axis('off')
plt.title('Original Image')
plt.subplot(1, 3, 2)
plt.imshow(np.squeeze(cv2.imread(test_df['masks_paths'].iloc[index])))
plt.axis('off')
plt.title('Original Mask')
plt.subplot(1, 3, 3)
plt.imshow(np.squeeze(predicted_img) > 0.5 )
plt.title('Prediction')
plt.axis('off')
12 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
plt.axis('off')
plt.show()
13 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
14 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
15 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
16 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
17 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
18 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
19 of 20 30-03-2025, 17:57
brain-tumor-segmentation-unet-dice-coef-89-6.ipynb - Colab https://colab.research.google.com/drive/1VS6XxwilS40jo7f7nZ3y2...
20 of 20 30-03-2025, 17:57