{"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":"code","source":"import os\nimport os\nimport numpy as np\nimport pandas as pd\nimport cv2\nfrom glob import glob\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport seaborn as sns\nimport pydicom as dcm\nimport math\nfrom tensorflow.keras.layers import Layer, Convolution2D, Flatten, Dense\nfrom tensorflow.keras.layers import Concatenate, UpSampling2D, Conv2D, Reshape, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications.mobilenet import MobileNet\nfrom tensorflow.keras.applications.mobilenet import preprocess_input \nimport tensorflow.keras.utils as pltUtil\nfrom tensorflow.keras.utils import Sequence\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-29T10:50:57.736948Z","iopub.execute_input":"2023-05-29T10:50:57.737296Z","iopub.status.idle":"2023-05-29T10:50:58.637606Z","shell.execute_reply.started":"2023-05-29T10:50:57.737268Z","shell.execute_reply":"2023-05-29T10:50:58.636570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv(\"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\")\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:58.639646Z","iopub.execute_input":"2023-05-29T10:50:58.641157Z","iopub.status.idle":"2023-05-29T10:50:58.716010Z","shell.execute_reply.started":"2023-05-29T10:50:58.641117Z","shell.execute_reply":"2023-05-29T10:50:58.714862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:58.717831Z","iopub.execute_input":"2023-05-29T10:50:58.718287Z","iopub.status.idle":"2023-05-29T10:50:58.725289Z","shell.execute_reply.started":"2023-05-29T10:50:58.718252Z","shell.execute_reply":"2023-05-29T10:50:58.724065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:58.729282Z","iopub.execute_input":"2023-05-29T10:50:58.729701Z","iopub.status.idle":"2023-05-29T10:50:58.765719Z","shell.execute_reply.started":"2023-05-29T10:50:58.729663Z","shell.execute_reply":"2023-05-29T10:50:58.764860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## we can see that all the null column values are with Target 0 indicating that those patients do not have penumonia\nlabels[labels.isnull().any(axis=1)].Target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:58.767040Z","iopub.execute_input":"2023-05-29T10:50:58.768241Z","iopub.status.idle":"2023-05-29T10:50:58.788788Z","shell.execute_reply.started":"2023-05-29T10:50:58.768198Z","shell.execute_reply":"2023-05-29T10:50:58.787488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## we can see that all the non null column values are with Target 1 indicating that those patients have pneumonia\nlabels[~labels.isnull().any(axis=1)].Target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:58.790947Z","iopub.execute_input":"2023-05-29T10:50:58.791754Z","iopub.status.idle":"2023-05-29T10:50:58.813098Z","shell.execute_reply.started":"2023-05-29T10:50:58.791716Z","shell.execute_reply":"2023-05-29T10:50:58.812003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Distubution of Targets , there are 20672 records with no pneumonia and 9555 with pneumonia\nlabels.Target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:58.816944Z","iopub.execute_input":"2023-05-29T10:50:58.817222Z","iopub.status.idle":"2023-05-29T10:50:58.825223Z","shell.execute_reply.started":"2023-05-29T10:50:58.817199Z","shell.execute_reply":"2023-05-29T10:50:58.824151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Disturbution of Target, there are 31% of patients with pneumonia and the remaining are no pneumonia\n## There is a class imbalance issue\nlabel_count=labels['Target'].value_counts()\nexplode = (0.01,0.01)  \n\nfig1, ax1 = plt.subplots(figsize=(5,5))\nax1.pie(label_count.values, explode=explode, labels=label_count.index, autopct='%1.1f%%',\n        shadow=True, startangle=90)\nax1.axis('equal') \nplt.title('Target Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:58.826531Z","iopub.execute_input":"2023-05-29T10:50:58.827082Z","iopub.status.idle":"2023-05-29T10:50:59.032040Z","shell.execute_reply.started":"2023-05-29T10:50:58.827048Z","shell.execute_reply":"2023-05-29T10:50:59.030781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Are there Unique Patients In Data Set ?? \" ,labels['patientId'].is_unique)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.037707Z","iopub.execute_input":"2023-05-29T10:50:59.041303Z","iopub.status.idle":"2023-05-29T10:50:59.062852Z","shell.execute_reply.started":"2023-05-29T10:50:59.041251Z","shell.execute_reply":"2023-05-29T10:50:59.061479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#labels.loc[labels.index.repeat(labels.patientId)]\nduplicateRowsDF = labels[labels.duplicated(['patientId'])]\nduplicateRowsDF.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.068479Z","iopub.execute_input":"2023-05-29T10:50:59.069248Z","iopub.status.idle":"2023-05-29T10:50:59.084520Z","shell.execute_reply.started":"2023-05-29T10:50:59.069189Z","shell.execute_reply":"2023-05-29T10:50:59.083088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"duplicateRowsDF.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.086353Z","iopub.execute_input":"2023-05-29T10:50:59.086993Z","iopub.status.idle":"2023-05-29T10:50:59.102685Z","shell.execute_reply.started":"2023-05-29T10:50:59.086956Z","shell.execute_reply":"2023-05-29T10:50:59.101836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Examining one of the patient id which is duplicate , we can see that the x,y, widht and height is not the same\n## This indicates that the same patient has two bounding boxes in the same dicom image\nlabels[labels.patientId=='00436515-870c-4b36-a041-de91049b9ab4']","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.104632Z","iopub.execute_input":"2023-05-29T10:50:59.105609Z","iopub.status.idle":"2023-05-29T10:50:59.125192Z","shell.execute_reply.started":"2023-05-29T10:50:59.105576Z","shell.execute_reply":"2023-05-29T10:50:59.124129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[labels.patientId=='00704310-78a8-4b38-8475-49f4573b2dbb']","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.128225Z","iopub.execute_input":"2023-05-29T10:50:59.129019Z","iopub.status.idle":"2023-05-29T10:50:59.151164Z","shell.execute_reply.started":"2023-05-29T10:50:59.128983Z","shell.execute_reply":"2023-05-29T10:50:59.150070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Reading the class Info Data Set**","metadata":{}},{"cell_type":"code","source":"## Reading the classes label , \nclass_labels = pd.read_csv('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_detailed_class_info.csv')\nclass_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.152551Z","iopub.execute_input":"2023-05-29T10:50:59.153370Z","iopub.status.idle":"2023-05-29T10:50:59.212991Z","shell.execute_reply.started":"2023-05-29T10:50:59.153338Z","shell.execute_reply":"2023-05-29T10:50:59.211848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.216954Z","iopub.execute_input":"2023-05-29T10:50:59.217254Z","iopub.status.idle":"2023-05-29T10:50:59.227051Z","shell.execute_reply.started":"2023-05-29T10:50:59.217228Z","shell.execute_reply":"2023-05-29T10:50:59.225901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.228841Z","iopub.execute_input":"2023-05-29T10:50:59.229299Z","iopub.status.idle":"2023-05-29T10:50:59.258941Z","shell.execute_reply.started":"2023-05-29T10:50:59.229262Z","shell.execute_reply":"2023-05-29T10:50:59.257855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels['class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.260211Z","iopub.execute_input":"2023-05-29T10:50:59.260978Z","iopub.status.idle":"2023-05-29T10:50:59.271908Z","shell.execute_reply.started":"2023-05-29T10:50:59.260947Z","shell.execute_reply":"2023-05-29T10:50:59.270811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_count=class_labels['class'].value_counts()\nexplode = (0.01,0.01,0.01)  \n\nfig1, ax1 = plt.subplots(figsize=(5,5))\nax1.pie(label_count.values, explode=explode, labels=label_count.index, autopct='%1.1f%%',\n        shadow=True, startangle=90)\nax1.axis('equal') \nplt.title('Class Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.273425Z","iopub.execute_input":"2023-05-29T10:50:59.274246Z","iopub.status.idle":"2023-05-29T10:50:59.464848Z","shell.execute_reply.started":"2023-05-29T10:50:59.274213Z","shell.execute_reply":"2023-05-29T10:50:59.463932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#labels.loc[labels.index.repeat(labels.patientId)]\nduplicateClassRowsDF = class_labels[class_labels.duplicated(['patientId'])]\nduplicateClassRowsDF.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.466312Z","iopub.execute_input":"2023-05-29T10:50:59.466921Z","iopub.status.idle":"2023-05-29T10:50:59.482253Z","shell.execute_reply.started":"2023-05-29T10:50:59.466863Z","shell.execute_reply":"2023-05-29T10:50:59.481108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"duplicateClassRowsDF.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.484801Z","iopub.execute_input":"2023-05-29T10:50:59.485449Z","iopub.status.idle":"2023-05-29T10:50:59.506438Z","shell.execute_reply.started":"2023-05-29T10:50:59.485410Z","shell.execute_reply":"2023-05-29T10:50:59.505479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## The same patient id has the same class even though they are duplicate\nclass_labels[class_labels.patientId=='00704310-78a8-4b38-8475-49f4573b2dbb']","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.508350Z","iopub.execute_input":"2023-05-29T10:50:59.509633Z","iopub.status.idle":"2023-05-29T10:50:59.538147Z","shell.execute_reply.started":"2023-05-29T10:50:59.509596Z","shell.execute_reply":"2023-05-29T10:50:59.537116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Merging the class and labels data set into training dataset","metadata":{}},{"cell_type":"code","source":"# Conctinating the two dataset - 'labels' and 'class_labels':\ntraining_data = pd.concat([labels, class_labels['class']], axis = 1)\n\ntraining_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.539581Z","iopub.execute_input":"2023-05-29T10:50:59.540216Z","iopub.status.idle":"2023-05-29T10:50:59.575743Z","shell.execute_reply.started":"2023-05-29T10:50:59.540180Z","shell.execute_reply":"2023-05-29T10:50:59.574700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows = 1, figsize = (12, 6))\ntemp = training_data.groupby('Target')['class'].value_counts()\ndata_target_class = pd.DataFrame(data = {'Values': temp.values}, index = temp.index).reset_index()\nsns.barplot(ax = ax, x = 'Target', y = 'Values', hue = 'class', data = data_target_class, palette = 'Set3')\nplt.title('Class and Target  Distrubution')\n\n## it shows that class distrubution grouped by Target \n## Target 0 has only Normal or No Lung Opacity class\n## Target 1 has only Lung Opacity class","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:50:59.577239Z","iopub.execute_input":"2023-05-29T10:50:59.577899Z","iopub.status.idle":"2023-05-29T10:51:00.043206Z","shell.execute_reply.started":"2023-05-29T10:50:59.577860Z","shell.execute_reply":"2023-05-29T10:51:00.042258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.patches as patches\n\ndef inspectImages(data):\n    img_data = list(data.T.to_dict().values())\n    f, ax = plt.subplots(3,3, figsize=(16,18))\n    for i,data_row in enumerate(img_data):\n        patientImage = data_row['patientId']\n        dcm_file = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/'+'{}.dcm'.format(patientImage)\n        data_row_img_data = dcm.read_file(dcm_file)\n        modality = data_row_img_data.Modality\n        age = data_row_img_data.PatientAge\n        sex = data_row_img_data.PatientSex\n        data_row_img = dcm.dcmread(dcm_file)\n        ax[i//3, i%3].imshow(data_row_img.pixel_array, cmap=plt.cm.bone) \n        ax[i//3, i%3].axis('off')\n        ax[i//3, i%3].set_title('ID: {}\\nModality: {} Age: {} Sex: {} Target: {}\\nClass: {}\\Bounds: {}:{}:{}:{}'.format(\n                data_row['patientId'],\n                modality, age, sex, data_row['Target'], data_row['class'], \n                data_row['x'],data_row['y'],data_row['width'],data_row['height']))\n        label = data_row[\"class\"]\n        if not math.isnan(data_row['x']):\n            x, y, width, height  =  data_row['x'],data_row['y'],data_row['width'],data_row['height']\n            rect = patches.Rectangle((x, y),width, height,\n                                 linewidth = 2,\n                                 edgecolor = 'r',\n                                 facecolor = 'none')\n\n        # Draw the bounding box on top of the image\n            ax[i//3, i%3].add_patch(rect)\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:51:00.044821Z","iopub.execute_input":"2023-05-29T10:51:00.045233Z","iopub.status.idle":"2023-05-29T10:51:00.060000Z","shell.execute_reply.started":"2023-05-29T10:51:00.045197Z","shell.execute_reply":"2023-05-29T10:51:00.058805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Displaying Chest X-ray Images of Patients who have Pneuomina","metadata":{}},{"cell_type":"code","source":"## checking few images which has pneuonia \ninspectImages(training_data[training_data['Target']==1].sample(9))","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:51:00.061436Z","iopub.execute_input":"2023-05-29T10:51:00.061895Z","iopub.status.idle":"2023-05-29T10:51:02.211325Z","shell.execute_reply.started":"2023-05-29T10:51:00.061846Z","shell.execute_reply":"2023-05-29T10:51:02.210084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## checking few images which does not have pneuonia \ninspectImages(training_data[training_data['Target']==0].sample(9))","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:51:02.212736Z","iopub.execute_input":"2023-05-29T10:51:02.213148Z","iopub.status.idle":"2023-05-29T10:51:04.338407Z","shell.execute_reply.started":"2023-05-29T10:51:02.213115Z","shell.execute_reply":"2023-05-29T10:51:04.337544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reading the Dicom images meta data and appending it to the training set","metadata":{}},{"cell_type":"code","source":"## DCIM image contain the meta data alon with it, \n## Function to read the dcim data and appending to the resultset\ndef readDCIMData(rowData):\n    dcm_file = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/'+'{}.dcm'.format(rowData.patientId)\n    dcm_data = dcm.read_file(dcm_file)\n    img = dcm_data.pixel_array\n    return dcm_data.PatientSex,dcm_data.PatientAge","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:51:04.339705Z","iopub.execute_input":"2023-05-29T10:51:04.340630Z","iopub.status.idle":"2023-05-29T10:51:04.346352Z","shell.execute_reply.started":"2023-05-29T10:51:04.340596Z","shell.execute_reply":"2023-05-29T10:51:04.345313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Reading the image data and append it to the training_data dataset\ntraining_data['sex'], training_data['age'] = zip(*training_data.apply(readDCIMData, axis=1))","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:51:04.357514Z","iopub.execute_input":"2023-05-29T10:51:04.357838Z","iopub.status.idle":"2023-05-29T10:57:06.032813Z","shell.execute_reply.started":"2023-05-29T10:51:04.357810Z","shell.execute_reply":"2023-05-29T10:57:06.031850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:06.034088Z","iopub.execute_input":"2023-05-29T10:57:06.034439Z","iopub.status.idle":"2023-05-29T10:57:06.083064Z","shell.execute_reply.started":"2023-05-29T10:57:06.034406Z","shell.execute_reply":"2023-05-29T10:57:06.082058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting age to Numeric as the current data type is a String\ntraining_data['age'] = training_data.age.astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:06.084681Z","iopub.execute_input":"2023-05-29T10:57:06.085120Z","iopub.status.idle":"2023-05-29T10:57:06.096848Z","shell.execute_reply.started":"2023-05-29T10:57:06.085084Z","shell.execute_reply":"2023-05-29T10:57:06.095841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data.describe(include=\"all\").T\n## The mean age is 46 years , where as minimum age is 1 year and the max age is 155 which seems to be an outlier\n## 50% of the patiens are of aroudn 49 age , the std deviation is 16 which sugges that age is not normally distubuted","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:06.098242Z","iopub.execute_input":"2023-05-29T10:57:06.098784Z","iopub.status.idle":"2023-05-29T10:57:06.177977Z","shell.execute_reply.started":"2023-05-29T10:57:06.098752Z","shell.execute_reply":"2023-05-29T10:57:06.176842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data.sex.value_counts()\n## there are only two genders ","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:06.179615Z","iopub.execute_input":"2023-05-29T10:57:06.180013Z","iopub.status.idle":"2023-05-29T10:57:06.191282Z","shell.execute_reply.started":"2023-05-29T10:57:06.179979Z","shell.execute_reply":"2023-05-29T10:57:06.190273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Distbution of Sex Among the tragets\nfig, ax = plt.subplots(nrows = 1, figsize = (12, 6))\ntemp = training_data.groupby('Target')['sex'].value_counts()\ndata_target_class = pd.DataFrame(data = {'Values': temp.values}, index = temp.index).reset_index()\nsns.barplot(ax = ax, x = 'Target', y = 'Values', hue = 'sex', data = data_target_class, palette = 'Set3')\nplt.title('Sex and Target for Chest Exams')\n\n## the number of males in both category are higher than women","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:06.192583Z","iopub.execute_input":"2023-05-29T10:57:06.195566Z","iopub.status.idle":"2023-05-29T10:57:06.549838Z","shell.execute_reply.started":"2023-05-29T10:57:06.195537Z","shell.execute_reply":"2023-05-29T10:57:06.548806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Distbution of Sex Among the classes\nfig, ax = plt.subplots(nrows = 1, figsize = (12, 6))\ntemp = training_data.groupby('class')['sex'].value_counts()\ndata_target_class = pd.DataFrame(data = {'Values': temp.values}, index = temp.index).reset_index()\nsns.barplot(ax = ax, x = 'class', y = 'Values', hue = 'sex', data = data_target_class, palette = 'Set3')\nplt.title('Sex and class for Chest Exams')\n\n## the number of males in all classes are higher than women","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:06.551270Z","iopub.execute_input":"2023-05-29T10:57:06.551722Z","iopub.status.idle":"2023-05-29T10:57:06.925405Z","shell.execute_reply.started":"2023-05-29T10:57:06.551684Z","shell.execute_reply":"2023-05-29T10:57:06.924529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(training_data.age)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:06.926911Z","iopub.execute_input":"2023-05-29T10:57:06.927278Z","iopub.status.idle":"2023-05-29T10:57:07.432721Z","shell.execute_reply.started":"2023-05-29T10:57:06.927243Z","shell.execute_reply":"2023-05-29T10:57:07.431797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))  # setting the figure size\nax = sns.barplot(x='class', y='age', data=training_data, palette='muted')  # barplot'\n## This is the distubution of Age with class, maximum age of person with pneuomina is arund 45","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:07.434298Z","iopub.execute_input":"2023-05-29T10:57:07.434628Z","iopub.status.idle":"2023-05-29T10:57:08.109800Z","shell.execute_reply.started":"2023-05-29T10:57:07.434596Z","shell.execute_reply":"2023-05-29T10:57:08.108823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))  # setting the figure size\nax = sns.barplot(x='Target', y='age', data=training_data, palette='muted')  # barplot'\n## This is the distubution of Age with class, maximum age of person with pneuomina is arund 45\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:08.111167Z","iopub.execute_input":"2023-05-29T10:57:08.111921Z","iopub.status.idle":"2023-05-29T10:57:08.723717Z","shell.execute_reply.started":"2023-05-29T10:57:08.111882Z","shell.execute_reply":"2023-05-29T10:57:08.722770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,7))\nsns.boxplot(x='class', y='age', data= training_data)\nplt.show()\n\n## The  class which has no pneuomia has few outliers , theie age is somewhere aroun 150 years\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:08.725183Z","iopub.execute_input":"2023-05-29T10:57:08.725541Z","iopub.status.idle":"2023-05-29T10:57:09.035939Z","shell.execute_reply.started":"2023-05-29T10:57:08.725506Z","shell.execute_reply":"2023-05-29T10:57:09.034831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\n\ndef fxn():\n    warnings.warn(\"deprecated\", DeprecationWarning)\n\nwith warnings.catch_warnings():\n    warnings.simplefilter(\"ignore\")\n    fxn()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:09.037754Z","iopub.execute_input":"2023-05-29T10:57:09.038156Z","iopub.status.idle":"2023-05-29T10:57:09.044508Z","shell.execute_reply.started":"2023-05-29T10:57:09.038119Z","shell.execute_reply":"2023-05-29T10:57:09.043272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Distribution of `Age`: Overall and Target = 1')\nfig = plt.figure(figsize=(10, 6))\n\nax = fig.add_subplot(121)\ng = sns.histplot(training_data['age'])\ng.set_title('Distribution of PatientAge')\n\nax = fig.add_subplot(122)\ng = sns.histplot(training_data.loc[training_data['Target'] == 1, 'age'])\ng.set_title('Distribution of PatientAge who have pneumonia')\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:09.046519Z","iopub.execute_input":"2023-05-29T10:57:09.046945Z","iopub.status.idle":"2023-05-29T10:57:09.915008Z","shell.execute_reply.started":"2023-05-29T10:57:09.046862Z","shell.execute_reply":"2023-05-29T10:57:09.913927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = training_data.corr(numeric_only=True)\nplt.figure(figsize=(12, 5))\nsns.heatmap(corr, annot=True)\n\n## There is high corelation between widht and height","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:09.916776Z","iopub.execute_input":"2023-05-29T10:57:09.917139Z","iopub.status.idle":"2023-05-29T10:57:10.370759Z","shell.execute_reply.started":"2023-05-29T10:57:09.917096Z","shell.execute_reply":"2023-05-29T10:57:10.369838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**MODEL BUILDING**","metadata":{}},{"cell_type":"code","source":"## Just taking a few samples from the dataset\nsample_trainigdata = training_data.groupby('class', group_keys=False).apply(lambda x: x.sample(800))","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:10.372098Z","iopub.execute_input":"2023-05-29T10:57:10.372730Z","iopub.status.idle":"2023-05-29T10:57:10.392466Z","shell.execute_reply.started":"2023-05-29T10:57:10.372694Z","shell.execute_reply":"2023-05-29T10:57:10.391593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Checking the training data set with class distbution \nsample_trainigdata[\"class\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:10.393793Z","iopub.execute_input":"2023-05-29T10:57:10.394279Z","iopub.status.idle":"2023-05-29T10:57:10.402844Z","shell.execute_reply.started":"2023-05-29T10:57:10.394242Z","shell.execute_reply":"2023-05-29T10:57:10.401985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_trainigdata.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:10.404395Z","iopub.execute_input":"2023-05-29T10:57:10.405078Z","iopub.status.idle":"2023-05-29T10:57:10.436947Z","shell.execute_reply.started":"2023-05-29T10:57:10.405039Z","shell.execute_reply":"2023-05-29T10:57:10.423003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Pre Processing the image\nfrom tensorflow.keras.applications.mobilenet import preprocess_input\n\nimages = []\nADJUSTED_IMAGE_SIZE = 128\nimageList = []\nclassLabels = []\nlabels = []\noriginalImage = []\n# Function to read the image from the path and reshape the image to size\ndef readAndReshapeImage(image):\n    img = np.array(image).astype(np.uint8)\n    ## Resize the image\n    res = cv2.resize(img,(ADJUSTED_IMAGE_SIZE,ADJUSTED_IMAGE_SIZE), interpolation = cv2.INTER_LINEAR)\n    return res\n\n## Read the imahge and resize the image\ndef populateImage(rowData):\n    for index, row in rowData.iterrows():\n        patientId = row.patientId\n        classlabel = row[\"class\"]\n        dcm_file = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/'+'{}.dcm'.format(patientId)\n        dcm_data = dcm.read_file(dcm_file)\n        img = dcm_data.pixel_array\n        ## Converting the image to 3 channels as the dicom image pixel does not have colour classes wiht it\n        if len(img.shape) != 3 or img.shape[2] != 3:\n            img = np.stack((img,) * 3, -1)\n        imageList.append(readAndReshapeImage(img))\n#         originalImage.append(img)\n        classLabels.append(classlabel)\n    tmpImages = np.array(imageList)\n    tmpLabels = np.array(classLabels)\n#     originalImages = np.array(originalImage)\n    return tmpImages,tmpLabels","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:10.438217Z","iopub.execute_input":"2023-05-29T10:57:10.438554Z","iopub.status.idle":"2023-05-29T10:57:10.449853Z","shell.execute_reply.started":"2023-05-29T10:57:10.438528Z","shell.execute_reply":"2023-05-29T10:57:10.448893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Reading the images into numpy array\nimages,labels = populateImage(sample_trainigdata)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:10.451658Z","iopub.execute_input":"2023-05-29T10:57:10.451994Z","iopub.status.idle":"2023-05-29T10:57:30.551042Z","shell.execute_reply.started":"2023-05-29T10:57:10.451965Z","shell.execute_reply":"2023-05-29T10:57:30.550006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images.shape , labels.shape\n## The image is of 128*128 with 3 channels","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:30.552599Z","iopub.execute_input":"2023-05-29T10:57:30.552968Z","iopub.status.idle":"2023-05-29T10:57:30.561916Z","shell.execute_reply.started":"2023-05-29T10:57:30.552934Z","shell.execute_reply":"2023-05-29T10:57:30.560907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Checking one of the converted image \nplt.imshow(images[100])","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:30.563028Z","iopub.execute_input":"2023-05-29T10:57:30.563375Z","iopub.status.idle":"2023-05-29T10:57:30.878233Z","shell.execute_reply.started":"2023-05-29T10:57:30.563326Z","shell.execute_reply":"2023-05-29T10:57:30.877291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## check the unique labels\nnp.unique(labels),len(np.unique(labels))","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:30.879486Z","iopub.execute_input":"2023-05-29T10:57:30.880549Z","iopub.status.idle":"2023-05-29T10:57:30.889066Z","shell.execute_reply.started":"2023-05-29T10:57:30.880515Z","shell.execute_reply":"2023-05-29T10:57:30.888183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nimport numpy as np\nimport tensorflow\nfrom tensorflow.keras.models import Sequential\n# define model\nfrom tensorflow.keras import losses,optimizers\nfrom tensorflow.keras.layers import Dense,  Activation, Flatten,Dropout,MaxPooling2D,BatchNormalization\nimport pandas as pd\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport scipy.stats as stats \nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\n#from keras.models import Sequential\n#from keras.layers import Dense\n#from sklearn.model_selection import StratifiedKFold\n%matplotlib inline\n#Test Train Split\nfrom sklearn.model_selection import train_test_split\n#Feature Scaling library\nfrom sklearn.preprocessing import StandardScaler\n#import pickle\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Flatten, Dense ,LeakyReLU\nfrom tensorflow.keras import regularizers, optimizers\nfrom sklearn.metrics import r2_score\nfrom tensorflow.keras.models import load_model\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom keras.models import Sequential  # initial NN\nfrom keras.layers import Dense, Dropout # construct each layer\nfrom keras.layers import Conv2D # swipe across the image by 1\nfrom keras.layers import MaxPooling2D # swipe across by pool size\nfrom keras.layers import Flatten, GlobalAveragePooling2D,GlobalMaxPooling2D\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:30.890250Z","iopub.execute_input":"2023-05-29T10:57:30.890824Z","iopub.status.idle":"2023-05-29T10:57:30.907241Z","shell.execute_reply.started":"2023-05-29T10:57:30.890789Z","shell.execute_reply":"2023-05-29T10:57:30.906303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## encoding the labels\nfrom sklearn.preprocessing import LabelBinarizer\nenc = LabelBinarizer()\ny2 = enc.fit_transform(labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:30.908586Z","iopub.execute_input":"2023-05-29T10:57:30.908971Z","iopub.status.idle":"2023-05-29T10:57:30.929819Z","shell.execute_reply.started":"2023-05-29T10:57:30.908940Z","shell.execute_reply":"2023-05-29T10:57:30.928724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## splitting into train ,test and validation data\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(images, y2, test_size=0.3, random_state=50)\nX_test, X_val, y_test, y_val = train_test_split(X_test,y_test, test_size = 0.5, random_state=50)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:30.931542Z","iopub.execute_input":"2023-05-29T10:57:30.931927Z","iopub.status.idle":"2023-05-29T10:57:30.990413Z","shell.execute_reply.started":"2023-05-29T10:57:30.931893Z","shell.execute_reply":"2023-05-29T10:57:30.989352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## FUnction to create a dataframe for results\ndef createResultDf(name,accuracy,testscore):\n    result = pd.DataFrame({'Method':[name], 'accuracy': [accuracy] ,'Test Score':[testscore]})\n    return result","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:30.992251Z","iopub.execute_input":"2023-05-29T10:57:30.992662Z","iopub.status.idle":"2023-05-29T10:57:31.000320Z","shell.execute_reply.started":"2023-05-29T10:57:30.992624Z","shell.execute_reply":"2023-05-29T10:57:30.997416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cnn_model(height, width, num_channels, num_classes, loss='categorical_crossentropy', metrics=['accuracy']):\n  batch_size = None\n\n  model = Sequential()\n\n  model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                  activation ='relu', batch_input_shape = (batch_size,height, width, num_channels)))\n\n\n  model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                  activation ='relu'))\n  model.add(MaxPooling2D(pool_size=(2,2)))\n  model.add(Dropout(0.2))\n\n\n  model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', \n                  activation ='relu'))\n  model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'same', \n                  activation ='relu'))\n  model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n  model.add(Dropout(0.3))\n\n  model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', \n                  activation ='relu'))\n  model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', \n                  activation ='relu'))\n  model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n  model.add(Dropout(0.4))\n\n\n\n  model.add(GlobalMaxPooling2D())\n  model.add(Dense(256, activation = \"relu\"))\n  model.add(Dropout(0.5))\n  model.add(Dense(num_classes, activation = \"softmax\"))\n\n  model.summary()\n  return model","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:31.002130Z","iopub.execute_input":"2023-05-29T10:57:31.002545Z","iopub.status.idle":"2023-05-29T10:57:31.016753Z","shell.execute_reply.started":"2023-05-29T10:57:31.002485Z","shell.execute_reply":"2023-05-29T10:57:31.015737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.optimizers.schedules import ExponentialDecay\n\n# Define your model\nADJUSTED_IMAGE_SIZE = 128  # or whatever your actual image size is\nnum_channels = 3  # for RGB images\nnum_classes = 3  # replace with the actual number of classes in your dataset\n\ncnn = cnn_model(ADJUSTED_IMAGE_SIZE, ADJUSTED_IMAGE_SIZE, num_channels, num_classes)\n\n# Define learning rate schedule\ninitial_learning_rate = 0.001\nlr_schedule = ExponentialDecay(initial_learning_rate, decay_steps=100000, decay_rate=0.96, staircase=True)\n\n# Compile your model\noptimizer = RMSprop(learning_rate=lr_schedule)\ncnn.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:31.018700Z","iopub.execute_input":"2023-05-29T10:57:31.019190Z","iopub.status.idle":"2023-05-29T10:57:35.755585Z","shell.execute_reply.started":"2023-05-29T10:57:31.019154Z","shell.execute_reply":"2023-05-29T10:57:35.754542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.compat.v1 import ConfigProto\nfrom tensorflow.compat.v1 import InteractiveSession\n\nconfig = ConfigProto()\nconfig.graph_options.optimizer_options.global_jit_level = tf.compat.v1.OptimizerOptions.OFF\nsession = InteractiveSession(config=config)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:57:35.757108Z","iopub.execute_input":"2023-05-29T10:57:35.757691Z","iopub.status.idle":"2023-05-29T10:57:35.772532Z","shell.execute_reply.started":"2023-05-29T10:57:35.757655Z","shell.execute_reply":"2023-05-29T10:57:35.771193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\nimport tensorflow as tf\n\n\n# Fit your model\nhistory = cnn.fit(X_train, y_train, epochs=30, validation_data=(X_val, y_val), batch_size=30)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T10:59:10.260815Z","iopub.execute_input":"2023-05-29T10:59:10.261308Z","iopub.status.idle":"2023-05-29T11:01:15.576104Z","shell.execute_reply.started":"2023-05-29T10:59:10.261270Z","shell.execute_reply":"2023-05-29T11:01:15.571574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfcl_loss, fcl_accuracy = cnn.evaluate(X_test, y_test, verbose=1)\nprint('Test loss:', fcl_loss)\nprint('Test accuracy:', fcl_accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:01:15.580227Z","iopub.execute_input":"2023-05-29T11:01:15.580583Z","iopub.status.idle":"2023-05-29T11:01:17.271934Z","shell.execute_reply.started":"2023-05-29T11:01:15.580550Z","shell.execute_reply":"2023-05-29T11:01:17.270935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract the history of accuracy and loss values\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(len(acc))\n\nplt.figure(figsize=(15, 15))\nplt.subplot(2, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:01:17.273491Z","iopub.execute_input":"2023-05-29T11:01:17.274005Z","iopub.status.idle":"2023-05-29T11:01:17.817754Z","shell.execute_reply.started":"2023-05-29T11:01:17.273968Z","shell.execute_reply":"2023-05-29T11:01:17.816810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultDF = createResultDf(\"CNN\",acc[-1],fcl_accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:01:17.820371Z","iopub.execute_input":"2023-05-29T11:01:17.822390Z","iopub.status.idle":"2023-05-29T11:01:17.827216Z","shell.execute_reply.started":"2023-05-29T11:01:17.822361Z","shell.execute_reply":"2023-05-29T11:01:17.826065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport itertools\nplt.subplots(figsize=(22,7)) #set the size of the plot \n\ndef plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n\n# Predict the values from the validation dataset\nY_pred = cnn.predict(X_test)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(y_test,axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n# plot the confusion matrix\nplot_confusion_matrix(confusion_mtx, classes = range(3))\n\n#Class 0 ,1 and 2\n#Class 0 is Lung Opacity\n#Class 1 is No Lung Opacity/Normal, the model has predicted mostly wrong in this case to the Target 0. Type 2 error\n#Class 2 is Normal\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:01:17.829045Z","iopub.execute_input":"2023-05-29T11:01:17.829714Z","iopub.status.idle":"2023-05-29T11:01:18.798570Z","shell.execute_reply.started":"2023-05-29T11:01:17.829611Z","shell.execute_reply":"2023-05-29T11:01:18.797565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import recall_score, confusion_matrix, precision_score, f1_score, accuracy_score, roc_auc_score,classification_report\nfrom sklearn.metrics import classification_report\n\nY_truepred = np.argmax(y_test,axis = 1) \n\nY_testPred = cnn.predict(X_test)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n\nreportData = classification_report(Y_truepred, Y_pred_classes,output_dict=True)\n\nfor data in reportData:\n    if(data == '-1' or data == '1'):\n        if(type(reportData[data]) is dict):\n            for subData in reportData[data]:\n                resultDF[data+\"_\"+subData] = reportData[data][subData]\n\nresultDF","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:01:18.800015Z","iopub.execute_input":"2023-05-29T11:01:18.800618Z","iopub.status.idle":"2023-05-29T11:01:19.066115Z","shell.execute_reply.started":"2023-05-29T11:01:18.800582Z","shell.execute_reply":"2023-05-29T11:01:19.065210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Creating a Copy\nX_train1 = X_train.copy()\nX_val1 = X_val.copy()\nX_test1 = X_test.copy()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:01:19.067429Z","iopub.execute_input":"2023-05-29T11:01:19.068296Z","iopub.status.idle":"2023-05-29T11:01:19.119235Z","shell.execute_reply.started":"2023-05-29T11:01:19.068258Z","shell.execute_reply":"2023-05-29T11:01:19.118223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**CNN with Tranfer learning using VGG16**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\n\n##VGGNet is a well-documented and globally used architecture for convolutional neural network\n## Include_top=False to remove the classification layer that was trained on the ImageNet dataset and set the model as not trainable\n\nbase_model = VGG16(weights=\"imagenet\", include_top=False, input_shape=X_train[0].shape)\nbase_model.trainable = False ## Not trainable weights\n\n## Preprocessing input\ntrain_ds = preprocess_input(X_train1) \ntrain_val_df = preprocess_input(X_val1)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:01:19.120751Z","iopub.execute_input":"2023-05-29T11:01:19.121341Z","iopub.status.idle":"2023-05-29T11:01:20.137648Z","shell.execute_reply.started":"2023-05-29T11:01:19.121305Z","shell.execute_reply":"2023-05-29T11:01:20.136557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers, models\n\nflatten_layer = layers.Flatten()\ndense_layer_1 = layers.Dense(100, activation='relu')\ndropout_layer_1 = layers.Dropout(0.3)\ndense_layer_2 = layers.Dense(50, activation='relu')\ndropout_layer_2 = layers.Dropout(0.3)\ndense_layer_3 = layers.Dense(20, activation='relu')\ndropout_layer_3 = layers.Dropout(0.3)\nprediction_layer = layers.Dense(3, activation='softmax')\n\ncnn_VGG16_model = models.Sequential([\n    base_model,\n    flatten_layer,\n    dense_layer_1,\n    dropout_layer_1,\n    dense_layer_2,\n    dropout_layer_2,\n    dense_layer_3,\n    dropout_layer_3,\n    prediction_layer\n])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:01:20.139046Z","iopub.execute_input":"2023-05-29T11:01:20.139408Z","iopub.status.idle":"2023-05-29T11:01:20.278447Z","shell.execute_reply.started":"2023-05-29T11:01:20.139374Z","shell.execute_reply":"2023-05-29T11:01:20.277528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_VGG16_model.compile(\n    optimizer='Adam',\n    loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n    metrics=['accuracy'],\n)\n\n#Trainign the model\nhistory = cnn_VGG16_model.fit(train_ds, y_train, epochs=30, validation_data=(train_val_df,y_val))\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:01:20.283063Z","iopub.execute_input":"2023-05-29T11:01:20.283353Z","iopub.status.idle":"2023-05-29T11:03:44.604911Z","shell.execute_reply.started":"2023-05-29T11:01:20.283326Z","shell.execute_reply":"2023-05-29T11:03:44.603920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = preprocess_input(X_test1) \nfcl_loss, fcl_accuracy = cnn_VGG16_model.evaluate(test_ds, y_test, verbose=1)\nprint('Test loss:', fcl_loss)\nprint('Test accuracy:', fcl_accuracy)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:03:44.606951Z","iopub.execute_input":"2023-05-29T11:03:44.607770Z","iopub.status.idle":"2023-05-29T11:03:45.547843Z","shell.execute_reply.started":"2023-05-29T11:03:44.607734Z","shell.execute_reply":"2023-05-29T11:03:45.546926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual_train_accuracy = history.history['accuracy'][-1]\nactual_val_accuracy = history.history['val_accuracy'][-1]\n\nprint('Actual Training Accuracy:', actual_train_accuracy)\nprint('Actual Validation Accuracy:', actual_val_accuracy)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:03:45.551200Z","iopub.execute_input":"2023-05-29T11:03:45.551498Z","iopub.status.idle":"2023-05-29T11:03:45.559085Z","shell.execute_reply.started":"2023-05-29T11:03:45.551471Z","shell.execute_reply":"2023-05-29T11:03:45.557884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf1 = pd.concat([resultDF, createResultDf(\"CNN With VGG16\",history.history['accuracy'][-1],fcl_accuracy)])","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:03:45.560735Z","iopub.execute_input":"2023-05-29T11:03:45.561515Z","iopub.status.idle":"2023-05-29T11:03:45.569572Z","shell.execute_reply.started":"2023-05-29T11:03:45.561478Z","shell.execute_reply":"2023-05-29T11:03:45.568614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf1.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:03:45.570943Z","iopub.execute_input":"2023-05-29T11:03:45.571497Z","iopub.status.idle":"2023-05-29T11:03:45.587425Z","shell.execute_reply.started":"2023-05-29T11:03:45.571461Z","shell.execute_reply":"2023-05-29T11:03:45.586360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import recall_score, confusion_matrix, precision_score, f1_score, accuracy_score, roc_auc_score,classification_report\nfrom sklearn.metrics import classification_report\n\nY_truepred = np.argmax(y_test,axis = 1) \n\nY_testPred = cnn.predict(test_ds)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n\nreportData = classification_report(Y_truepred, Y_pred_classes,output_dict=True)\n\nfor data in reportData:\n    if(data == '-1' or data == '1'):\n        if(type(reportData[data]) is dict):\n            for subData in reportData[data]:\n                resultsDf1[data+\"_\"+subData] = reportData[data][subData]\n\nresultsDf1","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:03:45.588797Z","iopub.execute_input":"2023-05-29T11:03:45.589307Z","iopub.status.idle":"2023-05-29T11:03:46.183712Z","shell.execute_reply.started":"2023-05-29T11:03:45.589273Z","shell.execute_reply":"2023-05-29T11:03:46.182654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**CNN with ResNet50**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\n\nfrom tensorflow.keras import layers\n\n# Existing layers\nresnet_base_model = ResNet50(include_top=False, weights='imagenet', input_shape=X_train[0].shape)\nflatten_layer = layers.Flatten()\ndense_layer_1 = layers.Dense(256, activation='relu')  # increased the number of neurons\ndense_layer_2 = layers.Dense(128, activation='relu')  # increased the number of neurons\nprediction_layer = layers.Dense(3, activation='softmax')\n\n# Additional layers\nadditional_dense_layer = layers.Dense(64, activation='relu')  # reduced the number of neurons\ndropout_layer_1 = layers.Dropout(0.5)  # You can adjust the dropout rate as needed\ndropout_layer_2 = layers.Dropout(0.3)  # You can adjust the dropout rate as needed\n\ncnn_resnet_model = models.Sequential([\n    resnet_base_model,\n    flatten_layer,\n    dense_layer_1,\n    dropout_layer_1,  # Add the dropout layer after the first dense layer\n    additional_dense_layer,  # Add the additional dense layer\n    dropout_layer_2,  # Add another dropout layer after the additional dense layer\n    dense_layer_2,\n    prediction_layer\n])","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:04:26.203771Z","iopub.execute_input":"2023-05-29T11:04:26.204418Z","iopub.status.idle":"2023-05-29T11:04:29.356485Z","shell.execute_reply.started":"2023-05-29T11:04:26.204376Z","shell.execute_reply":"2023-05-29T11:04:29.355498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\ncnn_resnet_model.compile(\n    optimizer='Adam',\n    loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n    metrics=['accuracy'],\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:04:30.667977Z","iopub.execute_input":"2023-05-29T11:04:30.668337Z","iopub.status.idle":"2023-05-29T11:04:30.688625Z","shell.execute_reply.started":"2023-05-29T11:04:30.668308Z","shell.execute_reply":"2023-05-29T11:04:30.687655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Trainign the model\nhistory =cnn_resnet_model.fit(train_ds, y_train, epochs=30, validation_data=(train_val_df,y_val))","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:04:33.445508Z","iopub.execute_input":"2023-05-29T11:04:33.446810Z","iopub.status.idle":"2023-05-29T11:09:12.402442Z","shell.execute_reply.started":"2023-05-29T11:04:33.446763Z","shell.execute_reply":"2023-05-29T11:09:12.401394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcl_loss, fcl_accuracy = cnn_resnet_model.evaluate(test_ds, y_test, verbose=1)\nprint('Test loss:', fcl_loss)\nprint('Test accuracy:', fcl_accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:09:12.404833Z","iopub.execute_input":"2023-05-29T11:09:12.405215Z","iopub.status.idle":"2023-05-29T11:09:13.205157Z","shell.execute_reply.started":"2023-05-29T11:09:12.405180Z","shell.execute_reply":"2023-05-29T11:09:13.204093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf2 = pd.concat([resultsDf1, createResultDf(\"CNN With ResNet50\",history.history['accuracy'][-1],fcl_accuracy)])","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:09:13.206610Z","iopub.execute_input":"2023-05-29T11:09:13.207596Z","iopub.status.idle":"2023-05-29T11:09:13.216591Z","shell.execute_reply.started":"2023-05-29T11:09:13.207543Z","shell.execute_reply":"2023-05-29T11:09:13.215409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf2.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:09:13.219686Z","iopub.execute_input":"2023-05-29T11:09:13.220156Z","iopub.status.idle":"2023-05-29T11:09:13.238033Z","shell.execute_reply.started":"2023-05-29T11:09:13.220112Z","shell.execute_reply":"2023-05-29T11:09:13.236912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import recall_score, confusion_matrix, precision_score, f1_score, accuracy_score, roc_auc_score,classification_report\nfrom sklearn.metrics import classification_report\n\nY_truepred = np.argmax(y_test,axis = 1) \n\nY_testPred = cnn.predict(test_ds)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n\nreportData = classification_report(Y_truepred, Y_pred_classes,output_dict=True)\n\nfor data in reportData:\n    if(data == '-1' or data == '1'):\n        if(type(reportData[data]) is dict):\n            for subData in reportData[data]:\n                resultsDf2[data+\"_\"+subData] = reportData[data][subData]","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:09:36.970736Z","iopub.execute_input":"2023-05-29T11:09:36.971437Z","iopub.status.idle":"2023-05-29T11:09:37.540470Z","shell.execute_reply.started":"2023-05-29T11:09:36.971388Z","shell.execute_reply":"2023-05-29T11:09:37.539376Z"},"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,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n\n# Predict the values from the validation dataset\nY_pred = cnn_resnet_model.predict(test_ds)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred, axis=1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(y_test, axis=1) \n# Compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n# Plot the confusion matrix\nplot_confusion_matrix(confusion_mtx, classes=range(3))\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:09:46.770623Z","iopub.execute_input":"2023-05-29T11:09:46.771007Z","iopub.status.idle":"2023-05-29T11:09:48.016171Z","shell.execute_reply.started":"2023-05-29T11:09:46.770976Z","shell.execute_reply":"2023-05-29T11:09:48.015281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf2.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:09:59.883302Z","iopub.execute_input":"2023-05-29T11:09:59.883689Z","iopub.status.idle":"2023-05-29T11:09:59.899222Z","shell.execute_reply.started":"2023-05-29T11:09:59.883655Z","shell.execute_reply":"2023-05-29T11:09:59.898273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**CNN with Xception**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import Xception\nfrom tensorflow.keras.applications.xception import preprocess_input\nfrom tensorflow.keras import layers, models\n\nxception_base_model = Xception(include_top=False, weights='imagenet', input_shape=X_train[0].shape)\n\ntrain_ds = preprocess_input(X_train1) \ntrain_val_df = preprocess_input(X_val1)\n\nflatten_layer = layers.Flatten()\ndense_layer_1 = layers.Dense(1024, activation='relu')\ndropout_layer_1 = layers.Dropout(0.3)\ndense_layer_2 = layers.Dense(512, activation='relu')\ndropout_layer_2 = layers.Dropout(0.3)\ndense_layer_3 = layers.Dense(256, activation='relu')\ndropout_layer_3 = layers.Dropout(0.3)\ndense_layer_4 = layers.Dense(128, activation='relu')\ndropout_layer_4 = layers.Dropout(0.3)\ndense_layer_5 = layers.Dense(64, activation='relu')\ndropout_layer_5 = layers.Dropout(0.3)\ndense_layer_6 = layers.Dense(32, activation='relu')\nprediction_layer = layers.Dense(3, activation='softmax')\n\ncnn_xception_model = models.Sequential([\n    xception_base_model,\n    flatten_layer,\n    dense_layer_1,\n    dropout_layer_1,\n    dense_layer_2,\n    dropout_layer_2,\n    dense_layer_3,\n    dropout_layer_3,\n    dense_layer_4,\n    dropout_layer_4,\n    dense_layer_5,\n    dropout_layer_5,\n    dense_layer_6,\n    prediction_layer\n])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:10:39.174587Z","iopub.execute_input":"2023-05-29T11:10:39.175001Z","iopub.status.idle":"2023-05-29T11:10:42.291374Z","shell.execute_reply.started":"2023-05-29T11:10:39.174968Z","shell.execute_reply":"2023-05-29T11:10:42.290414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\ncnn_xception_model.compile(\n    optimizer='Adam',\n    loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n    metrics=['accuracy'],\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:10:45.021753Z","iopub.execute_input":"2023-05-29T11:10:45.022551Z","iopub.status.idle":"2023-05-29T11:10:45.041051Z","shell.execute_reply.started":"2023-05-29T11:10:45.022512Z","shell.execute_reply":"2023-05-29T11:10:45.040174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = cnn_xception_model.fit(train_ds, y_train, epochs=30, validation_data=(train_val_df,y_val))","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:11:07.958848Z","iopub.execute_input":"2023-05-29T11:11:07.959581Z","iopub.status.idle":"2023-05-29T11:16:42.761130Z","shell.execute_reply.started":"2023-05-29T11:11:07.959545Z","shell.execute_reply":"2023-05-29T11:16:42.760079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcl_loss, fcl_accuracy = cnn_xception_model.evaluate(test_ds, y_test, verbose=1)\nprint('Test loss:', fcl_loss)\nprint('Test accuracy:', fcl_accuracy)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:16:42.767021Z","iopub.execute_input":"2023-05-29T11:16:42.767317Z","iopub.status.idle":"2023-05-29T11:16:43.881280Z","shell.execute_reply.started":"2023-05-29T11:16:42.767292Z","shell.execute_reply":"2023-05-29T11:16:43.875563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf3 = pd.concat([resultsDf2, createResultDf(\"CNN With Xception\",history.history['accuracy'][-1],fcl_accuracy)])","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:16:43.882702Z","iopub.execute_input":"2023-05-29T11:16:43.887452Z","iopub.status.idle":"2023-05-29T11:16:43.906323Z","shell.execute_reply.started":"2023-05-29T11:16:43.887412Z","shell.execute_reply":"2023-05-29T11:16:43.904529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf3.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:16:43.907986Z","iopub.execute_input":"2023-05-29T11:16:43.908309Z","iopub.status.idle":"2023-05-29T11:16:43.946977Z","shell.execute_reply.started":"2023-05-29T11:16:43.908279Z","shell.execute_reply":"2023-05-29T11:16:43.946113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import recall_score, confusion_matrix, precision_score, f1_score, accuracy_score, roc_auc_score,classification_report\nfrom sklearn.metrics import classification_report\n\nY_truepred = np.argmax(y_test,axis = 1) \n\nY_testPred = cnn.predict(test_ds)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n\nreportData = classification_report(Y_truepred, Y_pred_classes,output_dict=True)\n\nfor data in reportData:\n    if(data == '-1' or data == '1'):\n        if(type(reportData[data]) is dict):\n            for subData in reportData[data]:\n                resultsDf3[data+\"_\"+subData] = reportData[data][subData]\nresultsDf3","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:16:52.742394Z","iopub.execute_input":"2023-05-29T11:16:52.742770Z","iopub.status.idle":"2023-05-29T11:16:53.342625Z","shell.execute_reply.started":"2023-05-29T11:16:52.742738Z","shell.execute_reply":"2023-05-29T11:16:53.341557Z"},"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\n# Compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes)\n\n# Define the function to plot the confusion matrix\ndef plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    plt.figure(figsize=(8, 6))\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, format(cm[i, j], '.2f' if normalize else 'd'),\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n\n# Plot the confusion matrix\nclass_names = [0, 1, 2]  # Replace with your actual class names\nplot_confusion_matrix(confusion_mtx, classes=class_names, normalize=False)\n\n# Display the plot\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:16:59.505012Z","iopub.execute_input":"2023-05-29T11:16:59.505388Z","iopub.status.idle":"2023-05-29T11:16:59.888087Z","shell.execute_reply.started":"2023-05-29T11:16:59.505357Z","shell.execute_reply":"2023-05-29T11:16:59.886679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf3.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:18:03.064422Z","iopub.execute_input":"2023-05-29T11:18:03.064820Z","iopub.status.idle":"2023-05-29T11:18:03.079364Z","shell.execute_reply.started":"2023-05-29T11:18:03.064787Z","shell.execute_reply":"2023-05-29T11:18:03.078219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we can see the CNN With Xception giving good accuracy rather then other model**","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs_range = range(30)\n\nplt.figure(figsize=(15, 15))\nplt.subplot(2, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T11:22:12.613985Z","iopub.execute_input":"2023-05-29T11:22:12.614376Z","iopub.status.idle":"2023-05-29T11:22:13.148813Z","shell.execute_reply.started":"2023-05-29T11:22:12.614345Z","shell.execute_reply":"2023-05-29T11:22:13.147933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# > ***As per Deep Learnning module Xception we are getting good accuracy with minimal data loss***","metadata":{}}]}