{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"}],"dockerImageVersionId":30512,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#! pip install -q kaggle\n\n#! mkdir ~/.kaggle\n\n#! cp kaggle.json ~/.kaggle/\n\n#! chmod 600 ~/.kaggle/kaggle.json\n\n#!kaggle competitions download -c rsna-pneumonia-detection-challenge\n#!unzip rsna-pneumonia-detection-challenge -d rsna_pneumonia\n","metadata":{"id":"qMsa0TzdaOon","outputId":"6e34a362-ca28-493e-803b-fcbd69813fa1","execution":{"iopub.status.busy":"2023-07-15T16:15:11.482435Z","iopub.execute_input":"2023-07-15T16:15:11.48318Z","iopub.status.idle":"2023-07-15T16:15:11.492236Z","shell.execute_reply.started":"2023-07-15T16:15:11.483129Z","shell.execute_reply":"2023-07-15T16:15:11.491321Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Pneumonia is an infection that inflames the alveoli in one or both lungs.\nAlveoli may fill with fluid or pus.The pnumonia infectio is difficult to dignose, and it's beceause the symptoms are often similar to the other respiratory infections like bronchitis and asthema.\n\nHere are some of the symptoms of pneumonia infection:\n\n\n* Fever\n* Shiver\n* Cough\n* Chest pain\n* Diffically breathing\n* fatigue","metadata":{}},{"cell_type":"markdown","source":"# Importing libraries","metadata":{}},{"cell_type":"code","source":"#! pip install pydicom\nimport pydicom\nimport cv2\nimport os\nimport pandas as pd\nimport numpy as np\nimport glob\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n#from matplotlib.patches import Rectangle\nfrom sklearn.model_selection import train_test_split\n\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.layers import BatchNormalization, Activation\nfrom tensorflow.keras.layers import Dropout\n\n\n\n\nprint('successfully imported')","metadata":{"id":"dBrl3wp4aZBV","outputId":"97a2a219-2ea0-49f4-f8ae-95e2820a1442","execution":{"iopub.status.busy":"2023-07-18T17:25:59.678424Z","iopub.execute_input":"2023-07-18T17:25:59.679552Z","iopub.status.idle":"2023-07-18T17:25:59.688411Z","shell.execute_reply.started":"2023-07-18T17:25:59.679512Z","shell.execute_reply":"2023-07-18T17:25:59.687238Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Reading Data","metadata":{}},{"cell_type":"code","source":"detail_class = pd.read_csv('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_detailed_class_info.csv')\n\nbbox_info = pd.read_csv('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv')\n\n\ndicom_dir = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images'\n","metadata":{"id":"UVmgylWGa-RJ","execution":{"iopub.status.busy":"2023-07-18T17:26:28.021513Z","iopub.execute_input":"2023-07-18T17:26:28.021877Z","iopub.status.idle":"2023-07-18T17:26:28.144962Z","shell.execute_reply.started":"2023-07-18T17:26:28.021848Z","shell.execute_reply":"2023-07-18T17:26:28.143951Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#let's see how many image we have in dicom_dir\nnum_dicom = os.listdir(dicom_dir)\nprint(len(num_dicom))","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:26:29.301742Z","iopub.execute_input":"2023-07-18T17:26:29.303222Z","iopub.status.idle":"2023-07-18T17:26:30.42741Z","shell.execute_reply.started":"2023-07-18T17:26:29.303178Z","shell.execute_reply":"2023-07-18T17:26:30.426315Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(detail_class.shape, bbox_info.shape)","metadata":{"id":"R8qEa3eecHxB","outputId":"14bd76d3-09d5-4eed-c97a-5b27d0071448","execution":{"iopub.status.busy":"2023-07-18T17:27:09.619726Z","iopub.execute_input":"2023-07-18T17:27:09.620822Z","iopub.status.idle":"2023-07-18T17:27:09.626598Z","shell.execute_reply.started":"2023-07-18T17:27:09.620767Z","shell.execute_reply":"2023-07-18T17:27:09.625498Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"detail_class.head()","metadata":{"id":"5W83TaSiOjxM","outputId":"e8214dc5-1894-42e8-f9d9-53d6e921de4d","execution":{"iopub.status.busy":"2023-07-18T17:27:09.628999Z","iopub.execute_input":"2023-07-18T17:27:09.629799Z","iopub.status.idle":"2023-07-18T17:27:09.656498Z","shell.execute_reply.started":"2023-07-18T17:27:09.629761Z","shell.execute_reply":"2023-07-18T17:27:09.655306Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bbox_info.head()","metadata":{"id":"QVomWT6fOrxI","outputId":"443b4c6f-e65b-49ba-9040-7df049a523c1","execution":{"iopub.status.busy":"2023-07-18T17:27:14.471094Z","iopub.execute_input":"2023-07-18T17:27:14.471493Z","iopub.status.idle":"2023-07-18T17:27:14.49167Z","shell.execute_reply.started":"2023-07-18T17:27:14.471461Z","shell.execute_reply":"2023-07-18T17:27:14.490462Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pneumonia = pd.merge(detail_class, bbox_info, on='patientId', how='inner')","metadata":{"id":"EKlLEwiwbNaD","execution":{"iopub.status.busy":"2023-07-18T17:27:17.425441Z","iopub.execute_input":"2023-07-18T17:27:17.425811Z","iopub.status.idle":"2023-07-18T17:27:17.475482Z","shell.execute_reply.started":"2023-07-18T17:27:17.425779Z","shell.execute_reply":"2023-07-18T17:27:17.474135Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(pneumonia.shape)","metadata":{"id":"-KjjkfMiQtjx","outputId":"bff5ac50-0888-4e84-e87b-56edd9d9d397","execution":{"iopub.status.busy":"2023-07-18T17:27:18.596322Z","iopub.execute_input":"2023-07-18T17:27:18.596677Z","iopub.status.idle":"2023-07-18T17:27:18.602706Z","shell.execute_reply.started":"2023-07-18T17:27:18.596648Z","shell.execute_reply":"2023-07-18T17:27:18.601464Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pneumonia.head()","metadata":{"id":"ctoAhJpqOEv7","outputId":"4bfb2a9a-c358-49d6-9ae2-26ceeb1ac35d","execution":{"iopub.status.busy":"2023-07-18T17:27:18.605389Z","iopub.execute_input":"2023-07-18T17:27:18.605767Z","iopub.status.idle":"2023-07-18T17:27:18.628395Z","shell.execute_reply.started":"2023-07-18T17:27:18.605733Z","shell.execute_reply":"2023-07-18T17:27:18.627244Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(pneumonia['patientId'])","metadata":{"id":"PWKDKyWrb8Wh","outputId":"6b26cb62-c4da-4b47-9106-b4311edd1f02","execution":{"iopub.status.busy":"2023-07-18T17:27:23.476293Z","iopub.execute_input":"2023-07-18T17:27:23.476687Z","iopub.status.idle":"2023-07-18T17:27:23.484308Z","shell.execute_reply.started":"2023-07-18T17:27:23.476649Z","shell.execute_reply":"2023-07-18T17:27:23.483256Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"37629-30227","metadata":{"id":"nqE1KYQtO4rm","outputId":"8a16a9c6-79fb-434a-b325-a9751b57e79d","execution":{"iopub.status.busy":"2023-07-18T17:27:25.945474Z","iopub.execute_input":"2023-07-18T17:27:25.945869Z","iopub.status.idle":"2023-07-18T17:27:25.952161Z","shell.execute_reply.started":"2023-07-18T17:27:25.945836Z","shell.execute_reply":"2023-07-18T17:27:25.951105Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"* Because the patientId column is duplicated after merging detail_class and bbox_info, we must drop paitientId from detail_class.\n","metadata":{"id":"mmv97gHfR6YU"}},{"cell_type":"markdown","source":"# Make one CSV file containing all the features we need. ","metadata":{}},{"cell_type":"code","source":"pneumonia = pd.concat([detail_class.drop(columns = 'patientId'), bbox_info], axis = 1)","metadata":{"id":"aW3beV34b-24","execution":{"iopub.status.busy":"2023-07-16T20:01:24.907227Z","iopub.execute_input":"2023-07-16T20:01:24.907924Z","iopub.status.idle":"2023-07-16T20:01:24.915029Z","shell.execute_reply.started":"2023-07-16T20:01:24.907891Z","shell.execute_reply":"2023-07-16T20:01:24.914073Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Here we go\npneumonia.shape","metadata":{"id":"P1TwO8lPc9PE","outputId":"83dfacb7-ac4a-41d7-8423-817fb5dbfcce","execution":{"iopub.status.busy":"2023-07-18T17:27:32.426562Z","iopub.execute_input":"2023-07-18T17:27:32.426975Z","iopub.status.idle":"2023-07-18T17:27:32.434962Z","shell.execute_reply.started":"2023-07-18T17:27:32.426937Z","shell.execute_reply":"2023-07-18T17:27:32.433348Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pneumonia['patientId'].nunique()","metadata":{"id":"EklCPBvRe_l4","outputId":"0786c9c0-a451-430a-ba8b-f9c20d0b6af8","execution":{"iopub.status.busy":"2023-07-18T17:27:32.4378Z","iopub.execute_input":"2023-07-18T17:27:32.438574Z","iopub.status.idle":"2023-07-18T17:27:32.459239Z","shell.execute_reply.started":"2023-07-18T17:27:32.438537Z","shell.execute_reply":"2023-07-18T17:27:32.458153Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Add all Bound In Box parameters in 'bbox'\npneumonia['bbox'] = pneumonia[['x', 'y', 'height', 'width']].apply(lambda x: '-'.join(str(i) for i in x), axis=1)","metadata":{"id":"1KTjpBPUIVuk","execution":{"iopub.status.busy":"2023-07-18T17:27:32.461001Z","iopub.execute_input":"2023-07-18T17:27:32.461673Z","iopub.status.idle":"2023-07-18T17:27:32.89733Z","shell.execute_reply.started":"2023-07-18T17:27:32.461634Z","shell.execute_reply":"2023-07-18T17:27:32.896234Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pneumonia = pneumonia.drop(columns = ['x', 'y', 'height', 'width'])","metadata":{"id":"SDW3IcYzIeZw","execution":{"iopub.status.busy":"2023-07-18T17:27:34.230464Z","iopub.execute_input":"2023-07-18T17:27:34.23083Z","iopub.status.idle":"2023-07-18T17:27:34.239304Z","shell.execute_reply.started":"2023-07-18T17:27:34.230801Z","shell.execute_reply":"2023-07-18T17:27:34.238182Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pneumonia.head()","metadata":{"id":"f8VnmWO7IuPZ","outputId":"1324934d-6f41-4f3a-ff59-8abd669ce50f","execution":{"iopub.status.busy":"2023-07-18T17:27:34.242035Z","iopub.execute_input":"2023-07-18T17:27:34.242917Z","iopub.status.idle":"2023-07-18T17:27:34.259277Z","shell.execute_reply.started":"2023-07-18T17:27:34.242873Z","shell.execute_reply":"2023-07-18T17:27:34.258231Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Reading images","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/*.dcm'\n\nimages = os.listdir('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images')\n\npneumonia['image_path'] = ''\n\nfor i in range(len(pneumonia)):\n    patient_id = pneumonia['patientId'][i]\n    image_path = os.path.join('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images', patient_id + '.dcm')\n    pneumonia['image_path'][i] = image_path","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:32:09.126679Z","iopub.execute_input":"2023-07-18T17:32:09.127096Z","iopub.status.idle":"2023-07-18T17:32:21.075736Z","shell.execute_reply.started":"2023-07-18T17:32:09.127064Z","shell.execute_reply":"2023-07-18T17:32:21.074579Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#look at the meta data of random image from trian set\nrandom_patient_id = pneumonia['patientId'].sample().values[0]\ndicom_data = pydicom.read_file(image_path)\n\n\nprint(dicom_data)","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:32:22.165897Z","iopub.execute_input":"2023-07-18T17:32:22.166342Z","iopub.status.idle":"2023-07-18T17:32:22.194132Z","shell.execute_reply.started":"2023-07-18T17:32:22.166312Z","shell.execute_reply":"2023-07-18T17:32:22.192175Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Using glob libraries to read all images from the path.\n#Using pydicom to read dcm images.\n#Using matplotlib.pyplot as plt to show images.\n\n#Path to the train images\npath = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/*.dcm'\n\nfig, axes = plt.subplots(ncols=5, figsize=(10, 10))\nimages = glob.glob(path)\n\nfor i, ax in enumerate(axes.flat):\n    image_file = images[i]\n    dicom_image = pydicom.dcmread(image_file)\n    img = dicom_image.pixel_array\n    ax.imshow(img, cmap='bone')\n    #Remove (x,y)ticks\n    ax.set_xticks([])  \n    ax.set_yticks([])  \n\n#Adjust spacing between subplots\nplt.tight_layout()  \nplt.show()\nprint('-'*95)\nprint('Dicom images shape is:', img.shape)","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:33:22.360766Z","iopub.execute_input":"2023-07-18T17:33:22.361526Z","iopub.status.idle":"2023-07-18T17:33:23.607037Z","shell.execute_reply.started":"2023-07-18T17:33:22.361489Z","shell.execute_reply":"2023-07-18T17:33:23.606081Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def read_and_resize_images(pneumonia):\n    resized_images = []\n    boxes = []\n    for i in range(len(pneumonia)):\n        patient_id = pneumonia['patientId'][i]\n        image_path = pneumonia['image_path'][i]\n        target = pneumonia['Target'][i]\n        dicom_data = pydicom.read_file(image_path)\n        img = dicom_data.pixel_array\n        \n        #Resize image to 224x224\n        img = cv2.resize(img, (224, 224))\n        img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)\n        resized_images.append(img)\n        boxes.append(np.array(target, dtype=np.float32))\n    return np.array(resized_images), np.array(boxes)","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:34:40.257222Z","iopub.execute_input":"2023-07-18T17:34:40.257618Z","iopub.status.idle":"2023-07-18T17:34:40.265344Z","shell.execute_reply.started":"2023-07-18T17:34:40.257588Z","shell.execute_reply":"2023-07-18T17:34:40.264352Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Split the data to Train and Test","metadata":{}},{"cell_type":"code","source":"X, y = read_and_resize_images(pneumonia[:1000])","metadata":{"id":"vwhuONk8RQfY","execution":{"iopub.status.busy":"2023-07-18T17:35:14.610874Z","iopub.execute_input":"2023-07-18T17:35:14.6113Z","iopub.status.idle":"2023-07-18T17:35:21.238995Z","shell.execute_reply.started":"2023-07-18T17:35:14.611268Z","shell.execute_reply":"2023-07-18T17:35:21.237892Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)","metadata":{"id":"Bz111UemN5tB","execution":{"iopub.status.busy":"2023-07-18T17:36:01.285974Z","iopub.execute_input":"2023-07-18T17:36:01.28731Z","iopub.status.idle":"2023-07-18T17:36:01.338088Z","shell.execute_reply.started":"2023-07-18T17:36:01.287265Z","shell.execute_reply":"2023-07-18T17:36:01.337093Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type(y_train[0])","metadata":{"id":"Y0vDzYksQlmi","outputId":"30eff16a-4d11-4ec4-de34-430a8247b396","execution":{"iopub.status.busy":"2023-07-18T17:36:03.290673Z","iopub.execute_input":"2023-07-18T17:36:03.291092Z","iopub.status.idle":"2023-07-18T17:36:03.297832Z","shell.execute_reply.started":"2023-07-18T17:36:03.29106Z","shell.execute_reply":"2023-07-18T17:36:03.296817Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_train.shape, y_train.shape)\nprint(X_test.shape, y_test.shape)","metadata":{"id":"QstNQbBeN5pf","outputId":"4f2f6cdf-5540-4496-bd0c-c707dc4c342c","execution":{"iopub.status.busy":"2023-07-18T17:36:03.825183Z","iopub.execute_input":"2023-07-18T17:36:03.82559Z","iopub.status.idle":"2023-07-18T17:36:03.832379Z","shell.execute_reply.started":"2023-07-18T17:36:03.825559Z","shell.execute_reply":"2023-07-18T17:36:03.831364Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Base model = ResNet50","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.layers import BatchNormalization, Activation\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import regularizers\n\nprint('successfully imported')","metadata":{"id":"kJuktywOOua1","execution":{"iopub.status.busy":"2023-07-18T17:35:42.067021Z","iopub.execute_input":"2023-07-18T17:35:42.067738Z","iopub.status.idle":"2023-07-18T17:35:42.073875Z","shell.execute_reply.started":"2023-07-18T17:35:42.067701Z","shell.execute_reply":"2023-07-18T17:35:42.072724Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\n#Using ResNet50 as a base model\nbase_model_resnet = ResNet50(weights = 'imagenet', input_shape = (224,224,3) , include_top=False, pooling='avg' )\nfor l in base_model_resnet.layers[:-4]:\n    \n    l.trainable=False","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:41:43.125738Z","iopub.execute_input":"2023-07-18T17:41:43.126173Z","iopub.status.idle":"2023-07-18T17:41:45.119953Z","shell.execute_reply.started":"2023-07-18T17:41:43.126141Z","shell.execute_reply":"2023-07-18T17:41:45.118918Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Building Model","metadata":{}},{"cell_type":"code","source":"#Building a model\nmodel_resnet = Sequential()\nmodel_resnet.add(base_model_resnet)\nmodel_resnet.add(BatchNormalization())\n\n\nmodel_resnet.add(Dense(256, kernel_regularizer=regularizers.l2(0.01)))\nmodel_resnet.add(BatchNormalization())\nmodel_resnet.add(Activation(activation='relu'))\n\nmodel_resnet.add(Dropout(0.3))\n\n\n\nmodel_resnet.add(Dense(128, kernel_regularizer=regularizers.l2(0.01)))\nmodel_resnet.add(BatchNormalization())\nmodel_resnet.add(Activation(activation='relu'))\n\nmodel_resnet.add(Dropout(0.3))\n\n\n\nmodel_resnet.add(Dense(64, kernel_regularizer=regularizers.l2(0.01)))\nmodel_resnet.add(BatchNormalization())\nmodel_resnet.add(Activation(activation='relu'))\n\nmodel_resnet.add(Dropout(0.3))\n\n\nmodel_resnet.add(Dense(32, kernel_regularizer=regularizers.l2(0.01)))\nmodel_resnet.add(BatchNormalization())\nmodel_resnet.add(Activation(activation='relu'))\n\nmodel_resnet.add(Dropout(0.3))\n\n\nmodel_resnet.add(Dense(16, kernel_regularizer=regularizers.l2(0.01)))\nmodel_resnet.add(BatchNormalization())\nmodel_resnet.add(Activation(activation='relu'))\n\nmodel_resnet.add(Dropout(0.3))\n\nmodel_resnet.add(Dense(1, activation = 'sigmoid'))\n","metadata":{"id":"Ih0ewmVJN5na","execution":{"iopub.status.busy":"2023-07-18T17:41:50.393046Z","iopub.execute_input":"2023-07-18T17:41:50.393608Z","iopub.status.idle":"2023-07-18T17:41:51.43748Z","shell.execute_reply.started":"2023-07-18T17:41:50.393565Z","shell.execute_reply":"2023-07-18T17:41:51.436443Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_resnet.summary()","metadata":{"id":"Gl1L1TSqhlwj","outputId":"97ced1c6-f491-4d13-c8c7-4aef21463009","execution":{"iopub.status.busy":"2023-07-18T17:41:51.439296Z","iopub.execute_input":"2023-07-18T17:41:51.439897Z","iopub.status.idle":"2023-07-18T17:41:51.585614Z","shell.execute_reply.started":"2023-07-18T17:41:51.439859Z","shell.execute_reply":"2023-07-18T17:41:51.584823Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Compiling Model","metadata":{}},{"cell_type":"code","source":"model_resnet.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n\nhistory_resnet = model_resnet.fit(X_train, y_train, epochs = 15, validation_split=0.7)","metadata":{"id":"sPX_fBh5N5lV","execution":{"iopub.status.busy":"2023-07-18T17:41:55.820655Z","iopub.execute_input":"2023-07-18T17:41:55.821083Z","iopub.status.idle":"2023-07-18T17:42:32.042477Z","shell.execute_reply.started":"2023-07-18T17:41:55.821051Z","shell.execute_reply":"2023-07-18T17:42:32.041412Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(6,4))\n\nplt.plot(history_resnet.history['loss'], label='training loss');\nplt.plot(history_resnet.history['accuracy'], label='Accuracy');\nplt.plot(history_resnet.history['val_loss'], label='val_loss');\nplt.plot(history_resnet.history['val_accuracy'], label='val_accuracy')\nplt.legend()\nplt.show()","metadata":{"id":"8sLUTBy2N5jO","outputId":"905eec63-b0ef-48de-ea9c-24823e2e1984","execution":{"iopub.status.busy":"2023-07-18T17:42:57.749826Z","iopub.execute_input":"2023-07-18T17:42:57.750955Z","iopub.status.idle":"2023-07-18T17:42:58.077877Z","shell.execute_reply.started":"2023-07-18T17:42:57.750899Z","shell.execute_reply":"2023-07-18T17:42:58.076895Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_resnet.evaluate(x = X_train, y = y_train, batch_size = 32)","metadata":{"id":"6MHhQrPqbb9t","outputId":"8b51c313-cb46-46b2-b8c8-5159b57d0b6b","execution":{"iopub.status.busy":"2023-07-18T17:42:58.080318Z","iopub.execute_input":"2023-07-18T17:42:58.080684Z","iopub.status.idle":"2023-07-18T17:43:00.992159Z","shell.execute_reply.started":"2023-07-18T17:42:58.080649Z","shell.execute_reply":"2023-07-18T17:43:00.990981Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Building Model: EfficientNet B3","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB0, EfficientNetB3, EfficientNetB7","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:38:35.01578Z","iopub.execute_input":"2023-07-18T17:38:35.016204Z","iopub.status.idle":"2023-07-18T17:38:35.021207Z","shell.execute_reply.started":"2023-07-18T17:38:35.016167Z","shell.execute_reply":"2023-07-18T17:38:35.020022Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Compare the result of ResNet50 and EfficientNet B3 ","metadata":{}},{"cell_type":"code","source":"base_model_eff_b3 = EfficientNetB3(weights= 'imagenet',input_shape = (224,224,3) , include_top=False, pooling='avg')\nfor l in base_model_eff_b3.layers[:-6]:\n    l.trainable=False","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:39:20.39698Z","iopub.execute_input":"2023-07-18T17:39:20.397356Z","iopub.status.idle":"2023-07-18T17:39:23.970134Z","shell.execute_reply.started":"2023-07-18T17:39:20.397328Z","shell.execute_reply":"2023-07-18T17:39:23.96876Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Building a model\nmodel_efficient = Sequential()\nmodel_efficient.add(base_model_eff_b3)\nmodel_efficient.add(BatchNormalization())\n\n\nmodel_efficient.add(Dense(256, kernel_regularizer=regularizers.l2(0.01)))\nmodel_efficient.add(BatchNormalization())\nmodel_efficient.add(Activation(activation='relu'))\n\nmodel_efficient.add(Dropout(0.3))\n\n\n\nmodel_efficient.add(Dense(128, kernel_regularizer=regularizers.l2(0.01)))\nmodel_efficient.add(BatchNormalization())\nmodel_efficient.add(Activation(activation='relu'))\n\nmodel_efficient.add(Dropout(0.3))\n\n\n\nmodel_efficient.add(Dense(64, kernel_regularizer=regularizers.l2(0.01)))\nmodel_efficient.add(BatchNormalization())\nmodel_efficient.add(Activation(activation='relu'))\n\nmodel_efficient.add(Dropout(0.3))\n\n\nmodel_efficient.add(Dense(32, kernel_regularizer=regularizers.l2(0.01)))\nmodel_efficient.add(BatchNormalization())\nmodel_efficient.add(Activation(activation='relu'))\n\nmodel_efficient.add(Dropout(0.3))\n\n\nmodel_efficient.add(Dense(16, kernel_regularizer=regularizers.l2(0.01)))\nmodel_efficient.add(BatchNormalization())\nmodel_efficient.add(Activation(activation='relu'))\n\nmodel_efficient.add(Dropout(0.3))\n\nmodel_efficient.add(Dense(1, activation = 'sigmoid'))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:39:26.665778Z","iopub.execute_input":"2023-07-18T17:39:26.66722Z","iopub.status.idle":"2023-07-18T17:39:28.267134Z","shell.execute_reply.started":"2023-07-18T17:39:26.667175Z","shell.execute_reply":"2023-07-18T17:39:28.265935Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_efficient.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:39:28.269063Z","iopub.execute_input":"2023-07-18T17:39:28.26945Z","iopub.status.idle":"2023-07-18T17:39:28.372084Z","shell.execute_reply.started":"2023-07-18T17:39:28.269413Z","shell.execute_reply":"2023-07-18T17:39:28.371151Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_efficient.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n\nhistory_efficient = model_efficient.fit(X_train, y_train, epochs = 15, validation_split=0.7)","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:44:11.281903Z","iopub.execute_input":"2023-07-18T17:44:11.282339Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_efficient.evaluate(x = X_train, y = y_train, batch_size = 32)","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:40:40.220896Z","iopub.execute_input":"2023-07-18T17:40:40.222088Z","iopub.status.idle":"2023-07-18T17:40:43.096058Z","shell.execute_reply.started":"2023-07-18T17:40:40.222038Z","shell.execute_reply":"2023-07-18T17:40:43.094976Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(6,4))\n\nplt.plot(history_efficient.history['loss'], label='training loss');\nplt.plot(history_efficient.history['accuracy'], label='Accuracy');\nplt.plot(history_efficient.history['val_loss'], label='val_loss');\nplt.plot(history_efficient.history['val_accuracy'], label='val_accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:43:22.586671Z","iopub.execute_input":"2023-07-18T17:43:22.587446Z","iopub.status.idle":"2023-07-18T17:43:22.918033Z","shell.execute_reply.started":"2023-07-18T17:43:22.587403Z","shell.execute_reply":"2023-07-18T17:43:22.916938Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(6,4))\n\nplt.plot(history_efficient.history['accuracy'], label='EfficientNet_Accuracy');\nplt.plot(history_resnet.history['accuracy'], label='ResNet50_Accuracy');\n\nplt.legend()\n","metadata":{"execution":{"iopub.status.busy":"2023-07-18T17:43:14.682438Z","iopub.execute_input":"2023-07-18T17:43:14.682847Z","iopub.status.idle":"2023-07-18T17:43:15.038862Z","shell.execute_reply.started":"2023-07-18T17:43:14.682815Z","shell.execute_reply":"2023-07-18T17:43:15.037857Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Please write comments about my notebook. It can help me a lot to improve my skills.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}