{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport shutil\nimport pydicom\nimport matplotlib.pyplot as plt\nimport scipy.io\nimport numpy as np\nimport cv2\nfrom PIL import Image\nimport pandas as pd\nimport gc\nfrom tqdm import tqdm\n\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import applications\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout , BatchNormalization\n\nfrom sklearn.model_selection import RepeatedKFold, cross_val_score, train_test_split\nfrom sklearn.metrics import confusion_matrix, accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-04-20T09:09:24.298042Z","iopub.execute_input":"2023-04-20T09:09:24.298573Z","iopub.status.idle":"2023-04-20T09:09:37.121772Z","shell.execute_reply.started":"2023-04-20T09:09:24.298471Z","shell.execute_reply":"2023-04-20T09:09:37.120952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/0003b3d648eb/d2b2960c2bbf/03d7693b0405.dcm\")\ndcm_sample=ds.pixel_array.astype('float32')\nscaled_image = (np.maximum(dcm_sample, 0) / dcm_sample.max())\nplt.imshow(scaled_image)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T09:09:44.791934Z","iopub.execute_input":"2023-04-20T09:09:44.792235Z","iopub.status.idle":"2023-04-20T09:09:45.151886Z","shell.execute_reply.started":"2023-04-20T09:09:44.7922Z","shell.execute_reply":"2023-04-20T09:09:45.151112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#not_noraml\ndf = pd.read_csv(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train.csv\")\ndf = df.loc[df[\"pe_present_on_image\"]==1,:].reset_index(drop=True)\nprint(len(df))\ndf.tail()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T09:09:47.37553Z","iopub.execute_input":"2023-04-20T09:09:47.375884Z","iopub.status.idle":"2023-04-20T09:09:52.436445Z","shell.execute_reply.started":"2023-04-20T09:09:47.375847Z","shell.execute_reply":"2023-04-20T09:09:52.435641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#normal\ndf1 = pd.read_csv(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train.csv\")\ndf1 = df1.loc[(df1[\"pe_present_on_image\"] == 0) & (df1[\"negative_exam_for_pe\"] == 1) ,:].reset_index(drop=True)\nprint(len(df1))\ndf1.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T09:09:52.438382Z","iopub.execute_input":"2023-04-20T09:09:52.438961Z","iopub.status.idle":"2023-04-20T09:09:54.712637Z","shell.execute_reply.started":"2023-04-20T09:09:52.438911Z","shell.execute_reply":"2023-04-20T09:09:54.711878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/data\n\n!mkdir /kaggle/data/train\n!mkdir /kaggle/data/valid\n!mkdir /kaggle/data/test\n\n!mkdir /kaggle/data/train/normal\n!mkdir /kaggle/data/train/not_normal\n\n!mkdir /kaggle/data/valid/normal\n!mkdir /kaggle/data/valid/not_normal\n\n!mkdir /kaggle/data/test/normal\n!mkdir /kaggle/data/test/not_normal","metadata":{"execution":{"iopub.status.busy":"2023-04-20T09:09:56.39176Z","iopub.execute_input":"2023-04-20T09:09:56.392036Z","iopub.status.idle":"2023-04-20T09:10:06.918818Z","shell.execute_reply.started":"2023-04-20T09:09:56.392006Z","shell.execute_reply":"2023-04-20T09:10:06.917701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train not_normal\nfor i in tqdm(range(10000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df.loc[i,'StudyInstanceUID']+'/'+df.loc[i,'SeriesInstanceUID']+'/'+df.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/train/not_normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T09:13:02.970231Z","iopub.execute_input":"2023-04-20T09:13:02.970537Z","iopub.status.idle":"2023-04-20T09:19:05.899303Z","shell.execute_reply.started":"2023-04-20T09:13:02.970502Z","shell.execute_reply":"2023-04-20T09:19:05.898094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train normal\nfor i in tqdm(range(10000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df1.loc[i,'StudyInstanceUID']+'/'+df1.loc[i,'SeriesInstanceUID']+'/'+df1.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image = dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/train/normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T07:22:53.598624Z","iopub.execute_input":"2023-04-20T07:22:53.598926Z","iopub.status.idle":"2023-04-20T07:57:28.918887Z","shell.execute_reply.started":"2023-04-20T07:22:53.598895Z","shell.execute_reply":"2023-04-20T07:57:28.918178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Valid not_normal\nfor i in tqdm(range(10000,12000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df.loc[i,'StudyInstanceUID']+'/'+df.loc[i,'SeriesInstanceUID']+'/'+df.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/valid/not_normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T07:57:34.500211Z","iopub.execute_input":"2023-04-20T07:57:34.500559Z","iopub.status.idle":"2023-04-20T08:04:30.726206Z","shell.execute_reply.started":"2023-04-20T07:57:34.50052Z","shell.execute_reply":"2023-04-20T08:04:30.725275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Valid normal\nfor i in tqdm(range(10000,12000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df1.loc[i,'StudyInstanceUID']+'/'+df1.loc[i,'SeriesInstanceUID']+'/'+df1.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/valid/normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:05:01.856143Z","iopub.execute_input":"2023-04-20T08:05:01.856493Z","iopub.status.idle":"2023-04-20T08:11:59.536616Z","shell.execute_reply.started":"2023-04-20T08:05:01.85644Z","shell.execute_reply":"2023-04-20T08:11:59.535723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Test not_normal\nfor i in tqdm(range(12000,14000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df.loc[i,'StudyInstanceUID']+'/'+df.loc[i,'SeriesInstanceUID']+'/'+df.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/test/not_normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:14:03.433433Z","iopub.execute_input":"2023-04-20T08:14:03.434421Z","iopub.status.idle":"2023-04-20T08:21:01.544179Z","shell.execute_reply.started":"2023-04-20T08:14:03.434353Z","shell.execute_reply":"2023-04-20T08:21:01.542865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Test normal\nfor i in tqdm(range(12000,14000)):\n    dcm = pydicom.dcmread(\"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/\"+df1.loc[i,'StudyInstanceUID']+'/'+df1.loc[i,'SeriesInstanceUID']+'/'+df1.loc[i,'SOPInstanceUID']+'.dcm')\n    dc_image=dcm.pixel_array.astype('float32')\n    #scaled_image = (np.maximum(dc_image, 0) / dc_image.max())\n    #scaled_image = np.reshape(scaled_image,(scaled_image.shape[0], scaled_image.shape[1], 1))\n    im = Image.fromarray(dc_image).convert('RGB').resize((256,256))  \n    im.save(\"/kaggle/data/test/normal/\"+str(i)+\".jpg\")\n    del dcm, dc_image, im\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:22:48.502079Z","iopub.execute_input":"2023-04-20T08:22:48.502882Z","iopub.status.idle":"2023-04-20T08:29:46.849082Z","shell.execute_reply.started":"2023-04-20T08:22:48.502837Z","shell.execute_reply":"2023-04-20T08:29:46.848181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n      featurewise_center=False,  \n      samplewise_center=False, \n      featurewise_std_normalization=False,  \n      samplewise_std_normalization=False, \n      rescale=1./255,\n      rotation_range=20,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      shear_range=0.2,\n      zoom_range=0.2,\n      horizontal_flip=True,\n      vertical_flip=True,\n      fill_mode='nearest')","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:29:55.696203Z","iopub.execute_input":"2023-04-20T08:29:55.696485Z","iopub.status.idle":"2023-04-20T08:29:55.703844Z","shell.execute_reply.started":"2023-04-20T08:29:55.696456Z","shell.execute_reply":"2023-04-20T08:29:55.703189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n        '/kaggle/data/train',\n        target_size=(256, 256),\n        batch_size=64,\n        class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:29:58.225784Z","iopub.execute_input":"2023-04-20T08:29:58.226118Z","iopub.status.idle":"2023-04-20T08:29:58.695813Z","shell.execute_reply.started":"2023-04-20T08:29:58.226067Z","shell.execute_reply":"2023-04-20T08:29:58.69453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_datagen = ImageDataGenerator(\n      rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:30:05.19066Z","iopub.execute_input":"2023-04-20T08:30:05.191259Z","iopub.status.idle":"2023-04-20T08:30:05.195307Z","shell.execute_reply.started":"2023-04-20T08:30:05.191218Z","shell.execute_reply":"2023-04-20T08:30:05.194675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = valid_datagen.flow_from_directory(\n        '/kaggle/data/valid',\n        target_size=(256, 256),\n        batch_size=64,\n        class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:30:18.92554Z","iopub.execute_input":"2023-04-20T08:30:18.926139Z","iopub.status.idle":"2023-04-20T08:30:19.041072Z","shell.execute_reply.started":"2023-04-20T08:30:18.926087Z","shell.execute_reply":"2023-04-20T08:30:19.040027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(\n      rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:30:20.968557Z","iopub.execute_input":"2023-04-20T08:30:20.968894Z","iopub.status.idle":"2023-04-20T08:30:20.974157Z","shell.execute_reply.started":"2023-04-20T08:30:20.968856Z","shell.execute_reply":"2023-04-20T08:30:20.973389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = valid_datagen.flow_from_directory(\n        '/kaggle/data/test',\n        target_size=(256, 256),\n        batch_size=64,\n        class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:30:30.705447Z","iopub.execute_input":"2023-04-20T08:30:30.706293Z","iopub.status.idle":"2023-04-20T08:30:30.82244Z","shell.execute_reply.started":"2023-04-20T08:30:30.706234Z","shell.execute_reply":"2023-04-20T08:30:30.821464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:30:40.34153Z","iopub.execute_input":"2023-04-20T08:30:40.341966Z","iopub.status.idle":"2023-04-20T08:30:40.34827Z","shell.execute_reply.started":"2023-04-20T08:30:40.341934Z","shell.execute_reply":"2023-04-20T08:30:40.347465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader, Dataset\nfrom torchvision import transforms\n\n# Define the Swin Transformer model\nclass SwinTransformer(nn.Module):\n    def __init__(self, num_classes):\n        super(SwinTransformer, self).__init__()\n        # Define the Swin Transformer architecture\n        # ...\n\n        # Define the final classification layer\n        self.classifier = nn.Linear(hidden_size, num_classes)\n\n    def forward(self, x):\n        # Forward pass through the Swin Transformer layers\n        # ...\n\n        # Flatten the output and pass it through the classifier\n        x = x.flatten(start_dim=1)\n        x = self.classifier(x)\n\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:39:12.622863Z","iopub.execute_input":"2023-04-20T08:39:12.62375Z","iopub.status.idle":"2023-04-20T08:39:12.631882Z","shell.execute_reply.started":"2023-04-20T08:39:12.623707Z","shell.execute_reply":"2023-04-20T08:39:12.63085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:50:09.323978Z","iopub.execute_input":"2023-04-20T08:50:09.32442Z","iopub.status.idle":"2023-04-20T08:50:09.332114Z","shell.execute_reply.started":"2023-04-20T08:50:09.324379Z","shell.execute_reply":"2023-04-20T08:50:09.33121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install --upgrade torchvision","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:58:34.767909Z","iopub.execute_input":"2023-04-20T08:58:34.768251Z","iopub.status.idle":"2023-04-20T09:01:06.670184Z","shell.execute_reply.started":"2023-04-20T08:58:34.768218Z","shell.execute_reply":"2023-04-20T09:01:06.668977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\n# from torchvision.models import swin_t\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-04-20T09:11:54.433751Z","iopub.execute_input":"2023-04-20T09:11:54.434356Z","iopub.status.idle":"2023-04-20T09:11:54.441156Z","shell.execute_reply.started":"2023-04-20T09:11:54.434311Z","shell.execute_reply":"2023-04-20T09:11:54.440437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import resnet50, ResNet50_Weights","metadata":{"execution":{"iopub.status.busy":"2023-04-20T09:11:55.612489Z","iopub.execute_input":"2023-04-20T09:11:55.613307Z","iopub.status.idle":"2023-04-20T09:11:55.638305Z","shell.execute_reply.started":"2023-04-20T09:11:55.613263Z","shell.execute_reply":"2023-04-20T09:11:55.637004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import swin_t","metadata":{"execution":{"iopub.status.busy":"2023-04-20T09:11:57.365223Z","iopub.execute_input":"2023-04-20T09:11:57.366264Z","iopub.status.idle":"2023-04-20T09:11:57.390646Z","shell.execute_reply.started":"2023-04-20T09:11:57.366216Z","shell.execute_reply":"2023-04-20T09:11:57.389512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import swin_t\n\n# Use a pre-trained imagenet\n#model = swin_t(weights='DEFAULT')\n# Use no weights\nmodel = swin_t()\n\n# Update the fully connected layer based on the number of classes in the dataset\n#model.fc = torch.nn.Linear(model.fc.in_features, len(ds_train.labels.info.class_names))\nmodel.head = torch.nn.Linear(out_features=2)\n\nmodel.to(device)\n\nprint(model)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = Sequential()\n# model.add(Conv2D(32 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu' , input_shape = ( 256, 256, 3)))\n# model.add(BatchNormalization())\n# model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\n# model.add(Conv2D(64 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))\n# model.add(Dropout(0.1))\n# model.add(BatchNormalization())\n# model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\n# model.add(Conv2D(64 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))\n# model.add(BatchNormalization())\n# model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\n# model.add(Conv2D(128 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))\n# model.add(Dropout(0.2))\n# model.add(BatchNormalization())\n# model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\n# model.add(Conv2D(256 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))\n# model.add(Dropout(0.2))\n# model.add(BatchNormalization())\n# model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))\n# model.add(Flatten())\n# model.add(Dense(units = 128 , activation = 'relu'))\n# model.add(Dropout(0.2))\n# model.add(Dense(units = 1 , activation = 'sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-03-30T22:39:20.117628Z","iopub.execute_input":"2022-03-30T22:39:20.117888Z","iopub.status.idle":"2022-03-30T22:39:20.285128Z","shell.execute_reply.started":"2022-03-30T22:39:20.117859Z","shell.execute_reply":"2022-03-30T22:39:20.284352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/models","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:32:20.237342Z","iopub.execute_input":"2023-04-20T08:32:20.237626Z","iopub.status.idle":"2023-04-20T08:32:21.353636Z","shell.execute_reply.started":"2023-04-20T08:32:20.237596Z","shell.execute_reply":"2023-04-20T08:32:21.352343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = tf.keras.losses.BinaryCrossentropy()\nmodel.compile(loss=loss, \n              optimizer='Adam', \n              metrics=['binary_accuracy'])\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_binary_accuracy', patience = 2, verbose=1,factor=0.3, min_lr=0.000001)\n\nfilepath = \"/kaggle/models/saved-model-{epoch:02d}-{val_binary_accuracy:.2f}.hdf5\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, \n                             save_best_only=False,save_freq='epoch')\n","metadata":{"execution":{"iopub.status.busy":"2022-03-30T22:39:22.778416Z","iopub.execute_input":"2022-03-30T22:39:22.779012Z","iopub.status.idle":"2022-03-30T22:39:22.794935Z","shell.execute_reply.started":"2022-03-30T22:39:22.778974Z","shell.execute_reply":"2022-03-30T22:39:22.793998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\n      train_generator,\n      epochs=25,\n      validation_data=valid_generator,\n      validation_steps=4,\n      callbacks=[checkpoint,learning_rate_reduction],\n      verbose=1,\n    \n      )","metadata":{"execution":{"iopub.status.busy":"2022-03-30T22:39:24.352003Z","iopub.execute_input":"2022-03-30T22:39:24.352268Z","iopub.status.idle":"2022-03-31T00:23:35.500057Z","shell.execute_reply.started":"2022-03-30T22:39:24.352241Z","shell.execute_reply":"2022-03-31T00:23:35.495335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['binary_accuracy'], label='The score of correct predictions on the training set')\nplt.plot(history.history['val_binary_accuracy'], label='The score of correct predictions on the val set')\nplt.xlabel('Epoch')\nplt.ylabel('Score correct answers')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-31T00:23:35.502988Z","iopub.execute_input":"2022-03-31T00:23:35.503609Z","iopub.status.idle":"2022-03-31T00:23:35.756956Z","shell.execute_reply.started":"2022-03-31T00:23:35.50357Z","shell.execute_reply":"2022-03-31T00:23:35.754581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_acc = 0\nbest_model = \"\"\nfor i in os.listdir(\"/kaggle/models\"):\n    model.load_weights(\"/kaggle/models/\"+i)\n    loss, acc = model.evaluate_generator(test_generator, steps=3, verbose=0)\n    if acc > best_acc:\n        best_model = i\n        best_acc = acc","metadata":{"execution":{"iopub.status.busy":"2022-03-31T00:23:56.898496Z","iopub.execute_input":"2022-03-31T00:23:56.899678Z","iopub.status.idle":"2022-03-31T00:24:16.978947Z","shell.execute_reply.started":"2022-03-31T00:23:56.899636Z","shell.execute_reply":"2022-03-31T00:24:16.978143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights(\"/kaggle/models/\"+best_model)\nloss, acc = model.evaluate_generator(test_generator, steps=3, verbose=0)\nacc = acc *100\nprint(f\"accuracy is: {acc:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2022-03-31T00:27:27.294627Z","iopub.execute_input":"2022-03-31T00:27:27.29547Z","iopub.status.idle":"2022-03-31T00:27:28.263087Z","shell.execute_reply.started":"2022-03-31T00:27:27.295434Z","shell.execute_reply":"2022-03-31T00:27:28.262217Z"},"trusted":true},"execution_count":null,"outputs":[]}]}