{"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 warnings\nwarnings.filterwarnings('ignore')\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport cv2\n\n# from skimage import io\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold\n\nimport torch\nimport torch.nn as nn\nfrom torch.optim.lr_scheduler import ReduceLROnPlateau\nfrom torch.optim.lr_scheduler import OneCycleLR\n\nfrom torch.utils.data import Dataset\nfrom torch.utils.data import DataLoader\n\nimport torchmetrics\nfrom torchmetrics.classification import MulticlassF1Score\n\nfrom torchvision import transforms\nfrom torchvision import models\n\nimport pytorch_lightning as pl\nfrom pytorch_lightning import Trainer\nfrom pytorch_lightning.callbacks import ModelCheckpoint\nfrom pytorch_lightning.loggers import TensorBoardLogger\nfrom pytorch_lightning.callbacks import EarlyStopping\nfrom pytorch_lightning import Trainer, seed_everything\nseed_everything(42, workers=True)\n\nfrom PIL import Image\n","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:30.527696Z","iopub.execute_input":"2023-10-31T06:49:30.528112Z","iopub.status.idle":"2023-10-31T06:49:47.542034Z","shell.execute_reply.started":"2023-10-31T06:49:30.528067Z","shell.execute_reply":"2023-10-31T06:49:47.541199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/UBC-OCEAN/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:47.543825Z","iopub.execute_input":"2023-10-31T06:49:47.544181Z","iopub.status.idle":"2023-10-31T06:49:47.556693Z","shell.execute_reply.started":"2023-10-31T06:49:47.544152Z","shell.execute_reply":"2023-10-31T06:49:47.555586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:47.557981Z","iopub.execute_input":"2023-10-31T06:49:47.55836Z","iopub.status.idle":"2023-10-31T06:49:47.577087Z","shell.execute_reply.started":"2023-10-31T06:49:47.558331Z","shell.execute_reply":"2023-10-31T06:49:47.576149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.tail","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:47.57996Z","iopub.execute_input":"2023-10-31T06:49:47.580493Z","iopub.status.idle":"2023-10-31T06:49:47.587369Z","shell.execute_reply.started":"2023-10-31T06:49:47.58046Z","shell.execute_reply":"2023-10-31T06:49:47.586391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_THUMBNAILS = '/kaggle/input/UBC-OCEAN/test_thumbnails'\nTEST_IMAGES = '/kaggle/input/UBC-OCEAN/test_images'\n\ndef get_file_path(image_id):\n    if os.path.exists(f\"{TEST_THUMBNAILS}/{image_id}_thumbnail.png\"):\n        return f\"{TEST_THUMBNAILS}/{image_id}_thumbnail.png\"\n    else:\n        return f\"{TEST_IMAGES}/{image_id}.png\"\n    \n    \ntest_df['file_path'] = test_df['image_id'].apply(get_file_path)\ntest_df['label'] = 0","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:47.588713Z","iopub.execute_input":"2023-10-31T06:49:47.589057Z","iopub.status.idle":"2023-10-31T06:49:47.603357Z","shell.execute_reply.started":"2023-10-31T06:49:47.58903Z","shell.execute_reply":"2023-10-31T06:49:47.602412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = [0.48828688, 0.42932517, 0.49162089]\nstd = [0.41380908, 0.37492874, 0.41795654]","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:47.604659Z","iopub.execute_input":"2023-10-31T06:49:47.604996Z","iopub.status.idle":"2023-10-31T06:49:47.610771Z","shell.execute_reply.started":"2023-10-31T06:49:47.604969Z","shell.execute_reply":"2023-10-31T06:49:47.609904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UBCDataset(Dataset):\n    def __init__(self, df, transforms=None):\n        self.df = df\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        img_path = self.df.iloc[idx]['file_path']  \n        label = self.df.iloc[idx]['label']\n        \n        img = Image.open(img_path).convert(\"RGB\")\n        \n        if self.transforms:\n            img = self.transforms(img)\n\n        return img, label","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:47.614062Z","iopub.execute_input":"2023-10-31T06:49:47.614405Z","iopub.status.idle":"2023-10-31T06:49:47.622997Z","shell.execute_reply.started":"2023-10-31T06:49:47.614379Z","shell.execute_reply":"2023-10-31T06:49:47.622034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_transform = transforms.Compose([\n    transforms.Resize((300, 300)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean, std)\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:47.624026Z","iopub.execute_input":"2023-10-31T06:49:47.624293Z","iopub.status.idle":"2023-10-31T06:49:47.639217Z","shell.execute_reply.started":"2023-10-31T06:49:47.62427Z","shell.execute_reply":"2023-10-31T06:49:47.638323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = UBCDataset(df=test_df, transforms=test_transform)\ntest_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=2, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:47.640346Z","iopub.execute_input":"2023-10-31T06:49:47.640619Z","iopub.status.idle":"2023-10-31T06:49:47.65285Z","shell.execute_reply.started":"2023-10-31T06:49:47.640596Z","shell.execute_reply":"2023-10-31T06:49:47.651857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm\n\nclass UBCModel(pl.LightningModule):\n\n    def __init__(self, steps_per_epoch):\n        super(UBCModel, self).__init__()\n        self.num_classes = 5\n        self.steps_per_epoch = steps_per_epoch\n\n        self.model = timm.create_model('tf_efficientnet_b3',\n                                       checkpoint_path='/kaggle/input/tf-efficientnet/pytorch/tf-efficientnet-b3/1/tf_efficientnet_b3_aa-84b4657e.pth')\n\n        \n        self.model.classifier= torch.nn.Linear(in_features=1536, out_features=self.num_classes, bias=True)\n        self.criterion = nn.CrossEntropyLoss()\n        self.f1 = MulticlassF1Score(num_classes=self.num_classes, average='macro')\n        self.accuracy = torchmetrics.Accuracy(num_classes=self.num_classes, task='multiclass')\n        self.precision = torchmetrics.Precision(average='macro', num_classes=self.num_classes, task='multiclass')\n        self.recall = torchmetrics.Recall(average='macro', num_classes=self.num_classes, task='multiclass')\n        \n    def forward(self, x):\n        x = self.model(x)\n        return x\n\n    def training_step(self, batch, batch_idx):\n        x, y = batch\n        y_pred = self(x)\n        loss = self.criterion(y_pred, y)\n        self.log('train_loss', loss)\n        self.log('train_f1', self.f1(y_pred, y))\n        return loss\n\n    def validation_step(self, batch, batch_idx):\n        x, y = batch\n        y_pred = self(x)\n        loss = self.criterion(y_pred, y)\n        self.log('val_loss', loss)\n        self.log('val_f1', self.f1(y_pred, y))\n        self.log('val_acc', self.accuracy(y_pred, y))\n        self.log('val_precision', self.precision(y_pred, y))\n        self.log('val_recall', self.recall(y_pred, y))\n    \n    def configure_optimizers(self):\n        optimizer = torch.optim.Adam(self.parameters(), lr=1e-5, weight_decay=1e-5)\n#         scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=2)\n#         return {\n#             'optimizer': optimizer,\n#             'lr_scheduler': {\n#                 'scheduler': scheduler,\n#                 'interval': 'epoch',\n#                 'monitor': 'val_f1',\n#                 'frequency': 1,\n#                 'strict': True,\n#             }\n#         }\n        scheduler = OneCycleLR(optimizer, max_lr=1e-3, steps_per_epoch=self.steps_per_epoch, epochs=self.trainer.max_epochs)\n        return {\n            'optimizer': optimizer,\n            'lr_scheduler': {\n                'scheduler': scheduler,\n                'interval': 'step',\n                'frequency': 1,\n                'strict': True,\n            }\n        }\n","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:47.656304Z","iopub.execute_input":"2023-10-31T06:49:47.656892Z","iopub.status.idle":"2023-10-31T06:49:48.04086Z","shell.execute_reply.started":"2023-10-31T06:49:47.656831Z","shell.execute_reply":"2023-10-31T06:49:48.039725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\ncheckpoint_path1 = '/kaggle/input/sub-7-ovarian-cancer/epoch18-step646.ckpt'\ncheckpoint_path2 = '/kaggle/input/sub-7-ovarian-cancer/epoch29-step1020 (1).ckpt'\ncheckpoint_path3 = '/kaggle/input/sub-7-ovarian-cancer/epoch29-step1020 (2).ckpt'\ncheckpoint_path4 = '/kaggle/input/sub-7-ovarian-cancer/epoch29-step1020.ckpt'\n\nmodel1 = UBCModel.load_from_checkpoint(checkpoint_path1, map_location=device, steps_per_epoch=34)\nmodel2 = UBCModel.load_from_checkpoint(checkpoint_path2, map_location=device, steps_per_epoch=34)\nmodel3 = UBCModel.load_from_checkpoint(checkpoint_path3, map_location=device, steps_per_epoch=34)\nmodel4 = UBCModel.load_from_checkpoint(checkpoint_path3, map_location=device, steps_per_epoch=34)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:48.042235Z","iopub.execute_input":"2023-10-31T06:49:48.042542Z","iopub.status.idle":"2023-10-31T06:49:49.602135Z","shell.execute_reply.started":"2023-10-31T06:49:48.042518Z","shell.execute_reply":"2023-10-31T06:49:49.600722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\ncheckpoint_path1 = '/kaggle/input/sub-7-ovarian-cancer/epoch18-step646.ckpt'\ncheckpoint_path2 = '/kaggle/input/sub-7-ovarian-cancer/epoch29-step1020 (1).ckpt'\ncheckpoint_path3 = '/kaggle/input/sub-7-ovarian-cancer/epoch29-step1020 (2).ckpt'\ncheckpoint_path4 = '/kaggle/input/sub-7-ovarian-cancer/epoch29-step1020.ckpt'\n\nmodel1 = UBCModel.load_from_checkpoint(checkpoint_path1, map_location=device, steps_per_epoch=34)\nmodel2 = UBCModel.load_from_checkpoint(checkpoint_path2, map_location=device, steps_per_epoch=34)\nmodel3 = UBCModel.load_from_checkpoint(checkpoint_path3, map_location=device, steps_per_epoch=34)\nmodel4 = UBCModel.load_from_checkpoint(checkpoint_path4, map_location=device, steps_per_epoch=34)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-31T06:49:49.603103Z","iopub.status.idle":"2023-10-31T06:49:49.603488Z","shell.execute_reply.started":"2023-10-31T06:49:49.603309Z","shell.execute_reply":"2023-10-31T06:49:49.603325Z"},"trusted":true},"execution_count":null,"outputs":[]}]}