{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"}],"dockerImageVersionId":30498,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nlabels = pd.read_csv(\"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\")\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:18:59.571831Z","iopub.execute_input":"2023-12-03T16:18:59.572718Z","iopub.status.idle":"2023-12-03T16:18:59.619370Z","shell.execute_reply.started":"2023-12-03T16:18:59.572682Z","shell.execute_reply":"2023-12-03T16:18:59.618467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:18:59.620762Z","iopub.execute_input":"2023-12-03T16:18:59.621075Z","iopub.status.idle":"2023-12-03T16:18:59.627015Z","shell.execute_reply.started":"2023-12-03T16:18:59.621049Z","shell.execute_reply":"2023-12-03T16:18:59.625947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:18:59.628254Z","iopub.execute_input":"2023-12-03T16:18:59.628532Z","iopub.status.idle":"2023-12-03T16:18:59.651479Z","shell.execute_reply.started":"2023-12-03T16:18:59.628509Z","shell.execute_reply":"2023-12-03T16:18:59.650538Z"},"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-12-03T16:18:59.653469Z","iopub.execute_input":"2023-12-03T16:18:59.653770Z","iopub.status.idle":"2023-12-03T16:18:59.671624Z","shell.execute_reply.started":"2023-12-03T16:18:59.653734Z","shell.execute_reply":"2023-12-03T16:18:59.670819Z"},"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-12-03T16:18:59.672829Z","iopub.execute_input":"2023-12-03T16:18:59.673390Z","iopub.status.idle":"2023-12-03T16:18:59.691916Z","shell.execute_reply.started":"2023-12-03T16:18:59.673355Z","shell.execute_reply":"2023-12-03T16:18:59.691039Z"},"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-12-03T16:18:59.693096Z","iopub.execute_input":"2023-12-03T16:18:59.693373Z","iopub.status.idle":"2023-12-03T16:18:59.705827Z","shell.execute_reply.started":"2023-12-03T16:18:59.693349Z","shell.execute_reply":"2023-12-03T16:18:59.704986Z"},"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\nimport matplotlib.pyplot as plt\n\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-12-03T16:18:59.707033Z","iopub.execute_input":"2023-12-03T16:18:59.707311Z","iopub.status.idle":"2023-12-03T16:18:59.872315Z","shell.execute_reply.started":"2023-12-03T16:18:59.707286Z","shell.execute_reply":"2023-12-03T16:18:59.871060Z"},"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-12-03T16:18:59.873723Z","iopub.execute_input":"2023-12-03T16:18:59.874571Z","iopub.status.idle":"2023-12-03T16:18:59.891213Z","shell.execute_reply.started":"2023-12-03T16:18:59.874522Z","shell.execute_reply":"2023-12-03T16:18:59.890021Z"},"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-12-03T16:18:59.892971Z","iopub.execute_input":"2023-12-03T16:18:59.893906Z","iopub.status.idle":"2023-12-03T16:18:59.908576Z","shell.execute_reply.started":"2023-12-03T16:18:59.893860Z","shell.execute_reply":"2023-12-03T16:18:59.907319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"duplicateRowsDF.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:18:59.916407Z","iopub.execute_input":"2023-12-03T16:18:59.917392Z","iopub.status.idle":"2023-12-03T16:18:59.934556Z","shell.execute_reply.started":"2023-12-03T16:18:59.917356Z","shell.execute_reply":"2023-12-03T16:18:59.933234Z"},"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-12-03T16:18:59.936203Z","iopub.execute_input":"2023-12-03T16:18:59.936849Z","iopub.status.idle":"2023-12-03T16:18:59.954628Z","shell.execute_reply.started":"2023-12-03T16:18:59.936801Z","shell.execute_reply":"2023-12-03T16:18:59.953665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[labels.patientId=='00704310-78a8-4b38-8475-49f4573b2dbb']","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:18:59.956113Z","iopub.execute_input":"2023-12-03T16:18:59.956713Z","iopub.status.idle":"2023-12-03T16:18:59.977042Z","shell.execute_reply.started":"2023-12-03T16:18:59.956679Z","shell.execute_reply":"2023-12-03T16:18:59.976145Z"},"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-12-03T16:18:59.978416Z","iopub.execute_input":"2023-12-03T16:18:59.978701Z","iopub.status.idle":"2023-12-03T16:19:00.020332Z","shell.execute_reply.started":"2023-12-03T16:18:59.978677Z","shell.execute_reply":"2023-12-03T16:19:00.019427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:19:00.021360Z","iopub.execute_input":"2023-12-03T16:19:00.021619Z","iopub.status.idle":"2023-12-03T16:19:00.027562Z","shell.execute_reply.started":"2023-12-03T16:19:00.021596Z","shell.execute_reply":"2023-12-03T16:19:00.026272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:19:00.029109Z","iopub.execute_input":"2023-12-03T16:19:00.029838Z","iopub.status.idle":"2023-12-03T16:19:00.060786Z","shell.execute_reply.started":"2023-12-03T16:19:00.029784Z","shell.execute_reply":"2023-12-03T16:19:00.059817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels['class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:19:00.061921Z","iopub.execute_input":"2023-12-03T16:19:00.062292Z","iopub.status.idle":"2023-12-03T16:19:00.072376Z","shell.execute_reply.started":"2023-12-03T16:19:00.062259Z","shell.execute_reply":"2023-12-03T16:19:00.071528Z"},"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-12-03T16:19:00.073477Z","iopub.execute_input":"2023-12-03T16:19:00.073788Z","iopub.status.idle":"2023-12-03T16:19:00.259559Z","shell.execute_reply.started":"2023-12-03T16:19:00.073758Z","shell.execute_reply":"2023-12-03T16:19:00.258220Z"},"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-12-03T16:19:00.261621Z","iopub.execute_input":"2023-12-03T16:19:00.262519Z","iopub.status.idle":"2023-12-03T16:19:00.277727Z","shell.execute_reply.started":"2023-12-03T16:19:00.262453Z","shell.execute_reply":"2023-12-03T16:19:00.276318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"duplicateClassRowsDF.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:19:00.279938Z","iopub.execute_input":"2023-12-03T16:19:00.280873Z","iopub.status.idle":"2023-12-03T16:19:00.295628Z","shell.execute_reply.started":"2023-12-03T16:19:00.280820Z","shell.execute_reply":"2023-12-03T16:19:00.294165Z"},"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-12-03T16:19:00.297650Z","iopub.execute_input":"2023-12-03T16:19:00.298461Z","iopub.status.idle":"2023-12-03T16:19:00.319680Z","shell.execute_reply.started":"2023-12-03T16:19:00.298413Z","shell.execute_reply":"2023-12-03T16:19:00.318790Z"},"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-12-03T16:19:00.321681Z","iopub.execute_input":"2023-12-03T16:19:00.322133Z","iopub.status.idle":"2023-12-03T16:19:00.342953Z","shell.execute_reply.started":"2023-12-03T16:19:00.322108Z","shell.execute_reply":"2023-12-03T16:19:00.342025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfig, 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 Distribution')\n\n## it shows that class distribution 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-12-03T16:19:00.344126Z","iopub.execute_input":"2023-12-03T16:19:00.344399Z","iopub.status.idle":"2023-12-03T16:19:00.758901Z","shell.execute_reply.started":"2023-12-03T16:19:00.344375Z","shell.execute_reply":"2023-12-03T16:19:00.757682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.patches as patches\nimport pydicom as dcm\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-12-03T16:19:00.760544Z","iopub.execute_input":"2023-12-03T16:19:00.760969Z","iopub.status.idle":"2023-12-03T16:19:00.773022Z","shell.execute_reply.started":"2023-12-03T16:19:00.760935Z","shell.execute_reply":"2023-12-03T16:19:00.772014Z"},"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 \nimport math\ninspectImages(training_data[training_data['Target']==1].sample(9))","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:19:00.774511Z","iopub.execute_input":"2023-12-03T16:19:00.774780Z","iopub.status.idle":"2023-12-03T16:19:02.939710Z","shell.execute_reply.started":"2023-12-03T16:19:00.774756Z","shell.execute_reply":"2023-12-03T16:19:02.938673Z"},"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-12-03T16:19:02.941211Z","iopub.execute_input":"2023-12-03T16:19:02.941738Z","iopub.status.idle":"2023-12-03T16:19:05.040039Z","shell.execute_reply.started":"2023-12-03T16:19:02.941702Z","shell.execute_reply":"2023-12-03T16:19:05.039054Z"},"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-12-03T16:19:05.041489Z","iopub.execute_input":"2023-12-03T16:19:05.041823Z","iopub.status.idle":"2023-12-03T16:19:05.047068Z","shell.execute_reply.started":"2023-12-03T16:19:05.041793Z","shell.execute_reply":"2023-12-03T16:19:05.046183Z"},"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-12-03T16:19:05.048543Z","iopub.execute_input":"2023-12-03T16:19:05.049433Z","iopub.status.idle":"2023-12-03T16:22:43.604656Z","shell.execute_reply.started":"2023-12-03T16:19:05.049399Z","shell.execute_reply":"2023-12-03T16:22:43.603632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:22:43.613700Z","iopub.execute_input":"2023-12-03T16:22:43.614087Z","iopub.status.idle":"2023-12-03T16:22:43.659268Z","shell.execute_reply.started":"2023-12-03T16:22:43.614059Z","shell.execute_reply":"2023-12-03T16:22:43.658365Z"},"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-12-03T16:22:43.660427Z","iopub.execute_input":"2023-12-03T16:22:43.660700Z","iopub.status.idle":"2023-12-03T16:22:43.673605Z","shell.execute_reply.started":"2023-12-03T16:22:43.660675Z","shell.execute_reply":"2023-12-03T16:22:43.672696Z"},"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-12-03T16:22:43.674801Z","iopub.execute_input":"2023-12-03T16:22:43.675152Z","iopub.status.idle":"2023-12-03T16:22:43.751531Z","shell.execute_reply.started":"2023-12-03T16:22:43.675120Z","shell.execute_reply":"2023-12-03T16:22:43.750594Z"},"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-12-03T16:22:43.752615Z","iopub.execute_input":"2023-12-03T16:22:43.752881Z","iopub.status.idle":"2023-12-03T16:22:43.763976Z","shell.execute_reply.started":"2023-12-03T16:22:43.752857Z","shell.execute_reply":"2023-12-03T16:22:43.763039Z"},"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-12-03T16:22:43.765180Z","iopub.execute_input":"2023-12-03T16:22:43.765416Z","iopub.status.idle":"2023-12-03T16:22:44.130654Z","shell.execute_reply.started":"2023-12-03T16:22:43.765395Z","shell.execute_reply":"2023-12-03T16:22:44.129699Z"},"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-12-03T16:22:44.132011Z","iopub.execute_input":"2023-12-03T16:22:44.132370Z","iopub.status.idle":"2023-12-03T16:22:44.455275Z","shell.execute_reply.started":"2023-12-03T16:22:44.132337Z","shell.execute_reply":"2023-12-03T16:22:44.454316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(training_data.age)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:22:44.456400Z","iopub.execute_input":"2023-12-03T16:22:44.456698Z","iopub.status.idle":"2023-12-03T16:22:44.950724Z","shell.execute_reply.started":"2023-12-03T16:22:44.456672Z","shell.execute_reply":"2023-12-03T16:22:44.949794Z"},"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-12-03T16:22:44.951918Z","iopub.execute_input":"2023-12-03T16:22:44.952304Z","iopub.status.idle":"2023-12-03T16:22:45.530943Z","shell.execute_reply.started":"2023-12-03T16:22:44.952272Z","shell.execute_reply":"2023-12-03T16:22:45.529942Z"},"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-12-03T16:22:45.532521Z","iopub.execute_input":"2023-12-03T16:22:45.532891Z","iopub.status.idle":"2023-12-03T16:22:46.076754Z","shell.execute_reply.started":"2023-12-03T16:22:45.532858Z","shell.execute_reply":"2023-12-03T16:22:46.075647Z"},"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-12-03T16:22:46.079250Z","iopub.execute_input":"2023-12-03T16:22:46.079940Z","iopub.status.idle":"2023-12-03T16:22:46.305156Z","shell.execute_reply.started":"2023-12-03T16:22:46.079905Z","shell.execute_reply":"2023-12-03T16:22:46.304310Z"},"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-12-03T16:22:46.306300Z","iopub.execute_input":"2023-12-03T16:22:46.306643Z","iopub.status.idle":"2023-12-03T16:22:46.313821Z","shell.execute_reply.started":"2023-12-03T16:22:46.306609Z","shell.execute_reply":"2023-12-03T16:22:46.313034Z"},"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-12-03T16:22:46.314917Z","iopub.execute_input":"2023-12-03T16:22:46.315252Z","iopub.status.idle":"2023-12-03T16:22:47.069971Z","shell.execute_reply.started":"2023-12-03T16:22:46.315223Z","shell.execute_reply":"2023-12-03T16:22:47.069153Z"},"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-12-03T16:22:47.071126Z","iopub.execute_input":"2023-12-03T16:22:47.071474Z","iopub.status.idle":"2023-12-03T16:22:47.428529Z","shell.execute_reply.started":"2023-12-03T16:22:47.071439Z","shell.execute_reply":"2023-12-03T16:22:47.427666Z"},"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-12-03T16:22:47.429699Z","iopub.execute_input":"2023-12-03T16:22:47.430062Z","iopub.status.idle":"2023-12-03T16:22:47.447021Z","shell.execute_reply.started":"2023-12-03T16:22:47.430028Z","shell.execute_reply":"2023-12-03T16:22:47.446141Z"},"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-12-03T16:22:47.448525Z","iopub.execute_input":"2023-12-03T16:22:47.448925Z","iopub.status.idle":"2023-12-03T16:22:47.457613Z","shell.execute_reply.started":"2023-12-03T16:22:47.448891Z","shell.execute_reply":"2023-12-03T16:22:47.456759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_trainigdata.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:22:47.458764Z","iopub.execute_input":"2023-12-03T16:22:47.459107Z","iopub.status.idle":"2023-12-03T16:22:47.475842Z","shell.execute_reply.started":"2023-12-03T16:22:47.459076Z","shell.execute_reply":"2023-12-03T16:22:47.474953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Pre Processing the image\nfrom tensorflow.keras.applications.mobilenet import preprocess_input\nimport cv2\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-12-03T16:22:47.476855Z","iopub.execute_input":"2023-12-03T16:22:47.477169Z","iopub.status.idle":"2023-12-03T16:22:47.490797Z","shell.execute_reply.started":"2023-12-03T16:22:47.477145Z","shell.execute_reply":"2023-12-03T16:22:47.489766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Reading the images into numpy array\nimport numpy as np\nimages,labels = populateImage(sample_trainigdata)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:22:47.491777Z","iopub.execute_input":"2023-12-03T16:22:47.492049Z","iopub.status.idle":"2023-12-03T16:23:08.426936Z","shell.execute_reply.started":"2023-12-03T16:22:47.492021Z","shell.execute_reply":"2023-12-03T16:23:08.426127Z"},"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-12-03T16:23:08.428018Z","iopub.execute_input":"2023-12-03T16:23:08.428266Z","iopub.status.idle":"2023-12-03T16:23:08.434102Z","shell.execute_reply.started":"2023-12-03T16:23:08.428244Z","shell.execute_reply":"2023-12-03T16:23:08.433267Z"},"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-12-03T16:23:08.435694Z","iopub.execute_input":"2023-12-03T16:23:08.436054Z","iopub.status.idle":"2023-12-03T16:23:08.688202Z","shell.execute_reply.started":"2023-12-03T16:23:08.436022Z","shell.execute_reply":"2023-12-03T16:23:08.687300Z"},"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-12-03T16:23:08.689355Z","iopub.execute_input":"2023-12-03T16:23:08.689651Z","iopub.status.idle":"2023-12-03T16:23:08.696875Z","shell.execute_reply.started":"2023-12-03T16:23:08.689624Z","shell.execute_reply":"2023-12-03T16:23:08.695862Z"},"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-12-03T16:23:08.698102Z","iopub.execute_input":"2023-12-03T16:23:08.698388Z","iopub.status.idle":"2023-12-03T16:23:08.714323Z","shell.execute_reply.started":"2023-12-03T16:23:08.698355Z","shell.execute_reply":"2023-12-03T16:23:08.713294Z"},"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-12-03T16:23:08.715505Z","iopub.execute_input":"2023-12-03T16:23:08.715819Z","iopub.status.idle":"2023-12-03T16:23:08.736400Z","shell.execute_reply.started":"2023-12-03T16:23:08.715795Z","shell.execute_reply":"2023-12-03T16:23:08.735459Z"},"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-12-03T16:23:08.737533Z","iopub.execute_input":"2023-12-03T16:23:08.738277Z","iopub.status.idle":"2023-12-03T16:23:08.792361Z","shell.execute_reply.started":"2023-12-03T16:23:08.738248Z","shell.execute_reply":"2023-12-03T16:23:08.791498Z"},"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-12-03T16:23:08.793779Z","iopub.execute_input":"2023-12-03T16:23:08.794257Z","iopub.status.idle":"2023-12-03T16:23:08.800087Z","shell.execute_reply.started":"2023-12-03T16:23:08.794221Z","shell.execute_reply":"2023-12-03T16:23:08.799057Z"},"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-12-03T16:23:08.801376Z","iopub.execute_input":"2023-12-03T16:23:08.801723Z","iopub.status.idle":"2023-12-03T16:23:08.815348Z","shell.execute_reply.started":"2023-12-03T16:23:08.801644Z","shell.execute_reply":"2023-12-03T16:23:08.814219Z"},"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-12-03T16:23:08.818231Z","iopub.execute_input":"2023-12-03T16:23:08.818526Z","iopub.status.idle":"2023-12-03T16:23:08.990393Z","shell.execute_reply.started":"2023-12-03T16:23:08.818500Z","shell.execute_reply":"2023-12-03T16:23:08.989472Z"},"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-12-03T16:23:08.991614Z","iopub.execute_input":"2023-12-03T16:23:08.991896Z","iopub.status.idle":"2023-12-03T16:23:09.003117Z","shell.execute_reply.started":"2023-12-03T16:23:08.991872Z","shell.execute_reply":"2023-12-03T16:23:09.002355Z"},"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-12-03T16:23:09.004584Z","iopub.execute_input":"2023-12-03T16:23:09.004833Z","iopub.status.idle":"2023-12-03T16:25:13.716060Z","shell.execute_reply.started":"2023-12-03T16:23:09.004812Z","shell.execute_reply":"2023-12-03T16:25:13.715216Z"},"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-12-03T16:25:13.722053Z","iopub.execute_input":"2023-12-03T16:25:13.722403Z","iopub.status.idle":"2023-12-03T16:25:14.258308Z","shell.execute_reply.started":"2023-12-03T16:25:13.722377Z","shell.execute_reply":"2023-12-03T16:25:14.257402Z"},"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-12-03T16:25:14.259308Z","iopub.execute_input":"2023-12-03T16:25:14.259573Z","iopub.status.idle":"2023-12-03T16:25:14.714254Z","shell.execute_reply.started":"2023-12-03T16:25:14.259551Z","shell.execute_reply":"2023-12-03T16:25:14.713257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultDF = createResultDf(\"CNN\",acc[-1],fcl_accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:25:14.715626Z","iopub.execute_input":"2023-12-03T16:25:14.716060Z","iopub.status.idle":"2023-12-03T16:25:14.721836Z","shell.execute_reply.started":"2023-12-03T16:25:14.715965Z","shell.execute_reply":"2023-12-03T16:25:14.720893Z"},"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-12-03T16:25:14.723148Z","iopub.execute_input":"2023-12-03T16:25:14.723488Z","iopub.status.idle":"2023-12-03T16:25:15.404191Z","shell.execute_reply.started":"2023-12-03T16:25:14.723455Z","shell.execute_reply":"2023-12-03T16:25:15.403250Z"},"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-12-03T16:25:15.405347Z","iopub.execute_input":"2023-12-03T16:25:15.405616Z","iopub.status.idle":"2023-12-03T16:25:15.663625Z","shell.execute_reply.started":"2023-12-03T16:25:15.405593Z","shell.execute_reply":"2023-12-03T16:25:15.662762Z"},"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-12-03T16:25:15.664664Z","iopub.execute_input":"2023-12-03T16:25:15.664916Z","iopub.status.idle":"2023-12-03T16:25:15.709707Z","shell.execute_reply.started":"2023-12-03T16:25:15.664894Z","shell.execute_reply":"2023-12-03T16:25:15.708913Z"},"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-12-03T16:25:15.710812Z","iopub.execute_input":"2023-12-03T16:25:15.711096Z","iopub.status.idle":"2023-12-03T16:25:16.272123Z","shell.execute_reply.started":"2023-12-03T16:25:15.711072Z","shell.execute_reply":"2023-12-03T16:25:16.271292Z"},"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-12-03T16:25:16.273354Z","iopub.execute_input":"2023-12-03T16:25:16.273646Z","iopub.status.idle":"2023-12-03T16:25:16.396680Z","shell.execute_reply.started":"2023-12-03T16:25:16.273620Z","shell.execute_reply":"2023-12-03T16:25:16.395773Z"},"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-12-03T16:25:16.397775Z","iopub.execute_input":"2023-12-03T16:25:16.398069Z","iopub.status.idle":"2023-12-03T16:27:40.366598Z","shell.execute_reply.started":"2023-12-03T16:25:16.398043Z","shell.execute_reply":"2023-12-03T16:27:40.365711Z"},"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-12-03T16:27:40.368043Z","iopub.execute_input":"2023-12-03T16:27:40.368335Z","iopub.status.idle":"2023-12-03T16:27:41.176137Z","shell.execute_reply.started":"2023-12-03T16:27:40.368309Z","shell.execute_reply":"2023-12-03T16:27:41.175202Z"},"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-12-03T16:27:41.177360Z","iopub.execute_input":"2023-12-03T16:27:41.177682Z","iopub.status.idle":"2023-12-03T16:27:41.183387Z","shell.execute_reply.started":"2023-12-03T16:27:41.177654Z","shell.execute_reply":"2023-12-03T16:27:41.182415Z"},"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-12-03T16:27:41.184627Z","iopub.execute_input":"2023-12-03T16:27:41.184918Z","iopub.status.idle":"2023-12-03T16:27:41.198258Z","shell.execute_reply.started":"2023-12-03T16:27:41.184892Z","shell.execute_reply":"2023-12-03T16:27:41.197440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf1.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:27:41.199280Z","iopub.execute_input":"2023-12-03T16:27:41.199584Z","iopub.status.idle":"2023-12-03T16:27:41.217817Z","shell.execute_reply.started":"2023-12-03T16:27:41.199559Z","shell.execute_reply":"2023-12-03T16:27:41.216945Z"},"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-12-03T16:27:41.218833Z","iopub.execute_input":"2023-12-03T16:27:41.219118Z","iopub.status.idle":"2023-12-03T16:27:41.779709Z","shell.execute_reply.started":"2023-12-03T16:27:41.219095Z","shell.execute_reply":"2023-12-03T16:27:41.778754Z"},"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-12-03T16:27:41.781056Z","iopub.execute_input":"2023-12-03T16:27:41.781430Z","iopub.status.idle":"2023-12-03T16:27:43.975652Z","shell.execute_reply.started":"2023-12-03T16:27:41.781395Z","shell.execute_reply":"2023-12-03T16:27:43.974815Z"},"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-12-03T16:27:43.976847Z","iopub.execute_input":"2023-12-03T16:27:43.977185Z","iopub.status.idle":"2023-12-03T16:27:43.993525Z","shell.execute_reply.started":"2023-12-03T16:27:43.977159Z","shell.execute_reply":"2023-12-03T16:27:43.992516Z"},"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-12-03T16:27:43.994586Z","iopub.execute_input":"2023-12-03T16:27:43.994860Z","iopub.status.idle":"2023-12-03T16:31:43.188179Z","shell.execute_reply.started":"2023-12-03T16:27:43.994835Z","shell.execute_reply":"2023-12-03T16:31:43.187352Z"},"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-12-03T16:31:43.193140Z","iopub.execute_input":"2023-12-03T16:31:43.193428Z","iopub.status.idle":"2023-12-03T16:31:44.073740Z","shell.execute_reply.started":"2023-12-03T16:31:43.193403Z","shell.execute_reply":"2023-12-03T16:31:44.072794Z"},"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-12-03T16:31:44.074834Z","iopub.execute_input":"2023-12-03T16:31:44.075155Z","iopub.status.idle":"2023-12-03T16:31:44.082380Z","shell.execute_reply.started":"2023-12-03T16:31:44.075128Z","shell.execute_reply":"2023-12-03T16:31:44.081411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf2.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:31:44.083498Z","iopub.execute_input":"2023-12-03T16:31:44.083752Z","iopub.status.idle":"2023-12-03T16:31:44.103723Z","shell.execute_reply.started":"2023-12-03T16:31:44.083728Z","shell.execute_reply":"2023-12-03T16:31:44.102869Z"},"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-12-03T16:31:44.105066Z","iopub.execute_input":"2023-12-03T16:31:44.105371Z","iopub.status.idle":"2023-12-03T16:31:44.550370Z","shell.execute_reply.started":"2023-12-03T16:31:44.105345Z","shell.execute_reply":"2023-12-03T16:31:44.549445Z"},"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-12-03T16:31:44.551584Z","iopub.execute_input":"2023-12-03T16:31:44.551870Z","iopub.status.idle":"2023-12-03T16:31:46.476979Z","shell.execute_reply.started":"2023-12-03T16:31:44.551845Z","shell.execute_reply":"2023-12-03T16:31:46.476016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf2.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:31:46.478610Z","iopub.execute_input":"2023-12-03T16:31:46.478978Z","iopub.status.idle":"2023-12-03T16:31:46.492271Z","shell.execute_reply.started":"2023-12-03T16:31:46.478944Z","shell.execute_reply":"2023-12-03T16:31:46.491254Z"},"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-12-03T16:31:46.493495Z","iopub.execute_input":"2023-12-03T16:31:46.493801Z","iopub.status.idle":"2023-12-03T16:31:48.437560Z","shell.execute_reply.started":"2023-12-03T16:31:46.493773Z","shell.execute_reply":"2023-12-03T16:31:48.436746Z"},"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-12-03T16:31:48.438635Z","iopub.execute_input":"2023-12-03T16:31:48.438892Z","iopub.status.idle":"2023-12-03T16:31:48.453417Z","shell.execute_reply.started":"2023-12-03T16:31:48.438869Z","shell.execute_reply":"2023-12-03T16:31:48.452533Z"},"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-12-03T16:31:48.463200Z","iopub.execute_input":"2023-12-03T16:31:48.463448Z","iopub.status.idle":"2023-12-03T16:37:02.094756Z","shell.execute_reply.started":"2023-12-03T16:31:48.463427Z","shell.execute_reply":"2023-12-03T16:37:02.093877Z"},"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-12-03T16:37:02.096079Z","iopub.execute_input":"2023-12-03T16:37:02.096395Z","iopub.status.idle":"2023-12-03T16:37:02.859568Z","shell.execute_reply.started":"2023-12-03T16:37:02.096367Z","shell.execute_reply":"2023-12-03T16:37:02.858719Z"},"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-12-03T16:37:02.860751Z","iopub.execute_input":"2023-12-03T16:37:02.861092Z","iopub.status.idle":"2023-12-03T16:37:02.868681Z","shell.execute_reply.started":"2023-12-03T16:37:02.861064Z","shell.execute_reply":"2023-12-03T16:37:02.867693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf3.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:37:02.870039Z","iopub.execute_input":"2023-12-03T16:37:02.870527Z","iopub.status.idle":"2023-12-03T16:37:02.903416Z","shell.execute_reply.started":"2023-12-03T16:37:02.870491Z","shell.execute_reply":"2023-12-03T16:37:02.902485Z"},"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-12-03T16:37:02.904629Z","iopub.execute_input":"2023-12-03T16:37:02.904895Z","iopub.status.idle":"2023-12-03T16:37:03.370551Z","shell.execute_reply.started":"2023-12-03T16:37:02.904872Z","shell.execute_reply":"2023-12-03T16:37:03.369585Z"},"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-12-03T16:37:03.374563Z","iopub.execute_input":"2023-12-03T16:37:03.374841Z","iopub.status.idle":"2023-12-03T16:37:03.762045Z","shell.execute_reply.started":"2023-12-03T16:37:03.374817Z","shell.execute_reply":"2023-12-03T16:37:03.761150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDf3.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T16:37:03.763277Z","iopub.execute_input":"2023-12-03T16:37:03.763669Z","iopub.status.idle":"2023-12-03T16:37:03.777389Z","shell.execute_reply.started":"2023-12-03T16:37:03.763616Z","shell.execute_reply":"2023-12-03T16:37:03.776323Z"},"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-12-03T16:37:03.778762Z","iopub.execute_input":"2023-12-03T16:37:03.779609Z","iopub.status.idle":"2023-12-03T16:37:04.266952Z","shell.execute_reply.started":"2023-12-03T16:37:03.779573Z","shell.execute_reply":"2023-12-03T16:37:04.266043Z"},"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":{}}]}