{"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":"markdown","source":"### I probably should also make my training notebook public.  \nhttps://www.kaggle.com/code/parhamgousheh/ovarian-cancer-training","metadata":{}},{"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\nfrom sklearn.preprocessing import LabelEncoder\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\nfrom torch.optim.lr_scheduler import StepLR\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\nimport timm\nfrom PIL import Image\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-01T07:52:00.871948Z","iopub.execute_input":"2023-11-01T07:52:00.872325Z","iopub.status.idle":"2023-11-01T07:52:18.81314Z","shell.execute_reply.started":"2023-11-01T07:52:00.872293Z","shell.execute_reply":"2023-11-01T07:52:18.812169Z"},"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-11-01T07:52:18.815029Z","iopub.execute_input":"2023-11-01T07:52:18.815382Z","iopub.status.idle":"2023-11-01T07:52:18.828424Z","shell.execute_reply.started":"2023-11-01T07:52:18.815354Z","shell.execute_reply":"2023-11-01T07:52:18.827278Z"},"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-11-01T07:52:18.829836Z","iopub.execute_input":"2023-11-01T07:52:18.830263Z","iopub.status.idle":"2023-11-01T07:52:18.848438Z","shell.execute_reply.started":"2023-11-01T07:52:18.830226Z","shell.execute_reply":"2023-11-01T07:52:18.847477Z"},"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-11-01T07:52:18.851051Z","iopub.execute_input":"2023-11-01T07:52:18.851481Z","iopub.status.idle":"2023-11-01T07:52:18.859595Z","shell.execute_reply.started":"2023-11-01T07:52:18.851452Z","shell.execute_reply":"2023-11-01T07:52:18.858543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = [0.48828688, 0.42932517, 0.49162089]\nstd = [0.41380908, 0.37492874, 0.41795654]\n\n# mean=[0.485, 0.456, 0.406]\n# std=[0.229, 0.224, 0.225]\n\ntest_transform = transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean, std)\n])","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:52:18.860891Z","iopub.execute_input":"2023-11-01T07:52:18.861272Z","iopub.status.idle":"2023-11-01T07:52:18.877638Z","shell.execute_reply.started":"2023-11-01T07:52:18.861244Z","shell.execute_reply":"2023-11-01T07:52:18.876587Z"},"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-11-01T07:52:18.879807Z","iopub.execute_input":"2023-11-01T07:52:18.880189Z","iopub.status.idle":"2023-11-01T07:52:18.892022Z","shell.execute_reply.started":"2023-11-01T07:52:18.880157Z","shell.execute_reply":"2023-11-01T07:52:18.890797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UBCModel(pl.LightningModule):\n\n    def __init__(self):\n        super(UBCModel, self).__init__()\n        self.num_classes = 5\n        \n        self.model = timm.create_model('resnext50_32x4d', pretrained=False)\n        self.model.load_state_dict(torch.load('/kaggle/input/resnext50-32x4d/resnext50_32x4d.pth'))\n        \n        self.model.fc= torch.nn.Linear(in_features=2048, out_features=self.num_classes, bias=True)\n        self.criterion = nn.CrossEntropyLoss(weight=normalized_weights)\n        \n    def forward(self, x):\n        x = self.model(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:52:18.893578Z","iopub.execute_input":"2023-11-01T07:52:18.893915Z","iopub.status.idle":"2023-11-01T07:52:18.90819Z","shell.execute_reply.started":"2023-11-01T07:52:18.893887Z","shell.execute_reply":"2023-11-01T07:52:18.907117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_weights = torch.tensor([0.1538, 0.1228, 0.0686, 0.3239, 0.3310])\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ncheckpoint_path = '/kaggle/input/sub-19-ovarian-cancer/epoch34-step420.ckpt'\nmodel = UBCModel.load_from_checkpoint(checkpoint_path, map_location=device)\nmodel = model.eval()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:52:18.909442Z","iopub.execute_input":"2023-11-01T07:52:18.909772Z","iopub.status.idle":"2023-11-01T07:52:28.529601Z","shell.execute_reply.started":"2023-11-01T07:52:18.909743Z","shell.execute_reply":"2023-11-01T07:52:28.52839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\n\n# Make predictions on the test set\nwith torch.no_grad():\n    for image, label in test_loader:\n        image = image.to(device)\n        outputs = model(image)\n        \n        _, preds = torch.max(outputs, 1)\n        predictions.extend(preds.detach().cpu().numpy())","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:52:28.531504Z","iopub.execute_input":"2023-11-01T07:52:28.531827Z","iopub.status.idle":"2023-11-01T07:52:34.31691Z","shell.execute_reply.started":"2023-11-01T07:52:28.531799Z","shell.execute_reply":"2023-11-01T07:52:34.315539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib\n\n# Map the predictions back to their original labels\n# predictions = np.concatenate(predictions).flatten()\npredictions = np.array(predictions)\nencoder = joblib.load('/kaggle/input/label-encoder/label_encoder.pkl')\npred_labels = encoder.inverse_transform(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:52:34.32159Z","iopub.execute_input":"2023-11-01T07:52:34.322003Z","iopub.status.idle":"2023-11-01T07:52:34.334151Z","shell.execute_reply.started":"2023-11-01T07:52:34.321967Z","shell.execute_reply":"2023-11-01T07:52:34.333056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/UBC-OCEAN/sample_submission.csv\")\nsample_submission['label'] = pred_labels\nsample_submission.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:52:34.335514Z","iopub.execute_input":"2023-11-01T07:52:34.335914Z","iopub.status.idle":"2023-11-01T07:52:34.356919Z","shell.execute_reply.started":"2023-11-01T07:52:34.335876Z","shell.execute_reply":"2023-11-01T07:52:34.355992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission","metadata":{"execution":{"iopub.status.busy":"2023-11-01T07:52:34.358117Z","iopub.execute_input":"2023-11-01T07:52:34.35843Z","iopub.status.idle":"2023-11-01T07:52:34.373043Z","shell.execute_reply.started":"2023-11-01T07:52:34.358404Z","shell.execute_reply":"2023-11-01T07:52:34.371922Z"},"trusted":true},"execution_count":null,"outputs":[]}]}