{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import Necessary Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport os\nimport pandas as pd\nimport cv2\nfrom tqdm import tqdm\nimport io\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.utils import shuffle # Shuffle arrays or sparse matrices in a consistent way\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.models import Sequential, load_model\n\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport scikitplot as skplt","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:17:06.475633Z","iopub.execute_input":"2022-11-23T02:17:06.476264Z","iopub.status.idle":"2022-11-23T02:17:06.483910Z","shell.execute_reply.started":"2022-11-23T02:17:06.476223Z","shell.execute_reply":"2022-11-23T02:17:06.482415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining Labels","metadata":{}},{"cell_type":"code","source":"labels = ['glioma', 'meningioma', 'notumor', 'pituitary']","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:17:11.648271Z","iopub.execute_input":"2022-11-23T02:17:11.648664Z","iopub.status.idle":"2022-11-23T02:17:11.653443Z","shell.execute_reply.started":"2022-11-23T02:17:11.648629Z","shell.execute_reply":"2022-11-23T02:17:11.652298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Dataset\n","metadata":{}},{"cell_type":"code","source":"X_train = [] #Training Dataset\nY_train = [] #Training Labels\n\nimage_size=150\n\nfor i in labels:\n    folderPath = os.path.join('../input/brain-tumor-mri-dataset/', 'Training', i)\n    for j in tqdm(os.listdir(folderPath)):\n        image = cv2.imread(os.path.join(folderPath, j))\n        image = cv2.resize(image, (image_size, image_size))\n        X_train.append(image)\n        Y_train.append(i)\n        \n        \nfor i in labels:\n    folderPath = os.path.join('../input/brain-tumor-mri-dataset', 'Testing', i) # Join two or more pathname components\n    for j in tqdm(os.listdir(folderPath)):\n        image = cv2.imread(os.path.join(folderPath, j))\n        image = cv2.resize(image, (image_size, image_size))\n        X_train.append(image)\n        Y_train.append(i)\n        \n#Image and Label is appended as list, now is to be converted into array\nX_train = np.array(X_train)\nY_train = np.array(Y_train)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:17:17.328733Z","iopub.execute_input":"2022-11-23T02:17:17.329160Z","iopub.status.idle":"2022-11-23T02:18:00.547467Z","shell.execute_reply.started":"2022-11-23T02:17:17.329114Z","shell.execute_reply":"2022-11-23T02:18:00.546417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape #No of sample = 7023","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:18:09.116573Z","iopub.execute_input":"2022-11-23T02:18:09.116938Z","iopub.status.idle":"2022-11-23T02:18:09.127687Z","shell.execute_reply.started":"2022-11-23T02:18:09.116907Z","shell.execute_reply":"2022-11-23T02:18:09.126560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shuffling data\nX_train, Y_train = shuffle(X_train, Y_train, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:18:11.266203Z","iopub.execute_input":"2022-11-23T02:18:11.267107Z","iopub.status.idle":"2022-11-23T02:18:11.416841Z","shell.execute_reply.started":"2022-11-23T02:18:11.267064Z","shell.execute_reply":"2022-11-23T02:18:11.415806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#After shuffling sample size remains same\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:18:14.958899Z","iopub.execute_input":"2022-11-23T02:18:14.959259Z","iopub.status.idle":"2022-11-23T02:18:14.965374Z","shell.execute_reply.started":"2022-11-23T02:18:14.959227Z","shell.execute_reply":"2022-11-23T02:18:14.964451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_width = 4\ngrid_height = 4\nf, ax = plt.subplots(grid_width, grid_height)\nf.set_size_inches(8, 8)\n\nimg_idx = 0\nfor i in range(0, grid_width):\n    for j in range(0, grid_height):\n        ax[i][j].axis('off')\n        ax[i][j].set_title('Label: '+Y_train[img_idx])\n        ax[i][j].imshow(X_train[img_idx])\n        img_idx += 1\n\nplt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0.2, hspace=0.55)  ","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:19:42.641885Z","iopub.execute_input":"2022-11-23T02:19:42.642630Z","iopub.status.idle":"2022-11-23T02:19:43.370159Z","shell.execute_reply.started":"2022-11-23T02:19:42.642592Z","shell.execute_reply":"2022-11-23T02:19:43.369076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting data into : Train, Test, Valid as 70, 20, 10 ratio","metadata":{}},{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(X_train, Y_train, test_size=0.2, random_state=42)\nX_train, X_valid, Y_train, Y_valid = train_test_split(X_train, Y_train, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:20:05.098081Z","iopub.execute_input":"2022-11-23T02:20:05.098455Z","iopub.status.idle":"2022-11-23T02:20:05.344050Z","shell.execute_reply.started":"2022-11-23T02:20:05.098421Z","shell.execute_reply":"2022-11-23T02:20:05.342984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Converting String Label to categorical","metadata":{}},{"cell_type":"code","source":"y_train_new = []\ny_valid_new = []\ny_test_new = []\n\nfor i in Y_train:\n    y_train_new.append(labels.index(i))#Converting String Label to integer i.e\n                                       # glioma ---> 0, meningioma---> 1, notumor ---> 2, pituitary ---> 3\nY_train = to_categorical(y_train_new) #Converts a class vector (integers) to binary class matrix\n\nfor i in Y_valid:\n    y_valid_new.append(labels.index(i))\n\nY_valid = to_categorical(y_valid_new)\n\nfor i in Y_test:\n    y_test_new.append(labels.index(i))\n\nY_test = to_categorical(y_test_new)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:20:10.362711Z","iopub.execute_input":"2022-11-23T02:20:10.363472Z","iopub.status.idle":"2022-11-23T02:20:10.380352Z","shell.execute_reply.started":"2022-11-23T02:20:10.363427Z","shell.execute_reply":"2022-11-23T02:20:10.378954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:20:13.459152Z","iopub.execute_input":"2022-11-23T02:20:13.459551Z","iopub.status.idle":"2022-11-23T02:20:13.466939Z","shell.execute_reply.started":"2022-11-23T02:20:13.459482Z","shell.execute_reply":"2022-11-23T02:20:13.465893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Simple CNN Model","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(16, kernel_size=(3,3), activation='relu', input_shape=(150, 150, 3)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(64, kernel_size=(3,3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(128, kernel_size=(3,3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dense(4, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:20:17.154966Z","iopub.execute_input":"2022-11-23T02:20:17.156808Z","iopub.status.idle":"2022-11-23T02:20:20.835601Z","shell.execute_reply.started":"2022-11-23T02:20:17.156764Z","shell.execute_reply":"2022-11-23T02:20:20.834430Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='CategoricalCrossentropy', metrics=['Acc'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:20:24.700099Z","iopub.execute_input":"2022-11-23T02:20:24.700460Z","iopub.status.idle":"2022-11-23T02:20:24.716634Z","shell.execute_reply.started":"2022-11-23T02:20:24.700428Z","shell.execute_reply":"2022-11-23T02:20:24.715583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scaling Features : Train, Valid and Test ","metadata":{}},{"cell_type":"code","source":"# Scaling Train, Valid and Test Features\nX_train_scaled = X_train.astype('float32')\nX_valid_scaled = X_valid.astype('float32')\nX_test_scaled = X_test.astype('float32')\n\n\nX_train_scaled /= 255\nX_valid_scaled /= 255\nX_test_scaled /= 255","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:20:29.929027Z","iopub.execute_input":"2022-11-23T02:20:29.929703Z","iopub.status.idle":"2022-11-23T02:20:30.554968Z","shell.execute_reply.started":"2022-11-23T02:20:29.929665Z","shell.execute_reply":"2022-11-23T02:20:30.553907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the model","metadata":{}},{"cell_type":"code","source":"history = model.fit(x=X_train_scaled, y=Y_train,\n                   validation_data=(X_valid_scaled, Y_valid),\n                   batch_size=32,\n                   epochs=20,\n                   verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:20:33.705975Z","iopub.execute_input":"2022-11-23T02:20:33.706343Z","iopub.status.idle":"2022-11-23T02:21:58.832887Z","shell.execute_reply.started":"2022-11-23T02:20:33.706311Z","shell.execute_reply":"2022-11-23T02:21:58.831817Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Accuracy and Loss Visualization","metadata":{}},{"cell_type":"code","source":"f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\nt = f.suptitle('Basic CNN Performance', fontsize=12)\nf.subplots_adjust(top=0.85, wspace=0.3)\n\nepoch_list = list(range(1,21))\nax1.plot(epoch_list, history.history['Acc'], label='Train Accuracy')\nax1.plot(epoch_list, history.history['val_Acc'], label='Validation Accuracy')\nax1.set_xticks(np.arange(0, 21, 5))\nax1.set_ylabel('Accuracy Value')\nax1.set_xlabel('Epoch')\nax1.set_title('Accuracy')\nl1 = ax1.legend(loc=\"best\")\n\nax2.plot(epoch_list, history.history['loss'], label='Train Loss')\nax2.plot(epoch_list, history.history['val_loss'], label='Validation Loss')\nax2.set_xticks(np.arange(0, 21, 5))\nax2.set_ylabel('Loss Value')\nax2.set_xlabel('Epoch')\nax2.set_title('Loss')\nl2 = ax2.legend(loc=\"best\")","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:22:13.623133Z","iopub.execute_input":"2022-11-23T02:22:13.623516Z","iopub.status.idle":"2022-11-23T02:22:13.978336Z","shell.execute_reply.started":"2022-11-23T02:22:13.623465Z","shell.execute_reply":"2022-11-23T02:22:13.977346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Saving","metadata":{}},{"cell_type":"code","source":"model.save('Basic_CNN.h5')","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:22:23.448440Z","iopub.execute_input":"2022-11-23T02:22:23.448846Z","iopub.status.idle":"2022-11-23T02:22:23.935450Z","shell.execute_reply.started":"2022-11-23T02:22:23.448813Z","shell.execute_reply":"2022-11-23T02:22:23.934434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Getting model predictions : Classification Report","metadata":{}},{"cell_type":"code","source":"# Getting model predictions\ntest_predictions = model.predict(X_test_scaled)\npreds = np.argmax(test_predictions, axis=1)\nactual_label = np.argmax(Y_test, axis=1)\nprint(classification_report(actual_label, preds))","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:22:35.326901Z","iopub.execute_input":"2022-11-23T02:22:35.327307Z","iopub.status.idle":"2022-11-23T02:22:37.093651Z","shell.execute_reply.started":"2022-11-23T02:22:35.327272Z","shell.execute_reply":"2022-11-23T02:22:37.092558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Showing Confusion Matrix","metadata":{}},{"cell_type":"code","source":"cnf = confusion_matrix(actual_label, preds)\nplt.figure(figsize=(8,6), dpi=70, facecolor='w', edgecolor='k')\nax = sns.heatmap(cnf, cmap='Blues', annot=True, fmt = 'd', xticklabels=labels, yticklabels=labels)\nplt.title('Brain Tumor MRI Classification')\nplt.xlabel('Prediction')\nplt.ylabel('Ground Truth')\nplt.show(ax)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:22:46.338464Z","iopub.execute_input":"2022-11-23T02:22:46.338862Z","iopub.status.idle":"2022-11-23T02:22:46.635564Z","shell.execute_reply.started":"2022-11-23T02:22:46.338829Z","shell.execute_reply":"2022-11-23T02:22:46.634568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting roc curve","metadata":{}},{"cell_type":"code","source":"skplt.metrics.plot_roc(actual_label, test_predictions)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:22:52.899672Z","iopub.execute_input":"2022-11-23T02:22:52.900031Z","iopub.status.idle":"2022-11-23T02:22:53.178417Z","shell.execute_reply.started":"2022-11-23T02:22:52.900001Z","shell.execute_reply":"2022-11-23T02:22:53.177528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing Model Predictions with Confidence","metadata":{}},{"cell_type":"code","source":"grid_width = 5\ngrid_height = 5\nf, ax = plt.subplots(grid_width, grid_height)\nf.set_size_inches(15, 15)\n\nimg_idx = 0\nfor i in range(0, grid_width):\n    for j in range(0, grid_height):\n        actual = actual_label[img_idx]\n        predicted = preds[img_idx]\n        confidence = round(test_predictions[img_idx][predicted], 2)\n        ax[i][j].axis('off')\n        ax[i][j].set_title('Actual: '+labels[actual]+'\\nPred: '+labels[predicted] + '\\nConf: ' +str(confidence))\n        ax[i][j].imshow(X_test[img_idx])\n        img_idx += 1\n\nplt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0.5, hspace=0.55)   ","metadata":{"execution":{"iopub.status.busy":"2022-11-23T02:23:01.723166Z","iopub.execute_input":"2022-11-23T02:23:01.723560Z","iopub.status.idle":"2022-11-23T02:23:03.113197Z","shell.execute_reply.started":"2022-11-23T02:23:01.723522Z","shell.execute_reply":"2022-11-23T02:23:03.112398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}