Brain Tumor Multi-Classification With PSO: Import As Import As Import
Brain Tumor Multi-Classification With PSO: Import As Import As Import
import numpy as np
import pandas as pd
import os
base_path = "/kaggle/input/pmram-bangladeshi-brain-cancer-mri-dataset/PMRAM
Bangladeshi Brain Cancer - MRI Dataset/PMRAM Bangladeshi Brain Cancer - MRI
Dataset/Augmented Data/Augmented"
categories = ["512Glioma","512Meningioma","512Normal","512Pituitary" ]
image_paths = []
labels = []
df.head()
image_path label
0 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Glioma
1 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Glioma
2 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Glioma
3 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Glioma
4 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Glioma
df.tail()
image_path label
5999 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Pituitary
6000 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Pituitary
6001 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Pituitary
6002 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Pituitary
6003 /kaggle/input/pmram-bangladeshi-brain-cancer-m... 512Pituitary
df.shape
(6004, 2)
df.columns
df.duplicated().sum()
df.isnull().sum()
image_path 0
label 0
dtype: int64
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6004 entries, 0 to 6003
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 image_path 6004 non-null object
1 label 6004 non-null object
dtypes: object(2)
memory usage: 93.9+ KB
df['label'].unique()
df['label'].value_counts()
label
512Glioma 1501
512Meningioma 1501
512Normal 1501
512Pituitary 1501
Name: count, dtype: int64
sns.set_style("whitegrid")
for p in ax.patches:
ax.annotate(f'{int(p.get_height())}',
(p.get_x() + p.get_width() / 2., p.get_height()),
ha='center', va='bottom', fontsize=11, color='black',
xytext=(0, 5), textcoords='offset points')
plt.show()
label_counts = df["label"].value_counts()
plt.show()
import cv2
num_images = 5
plt.figure(figsize=(15, 12))
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.tight_layout()
plt.show()
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense,
Activation, Dropout, BatchNormalization
from tensorflow.keras import regularizers
import warnings
warnings.filterwarnings("ignore")
print ('check')
check
batch_size = 16
img_size = (224, 224)
channels = 3
img_shape = (img_size[0], img_size[1], channels)
tr_gen = ImageDataGenerator(rescale=1./255)
ts_gen = ImageDataGenerator(rescale=1./255)
train_gen_new = tr_gen.flow_from_dataframe(
train_df_new,
x_col='image_path',
y_col='label',
target_size=img_size,
class_mode='sparse',
color_mode='rgb',
shuffle=True,
batch_size=batch_size
)
valid_gen_new = ts_gen.flow_from_dataframe(
valid_df_new,
x_col='image_path',
y_col='label',
target_size=img_size,
class_mode='sparse',
color_mode='rgb',
shuffle=True,
batch_size=batch_size
)
test_gen_new = ts_gen.flow_from_dataframe(
test_df_new,
x_col='image_path',
y_col='label',
target_size=img_size,
class_mode='sparse',
color_mode='rgb',
shuffle=False,
batch_size=batch_size
)
import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
print("GPU is set for TensorFlow")
except RuntimeError as e:
print(e)
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D,
GlobalAveragePooling2D, Dense, BatchNormalization
from tensorflow.keras import backend as K
import numpy as np
from tensorflow.keras.losses import SparseCategoricalCrossentropy
IMG_WIDTH = 224
IMG_HEIGHT = 224
IMG_CHANNELS = 3
NUM_CLASSES = 4
NUM_PARTICLES = 5
EPOCHS = 3
BATCH_SIZE = 16
W = 0.5
C1 = 1
C2 = 1
LEARNING_RATE_ADAM = 1e-4
gap = GlobalAveragePooling2D()(c5)
d1 = Dense(512, activation='relu')(gap)
d1 = BatchNormalization()(d1)
outputs = Dense(NUM_CLASSES, activation='softmax')(d1)
def get_flattened_weights(model):
weights = model.get_weights()
flattened = np.concatenate([w.flatten() for w in weights])
return flattened
scce = SparseCategoricalCrossentropy()
initial_weights = get_flattened_weights(model)
dim = len(initial_weights)
personal_bests = particles.copy()
personal_best_fitness = np.array([float('inf')] * num_particles)
global_best = None
global_best_fitness = float('inf')
for i in range(num_particles):
current_fitness = calculate_fitness(model, particles[i],
train_gen, loss_function)
for i in range(num_particles):
r1 = np.random.rand(dim)
r2 = np.random.rand(dim)
velocities[i] = (w * velocities[i]
+ c1 * r1 * (personal_bests[i] - particles[i])
+ c2 * r2 * (global_best - particles[i]))
particles[i] += velocities[i]
set_weights_from_flat(model, global_best)
return model
if __name__ == '__main__':
print("Building classifier model...")
model_pso = build_classifier()
print("Training classifier with PSO...")
try:
trained_model_pso = train_classifier_with_pso(
model_pso,
train_gen_new,
valid_gen_new,
scce
)
trained_model_pso.summary()
optimizer=tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE_ADAM),
loss=SparseCategoricalCrossentropy(),
metrics=['accuracy']
)
history_adam = model_adam.fit(
train_gen_new,
validation_data=valid_gen_new,
epochs=EPOCHS,
verbose=1
)
model_adam.summary()
except ValueError as e:
print(f"An error occurred during training: {e}")
Model: "functional"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━
━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃
Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━
━━━━━━━━━━┩
│ input_layer (InputLayer) │ (None, 224, 224, 3) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d (Conv2D) │ (None, 224, 224, 32) │
896 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization │ (None, 224, 224, 32) │
128 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ max_pooling2d (MaxPooling2D) │ (None, 112, 112, 32) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d_1 (Conv2D) │ (None, 112, 112, 64) │
18,496 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_1 │ (None, 112, 112, 64) │
256 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ max_pooling2d_1 (MaxPooling2D) │ (None, 56, 56, 64) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d_2 (Conv2D) │ (None, 56, 56, 128) │
73,856 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_2 │ (None, 56, 56, 128) │
512 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ max_pooling2d_2 (MaxPooling2D) │ (None, 28, 28, 128) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d_3 (Conv2D) │ (None, 28, 28, 256) │
295,168 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_3 │ (None, 28, 28, 256) │
1,024 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ max_pooling2d_3 (MaxPooling2D) │ (None, 14, 14, 256) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d_4 (Conv2D) │ (None, 14, 14, 512) │
1,180,160 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_4 │ (None, 14, 14, 512) │
2,048 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ global_average_pooling2d │ (None, 512) │
0 │
│ (GlobalAveragePooling2D) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ dense (Dense) │ (None, 512) │
262,656 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_5 │ (None, 512) │
2,048 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ dense_1 (Dense) │ (None, 4) │
2,052 │
└──────────────────────────────────────┴─────────────────────────────┴───────
──────────┘
Model: "functional_1"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━
━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃
Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━
━━━━━━━━━━┩
│ input_layer_1 (InputLayer) │ (None, 224, 224, 3) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d_5 (Conv2D) │ (None, 224, 224, 32) │
896 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_6 │ (None, 224, 224, 32) │
128 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ max_pooling2d_4 (MaxPooling2D) │ (None, 112, 112, 32) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d_6 (Conv2D) │ (None, 112, 112, 64) │
18,496 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_7 │ (None, 112, 112, 64) │
256 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ max_pooling2d_5 (MaxPooling2D) │ (None, 56, 56, 64) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d_7 (Conv2D) │ (None, 56, 56, 128) │
73,856 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_8 │ (None, 56, 56, 128) │
512 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ max_pooling2d_6 (MaxPooling2D) │ (None, 28, 28, 128) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d_8 (Conv2D) │ (None, 28, 28, 256) │
295,168 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_9 │ (None, 28, 28, 256) │
1,024 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ max_pooling2d_7 (MaxPooling2D) │ (None, 14, 14, 256) │
0 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ conv2d_9 (Conv2D) │ (None, 14, 14, 512) │
1,180,160 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_10 │ (None, 14, 14, 512) │
2,048 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ global_average_pooling2d_1 │ (None, 512) │
0 │
│ (GlobalAveragePooling2D) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ dense_2 (Dense) │ (None, 512) │
262,656 │
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ batch_normalization_11 │ (None, 512) │
2,048 │
│ (BatchNormalization) │ │
│
├──────────────────────────────────────┼─────────────────────────────┼───────
──────────┤
│ dense_3 (Dense) │ (None, 4) │
2,052 │
└──────────────────────────────────────┴─────────────────────────────┴───────
──────────┘
def plot_training_history(history):
epochs = range(1, len(history.history['loss']) + 1)
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 2)
plt.plot(epochs, history.history['accuracy'], 'bo-', label='Training
Accuracy')
plt.plot(epochs, history.history['val_accuracy'], 'r^-',
label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
plt.show()
plot_training_history(history_adam)
import seaborn as sns
from sklearn.metrics import classification_report, confusion_matrix
test_gen_new.reset()
y_true = test_gen_new.classes
print("Classification Report:")
print(classification_report(y_true, y_pred))
cm = confusion_matrix(y_true, y_pred)
plt.figure(figsize=(8,6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=range(NUM_CLASSES), yticklabels=range(NUM_CLASSES))
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.title("Confusion Matrix")
plt.show()