{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":2645886,"sourceType":"datasetVersion","datasetId":1608934}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# ! pip install imutils","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:25:49.477975Z","iopub.execute_input":"2023-12-27T20:25:49.478409Z","iopub.status.idle":"2023-12-27T20:25:49.483172Z","shell.execute_reply.started":"2023-12-27T20:25:49.478372Z","shell.execute_reply":"2023-12-27T20:25:49.482063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom keras.models import Sequential\nfrom PIL import Image\nimport tensorflow as tf\nimport cv2\nimport seaborn as sns\nfrom tensorflow.keras.optimizers import Adamax\nfrom tensorflow.keras.metrics import Precision, Recall\nimport os\nfrom tqdm import tqdm\nimport imutils\nimport keras\nfrom keras.applications import resnet\nfrom keras import backend as K\nfrom keras.layers import Dense,Flatten,Dropout,Conv2D,MaxPooling2D,BatchNormalization,Activation,GlobalAveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing import image\nfrom keras.applications import imagenet_utils\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport itertools\nimport matplotlib.pyplot as plt\nIMG_SIZE = 256\ntraining = '/kaggle/input/brain-tumor-mri-dataset/Training'\ntesting = '/kaggle/input/brain-tumor-mri-dataset/Testing'","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:25:49.492473Z","iopub.execute_input":"2023-12-27T20:25:49.493012Z","iopub.status.idle":"2023-12-27T20:25:49.500611Z","shell.execute_reply.started":"2023-12-27T20:25:49.492985Z","shell.execute_reply":"2023-12-27T20:25:49.499642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def crop_img(img):\n#     gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n#     gray = cv2.GaussianBlur(gray, (3, 3), 0)\n\n#     # threshold the image, then perform a series of erosions +\n#     # dilations to remove any small regions of noise\n#     thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]\n#     thresh = cv2.erode(thresh, None, iterations=2)\n#     thresh = cv2.dilate(thresh, None, iterations=2)\n\n#     # find contours in thresholded image, then grab the largest one\n#     cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n#     cnts = imutils.grab_contours(cnts)\n#     c = max(cnts, key=cv2.contourArea)\n\n#     # find the extreme points\n#     extLeft = tuple(c[c[:, :, 0].argmin()][0])\n#     extRight = tuple(c[c[:, :, 0].argmax()][0])\n#     extTop = tuple(c[c[:, :, 1].argmin()][0])\n#     extBot = tuple(c[c[:, :, 1].argmax()][0])\n#     ADD_PIXELS = 0\n#     new_img = img[extTop[1]-ADD_PIXELS:extBot[1]+ADD_PIXELS, extLeft[0]-ADD_PIXELS:extRight[0]+ADD_PIXELS].copy()\n\n#     return new_img","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:25:49.504939Z","iopub.execute_input":"2023-12-27T20:25:49.505485Z","iopub.status.idle":"2023-12-27T20:25:49.515937Z","shell.execute_reply.started":"2023-12-27T20:25:49.505459Z","shell.execute_reply":"2023-12-27T20:25:49.514955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training_dir = os.listdir(training)\n# testing_dir = os.listdir(testing)\n# trainData = []\n# trainLabels = []\n# label = 0\n\n# for dir in training_dir:\n#     save_path = 'cleaned/Training/'+ dir\n#     path = os.path.join(training,dir)\n#     image_dir = os.listdir(path)\n#     for img in image_dir:\n#         image = cv2.imread(os.path.join(path,img))\n#         new_img = crop_img(image)\n#         new_img = cv2.resize(new_img,(IMG_SIZE,IMG_SIZE))\n#         if not os.path.exists(save_path):\n#             os.makedirs(save_path)\n#         cv2.imwrite(save_path+'/'+img, new_img)\n\n# for dir in testing_dir:\n#     save_path = 'cleaned/Testing/'+ dir\n#     path = os.path.join(testing,dir)\n#     image_dir = os.listdir(path)\n#     for img in image_dir:\n#         image = cv2.imread(os.path.join(path,img))\n#         new_img = crop_img(image)\n#         new_img = cv2.resize(new_img,(IMG_SIZE,IMG_SIZE))\n#         if not os.path.exists(save_path):\n#             os.makedirs(save_path)\n#         cv2.imwrite(save_path+'/'+img, new_img)\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-27T20:25:49.517618Z","iopub.execute_input":"2023-12-27T20:25:49.518347Z","iopub.status.idle":"2023-12-27T20:25:49.525752Z","shell.execute_reply.started":"2023-12-27T20:25:49.518312Z","shell.execute_reply":"2023-12-27T20:25:49.524751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainData=[]\ntrainLabels=[]\n\nfor root, _, files in os.walk('/kaggle/input/brain-tumor-mri-dataset/Training/notumor'):\n  for file in files:\n    if file.endswith('.jpg'):\n      image_path = os.path.join(root,file)\n      if 'notumor' in image_path:\n        label = 0\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,(224,224))\n        image_array = np.array(image)\n        trainData.append(image_array)\n        trainLabels.append(label)\n        #print(f\"Image Path: {image_path}, Label: {label}\")\nfor root, _, files in os.walk('/kaggle/input/brain-tumor-mri-dataset/Training/glioma'):\n  for file in files:\n    if file.endswith('.jpg'):\n      image_path = os.path.join(root,file)\n      if 'glioma' in image_path:\n        label = 1\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,(224,224))\n        image_array = np.array(image)\n        trainData.append(image_array)\n        trainLabels.append(label)\n        #print(f\"Image Path: {image_path}, Label: {label}\")\nfor root, _, files in os.walk('/kaggle/input/brain-tumor-mri-dataset/Training/pituitary'):\n  for file in files:\n    if file.endswith('.jpg'):\n      image_path = os.path.join(root,file)\n      if 'pituitary' in image_path:\n        label = 2\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,(224,224))\n        image_array = np.array(image)\n        trainData.append(image_array)\n        trainLabels.append(label)\n        #print(f\"Image Path: {image_path}, Label: {label}\")\nfor root, _, files in os.walk('/kaggle/input/brain-tumor-mri-dataset/Training/meningioma'):\n  for file in files:\n    if file.endswith('.jpg'):\n      image_path = os.path.join(root,file)\n      if 'meningioma' in image_path:\n        label = 3\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,(224,224))\n        image_array = np.array(image)\n        trainData.append(image_array)\n        trainLabels.append(label)\n        #print(f\"Image Path: {image_path}, Label: {label}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:25:49.539657Z","iopub.execute_input":"2023-12-27T20:25:49.539955Z","iopub.status.idle":"2023-12-27T20:26:09.768090Z","shell.execute_reply.started":"2023-12-27T20:25:49.539912Z","shell.execute_reply":"2023-12-27T20:26:09.767020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trainData = np.array(trainData)\n# trainData = trainData/255.0\n# print(trainData.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:09.769807Z","iopub.execute_input":"2023-12-27T20:26:09.770129Z","iopub.status.idle":"2023-12-27T20:26:09.774272Z","shell.execute_reply.started":"2023-12-27T20:26:09.770102Z","shell.execute_reply":"2023-12-27T20:26:09.773357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testData=[]\ntestLabels=[]\n\nfor root, _, files in os.walk('/kaggle/input/brain-tumor-mri-dataset/Testing/notumor'):\n  for file in files:\n    if file.endswith('.jpg'):\n      image_path = os.path.join(root,file)\n      if 'notumor' in image_path:\n        label = 0\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,(224,224))\n        image_array = np.array(image)\n        testData.append(image_array)\n        testLabels.append(label)\n        #print(f\"Image Path: {image_path}, Label: {label}\")\nfor root, _, files in os.walk('/kaggle/input/brain-tumor-mri-dataset/Testing/glioma'):\n  for file in files:\n    if file.endswith('.jpg'):\n      image_path = os.path.join(root,file)\n      if 'glioma' in image_path:\n        label = 1\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,(224,224))\n        image_array = np.array(image)\n        testData.append(image_array)\n        testLabels.append(label)\n        #print(f\"Image Path: {image_path}, Label: {label}\")\nfor root, _, files in os.walk('/kaggle/input/brain-tumor-mri-dataset/Testing/pituitary'):\n  for file in files:\n    if file.endswith('.jpg'):\n      image_path = os.path.join(root,file)\n      if 'pituitary' in image_path:\n        label = 2\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,(224,224))\n        image_array = np.array(image)\n        testData.append(image_array)\n        testLabels.append(label)\n        #print(f\"Image Path: {image_path}, Label: {label}\")\nfor root, _, files in os.walk('/kaggle/input/brain-tumor-mri-dataset/Testing/meningioma'):\n  for file in files:\n    if file.endswith('.jpg'):\n      image_path = os.path.join(root,file)\n      if 'meningioma' in image_path:\n        label = 3\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,(224,224))\n        image_array = np.array(image)\n        testData.append(image_array)\n        testLabels.append(label)\n        #print(f\"Image Path: {image_path}, Label: {label}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:09.775558Z","iopub.execute_input":"2023-12-27T20:26:09.775891Z","iopub.status.idle":"2023-12-27T20:26:14.739596Z","shell.execute_reply.started":"2023-12-27T20:26:09.775860Z","shell.execute_reply":"2023-12-27T20:26:14.738691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# testData = np.array(testData)\n# testData = testData/255.0\n# print(testData.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:14.741689Z","iopub.execute_input":"2023-12-27T20:26:14.742020Z","iopub.status.idle":"2023-12-27T20:26:14.746181Z","shell.execute_reply.started":"2023-12-27T20:26:14.741992Z","shell.execute_reply":"2023-12-27T20:26:14.745276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# testLabels = np.array(testLabels)\n# print(testLabels.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:14.747487Z","iopub.execute_input":"2023-12-27T20:26:14.747778Z","iopub.status.idle":"2023-12-27T20:26:14.760690Z","shell.execute_reply.started":"2023-12-27T20:26:14.747754Z","shell.execute_reply":"2023-12-27T20:26:14.759764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trainLabels = np.array(trainLabels)\n# print(trainLabels.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:14.761933Z","iopub.execute_input":"2023-12-27T20:26:14.762229Z","iopub.status.idle":"2023-12-27T20:26:14.770773Z","shell.execute_reply.started":"2023-12-27T20:26:14.762203Z","shell.execute_reply":"2023-12-27T20:26:14.769836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# valData, testData = train_test_split(testData, train_size=.5, random_state=20)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:14.772312Z","iopub.execute_input":"2023-12-27T20:26:14.772881Z","iopub.status.idle":"2023-12-27T20:26:14.780802Z","shell.execute_reply.started":"2023-12-27T20:26:14.772854Z","shell.execute_reply":"2023-12-27T20:26:14.779849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# valLabels, testLabels = train_test_split(testLabels, train_size=.5, random_state=20)\n# print(valLabels.shape)\n# print(testLabels.shape)\n# print(valData.shape)\n# print(testData.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:14.781809Z","iopub.execute_input":"2023-12-27T20:26:14.782427Z","iopub.status.idle":"2023-12-27T20:26:14.789941Z","shell.execute_reply.started":"2023-12-27T20:26:14.782399Z","shell.execute_reply":"2023-12-27T20:26:14.789161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\nX_train = trainData\ny_train = trainLabels\nX_test = testData\ny_test = testLabels\n\ny_train = to_categorical(y_train, num_classes=4)\ny_test = to_categorical(y_test, num_classes=4)\n\nX_val, X_test = train_test_split(X_test, train_size=.3, random_state=20)\ny_val, y_test = train_test_split(y_test, train_size=.3, random_state=20)\n\ny_train = np.array(y_train)\ny_test = np.array(y_test)\ny_val = np.array(y_val)\nX_train = np.array(X_train)\nX_test = np.array(X_test)\nX_val = np.array(X_val)\n\n\nprint(f\"Shape of images in X_train: {X_train.shape}\")\nprint(f\"Shape of images in X_test: {X_test.shape}\")\nprint(f\"Shape of images in X_val: {X_val.shape}\")\nprint(f\"Shape of images in y_train: {y_train.shape}\")\nprint(f\"Shape of images in y_test: {y_test.shape}\")\nprint(f\"Shape of images in y_val: {y_val.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:14.790918Z","iopub.execute_input":"2023-12-27T20:26:14.791201Z","iopub.status.idle":"2023-12-27T20:26:15.138245Z","shell.execute_reply.started":"2023-12-27T20:26:14.791178Z","shell.execute_reply":"2023-12-27T20:26:15.137279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = resnet.ResNet50(\n      input_shape = (224, 224, 3),\n      include_top = False,\n      weights = 'imagenet'\n    )\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:15.144425Z","iopub.execute_input":"2023-12-27T20:26:15.144871Z","iopub.status.idle":"2023-12-27T20:26:17.490771Z","shell.execute_reply.started":"2023-12-27T20:26:15.144829Z","shell.execute_reply":"2023-12-27T20:26:17.489794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layers in model.layers:\n    layers.trainable = False\n    \ntopLayer = Flatten()(model.output)\ntopLayer = Dropout(0.5)(topLayer)\ntopLayer = Dense(4, activation = \"softmax\")(topLayer)\nmodel = keras.Model(model.input, topLayer)\n\nmodel.compile(loss = \"binary_crossentropy\", optimizer = \"adam\", metrics = [\n    'accuracy',\n    tf.keras.metrics.AUC(),\n    tf.keras.metrics.Recall(),\n    tf.keras.metrics.Precision(),\n    tf.keras.metrics.F1Score()])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:26:17.492013Z","iopub.execute_input":"2023-12-27T20:26:17.492351Z","iopub.status.idle":"2023-12-27T20:26:18.024121Z","shell.execute_reply.started":"2023-12-27T20:26:17.492322Z","shell.execute_reply":"2023-12-27T20:26:18.023362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ESM = tf.keras.callbacks.EarlyStopping(patience=4, monitor='accuracy')\nrn = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=30, verbose=1, callbacks=ESM)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:29:15.240620Z","iopub.execute_input":"2023-12-27T20:29:15.241532Z","iopub.status.idle":"2023-12-27T20:30:41.132211Z","shell.execute_reply.started":"2023-12-27T20:29:15.241498Z","shell.execute_reply":"2023-12-27T20:30:41.131085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:28:54.275465Z","iopub.execute_input":"2023-12-27T20:28:54.276271Z","iopub.status.idle":"2023-12-27T20:28:56.233880Z","shell.execute_reply.started":"2023-12-27T20:28:54.276224Z","shell.execute_reply":"2023-12-27T20:28:56.232860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = rn.history['loss']\nvalidation_accuracy = rn.history['val_loss']\n\nepochs = range(len(accuracy))\n\nplt.plot(epochs, accuracy, 'r', label='Training Accuracy')\nplt.plot(epochs,  validation_accuracy, 'g', label=\"Validation Accuracy\")\nplt.legend(loc=0)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:38:08.589823Z","iopub.execute_input":"2023-12-27T20:38:08.590602Z","iopub.status.idle":"2023-12-27T20:38:08.858471Z","shell.execute_reply.started":"2023-12-27T20:38:08.590568Z","shell.execute_reply":"2023-12-27T20:38:08.857518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X_test)\nclassPred = np.argmax(pred, axis=1)\nclassLabel = np.argmax(y_test, axis=1)\ncon_matrix = confusion_matrix(classLabel, classPred)\n\nplt.figure(figsize=(8, 6))\nsns.heatmap(con_matrix, annot=True, fmt='d', cmap='Reds', \n            xticklabels=['notumor','glioma', 'pituitary' ,'meningioma'], \n            yticklabels=['notumor','glioma', 'pituitary' ,'meningioma'])\n\nplt.xlabel('Predictions')\nplt.ylabel('Real')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T21:27:30.849754Z","iopub.execute_input":"2023-12-27T21:27:30.850241Z","iopub.status.idle":"2023-12-27T21:27:33.772502Z","shell.execute_reply.started":"2023-12-27T21:27:30.850206Z","shell.execute_reply":"2023-12-27T21:27:33.771575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobileModel = keras.applications.MobileNet(input_shape=(224,224,3), include_top=False)\nnewModel = Sequential()\nnewModel.add(mobileModel)\nnewModel.add(Flatten())\nnewModel.add(Dropout(0.5))\nnewModel.add(Dense(4, activation=\"softmax\"))\n\nnewModel.compile(loss = \"binary_crossentropy\", optimizer = \"adam\", metrics = [\n    'accuracy',\n    tf.keras.metrics.AUC(),\n    tf.keras.metrics.Recall(),\n    tf.keras.metrics.Precision(),\n    tf.keras.metrics.F1Score()])\n\nnewModel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:33:28.956504Z","iopub.execute_input":"2023-12-27T20:33:28.957526Z","iopub.status.idle":"2023-12-27T20:33:30.682660Z","shell.execute_reply.started":"2023-12-27T20:33:28.957488Z","shell.execute_reply":"2023-12-27T20:33:30.681656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newModel.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10, verbose=1, callbacks=ESM)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:33:50.635451Z","iopub.execute_input":"2023-12-27T20:33:50.636206Z","iopub.status.idle":"2023-12-27T20:36:19.831184Z","shell.execute_reply.started":"2023-12-27T20:33:50.636168Z","shell.execute_reply":"2023-12-27T20:36:19.830243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newModel.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T20:36:19.907201Z","iopub.execute_input":"2023-12-27T20:36:19.907529Z","iopub.status.idle":"2023-12-27T20:36:21.056296Z","shell.execute_reply.started":"2023-12-27T20:36:19.907501Z","shell.execute_reply":"2023-12-27T20:36:21.055202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\"/kaggle/input/brain-tumor-mri-dataset/Testing\")\ndict_out = {}\ntest_sub_dirs = os.listdir()[::-1]\nfor i in range(len(test_sub_dirs)):\n    dict_out.update({test_sub_dirs[i]: i})\n    \n\nactual_classes = [list(dict_out.keys())[list(dict_out.values()).index(val)] for val in classLabel]\n\nfig, axes = plt.subplots(4, 5, figsize=(15, 15))\n\nfor i in range(20):\n    axes[i//5, i%5].imshow(X_test[i])\n    axes[i//5, i%5].set_title(f\"Predicted: {classLabel[i]} \\n Actual: {actual_classes[i]}\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T21:47:45.953479Z","iopub.execute_input":"2023-12-27T21:47:45.954523Z","iopub.status.idle":"2023-12-27T21:47:50.015965Z","shell.execute_reply.started":"2023-12-27T21:47:45.954485Z","shell.execute_reply":"2023-12-27T21:47:50.015025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}