{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"},{"sourceId":1463747,"sourceType":"datasetVersion","datasetId":858376},{"sourceId":1493395,"sourceType":"datasetVersion","datasetId":876887},{"sourceId":3678509,"sourceType":"datasetVersion","datasetId":1894379}],"dockerImageVersionId":30192,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"id":"84hZ7KIoTgbM","outputId":"e2ae0d73-8d83-4550-97af-a1cb81bf1bae"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pip freeze","metadata":{"id":"_50cF9RGWKLH"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pip install pydicom","metadata":{"id":"ghaALEC6e7If","execution":{"iopub.status.busy":"2022-06-03T15:58:38.460996Z","iopub.execute_input":"2022-06-03T15:58:38.461609Z","iopub.status.idle":"2022-06-03T15:58:49.432234Z","shell.execute_reply.started":"2022-06-03T15:58:38.461569Z","shell.execute_reply":"2022-06-03T15:58:49.43127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom tqdm import tqdm\nimport os\nimport numpy as np\nimport cv2\n%matplotlib inline\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os","metadata":{"id":"dGhcmy_T10wy","outputId":"4483f496-6e4c-41f6-932a-e40229953b15","execution":{"iopub.status.busy":"2024-04-23T21:23:49.927060Z","iopub.execute_input":"2024-04-23T21:23:49.927714Z","iopub.status.idle":"2024-04-23T21:23:50.206907Z","shell.execute_reply.started":"2024-04-23T21:23:49.927605Z","shell.execute_reply":"2024-04-23T21:23:50.206173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"VShkJugPTi2S","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# params we will probably want to do some hyperparameter optimization later\nBASE_MODEL=  'DenseNet121' #'ResNet101' # ['VGG16', 'RESNET52', 'InceptionV3', 'Xception', 'DenseNet169', 'DenseNet121']\nIMG_SIZE = (224, 224) # [(224, 224), (384, 384), (512, 512), (640, 640)]\nBATCH_SIZE = 16 # [1, 8, 16, 24]\nDENSE_COUNT = 128 # [32, 64, 128, 256]\nDROPOUT = 0.25 # [0, 0.25, 0.5]\nLEARN_RATE = 1e-4 # [1e-4, 1e-3, 4e-3]\n# TRAIN_SAMPLES = 8000 # [3000, 6000, 15000]\n# TEST_SAMPLES = 800\n# USE_ATTN = False # [True, False]","metadata":{"id":"aeul9Jf2Tnil","execution":{"iopub.status.busy":"2024-04-23T21:23:50.208627Z","iopub.execute_input":"2024-04-23T21:23:50.208905Z","iopub.status.idle":"2024-04-23T21:23:50.214257Z","shell.execute_reply.started":"2024-04-23T21:23:50.208866Z","shell.execute_reply":"2024-04-23T21:23:50.213484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\ndet_class_path = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_detailed_class_info.csv'\nbbox_path = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv'\ndicom_dir = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/'\n\ndet_class_df = pd.read_csv(det_class_path)\nbbox_df = pd.read_csv(bbox_path)\n\ncomb_bbox_df = pd.concat([bbox_df, \n                        det_class_df.drop('patientId',1)], 1)\n\n\n\nbox_df = comb_bbox_df.groupby('patientId').\\\n    size().\\\n    reset_index(name='boxes')\ncomb_box_df = pd.merge(comb_bbox_df, box_df, on='patientId')\n\nimage_df = pd.DataFrame({'path': glob(os.path.join(dicom_dir, '*.dcm'))})\n# print(image_df)\nimage_df['patientId'] = image_df['path'].map(lambda x: os.path.splitext(os.path.basename(x))[0])\n\nimage_bbox_df = pd.merge(comb_box_df, \n                         image_df, \n                         on='patientId',\n                        how='left').sort_values('patientId')\nprint(image_bbox_df.shape[0], 'image bounding boxes')\nimage_bbox_df.reset_index(inplace=True, drop=True)\ndf = image_bbox_df\ndf.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-23T21:46:10.105831Z","iopub.execute_input":"2024-04-23T21:46:10.106155Z","iopub.status.idle":"2024-04-23T21:46:10.602032Z","shell.execute_reply.started":"2024-04-23T21:46:10.106116Z","shell.execute_reply":"2024-04-23T21:46:10.601184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n\n# df = pd.read_csv(\"/kaggle/input/lung-opacity-overview/image_bbox_full.csv\")\n","metadata":{"id":"8Ns2X7NfTnzv","execution":{"iopub.status.busy":"2024-04-23T21:46:10.625215Z","iopub.execute_input":"2024-04-23T21:46:10.625506Z","iopub.status.idle":"2024-04-23T21:46:10.629197Z","shell.execute_reply.started":"2024-04-23T21:46:10.625470Z","shell.execute_reply":"2024-04-23T21:46:10.628422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-23T21:46:13.200905Z","iopub.execute_input":"2024-04-23T21:46:13.201230Z","iopub.status.idle":"2024-04-23T21:46:13.207345Z","shell.execute_reply.started":"2024-04-23T21:46:13.201194Z","shell.execute_reply":"2024-04-23T21:46:13.206478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T21:46:15.593308Z","iopub.execute_input":"2024-04-23T21:46:15.594164Z","iopub.status.idle":"2024-04-23T21:46:15.616539Z","shell.execute_reply.started":"2024-04-23T21:46:15.594114Z","shell.execute_reply":"2024-04-23T21:46:15.615481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#new column, male  = 0, female  = 1\nfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder\nfrom sklearn.model_selection import train_test_split\n\ndef preprocess(working_df ):\n\n\n\n    working_df = working_df.drop(working_df[working_df['class'] == 'No Lung Opacity / Not Normal'].index)\n    working_df['new_class'] = working_df['class'].map({ 'Normal':'Normal','Lung Opacity': 'Pneumonia' })\n    working_df.rename(columns = {'Unnamed: 0' :'unique_id'}, inplace = True)\n    \n    working_df['path'] = working_df['path'].str.replace('../input/' ,'../input/rsna-pneumonia-detection-challenge/') #../input/rsna-pneumonia-detection-challenge ##../input/\n\n    # get the labels in the right format\n\n    class_enc = LabelEncoder()\n    working_df['class_idx'] = class_enc.fit_transform(working_df['new_class'])\n    oh_enc = OneHotEncoder(sparse=False)\n    working_df['class_vec'] = oh_enc.fit_transform(\n        working_df['class_idx'].values.reshape(-1, 1)).tolist() \n    #working_df.sample(3)\n\n    image_df = working_df.groupby('patientId').apply(lambda x: x.sample(1))\n    return  image_df\n    ","metadata":{"id":"RMWyHFZFTn4N","outputId":"3d12a2f9-233b-4cce-cf29-fcec172b85db","execution":{"iopub.status.busy":"2024-04-23T21:46:19.249718Z","iopub.execute_input":"2024-04-23T21:46:19.250029Z","iopub.status.idle":"2024-04-23T21:46:19.992310Z","shell.execute_reply.started":"2024-04-23T21:46:19.249993Z","shell.execute_reply":"2024-04-23T21:46:19.991374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" image_df = preprocess(df)","metadata":{"id":"tc_jXgMEUB7Y","outputId":"8b39a0c3-916a-4420-d5b8-126f65e1e25c","execution":{"iopub.status.busy":"2024-04-23T21:46:20.389263Z","iopub.execute_input":"2024-04-23T21:46:20.390034Z","iopub.status.idle":"2024-04-23T21:47:41.052527Z","shell.execute_reply.started":"2024-04-23T21:46:20.389993Z","shell.execute_reply":"2024-04-23T21:47:41.051801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T21:49:21.396991Z","iopub.execute_input":"2024-04-23T21:49:21.397621Z","iopub.status.idle":"2024-04-23T21:49:21.425389Z","shell.execute_reply.started":"2024-04-23T21:49:21.397576Z","shell.execute_reply":"2024-04-23T21:49:21.424330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" image_df.groupby('new_class').size()","metadata":{"execution":{"iopub.status.busy":"2024-04-16T01:20:17.415170Z","iopub.execute_input":"2024-04-16T01:20:17.415487Z","iopub.status.idle":"2024-04-16T01:20:17.426569Z","shell.execute_reply.started":"2024-04-16T01:20:17.415441Z","shell.execute_reply":"2024-04-16T01:20:17.425547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (20, 10))\nimage_df.groupby('new_class').size().plot.bar(ax=ax1)\n#train_df = raw_train_df.groupby('new_class').\\\n#   apply(lambda x: x.sample(12000//3)).\\\n#   reset_index(drop=True)\nimage_df =  image_df.groupby('class').apply(lambda x: x.sample(n={'Lung Opacity':6012, 'Normal':6012}.get(x.name), random_state = 2018)).reset_index(drop = True) #941 #383 #558 #1345\n\nimage_df.groupby('new_class').size().plot.bar(ax=ax2) \n#train_df.groupby('class').size().plot.bar() \nprint(image_df.shape[0], 'new training size')","metadata":{"execution":{"iopub.status.busy":"2024-04-16T01:20:17.428806Z","iopub.execute_input":"2024-04-16T01:20:17.429110Z","iopub.status.idle":"2024-04-16T01:20:17.819342Z","shell.execute_reply.started":"2024-04-16T01:20:17.429048Z","shell.execute_reply":"2024-04-16T01:20:17.818582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, remain_df = train_test_split(image_df, test_size=0.20, random_state=2018,\n                                        stratify=image_df['new_class'])\nprint(train_df.shape, 'training data')\nprint(remain_df.shape, 'test data')\n\nvalid_df, test_df = train_test_split(remain_df, test_size=0.5, random_state=2018,\n                                    stratify=remain_df['new_class'])\nprint(train_df.shape, 'train_df')\nprint(valid_df.shape, 'valid data')\nprint(test_df.shape, 'test data')\n","metadata":{"id":"4FFHmfjqUB86","execution":{"iopub.status.busy":"2024-04-16T01:20:17.820593Z","iopub.execute_input":"2024-04-16T01:20:17.820916Z","iopub.status.idle":"2024-04-16T01:20:17.852349Z","shell.execute_reply.started":"2024-04-16T01:20:17.820875Z","shell.execute_reply":"2024-04-16T01:20:17.851645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.groupby('new_class').size()","metadata":{"execution":{"iopub.status.busy":"2024-04-16T01:20:17.853319Z","iopub.execute_input":"2024-04-16T01:20:17.853510Z","iopub.status.idle":"2024-04-16T01:20:17.861676Z","shell.execute_reply.started":"2024-04-16T01:20:17.853485Z","shell.execute_reply":"2024-04-16T01:20:17.860967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df.groupby('new_class').size()","metadata":{"execution":{"iopub.status.busy":"2024-04-16T01:20:17.862670Z","iopub.execute_input":"2024-04-16T01:20:17.862955Z","iopub.status.idle":"2024-04-16T01:20:17.871801Z","shell.execute_reply.started":"2024-04-16T01:20:17.862903Z","shell.execute_reply":"2024-04-16T01:20:17.871096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.groupby('new_class').size()","metadata":{"execution":{"iopub.status.busy":"2024-04-16T01:20:17.872652Z","iopub.execute_input":"2024-04-16T01:20:17.872828Z","iopub.status.idle":"2024-04-16T01:20:17.885378Z","shell.execute_reply.started":"2024-04-16T01:20:17.872805Z","shell.execute_reply":"2024-04-16T01:20:17.884599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# raw_train_df.to_csv('raw_train_df.csv')\n# valid_df.to_csv('valid_df.csv')\n# test_df.to_csv('test_df.csv')","metadata":{"id":"ludOO_1N8m3N","execution":{"iopub.status.busy":"2022-09-11T16:58:01.30603Z","iopub.status.idle":"2022-09-11T16:58:01.306663Z","shell.execute_reply.started":"2022-09-11T16:58:01.306425Z","shell.execute_reply":"2022-09-11T16:58:01.306449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# raw_train_df = pd.read_csv('../input/rsna-train-test-split-v11092022/raw_train_df.csv')\n# valid_df = pd. read_csv('../input/rsna-train-test-split-v11092022/valid_df.csv')\n# test_df = pd.read_csv('../input/rsna-train-test-split-v11092022/test_df.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-11T17:26:37.551912Z","iopub.execute_input":"2022-09-11T17:26:37.552526Z","iopub.status.idle":"2022-09-11T17:26:37.6269Z","shell.execute_reply.started":"2022-09-11T17:26:37.55249Z","shell.execute_reply":"2022-09-11T17:26:37.626153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# raw_train_df = raw_train_df.drop('Unnamed: 0', axis = 1)\n# valid_df = valid_df.drop('Unnamed: 0', axis = 1)\n# test_df = test_df.drop('Unnamed: 0', axis = 1)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T17:26:37.628315Z","iopub.execute_input":"2022-09-11T17:26:37.628561Z","iopub.status.idle":"2022-09-11T17:26:37.636044Z","shell.execute_reply.started":"2022-09-11T17:26:37.628527Z","shell.execute_reply":"2022-09-11T17:26:37.635236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#working_df = working_df.drop('Unnamed: 0', axis = 1)\n#working_df['new_class'] = working_df['class'].map({'No Lung Opacity / Not Normal' : 'Non-Pneumonia', 'Normal':'Non-Pneumonia','Lung Opacity': 'Pneumonia' })","metadata":{"id":"fhioZeGeUIf8","execution":{"iopub.status.busy":"2022-09-11T16:58:01.30794Z","iopub.status.idle":"2022-09-11T16:58:01.308582Z","shell.execute_reply.started":"2022-09-11T16:58:01.308351Z","shell.execute_reply":"2022-09-11T16:58:01.308375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"2p3KOlZ1bWov","outputId":"a8043336-908f-45e8-d29a-7bb890caa232","execution":{"iopub.status.busy":"2022-05-18T08:51:12.705762Z","iopub.execute_input":"2022-05-18T08:51:12.706281Z","iopub.status.idle":"2022-05-18T08:51:12.709605Z","shell.execute_reply.started":"2022-05-18T08:51:12.706244Z","shell.execute_reply":"2022-05-18T08:51:12.708679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# /kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/0004cfab-14fd-4e49-80ba-63a80b6bddd6.dcm","metadata":{"id":"blbK21LnXQMX","execution":{"iopub.status.busy":"2022-05-18T08:51:12.938358Z","iopub.execute_input":"2022-05-18T08:51:12.938886Z","iopub.status.idle":"2022-05-18T08:51:12.943587Z","shell.execute_reply.started":"2022-05-18T08:51:12.938851Z","shell.execute_reply":"2022-05-18T08:51:12.942824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    # keras 2.2\n    import keras_preprocessing.image.utils as KPImageUtils\n    import keras_preprocessing.image as KPImage\nexcept:\n    # keras 2.1\n    import keras.preprocessing.image as KPImage\n    \nfrom PIL import Image\nimport pydicom\ndef read_dicom_image(in_path):\n    img_arr = pydicom.read_file(in_path).pixel_array\n    return img_arr/img_arr.max()\n    \nclass medical_pil():\n    @staticmethod\n    def open(in_path):\n        if '.dcm' in in_path:\n            c_slice = read_dicom_image(in_path)\n            int_slice =  (255*c_slice).clip(0, 255).astype(np.uint8) # 8bit images are more friendly\n            return Image.fromarray(int_slice)\n        else:\n            return Image.open(in_path)\n    fromarray = Image.fromarray\nKPImageUtils.pil_image = medical_pil","metadata":{"id":"UaA6281JUVJq","execution":{"iopub.status.busy":"2024-04-16T01:20:17.886599Z","iopub.execute_input":"2024-04-16T01:20:17.887239Z","iopub.status.idle":"2024-04-16T01:20:17.913598Z","shell.execute_reply.started":"2024-04-16T01:20:17.887198Z","shell.execute_reply":"2024-04-16T01:20:17.912954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"DJ-bIBnoUeyg","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#KPImage.pil_image = medical_pil","metadata":{"id":"uP9esV3Q--ok","execution":{"iopub.status.busy":"2024-04-16T01:20:17.915756Z","iopub.execute_input":"2024-04-16T01:20:17.915994Z","iopub.status.idle":"2024-04-16T01:20:17.919675Z","shell.execute_reply.started":"2024-04-16T01:20:17.915965Z","shell.execute_reply":"2024-04-16T01:20:17.918840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nKPImageUtils._PIL_INTERPOLATION_METHODS = {\n        'nearest': Image.NEAREST,\n        'bilinear': Image.BILINEAR,\n        'bicubic': Image.BICUBIC,\n    }\n\ndef load_img(path, grayscale=False, color_mode='rgb', target_size=None,\n                 interpolation='nearest'):\n        \"\"\"Loads an image into PIL format.\n        # Arguments\n            path: Path to image file.\n            grayscale: DEPRECATED use `color_mode=\"grayscale\"`.\n            color_mode: The desired image format. One of \"grayscale\", \"rgb\", \"rgba\".\n                \"grayscale\" supports 8-bit images and 32-bit signed integer images.\n                Default: \"rgb\".\n            target_size: Either `None` (default to original size)\n                or tuple of ints `(img_height, img_width)`.\n            interpolation: Interpolation method used to resample the image if the\n                target size is different from that of the loaded image.\n                Supported methods are \"nearest\", \"bilinear\", and \"bicubic\".\n                If PIL version 1.1.3 or newer is installed, \"lanczos\" is also\n                supported. If PIL version 3.4.0 or newer is installed, \"box\" and\n                \"hamming\" are also supported.\n                Default: \"nearest\".\n        # Returns\n            A PIL Image instance.\n        # Raises\n            ImportError: if PIL is not available.\n            ValueError: if interpolation method is not supported.\n        \"\"\"\n        if grayscale is True:\n            warnings.warn('grayscale is deprecated. Please use '\n                          'color_mode = \"grayscale\"')\n            color_mode = 'grayscale'\n        if KPImageUtils.pil_image is None:\n            raise ImportError('Could not import PIL.Image. '\n                              'The use of `load_img` requires PIL.')\n        with open(path, 'rb') as f:\n            img = KPImageUtils.pil_image.open(path)\n            if color_mode == 'grayscale':\n                # if image is not already an 8-bit, 16-bit or 32-bit grayscale image\n                # convert it to an 8-bit grayscale image.\n                if img.mode not in ('L', 'I;16', 'I'):\n                    img = img.convert('L')\n            elif color_mode == 'rgba':\n                if img.mode != 'RGBA':\n                    img = img.convert('RGBA')\n            elif color_mode == 'rgb':\n                if img.mode != 'RGB':\n                    img = img.convert('RGB')\n            else:\n                raise ValueError('color_mode must be \"grayscale\", \"rgb\", or \"rgba\"')\n            if target_size is not None:\n                width_height_tuple = (target_size[1], target_size[0])\n                if img.size != width_height_tuple:\n                    if interpolation not in KPImageUtils._PIL_INTERPOLATION_METHODS:\n                        raise ValueError(\n                            'Invalid interpolation method {} specified. Supported '\n                            'methods are {}'.format(\n                                interpolation,\n                                \", \".join(KPImageUtils._PIL_INTERPOLATION_METHODS.keys())))\n                    resample = KPImageUtils._PIL_INTERPOLATION_METHODS[interpolation]\n                    img = img.resize(width_height_tuple, resample)\n            return img\n\n        \nKPImageUtils.load_img = load_img        \ndef _get_batches_of_transformed_samples(self, index_array):\n    \"\"\"Gets a batch of transformed samples.\n    # Arguments\n        index_array: Array of sample indices to include in batch.\n    # Returns\n        A batch of transformed samples.\n    \"\"\"\n    #for i, n_observation in enumerate(index_array):\n     #   print(str(i) + \"+ \" + str( n_observation))\n    batch_x = np.zeros((len(index_array),) + self.image_shape, dtype=self.dtype)\n    # build batch of image data\n    # self.filepaths is dynamic, is better to call it once outside the loop\n    filepaths = self.filepaths\n    for i, j in enumerate(index_array):\n        img = KPImageUtils.load_img(filepaths[j],\n                       color_mode=self.color_mode,\n                       target_size=self.target_size,\n                       interpolation=self.interpolation)\n        x = KPImageUtils.img_to_array(img, data_format=self.data_format)\n        # Pillow images should be closed after `load_img`,\n        # but not PIL images.\n        if hasattr(img, 'close'):\n            img.close()\n        if self.image_data_generator:\n            params = self.image_data_generator.get_random_transform(x.shape)\n            x = self.image_data_generator.apply_transform(x, params)\n            x = self.image_data_generator.standardize(x)\n        batch_x[i] = x\n    # optionally save augmented images to disk for debugging purposes\n    if self.save_to_dir:\n        for i, j in enumerate(index_array):\n            img = KPImageUtils.array_to_img(batch_x[i], self.data_format, scale=True)\n            fname = '{prefix}_{index}_{hash}.{format}'.format(\n                prefix=self.save_prefix,\n                index=j,\n                hash=np.random.randint(1e7),\n                format=self.save_format)\n            img.save(os.path.join(self.save_to_dir, fname))\n    # build batch of labels\n    if self.class_mode == 'input':\n        batch_y = batch_x.copy()\n    elif self.class_mode in {'binary', 'sparse'}:\n        #print(str(self.classes.shape))\n        #print(str(len(batch_x)))\n        try:\n            batch_y = np.empty([len(batch_x), self.classes.shape[1]], dtype=self.dtype)\n        except:\n            batch_y = np.empty([len(batch_x), 1], dtype=self.dtype)\n        #print(batch_y)\n        \n        #print(str(len(index_array)))\n        \n        #print(str(batch_y.shape))\n        \n        for i, n_observation in enumerate(index_array):\n            #print(self.classes[n_observation])\n            #print(batch_y[i])\n            batch_y[i] = self.classes[n_observation]\n    elif self.class_mode == 'categorical':\n        batch_y = np.zeros((len(batch_x), len(self.class_indices)),\n                           dtype=self.dtype)\n        for i, n_observation in enumerate(index_array):\n            batch_y[i, self.classes[n_observation]] = 1.\n    elif self.class_mode == 'multi_output':\n        batch_y = [output[index_array] for output in self.labels]\n    elif self.class_mode == 'raw':\n        batch_y = self.labels[index_array]\n    else:\n        return batch_x\n    if self.sample_weight is None:\n        return batch_x, batch_y\n    else:\n        return batch_x, batch_y, self.sample_weight[index_array]","metadata":{"id":"TzppJDZJUe0O","execution":{"iopub.status.busy":"2024-04-16T01:20:17.921026Z","iopub.execute_input":"2024-04-16T01:20:17.921310Z","iopub.status.idle":"2024-04-16T01:20:17.950976Z","shell.execute_reply.started":"2024-04-16T01:20:17.921273Z","shell.execute_reply":"2024-04-16T01:20:17.950108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras_preprocessing.image.iterator import BatchFromFilesMixin\nBatchFromFilesMixin._get_batches_of_transformed_samples = _get_batches_of_transformed_samples","metadata":{"id":"nw4KYx9OUe3l","execution":{"iopub.status.busy":"2024-04-16T01:20:17.951943Z","iopub.execute_input":"2024-04-16T01:20:17.952165Z","iopub.status.idle":"2024-04-16T01:20:17.964866Z","shell.execute_reply.started":"2024-04-16T01:20:17.952131Z","shell.execute_reply":"2024-04-16T01:20:17.964113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For keras:","metadata":{"id":"O4i0nosp9q2N"}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nif BASE_MODEL=='VGG16':\n    from keras.applications.vgg16 import VGG16 as PTModel, preprocess_input\nelif BASE_MODEL=='RESNET52':\n    from keras.applications.resnet50 import ResNet50 as PTModel, preprocess_input\nelif BASE_MODEL=='InceptionV3':\n    from keras.applications.inception_v3 import InceptionV3 as PTModel, preprocess_input\nelif BASE_MODEL=='Xception':\n    from keras.applications.xception import Xception as PTModel, preprocess_input\nelif BASE_MODEL=='DenseNet169': \n    from keras.applications.densenet import DenseNet169 as PTModel, preprocess_input\nelif BASE_MODEL=='DenseNet121':\n    from keras.applications.densenet import DenseNet121 as PTModel, preprocess_input\nelif BASE_MODEL=='ResNet50':\n    from keras.applications.resnet50 import ResNet50 as PTModel, preprocess_input\nelif BASE_MODEL=='ResNet101':\n    from keras.applications.resnet import ResNet101 as PTModel, preprocess_input\nelif BASE_MODEL=='DenseNet201':\n    from keras.applications import DenseNet201 as PTModel\n    from keras.applications.densenet import  preprocess_input\nelse:\n    raise ValueError('Unknown model: {}'.format(BASE_MODEL))","metadata":{"id":"jXoaPA-BUe5N","execution":{"iopub.status.busy":"2024-04-16T02:40:10.349535Z","iopub.execute_input":"2024-04-16T02:40:10.350185Z","iopub.status.idle":"2024-04-16T02:40:10.359199Z","shell.execute_reply.started":"2024-04-16T02:40:10.350148Z","shell.execute_reply":"2024-04-16T02:40:10.358282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Image Augmentation","metadata":{"id":"igNgZfWwf0Ft"}},{"cell_type":"markdown","source":"For Tensorflow","metadata":{"id":"z3He3SwV9ueG"}},{"cell_type":"code","source":"'''from tensorflow.keras.preprocessing.image import ImageDataGenerator\nif BASE_MODEL=='VGG16':\n    from tensorflow.keras.applications.vgg16 import VGG16 as PTModel, preprocess_input\nelif BASE_MODEL=='RESNET52':\n    from tensorflow.keras.applications.resnet50 import ResNet50 as PTModel, preprocess_input\nelif BASE_MODEL=='InceptionV3':\n    from tensorflow.keras.applications.inception_v3 import InceptionV3 as PTModel, preprocess_input\nelif BASE_MODEL=='Xception':\n    from tensorflow.keras.applications.xception import Xception as PTModel, preprocess_input\nelif BASE_MODEL=='DenseNet169': \n    from tensorflow.keras.applications.densenet import DenseNet169 as PTModel, preprocess_input\nelif BASE_MODEL=='DenseNet121':\n    from tensorflow.keras.applications.densenet import DenseNet121 as PTModel, preprocess_input\nelif BASE_MODEL=='ResNet50':\n    from tensorflow.keras.applications.resnet50 import ResNet50 as PTModel, preprocess_input\nelif BASE_MODEL=='ResNet101':\n    from tensorflow.keras.applications.resnet import ResNet101 as PTModel, preprocess_input\nelif BASE_MODEL=='DenseNet201':\n    from tensorflow.keras.applications import DenseNet201 as PTModel\n    from tensorflow.keras.applications.densenet import  preprocess_input\nelse:\n    raise ValueError('Unknown model: {}'.format(BASE_MODEL))'''","metadata":{"id":"cxyX4LBT9w4S","execution":{"iopub.status.busy":"2024-04-16T01:20:22.665937Z","iopub.execute_input":"2024-04-16T01:20:22.666174Z","iopub.status.idle":"2024-04-16T01:20:22.673290Z","shell.execute_reply.started":"2024-04-16T01:20:22.666145Z","shell.execute_reply":"2024-04-16T01:20:22.672399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_gen_args = dict(samplewise_center=False, \n                              samplewise_std_normalization=False, \n                              horizontal_flip = True, \n                              vertical_flip = False, \n                              height_shift_range = 0.2, #0.05, \n                              width_shift_range = 0.15, #0.02, \n                              rotation_range = 45, #3 \n                              shear_range = 16, #0.01,\n                              fill_mode = 'nearest',\n                              zoom_range=0.2, #[0.8, 1.5], #, #0.05,\n                              preprocessing_function=preprocess_input,) # preprocessing_function=preprocess_input ###from vgg16 inplace of rescale \nimg_gen = ImageDataGenerator(**img_gen_args)\nimg_gen_test = ImageDataGenerator(preprocessing_function=preprocess_input)","metadata":{"id":"33Tw1WX7yqKc","execution":{"iopub.status.busy":"2024-04-16T01:20:22.674584Z","iopub.execute_input":"2024-04-16T01:20:22.674858Z","iopub.status.idle":"2024-04-16T01:20:22.693264Z","shell.execute_reply.started":"2024-04-16T01:20:22.674820Z","shell.execute_reply":"2024-04-16T01:20:22.692489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef flow_from_dataframe(img_data_gen, in_df, path_col, y_col, seed = None, **dflow_args):\n    base_dir = os.path.dirname(in_df[path_col].values[0])\n    print('## Ignore next message from keras, values are replaced anyways: seed: {}'.format(seed))\n    df_gen = img_data_gen.flow_from_directory(base_dir, \n                                     class_mode = 'binary',\n                                              seed = seed,\n                                    **dflow_args)\n    df_gen.filenames = in_df[path_col].values\n    df_gen.classes = np.stack(in_df[y_col].values,0)\n    df_gen.filepaths.extend(df_gen.filenames)\n    df_gen.samples = in_df.shape[0]\n    df_gen.n = in_df.shape[0]\n    df_gen._set_index_array()\n    df_gen.directory = '' # since we have the full path\n    print('Reinserting dataframe: {} images'.format(in_df.shape[0]))\n    return df_gen","metadata":{"id":"e2jM6n8EU7qa","execution":{"iopub.status.busy":"2024-04-16T01:20:22.694302Z","iopub.execute_input":"2024-04-16T01:20:22.694511Z","iopub.status.idle":"2024-04-16T01:20:22.708371Z","shell.execute_reply.started":"2024-04-16T01:20:22.694485Z","shell.execute_reply":"2024-04-16T01:20:22.707719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"id":"29MRwn9PwCx9","outputId":"3d3142a0-7796-499d-dc8e-6eac66594fc1","execution":{"iopub.status.busy":"2024-04-16T01:20:22.709308Z","iopub.execute_input":"2024-04-16T01:20:22.709498Z","iopub.status.idle":"2024-04-16T01:20:22.734356Z","shell.execute_reply.started":"2024-04-16T01:20:22.709474Z","shell.execute_reply":"2024-04-16T01:20:22.733592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = flow_from_dataframe(img_gen, train_df, #raw train df if use unbalance data\n                             path_col = 'path',\n                            y_col = 'class_vec', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb',\n                            batch_size = BATCH_SIZE)\n\nvalid_gen = flow_from_dataframe(img_gen, valid_df, \n                             path_col = 'path',\n                            y_col = 'class_vec', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb',\n                            batch_size = 256) # we can use much larger batches for evaluation 256\n# used a fixed dataset for evaluating the algorithm\nvalid_X, valid_Y = next(flow_from_dataframe(img_gen, \n                               valid_df, \n                             path_col = 'path',\n                            y_col = 'class_vec', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb', #rgb\n                            batch_size = valid_df.shape[0])) # one big batch\n\ntest_gen = flow_from_dataframe(img_gen_test, test_df, \n                             path_col = 'path',\n                            y_col = 'class_vec', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb',\n                            batch_size = 256) # we can use much larger batches for evaluation 256\n# used a fixed dataset for evaluating the algorithm\ntest_X, test_Y = next(flow_from_dataframe(img_gen_test, \n                               test_df, \n                             path_col = 'path',\n                            y_col = 'class_vec', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb', #rgb\n                            batch_size = test_df.shape[0])) # one big batch\n                         ","metadata":{"id":"Mn_NvP3zU7uj","outputId":"316eb6ed-ae1e-4f1f-d47e-6b003791eba8","execution":{"iopub.status.busy":"2024-04-16T01:20:22.735821Z","iopub.execute_input":"2024-04-16T01:20:22.736159Z","iopub.status.idle":"2024-04-16T01:23:34.130198Z","shell.execute_reply.started":"2024-04-16T01:20:22.736120Z","shell.execute_reply":"2024-04-16T01:23:34.129325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_x, t_y = next(train_gen)\nprint(t_x.shape, t_y.shape)","metadata":{"id":"sKlLOe11VAKd","execution":{"iopub.status.busy":"2024-04-16T01:23:34.131480Z","iopub.execute_input":"2024-04-16T01:23:34.131757Z","iopub.status.idle":"2024-04-16T01:23:34.794077Z","shell.execute_reply.started":"2024-04-16T01:23:34.131720Z","shell.execute_reply":"2024-04-16T01:23:34.793191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen.classes","metadata":{"id":"3F7o1MwC-TFq","outputId":"398715ed-6074-490f-b828-6f7267d86500","execution":{"iopub.status.busy":"2022-05-18T15:37:51.908104Z","iopub.execute_input":"2022-05-18T15:37:51.908421Z","iopub.status.idle":"2022-05-18T15:37:51.916038Z","shell.execute_reply.started":"2022-05-18T15:37:51.908371Z","shell.execute_reply":"2022-05-18T15:37:51.914948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras.models import Sequential\n# from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n\n# # Define the model\n# small_cnn_model = Sequential()\n\n# # Convolutional layers\n# small_cnn_model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)))\n# small_cnn_model.add(MaxPooling2D((2, 2)))\n\n# small_cnn_model.add(Conv2D(64, (3, 3), activation='relu'))\n# small_cnn_model.add(MaxPooling2D((2, 2)))\n\n# small_cnn_model.add(Conv2D(128, (3, 3), activation='relu'))\n# small_cnn_model.add(MaxPooling2D((2, 2)))\n\n# # Flatten layer to transition from convolutional to dense layers\n# small_cnn_model.add(Flatten())\n\n# # Dense layers\n# small_cnn_model.add(Dense(512, activation='relu'))\n# small_cnn_model.add(Dropout(0.5))\n# small_cnn_model.add(Dense(2, activation='softmax'))  # Assuming binary classification\n\n# # Compile the model\n# small_cnn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\n# # Summary of the model\n# small_cnn_model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-28T05:23:51.506604Z","iopub.execute_input":"2024-03-28T05:23:51.507007Z","iopub.status.idle":"2024-03-28T05:23:51.619440Z","shell.execute_reply.started":"2024-03-28T05:23:51.506953Z","shell.execute_reply":"2024-03-28T05:23:51.618286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras.utils.vis_utils import plot_model\n# # Assuming you've defined your model as 'small_cnn_model' in the previous code\n\n# # Plot the model architecture\n# plot_model(small_cnn_model, to_file='small_cnn_model.png', show_shapes=True, show_layer_names=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-28T06:09:27.797932Z","iopub.execute_input":"2024-03-28T06:09:27.798940Z","iopub.status.idle":"2024-03-28T06:09:29.122662Z","shell.execute_reply.started":"2024-03-28T06:09:27.798882Z","shell.execute_reply":"2024-03-28T06:09:29.121618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python --version","metadata":{"id":"Hxi31O5wf09Z","outputId":"e6c6b768-eedd-460e-a29c-c23d774913bc","execution":{"iopub.status.busy":"2022-05-18T08:53:35.899627Z","iopub.execute_input":"2022-05-18T08:53:35.90002Z","iopub.status.idle":"2022-05-18T08:53:36.59153Z","shell.execute_reply.started":"2022-05-18T08:53:35.899982Z","shell.execute_reply":"2022-05-18T08:53:36.590739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import __version__","metadata":{"id":"3CDxdxEc0SDd","execution":{"iopub.status.busy":"2022-05-18T08:53:36.594296Z","iopub.execute_input":"2022-05-18T08:53:36.594582Z","iopub.status.idle":"2022-05-18T08:53:36.598154Z","shell.execute_reply.started":"2022-05-18T08:53:36.594547Z","shell.execute_reply":"2022-05-18T08:53:36.59732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"__version__","metadata":{"id":"0SLRgJxa0UL_","outputId":"b095bf75-d46a-4dec-c870-0de8cba4782a","execution":{"iopub.status.busy":"2022-05-18T08:53:36.59945Z","iopub.execute_input":"2022-05-18T08:53:36.600006Z","iopub.status.idle":"2022-05-18T08:53:36.609222Z","shell.execute_reply.started":"2022-05-18T08:53:36.599967Z","shell.execute_reply":"2022-05-18T08:53:36.608419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import rmsprop_v2","metadata":{"id":"318G53U77viv","outputId":"a352f5e1-32ca-4024-e3f6-14bdd60dc746","execution":{"iopub.status.busy":"2024-04-16T01:32:58.892877Z","iopub.execute_input":"2024-04-16T01:32:58.893593Z","iopub.status.idle":"2024-04-16T01:32:58.898068Z","shell.execute_reply.started":"2024-04-16T01:32:58.893548Z","shell.execute_reply":"2024-04-16T01:32:58.897071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Model architecture","metadata":{"id":"wZvRRaYff33p"}},{"cell_type":"code","source":"from keras.layers import GlobalAveragePooling2D, GlobalMaxPooling2D, Dense, Dropout, Flatten, Input, Conv2D, multiply, LocallyConnected2D, Lambda, AvgPool2D, LeakyReLU\nfrom keras.models import Model\n\nfrom keras import regularizers\nfrom keras.models import Sequential\n#from keras.optimizers import Adam,  RMSprop\nfrom keras.optimizers import adam_v2,  rmsprop_v2\n#with strategy.scope():\nbase_pretrained_model = PTModel(input_shape =  t_x.shape[1:], \n                          include_top = False, weights = 'imagenet')\n\n\n","metadata":{"id":"4ov2WkH5i36i","outputId":"153125ac-5e9b-480f-c599-e676e8e567eb","execution":{"iopub.status.busy":"2024-04-16T02:40:24.896854Z","iopub.execute_input":"2024-04-16T02:40:24.897157Z","iopub.status.idle":"2024-04-16T02:40:27.652284Z","shell.execute_reply.started":"2024-04-16T02:40:24.897122Z","shell.execute_reply":"2024-04-16T02:40:27.651630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"nFFSbYdg7J7O"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for layer in base_pretrained_model.layers:\n#   if layer.output.shape[1:3] == [7,7] : #or layer.output.shape[1:3] == [14,14] :\n#     layer.trainable = True\n#   else :\n#     layer.trainable = False","metadata":{"id":"bjz8y99t6ojS","execution":{"iopub.status.busy":"2024-04-16T01:33:03.164416Z","iopub.execute_input":"2024-04-16T01:33:03.164653Z","iopub.status.idle":"2024-04-16T01:33:03.168085Z","shell.execute_reply.started":"2024-04-16T01:33:03.164623Z","shell.execute_reply":"2024-04-16T01:33:03.167363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#base_pretrained_model.layers[-288].trainable","metadata":{"id":"BospVCc75mMi","execution":{"iopub.status.busy":"2024-04-16T01:33:03.169486Z","iopub.execute_input":"2024-04-16T01:33:03.169711Z","iopub.status.idle":"2024-04-16T01:33:03.181874Z","shell.execute_reply.started":"2024-04-16T01:33:03.169682Z","shell.execute_reply":"2024-04-16T01:33:03.181291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#base_pretrained_model.trainable = False","metadata":{"id":"U3cEelId5P7U","execution":{"iopub.status.busy":"2024-04-16T01:33:03.183620Z","iopub.execute_input":"2024-04-16T01:33:03.183988Z","iopub.status.idle":"2024-04-16T01:33:03.191333Z","shell.execute_reply.started":"2024-04-16T01:33:03.183949Z","shell.execute_reply":"2024-04-16T01:33:03.190756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.vis_utils import plot_model\n# Assuming you've defined your model as 'small_cnn_model' in the previous code\n\n# Plot the model architecture\nplot_model(base_pretrained_model, to_file='DenseNet121_cnn_model.png', show_shapes=True, show_layer_names=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-16T02:40:44.634644Z","iopub.execute_input":"2024-04-16T02:40:44.635439Z","iopub.status.idle":"2024-04-16T02:40:47.992124Z","shell.execute_reply.started":"2024-04-16T02:40:44.635384Z","shell.execute_reply":"2024-04-16T02:40:47.991186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_pretrained_model.summary()","metadata":{"id":"Buit1gZlvuGR","outputId":"0f841dc3-e14a-46df-b40e-2f606b282471","execution":{"iopub.status.busy":"2024-04-16T02:40:47.994629Z","iopub.execute_input":"2024-04-16T02:40:47.995039Z","iopub.status.idle":"2024-04-16T02:40:48.194256Z","shell.execute_reply.started":"2024-04-16T02:40:47.994986Z","shell.execute_reply":"2024-04-16T02:40:48.193473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DROPOUT = 0.3","metadata":{"execution":{"iopub.status.busy":"2024-04-16T02:41:02.370328Z","iopub.execute_input":"2024-04-16T02:41:02.370866Z","iopub.status.idle":"2024-04-16T02:41:02.374485Z","shell.execute_reply.started":"2024-04-16T02:41:02.370827Z","shell.execute_reply":"2024-04-16T02:41:02.373685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npt_features = Input(base_pretrained_model.layers[-1].output.shape[1:], name = 'feature_input')\npt_depth = base_pretrained_model.layers[-1].output.shape[1:]\n\nfrom keras.layers import BatchNormalization\n\nbn_features = BatchNormalization()(pt_features)\ngap = GlobalAveragePooling2D()(bn_features)\n\ngap_dr = Dropout(DROPOUT)(gap)\ndr_steps = Dropout(DROPOUT)(Dense(DENSE_COUNT, activation = 'elu')(gap_dr))\n\n\nout_layer = Dense(t_y.shape[1], activation = 'softmax')(dr_steps)\n\nattn_model = Model(inputs = [pt_features], \n                   outputs = [out_layer], name = 'trained_model')\npneu_model = Sequential(name = 'combined_model')\n#base_pretrained_model.trainable = False\npneu_model.add(base_pretrained_model)\npneu_model.add(attn_model)\npneu_model.compile(optimizer = rmsprop_v2.RMSprop(learning_rate=0.0001, decay=1e-5), loss = 'binary_crossentropy', #Adam(lr = LEARN_RATE)\n                           metrics = ['binary_accuracy'])\n#pneu_model.build((None, 224, 224, 3))\npneu_model.summary()","metadata":{"id":"vh1ug9NGVAMM","outputId":"45010555-abda-47d6-cd5c-b94901fdf03c","execution":{"iopub.status.busy":"2024-04-16T02:41:02.585466Z","iopub.execute_input":"2024-04-16T02:41:02.585794Z","iopub.status.idle":"2024-04-16T02:41:03.517725Z","shell.execute_reply.started":"2024-04-16T02:41:02.585745Z","shell.execute_reply":"2024-04-16T02:41:03.516782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.vis_utils import plot_model\n# Assuming you've defined your model as 'small_cnn_model' in the previous code\n\n# Plot the model architecture\nplot_model(pneu_model, to_file='vgg16_cnn_model.png', show_shapes=True, show_layer_names=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-16T01:33:15.374711Z","iopub.execute_input":"2024-04-16T01:33:15.375487Z","iopub.status.idle":"2024-04-16T01:33:15.585018Z","shell.execute_reply.started":"2024-04-16T01:33:15.375447Z","shell.execute_reply":"2024-04-16T01:33:15.583965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping, ReduceLROnPlateau\nweight_path=\"{}_weights.best.hdf5\".format('lung_opacity')\n\ncheckpoint = ModelCheckpoint(weight_path, monitor='val_loss', verbose=1, \n                             save_best_only=True, mode='min', save_weights_only = True)\n\nreduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.8, \n                                   patience=10, verbose=1, mode='auto', \n                                   min_delta=0.0001, cooldown=5, min_lr=0.0001)\nearly = EarlyStopping(monitor=\"val_loss\", \n                      mode=\"min\", \n                      patience=10) # probably needs to be more patient, but kaggle time is limited\ncallbacks_list = [checkpoint, early, reduceLROnPlat]","metadata":{"id":"avhrYGCLVAPv","execution":{"iopub.status.busy":"2024-04-16T02:41:12.680861Z","iopub.execute_input":"2024-04-16T02:41:12.681518Z","iopub.status.idle":"2024-04-16T02:41:12.688097Z","shell.execute_reply.started":"2024-04-16T02:41:12.681474Z","shell.execute_reply":"2024-04-16T02:41:12.687356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen.batch_size = BATCH_SIZE\nhistory = pneu_model.fit_generator(train_gen, \n                         steps_per_epoch=train_gen.n//BATCH_SIZE,\n                         validation_data=(valid_X, valid_Y), \n                         epochs=20, \n                         callbacks=callbacks_list,\n                         workers=2)","metadata":{"id":"FfBFu_0PVARZ","outputId":"841f7f2a-2304-4621-8550-08dd73315cd4","execution":{"iopub.status.busy":"2024-04-16T02:41:13.727593Z","iopub.execute_input":"2024-04-16T02:41:13.727859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\npneu_model = load_model('../input/densenet121-models/densenet121_80-10-10_may_19th_2022_3.h5')\n# pneu_model = load_model('../input/densenet121-models/densenet121_18-11-21.h5')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T17:51:57.259105Z","iopub.execute_input":"2022-09-11T17:51:57.259785Z","iopub.status.idle":"2022-09-11T17:52:02.094989Z","shell.execute_reply.started":"2022-09-11T17:51:57.259746Z","shell.execute_reply":"2022-09-11T17:52:02.094245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\npred_Y = pneu_model.predict(valid_X, \n                          batch_size = BATCH_SIZE,  \n                          verbose = True\n                          ) \n\n","metadata":{"id":"dtDjH6PGVAVA","outputId":"6d215616-7913-4212-9cea-f643e321138f","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npred_Y_2 = pneu_model.predict(test_X, \n                          batch_size = BATCH_SIZE,  \n                          verbose = True\n                          ) \n\n","metadata":{"id":"X1xJLGch8XoM","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"AjWL3cx8hsMF"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\nplt.matshow(confusion_matrix(np.argmax(valid_Y, -1), np.argmax(pred_Y,-1)))\nprint(classification_report(np.argmax(valid_Y, -1), \n                            np.argmax(pred_Y,-1), target_names = ['normal', 'pneumonea']))","metadata":{"id":"42J7pYGJVQlw","outputId":"24f270f9-a2f6-47c3-f881-c81d2ebe7f8d","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history.history.keys())\n# summarize history for accuracy\nplt.plot(history.history['binary_accuracy'])\nplt.plot(history.history['val_binary_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()","metadata":{"id":"1w3rauFmKTgi","outputId":"15a85bcd-6030-4370-8acd-63742d73261c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\nplt.matshow(confusion_matrix(np.argmax(test_Y, -1), np.argmax(pred_Y_2,-1)))\nprint(classification_report(np.argmax(test_Y, -1), \n                            np.argmax(pred_Y_2,-1), target_names = ['normal', 'pneumonea']))","metadata":{"id":"SPthFzwb8oFv","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, roc_auc_score\nfpr, tpr, _ = roc_curve(np.argmax(test_Y,-1)==0, pred_Y_2[:,0])\nfig, ax1 = plt.subplots(1,1, figsize = (5, 5), dpi = 250)\nax1.plot(fpr, tpr, 'b.-', label = 'DenseNet121:%2.2f)' % roc_auc_score(np.argmax(test_Y,-1)==0, pred_Y_2[:,0]))\nax1.plot(fpr, fpr, 'k-', label = 'Random Guessing')\nax1.legend(loc = 4)\nax1.set_xlabel('False Positive Rate')\nax1.set_ylabel('True Positive Rate');\nax1.set_title('Lung Opacity ROC Curve')\nfig.savefig('roc_valid.pdf') ","metadata":{"id":"AwI3zQSXcjGg","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pneu_model.save(\"densenet.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-05-19T17:12:31.853755Z","iopub.execute_input":"2022-05-19T17:12:31.854024Z","iopub.status.idle":"2022-05-19T17:12:32.735885Z","shell.execute_reply.started":"2022-05-19T17:12:31.853997Z","shell.execute_reply":"2022-05-19T17:12:32.734953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pneu_model.evaluate(test_X, test_Y)","metadata":{"id":"VUvBHHMLWgF0","outputId":"ffb9a4b5-1b1c-4568-d0f4-8d1eb837f21f","execution":{"iopub.status.busy":"2022-05-19T17:12:36.947277Z","iopub.execute_input":"2022-05-19T17:12:36.947557Z","iopub.status.idle":"2022-05-19T17:12:37.825795Z","shell.execute_reply.started":"2022-05-19T17:12:36.947516Z","shell.execute_reply":"2022-05-19T17:12:37.825094Z"},"trusted":true},"execution_count":null,"outputs":[]}]}