CoolFace
Apppublic

abhipatel1349/Brain_tumor_detection

sourceHugging Facebigcode-openrail-mupdated 2y agoView on Hugging Face
0likes
app.py209 linesDownload Raw Back to root
1import pandas as pd2import numpy as np3import seaborn as sns4import matplotlib.pyplot as plt5from warnings import filterwarnings6from sklearn.metrics import confusion_matrix, accuracy_score, classification_report, roc_auc_score, roc_curve7from tensorflow.keras.models import Sequential8from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization,MaxPooling2D9from keras import models10from keras import layers11import tensorflow as tf12import os13import os.path14from pathlib import Path15import cv216from tensorflow.keras.preprocessing.image import ImageDataGenerator17from sklearn.model_selection import train_test_split18from keras import regularizers19from keras.optimizers import RMSprop,Adam20import glob21from PIL import Image22 23No_Data_Path = Path("../input/brain-tumor-detection/no")24Yes_Data_Path = Path("../input/brain-tumor-detection/yes")25 26No_JPG_Path = list(No_Data_Path.glob(r"*.jpg"))27Yes_JPG_Path = list(Yes_Data_Path.glob(r"*.jpg"))28 29Yes_No_List = []30 31for No_JPG in No_JPG_Path:32    Yes_No_List.append(No_JPG)33    34for Yes_JPG in Yes_JPG_Path:35    Yes_No_List.append(Yes_JPG)36 37    print(Yes_No_List[0:10])38    JPG_Labels = list(map(lambda x: os.path.split(os.path.split(x)[0])[1],Yes_No_List))39    print(JPG_Labels[0:10])40    print("NO COUNTING: ", JPG_Labels.count("no"))41print("YES COUNTING: ", JPG_Labels.count("yes"))42 43JPG_Path_Series = pd.Series(Yes_No_List,name="JPG").astype(str)44JPG_Category_Series = pd.Series(JPG_Labels,name="TUMOR_CATEGORY")45Train_data = pd.concat([JPG_Path_Series,JPG_Category_Series],axis=1)46 47print(Train_data.head(-1))48Prediction_Path = Path("../input/brain-tumor-detection/pred")49Test_JPG_Path = list(Prediction_Path.glob(r"*.jpg"))50 51print(Test_JPG_Path[0:5])52Test_JPG_Labels = list(map(lambda x: os.path.split(os.path.split(x)[0])[1],Test_JPG_Path))53print(Test_JPG_Labels[0:5])54 55Test_JPG_Path_Series = pd.Series(Test_JPG_Path,name="JPG").astype(str)56Test_JPG_Labels_Series = pd.Series(Test_JPG_Labels,name="TUMOR_CATEGORY")57Test_data = pd.concat([Test_JPG_Path_Series,Test_JPG_Labels_Series],axis=1)58print(Test_data.head())59 60Main_Train_Data = Train_data.sample(frac=1).reset_index(drop=True)61print(Main_Train_Data.head(-1))62figure = plt.figure(figsize=(10,10))63plt.imshow(plt.imread(Main_Train_Data["JPG"][10]))64plt.title(Main_Train_Data["TUMOR_CATEGORY"][10])65 66figure = plt.figure(figsize=(10,10))67plt.imshow(plt.imread(Main_Train_Data["JPG"][2997]))68plt.title(Main_Train_Data["TUMOR_CATEGORY"][2997])69 70fig, axes = plt.subplots(nrows=5, ncols=5, figsize=(10, 10),71                        subplot_kw={'xticks': [], 'yticks': []})72 73for i, ax in enumerate(axes.flat):74    ax.imshow(plt.imread(Main_Train_Data["JPG"][i]))75    ax.set_title(Main_Train_Data["TUMOR_CATEGORY"][i])76plt.tight_layout()77plt.show()78 79train_data,test_data = train_test_split(Train_data,train_size=0.8,random_state=42)80train_data.shape81train_data.head()82test_data.shape83test_data.head()84 85from tensorflow.keras.preprocessing.image import ImageDataGenerator86# Augmenting the training data87binary_datagen = ImageDataGenerator(88    rescale=1./255,89#     rotation_range=20,  # Rotate images up to 20 degrees90#     width_shift_range=0.2,  # Horizontal shift91#     height_shift_range=0.2,  # Vertical shift92#     shear_range=0.2,  # Shear transformation93#     zoom_range=0.2,  # Zoom in/out94#     horizontal_flip=True,  # Randomly flip images horizontally95#     fill_mode='nearest',  # Filling strategy96    validation_split=0.2  # Split validation set97)98 99Train_Set = binary_datagen.flow_from_dataframe(100    dataframe=train_data,101    x_col="JPG",102    y_col="TUMOR_CATEGORY",103    color_mode="grayscale",  # Ensure grayscale mode is set104    class_mode="categorical",105    subset="training",106    batch_size=20,107    target_size=(224, 224)108)109 110Validation_Set = binary_datagen.flow_from_dataframe(111    dataframe=train_data,112    x_col="JPG",113    y_col="TUMOR_CATEGORY",114    color_mode="grayscale",  # Ensure grayscale mode is set115    class_mode="categorical",116    subset="validation",117    batch_size=20,118    target_size=(224, 224)119)120 121Test_Set = binary_datagen.flow_from_dataframe(122    dataframe=test_data,123    x_col="JPG",124    y_col="TUMOR_CATEGORY",125    color_mode="grayscale",  # Ensure grayscale mode is set126    class_mode="categorical",127    batch_size=20,128    target_size=(224, 224)129)130# Model architecture remains the same as the previous multiclass CNN131from tensorflow.keras.callbacks import EarlyStopping132early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)133 134 135# Model architecture remains the same as the previous CNN136 137model = Sequential([138    Conv2D(32, (5, 5), activation='relu', input_shape=(224, 224, 1)),  # Larger kernel size (5,5)139    MaxPooling2D(pool_size=(2, 2)),140    Dropout(0.2),  # Dropout layer after the first Conv block141    142    Conv2D(64, (3, 3), activation='relu'),  # Keep kernel size (3,3) consistent with your original model143    MaxPooling2D(pool_size=(2, 2)),144    Dropout(0.2),  # Dropout layer after the second Conv block145 146    Conv2D(128, (3, 3), activation='relu'),147    MaxPooling2D(pool_size=(2, 2)),148    Dropout(0.2),  # Dropout layer after the third Conv block149 150    Conv2D(256, (3, 3), activation='relu'),  # Adding one more Conv block with 256 filters151    MaxPooling2D(pool_size=(2, 2)),152    Dropout(0.2),153 154    Flatten(),155    Dropout(0.5),  # Add dropout before the dense layer156    Dense(512, activation='relu'),  # Increased dense layer size157    Dense(2, activation='softmax')  #softmax for classification158])159 160model.compile(optimizer=RMSprop(learning_rate=0.001),loss="categorical_crossentropy",metrics=["accuracy"])161# model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])162 163# Train the model with EarlyStopping164# model_binary_no_transfer_augmented.fit(165#     Train_Set,166#     validation_data=Validation_Set,167#     epochs=50,  # Use higher epochs since early stopping will halt training automatically168#     callbacks=[early_stop]  # Adding EarlyStopping here169# )170model.summary()171# model.compile(optimizer='adam',loss="categorical_crossentropy",metrics=["accuracy"])172history = model.fit(Train_Set,validation_data=Validation_Set,epochs=30,steps_per_epoch=120)173Hist = history.history174 175val_losses = Hist["val_loss"]176val_acc = Hist["val_accuracy"]177acc = Hist["accuracy"]178losses = Hist["loss"]179epochs = range(1,len(val_losses)+1)180 181plt.plot(history.history["accuracy"])182plt.plot(history.history["val_accuracy"])183plt.ylabel("ACCURACY")184plt.legend()185plt.show()186 187plt.plot(epochs,losses,"k-",label="LOSS")188plt.plot(epochs,val_losses,"ro",label="LOSS VALIDATION")189plt.title("LOSS & LOSS VAL")190plt.xlabel("EPOCH")191plt.ylabel("LOSS & LOSS VAL")192plt.legend()193plt.show()194 195plt.plot(epochs,acc,"k-",label="ACCURACY")196plt.plot(epochs,val_acc,"ro",label="ACCURACY VALIDATION")197plt.title("ACCURACY & ACCURACY VAL")198plt.xlabel("EPOCH")199plt.ylabel("ACCURACY & ACCURACY VAL")200plt.legend()201plt.show()202 203pd.DataFrame(history.history).plot()204 205Result = model.evaluate(Test_Set,verbose=False)206print("LOSS:  " + "%.4f" % Result[0])207print("ACCURACY:  " + "%.3f" % Result[1])208 209