import os from PIL import Image import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings("ignore") from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix,classification_report import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout from tensorflow.keras.optimizers import Adamax print ('modules loaded') #data loading tr_df_path="/content/drive/MyDrive/brain/Training" labels=[] filepaths=[] folds=os.listdir(tr_df_path) for fold in folds: foldpath=os.path.join(tr_df_path,fold) foldlist=os.listdir(foldpath) for file in foldlist: filepath=os.path.join(foldpath,file)
labels.append(fold)
filepaths.append(filepath)
l=pd.Series(labels,name="labels") f=pd.Series(filepaths,name="filepaths")
tr_df=pd.concat([f,l],axis=1) tr_df tst_df_path="/content/drive/MyDrive/brain/Testing" labels=[] filepaths=[] folds=os.listdir(tst_df_path) for fold in folds: foldpath=os.path.join(tst_df_path,fold) foldlist=os.listdir(foldpath) for file in foldlist: filepath=os.path.join(foldpath,file)
labels.append(fold)
filepaths.append(filepath)
l=pd.Series(labels,name="labels") f=pd.Series(filepaths,name="filepaths")
ts_df=pd.concat([f,l],axis=1) fig,ax=plt.subplots(figsize=(9,3)) fig.patch.set_facecolor("#f6f5f7") ax.set_facecolor("#f6f5f7") for i in ["top","right"]: ax.spines[i].set_visible(False)
x = sns.countplot(data=tr_df, y=tr_df["labels"],palette='Blues') for container in x.containers: x.bar_label(container) plt.title("\nThe Count of images in each folder\n",weight="bold"); fig,ax=plt.subplots(figsize=(9,3)) fig.patch.set_facecolor("#f6f5f7") ax.set_facecolor("#f6f5f7") for i in ["right","top"]: ax.spines[i].set_visible(False)
i=sns.countplot(data=ts_df,y=ts_df["labels"],palette='Blues') for container in i.containers: i.bar_label(container)
plt.title("\nThe Count of images in each folder\n",weight="bold"); ts_df.shape valid_df,tst_df=train_test_split(ts_df,test_size=0.5,random_state=50,stratify=ts_df["labels"]) print(f"ts_df shape: {ts_df.shape}") print("---"*10) print(f"valid data shape: {valid_df.shape}") print(f"test data shape: {tst_df.shape}") valid_df #creating image data genrator img_size=(224,224)
tr=ImageDataGenerator() ts=ImageDataGenerator()
train_gen=tr.flow_from_dataframe(tr_df,x_col="filepaths",y_col="labels", target_size=img_size, batch_size=16,shuffle=True, class_mode='categorical',color_mode="rgb")
valid_gen=ts.flow_from_dataframe(ts_df,x_col='filepaths',y_col='labels', target_size=img_size, class_mode="categorical",color_mode="rgb", shuffle=True,batch_size=16,)
test_gen=ts.flow_from_dataframe(ts_df, x_col='filepaths', y_col='labels', target_size=img_size, batch_size=16, shuffle=False, color_mode="rgb", class_mode="categorical")
gen_dict = train_gen.class_indices classes = list(gen_dict.keys()) images , labels = next(train_gen)
plt.figure(figsize= (20,20))
for i in range(16): plt.subplot(4,4,i+1) image = images[i] / 255 plt.imshow(image) index = np.argmax(labels[i]) class_name = classes[index] plt.title(class_name , color = 'black' , fontsize= 17,weight="bold") plt.axis('off') plt.show() #building a model img_shape=(224,224,3) base_Model = tf.keras.applications.Xception(include_top= False,weights= "imagenet", input_shape= img_shape, pooling= 'max')
Model = Sequential([ base_Model, Dropout(rate= 0.5), Dense(64, activation= 'relu'), Dropout(rate= 0.25), Dense(4, activation= 'softmax') ])
Model.compile(Adamax(learning_rate= 0.001), loss= 'categorical_crossentropy', metrics= ['accuracy'])
Model.summary() tf.keras.utils.plot_model(Model,show_shapes=True) #training of model historyy=Model.fit(train_gen,epochs=2,validation_data=valid_gen, shuffle=False) historyy.history.keys() plt.figure(figsize=(20, 8)) tr_loss=historyy.history["loss"] val_loss=historyy.history["val_loss"] index_loss=np.argmin(val_loss) loss_lowest=val_loss[index_loss] l_label=f"best epoch {index_loss+1}"
tr_accuracy=historyy.history["accuracy"] val_accuracy=historyy.history["val_accuracy"] index_acc=np.argmax(val_accuracy) acc_highest=tr_accuracy[index_acc] c_label=f"best epoch {index_acc+1}"
epochs=[i+1 for i in range (len(tr_accuracy))]
plt.figure(figsize=(20,10)) plt.style.use('fivethirtyeight')
plt.subplot(1, 2, 1) plt.plot(epochs,tr_loss,"r",label="train loss") plt.plot(epochs,val_loss,"g",label="valid loss") plt.scatter(index_loss+1,loss_lowest,c="b",s=150,label=l_label) plt.title('Training and Validation loss') plt.xlabel("epochs") plt.ylabel("loss") plt.legend()
plt.subplot(1, 2, 2) plt.plot(epochs,tr_accuracy,"r",label="train accuracy") plt.plot(epochs,val_accuracy,"g",label="valid accuracy") plt.scatter(index_acc+1,acc_highest,c="b",s=150,label=c_label) plt.title('Training and Validation Accuracy') plt.xlabel("epochs") plt.ylabel("accuracy") plt.legend()
plt.suptitle('Model Training Metrics Over Epochs', fontsize=20); train_score=Model.evaluate(train_gen) valid_score=Model.evaluate(valid_gen) test_score=Model.evaluate(test_gen)
print(f"Train Loss : {train_score[0]:.3f}") print(f"Train Accuracy : {train_score[1]*100:.2f}%") print("-"*20) print(f"Validation Loss : {valid_score[0]:.3f}") print(f"Validation Accuracy : {valid_score[1]*100:.2f}%") print("-"*20) print(f"Test Loss: {test_score[0]:.3f}") print(f"Test Accuracy: {test_score[1]*100:.2f}%")
preds=Model.predict(test_gen) y_pred=np.argmax(preds,axis=1)
plt.figure(figsize=(10,5)) plt.style.use('default') cm=confusion_matrix(test_gen.classes,y_pred) labels = list(test_gen.class_indices.keys()) sns.heatmap(cm,annot=True,fmt="d",xticklabels=labels,yticklabels=labels,cmap="Blues", linewidths=.5) plt.xlabel('\nPredicted Label',fontsize=13) plt.ylabel('Actual Label\n',fontsize=13);
cr=classification_report(test_gen.classes,y_pred) print(cr)
import cv2 img = cv2.imread('/content/drive/MyDrive/brain/Testing/notumor/Te-noTr_0000.jpg') img = cv2.resize(img,(224,224)) img_array = np.array(img) img_array.shape
img_array = img_array.reshape(1,224,224,3) img_array.shape