{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7888675,"sourceType":"datasetVersion","datasetId":4631233}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","scrolled":true,"execution":{"iopub.status.busy":"2024-05-06T19:01:15.329184Z","iopub.execute_input":"2024-05-06T19:01:15.329799Z","iopub.status.idle":"2024-05-06T19:01:16.904910Z","shell.execute_reply.started":"2024-05-06T19:01:15.329758Z","shell.execute_reply":"2024-05-06T19:01:16.903634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport skimage.io\nimport os \nimport tqdm\nimport glob\nimport tensorflow \nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport tensorflow as tf\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.data.experimental import AUTOTUNE\nfrom tensorflow.keras import Sequential, Input, Model\nfrom tensorflow.keras.layers import RandomRotation, RandomZoom\nfrom tensorflow.keras.layers.experimental.preprocessing import Rescaling\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\nfrom tensorflow.keras import applications\nfrom tensorflow.keras.losses import CategoricalCrossentropy\nfrom tensorflow.keras.optimizers import Adam\n\n\nfrom tqdm import tqdm\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.metrics import Precision, AUC,Recall\nfrom tensorflow.keras.layers import InputLayer, BatchNormalization, Dropout, Flatten, Dense, Activation, MaxPool2D, Conv2D\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.applications.densenet import DenseNet169\nimport copy\nimport warnings\nwarnings.filterwarnings('ignore')\nimport tensorflow as tf\nimport cv2\nimport keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import load_img, img_to_array\nimport matplotlib\nimport matplotlib.pylab as plt\nimport seaborn as sns\nfrom sklearn.utils import shuffle\nfrom sklearn.metrics import confusion_matrix\nfrom keras.applications.vgg16 import VGG16,preprocess_input\nfrom keras.applications.vgg19 import VGG19,preprocess_input\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.optimizers import legacy","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:16.907272Z","iopub.execute_input":"2024-05-06T19:01:16.907806Z","iopub.status.idle":"2024-05-06T19:01:30.190347Z","shell.execute_reply.started":"2024-05-06T19:01:16.907767Z","shell.execute_reply":"2024-05-06T19:01:30.189377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/input/mridataset/cleaned/Training/'\ntest_dir = '/kaggle/input/mridataset/cleaned/Testing/'\nSEED=1345\ntrain_paths = []\ntrain_labels = []\n\nfor label in os.listdir(train_dir):\n    for image in os.listdir(train_dir+label):\n        train_paths.append(train_dir+label+'/'+image)\n        train_labels.append(label)\n\ntrain_paths, train_labels = shuffle(train_paths, train_labels,seed=SEED)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:30.191497Z","iopub.execute_input":"2024-05-06T19:01:30.191986Z","iopub.status.idle":"2024-05-06T19:01:30.210304Z","shell.execute_reply.started":"2024-05-06T19:01:30.191936Z","shell.execute_reply":"2024-05-06T19:01:30.209146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_paths = []\ntest_labels = []\n\nfor label in os.listdir(test_dir):\n    for image in os.listdir(test_dir+label):\n        test_paths.append(test_dir+label+'/'+image)\n        test_labels.append(label)\n\ntest_paths, test_labels = shuffle(test_paths, test_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:30.214860Z","iopub.execute_input":"2024-05-06T19:01:30.215570Z","iopub.status.idle":"2024-05-06T19:01:30.319948Z","shell.execute_reply.started":"2024-05-06T19:01:30.215544Z","shell.execute_reply":"2024-05-06T19:01:30.319013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sizes = [\n    len([x for x in train_labels if x == 'Pituitary']),\n    len([x for x in train_labels if x == 'Meningioma']),\n    len([x for x in train_labels if x == 'Glioma'])\n]\nsizes","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:30.321040Z","iopub.execute_input":"2024-05-06T19:01:30.321292Z","iopub.status.idle":"2024-05-06T19:01:30.329938Z","shell.execute_reply.started":"2024-05-06T19:01:30.321270Z","shell.execute_reply":"2024-05-06T19:01:30.329105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing class distribution in traning data","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Data\nlabels = ['Pituitary',  'Meningioma', 'Glioma']\nsizes = [\n    len([x for x in train_labels if x == 'Pituitary']),\n    len([x for x in train_labels if x == 'Meningioma']),\n    len([x for x in train_labels if x == 'Glioma'])\n]\n\n# Custom color palette for a brain tumor vibe\ncolors = ['#D4A5A5', '#D1C0E0', '#B8D8D8']\nexplode = (0.015, 0.015, 0.015)\n\n# Plotting\nplt.figure(figsize=(10, 6))\nplt.pie(sizes, labels=labels, colors=colors, autopct='%.1f%%', explode=explode, startangle=30, wedgeprops=dict(width=0.2))\n\n# Title\nplt.title('Distribution of Tumor Types', fontsize=20)\n\n# Add a legend\nplt.legend(labels, title='Tumor Types', loc='upper right', bbox_to_anchor=(1, 0, 0.5, 1))\n\n# Equal aspect ratio ensures that pie is drawn as a circle.\nplt.axis('equal')\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:30.331040Z","iopub.execute_input":"2024-05-06T19:01:30.331309Z","iopub.status.idle":"2024-05-06T19:01:30.539922Z","shell.execute_reply.started":"2024-05-06T19:01:30.331286Z","shell.execute_reply":"2024-05-06T19:01:30.539018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Combine train and test labels\nall_labels = train_labels + test_labels\n\n# Data\nlabels = ['Pituitary', 'Meningioma', 'Glioma']\nsizes = [\n    len([x for x in all_labels if x == 'Pituitary']),\n    len([x for x in all_labels if x == 'Meningioma']),\n    len([x for x in all_labels if x == 'Glioma'])\n]\n\n# Custom color palette for a brain tumor vibe\ncolors = ['#D4A5A5', '#D1C0E0', '#B8D8D8']\nexplode = (0.015, 0.015, 0.015)\n\n# Plotting\nplt.figure(figsize=(10, 6))\nwedges, texts, autotexts = plt.pie(sizes, labels=labels, colors=colors, autopct=lambda pct: \"{:.1f}% ({:d})\".format(pct, int(pct/100 * sum(sizes))+1), explode=explode, startangle=30, wedgeprops=dict(width=0.2))\n\n# Title\nplt.title('Total Distribution of Tumor Types', fontsize=20)\n\n# Add a legend\nplt.legend(wedges, labels, title='Tumor Types', loc='upper right', bbox_to_anchor=(1, 0, 0.5, 1))\n\n# Equal aspect ratio ensures that pie is drawn as a circle.\nplt.axis('equal')\n\n# Show the plot\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:30.541281Z","iopub.execute_input":"2024-05-06T19:01:30.541635Z","iopub.status.idle":"2024-05-06T19:01:30.716474Z","shell.execute_reply.started":"2024-05-06T19:01:30.541602Z","shell.execute_reply":"2024-05-06T19:01:30.715553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Distribution of Data between Train and Test Sets","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Data\nlabels = ['Train', 'Test']\nsizes = [len(train_labels), len(test_labels)]\ncolors = ['#BEF0CB', '#C1AEFC']\nexplode = (0.05, 0)\n\n# Plotting\nplt.figure(figsize=(6, 4))\nplt.pie(sizes, labels=labels, colors=colors, autopct='%.1f%%', explode=explode, startangle=30, wedgeprops=dict(width=0.2))\n\n# Title\nplt.title('Distribution of Data between Train and Test Sets', fontsize=20)\n\n# Equal aspect ratio ensures that pie is drawn as a circle.\nplt.axis('equal')\n\n# Show the plot\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:30.717655Z","iopub.execute_input":"2024-05-06T19:01:30.718016Z","iopub.status.idle":"2024-05-06T19:01:30.846239Z","shell.execute_reply.started":"2024-05-06T19:01:30.717984Z","shell.execute_reply":"2024-05-06T19:01:30.845318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimage = cv2.imread(train_paths[0])\nplt.imshow(image)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:30.847430Z","iopub.execute_input":"2024-05-06T19:01:30.847702Z","iopub.status.idle":"2024-05-06T19:01:31.103298Z","shell.execute_reply.started":"2024-05-06T19:01:30.847678Z","shell.execute_reply":"2024-05-06T19:01:31.102341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:31.106651Z","iopub.execute_input":"2024-05-06T19:01:31.106947Z","iopub.status.idle":"2024-05-06T19:01:31.112791Z","shell.execute_reply.started":"2024-05-06T19:01:31.106923Z","shell.execute_reply":"2024-05-06T19:01:31.111844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Count of each class","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,4))\ncolors = ['#FF6347',  # Red for glioma\n          '#4169E1',  # Blue for meningioma\n          '#FFA500']  # Orange for pituitary\nplt.bar(['Glioma','Meningioma','Pituitary'],[1426,708,930],color=colors)\n\nplt.xlabel('Classes')\nplt.ylabel('Counts')\nplt.title('Visualization of Data')\nplt.gca().set_facecolor('#F5F5F5')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:31.113869Z","iopub.execute_input":"2024-05-06T19:01:31.114184Z","iopub.status.idle":"2024-05-06T19:01:31.289973Z","shell.execute_reply.started":"2024-05-06T19:01:31.114160Z","shell.execute_reply":"2024-05-06T19:01:31.288998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_df(tr_path):\n    classes = []\n    class_paths = []\n    files = os.listdir(tr_path)\n    for file in files:\n        label_dir = os.path.join(tr_path, file)\n        label = os.listdir(label_dir)\n        for image in label:\n            image_path = os.path.join(label_dir, image)\n            class_paths.append(image_path)\n            classes.append(file)\n    image_classes = pd.Series(classes, name='Class')\n    image_paths = pd.Series(class_paths, name='Class Path')\n    tr_df = pd.concat([image_paths, image_classes], axis=1)\n    return tr_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:31.291017Z","iopub.execute_input":"2024-05-06T19:01:31.291271Z","iopub.status.idle":"2024-05-06T19:01:31.298028Z","shell.execute_reply.started":"2024-05-06T19:01:31.291249Z","shell.execute_reply":"2024-05-06T19:01:31.296991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_df(ts_path):\n    classes = []\n    class_paths = []\n    files = os.listdir(ts_path)\n    for file in files:\n        label_dir = os.path.join(ts_path, file)\n        label = os.listdir(label_dir)\n        for image in label:\n            image_path = os.path.join(label_dir, image)\n            class_paths.append(image_path)\n            classes.append(file)\n    image_classes = pd.Series(classes, name='Class')\n    image_paths = pd.Series(class_paths, name='Class Path')\n    ts_df = pd.concat([image_paths, image_classes], axis=1)\n    return ts_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:31.299434Z","iopub.execute_input":"2024-05-06T19:01:31.299878Z","iopub.status.idle":"2024-05-06T19:01:31.307618Z","shell.execute_reply.started":"2024-05-06T19:01:31.299846Z","shell.execute_reply":"2024-05-06T19:01:31.306795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr_df = train_df('/kaggle/input/mridataset/cleaned/Training')\nts_df = test_df('/kaggle/input/mridataset/cleaned/Testing')","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:31.308661Z","iopub.execute_input":"2024-05-06T19:01:31.308922Z","iopub.status.idle":"2024-05-06T19:01:31.334745Z","shell.execute_reply.started":"2024-05-06T19:01:31.308900Z","shell.execute_reply":"2024-05-06T19:01:31.333996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating validation dataset","metadata":{}},{"cell_type":"code","source":"train_df,valid_df = train_test_split(tr_df, train_size=.9, random_state=20)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:31.335687Z","iopub.execute_input":"2024-05-06T19:01:31.335923Z","iopub.status.idle":"2024-05-06T19:01:31.344566Z","shell.execute_reply.started":"2024-05-06T19:01:31.335901Z","shell.execute_reply":"2024-05-06T19:01:31.343770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data generators for Image Augmentation","metadata":{}},{"cell_type":"code","source":"image_size = (256, 256)\n\n# Training batch size\nbatch_size = 32\n\nSEED=1345\n\ntrain_datagen=ImageDataGenerator(rescale=1./255,\n                                   rotation_range=10,\n                                   brightness_range=(0.85, 1.15),\n                                   width_shift_range=0.002,\n                                   height_shift_range=0.002,\n                                   shear_range=12.5,\n                                   zoom_range=0,\n                                   horizontal_flip=True,\n                                   vertical_flip=False,\n                                   fill_mode=\"nearest\")\n\nvalidation_datagen=ImageDataGenerator(rescale=1./255)\ntest_datagen=ImageDataGenerator(rescale=1./255)\n\n\ntr_gen = train_datagen.flow_from_dataframe(train_df, x_col='Class Path',\n                                 y_col='Class',shuffle=True,\n                                 target_size=image_size,seed = SEED,\n                                 batch_size=32,\n                                 class_mode ='categorical',)\n\nvalid_gen = validation_datagen.flow_from_dataframe(valid_df, x_col='Class Path',\n                                    y_col='Class',\n                                    target_size=image_size,seed = SEED,\n                                    batch_size=32,\n                                    class_mode ='categorical',)\n\nts_gen = test_datagen.flow_from_dataframe(ts_df, x_col='Class Path',\n                                y_col='Class',\n                                 target_size=image_size,shuffle=False,\n                                 seed = SEED,\n                                 batch_size=32,\n                                 class_mode ='categorical',)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:31.345699Z","iopub.execute_input":"2024-05-06T19:01:31.346489Z","iopub.status.idle":"2024-05-06T19:01:32.629390Z","shell.execute_reply.started":"2024-05-06T19:01:31.346463Z","shell.execute_reply":"2024-05-06T19:01:32.628474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_images = ts_gen.samples\nprint(\"Total images:\", total_images)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:32.630668Z","iopub.execute_input":"2024-05-06T19:01:32.631377Z","iopub.status.idle":"2024-05-06T19:01:32.635994Z","shell.execute_reply.started":"2024-05-06T19:01:32.631343Z","shell.execute_reply":"2024-05-06T19:01:32.635064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = ['glioma', 'meningioma', 'pituitary']\nplt.figure(figsize=(5, 5))\nfor images, labels in tr_gen:\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i])\n        plt.title(class_names[np.argmax(labels[i])])\n        plt.axis(\"off\")\n    break","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:32.637156Z","iopub.execute_input":"2024-05-06T19:01:32.637391Z","iopub.status.idle":"2024-05-06T19:01:33.948448Z","shell.execute_reply.started":"2024-05-06T19:01:32.637370Z","shell.execute_reply":"2024-05-06T19:01:33.947493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = './cnn_best_weights.hdf5'\nearlystopping=EarlyStopping(monitor='val_accuracy',\n                           mode='max',\n                           patience=15,\n                           verbose=1)\n\n\ncallback_list=[earlystopping]","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:33.949822Z","iopub.execute_input":"2024-05-06T19:01:33.950209Z","iopub.status.idle":"2024-05-06T19:01:33.955174Z","shell.execute_reply.started":"2024-05-06T19:01:33.950175Z","shell.execute_reply":"2024-05-06T19:01:33.954321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Creation","metadata":{}},{"cell_type":"code","source":"image_size = (256,256,3)\n\n\nclass_count = len(list(tr_gen.class_indices.keys()))\nmodel = Sequential([\n    \n    # Convolutional layer 1\n    Conv2D(32, (4, 4), activation=\"relu\", input_shape=image_size),\n    MaxPooling2D(pool_size=(3, 3)),\n\n    # Convolutional layer 2\n    Conv2D(64, (4, 4), activation=\"relu\"),\n    MaxPooling2D(pool_size=(3, 3)),\n\n    # Convolutional layer 3\n    Conv2D(128, (4, 4), activation=\"relu\"),\n    MaxPooling2D(pool_size=(3, 3)),\n\n    # Convolutional layer 4\n    Conv2D(128, (4, 4), activation=\"relu\"),\n    Flatten(),\n\n    # Full connect layers\n    Dense(512, activation=\"relu\"),\n#     Dropout(0.5, seed=SEED),\n    Dense(class_count, activation=\"softmax\")\n])\noptimizer = legacy.Nadam(learning_rate=0.001, beta_1=0.869, beta_2=0.995)\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy',tf.keras.metrics.AUC(),\n                        tf.keras.metrics.Precision(),\n                        tf.keras.metrics.Recall(),])","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:33.956330Z","iopub.execute_input":"2024-05-06T19:01:33.956605Z","iopub.status.idle":"2024-05-06T19:01:34.943558Z","shell.execute_reply.started":"2024-05-06T19:01:33.956581Z","shell.execute_reply":"2024-05-06T19:01:34.942571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:34.946779Z","iopub.execute_input":"2024-05-06T19:01:34.947513Z","iopub.status.idle":"2024-05-06T19:01:34.978631Z","shell.execute_reply.started":"2024-05-06T19:01:34.947486Z","shell.execute_reply":"2024-05-06T19:01:34.977070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model, to_file='modelCNN_plot.png', show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:01:34.980884Z","iopub.execute_input":"2024-05-06T19:01:34.981188Z","iopub.status.idle":"2024-05-06T19:01:35.222894Z","shell.execute_reply.started":"2024-05-06T19:01:34.981164Z","shell.execute_reply":"2024-05-06T19:01:35.222053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"steps_per_epoch = tr_gen.samples // 32\nvalidation_steps = valid_gen.samples // 32\nmodel_es = EarlyStopping(monitor='loss', min_delta=1e-9, patience=8, verbose=True)\nmodel_rlr = ReduceLROnPlateau(monitor='val_accuracy',\n                           mode='max', factor=0.3, patience=5, verbose=True)\n\n# Training the model\nhistory = model.fit(tr_gen,\n                    steps_per_epoch=len(tr_gen),\n                    epochs=30,verbose = 1,\n                    validation_data=valid_gen,\n                    validation_steps=validation_steps,\n                    callbacks=[model_es, model_rlr])","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-05-06T19:01:35.224200Z","iopub.execute_input":"2024-05-06T19:01:35.224805Z","iopub.status.idle":"2024-05-06T19:19:43.412184Z","shell.execute_reply.started":"2024-05-06T19:01:35.224772Z","shell.execute_reply":"2024-05-06T19:19:43.411374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.evaluate(ts_gen)\ntest_loss = result[0]\ntest_accuracy = result[1]\ntest_AUC = result[2]\ntest_pre = result[3]\ntest_rec = result[4]\nprint(f'Test Loss = {test_loss}')\nprint(f'Test Accuracy = {test_accuracy}')\nprint(f'Test AUC = {test_AUC}')\nprint(f'Test Precision = {test_pre}')\nprint(f'Test Recall = {test_rec}')","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:19:43.413399Z","iopub.execute_input":"2024-05-06T19:19:43.413683Z","iopub.status.idle":"2024-05-06T19:19:49.629131Z","shell.execute_reply.started":"2024-05-06T19:19:43.413658Z","shell.execute_reply":"2024-05-06T19:19:49.628208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Training Metrics Over Epochs","metadata":{}},{"cell_type":"code","source":"history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:19:49.630202Z","iopub.execute_input":"2024-05-06T19:19:49.630512Z","iopub.status.idle":"2024-05-06T19:19:49.636667Z","shell.execute_reply.started":"2024-05-06T19:19:49.630486Z","shell.execute_reply":"2024-05-06T19:19:49.635750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure , axis = plt.subplots(2,2,figsize=(12,12))\n\naxis[0,0].plot(history.history['loss'] , label='train')\naxis[0,0].plot(history.history['val_loss'] , label='val')\naxis[0,0].set_title('Training/validation loss over Epochs')\naxis[0,0].set_xlabel('Epochs')\naxis[0,0].set_ylabel('loss')\naxis[0,0].legend()\n\naxis[1,0].plot(history.history['accuracy'], label='train')\naxis[1,0].plot(history.history['val_accuracy'], label='val')\naxis[1,0].set_title('Training/validation accuracy over Epochs')\naxis[1,0].set_xlabel('epoch')\naxis[1,0].set_ylabel('Accuracy')\naxis[1,0].legend()\n\naxis[0,1].plot(history.history['precision'], label='train')\naxis[0,1].plot(history.history['val_precision'], label='val')\naxis[0,1].set_title('Training/validation precision over Epochs')\naxis[0,1].set_xlabel('epoch')\naxis[0,1].set_ylabel('Precision')\naxis[0,1].legend()\n\naxis[1,1].plot(history.history['recall'], label='train')\naxis[1,1].plot(history.history['val_recall'], label='val')\naxis[1,1].set_title('Training/validation recall over Epochs')\naxis[1,1].set_xlabel('epoch')\naxis[1,1].set_ylabel('Recall')\naxis[1,1].legend()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:19:49.637745Z","iopub.execute_input":"2024-05-06T19:19:49.638027Z","iopub.status.idle":"2024-05-06T19:19:50.598853Z","shell.execute_reply.started":"2024-05-06T19:19:49.638003Z","shell.execute_reply":"2024-05-06T19:19:50.597886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test data predictions","metadata":{}},{"cell_type":"code","source":"test_label=ts_gen.classes\nY_pred=model.predict(ts_gen)\ny_pred=[]\n[y_pred.append(np.argmax(l)) for l in Y_pred ]","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-05-06T19:19:50.600430Z","iopub.execute_input":"2024-05-06T19:19:50.600789Z","iopub.status.idle":"2024-05-06T19:19:53.182849Z","shell.execute_reply.started":"2024-05-06T19:19:50.600757Z","shell.execute_reply":"2024-05-06T19:19:53.181926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=np.array(y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:19:53.187324Z","iopub.execute_input":"2024-05-06T19:19:53.187591Z","iopub.status.idle":"2024-05-06T19:19:53.191783Z","shell.execute_reply.started":"2024-05-06T19:19:53.187569Z","shell.execute_reply":"2024-05-06T19:19:53.191013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion Matrix","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report\n# Compute the classification report\nreport = classification_report(test_label, y_pred)\nprint(\"Classification Report:\")\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:19:53.192841Z","iopub.execute_input":"2024-05-06T19:19:53.193169Z","iopub.status.idle":"2024-05-06T19:19:53.210857Z","shell.execute_reply.started":"2024-05-06T19:19:53.193146Z","shell.execute_reply":"2024-05-06T19:19:53.210019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport itertools\nimport matplotlib.pyplot as plt\nimport numpy as np\n\ndef plot_confusion_matrix(cm, classes, cmap=plt.cm.Purples):\n    plt.figure(figsize=(10, 8))\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title('Confusion Matrix', fontsize=16)\n    plt.colorbar()\n\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45, fontsize=12)\n    plt.yticks(tick_marks, classes, fontsize=12)\n\n    fmt = 'd'\n    \n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, format(cm[i, j], fmt),\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > cm.max() / 2 else \"black\",\n                 fontsize=10)\n\n    plt.ylabel('True label', fontsize=14)\n    plt.xlabel('Predicted label', fontsize=14)\n    plt.title('Confusion Matrix', fontsize=16)\n    plt.tight_layout()\n\n# Example confusion matrix\ncm = confusion_matrix(test_label, y_pred)\nplt.figure(figsize=(8, 6))\nplot_confusion_matrix(cm, class_names)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:19:53.211888Z","iopub.execute_input":"2024-05-06T19:19:53.212273Z","iopub.status.idle":"2024-05-06T19:19:53.491903Z","shell.execute_reply.started":"2024-05-06T19:19:53.212250Z","shell.execute_reply":"2024-05-06T19:19:53.491025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('CNN.h5')","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:19:53.493084Z","iopub.execute_input":"2024-05-06T19:19:53.493373Z","iopub.status.idle":"2024-05-06T19:19:53.572890Z","shell.execute_reply.started":"2024-05-06T19:19:53.493347Z","shell.execute_reply":"2024-05-06T19:19:53.572133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ncm = confusion_matrix(test_label, y_pred)\ncm","metadata":{"execution":{"iopub.status.busy":"2024-05-06T19:19:53.574099Z","iopub.execute_input":"2024-05-06T19:19:53.574779Z","iopub.status.idle":"2024-05-06T19:19:53.582825Z","shell.execute_reply.started":"2024-05-06T19:19:53.574745Z","shell.execute_reply":"2024-05-06T19:19:53.582002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}