{"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 torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader, WeightedRandomSampler, Subset\nfrom torchvision import models, transforms\nfrom torchvision.transforms import ToPILImage\nfrom torchvision.io import read_image\nfrom torch.optim.lr_scheduler import CyclicLR\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom sklearn.model_selection import StratifiedShuffleSplit\nfrom sklearn.metrics import roc_auc_score, precision_recall_curve, auc, roc_curve\nfrom sklearn.preprocessing import label_binarize\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-10-11T09:55:22.035377Z","iopub.execute_input":"2023-10-11T09:55:22.035659Z","iopub.status.idle":"2023-10-11T09:55:26.511004Z","shell.execute_reply.started":"2023-10-11T09:55:22.035637Z","shell.execute_reply":"2023-10-11T09:55:26.510038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.getcwd()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T09:55:27.362548Z","iopub.execute_input":"2023-10-11T09:55:27.363025Z","iopub.status.idle":"2023-10-11T09:55:27.369938Z","shell.execute_reply.started":"2023-10-11T09:55:27.362993Z","shell.execute_reply":"2023-10-11T09:55:27.368718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 1. Load the  trained model\nmodel = models.resnet50(weights=None)\nnum_ftrs = model.fc.in_features\nmodel.fc = nn.Linear(num_ftrs, 5)\ndevice = torch.device(\"cpu\")\nmodel_weights_path = os.path.join('/kaggle/input/resnet50-weights-3/', 'resnet50_weights_3.pth')\n# model.load_state_dict(torch.load(model_weights_path), map_location=torch.device('cpu'))\n# model_weights = torch.load(model_weights_path, map_location=torch.device('cpu'))\nmodel_weights = torch.load(model_weights_path, map_location=device)\nmodel.load_state_dict(model_weights)\n\n# model = model.to(device)\nmodel.eval()  \n\n# 2. Create a custom dataset for the test images\nclass TestDataset(torch.utils.data.Dataset):\n    def __init__(self, csv_file, img_dir, transform=None):\n        self.dataframe = pd.read_csv(csv_file)\n        self.img_dir = img_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.dataframe)\n\n    def __getitem__(self, idx):\n        img_path = os.path.join(self.img_dir, self.dataframe.iloc[idx, 0].astype(str) + '.png')\n        # image = Image.open(img_path)\n        image = read_image(img_path)\n        image = ToPILImage()(image)  # Convert tensor to PIL Image\n        if self.transform:\n            image = self.transform(image)\n        return image\n\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Resize to a consistent size. Adjust as needed.\n    transforms.ToTensor()\n])\n\ntest_dataset = TestDataset(csv_file='/kaggle/input/UBC-OCEAN/test.csv', img_dir='/kaggle/input/UBC-OCEAN/test_images', transform=transform)\ntest_loader = DataLoader(test_dataset, batch_size=2, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-11T09:55:30.651102Z","iopub.execute_input":"2023-10-11T09:55:30.651477Z","iopub.status.idle":"2023-10-11T09:55:31.924212Z","shell.execute_reply.started":"2023-10-11T09:55:30.651451Z","shell.execute_reply":"2023-10-11T09:55:31.922797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 3. Use the model to make predictions on the test dataset\npredictions = []\n# model = model.to(device)\nwith torch.no_grad():\n    for images in test_loader:\n#         images = images.to(device)\n        outputs = model(images)\n        _, predicted = torch.max(outputs, 1)\n        predictions.extend(predicted.cpu().numpy())","metadata":{"execution":{"iopub.status.busy":"2023-10-11T09:55:39.600034Z","iopub.execute_input":"2023-10-11T09:55:39.600397Z","iopub.status.idle":"2023-10-11T09:55:56.628026Z","shell.execute_reply.started":"2023-10-11T09:55:39.600368Z","shell.execute_reply":"2023-10-11T09:55:56.626742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert numerical labels to original string labels\nlabel_map = {\n    0: \"HGSC\",\n    1: \"EC\",\n    2: \"CC\",\n    3: \"LGSC\",\n    4: \"MC\"\n}\npredictions = [label_map[p] for p in predictions]","metadata":{"execution":{"iopub.status.busy":"2023-10-11T09:55:59.096632Z","iopub.execute_input":"2023-10-11T09:55:59.097146Z","iopub.status.idle":"2023-10-11T09:55:59.101271Z","shell.execute_reply.started":"2023-10-11T09:55:59.097117Z","shell.execute_reply":"2023-10-11T09:55:59.100058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 4. Save the predictions to a submission.csv file\ntest_df = pd.read_csv('/kaggle/input/UBC-OCEAN/test.csv')\nsubmission_df = pd.DataFrame({\n    'image_id': test_df['image_id'],\n    'label': predictions\n})\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-11T09:56:04.130997Z","iopub.execute_input":"2023-10-11T09:56:04.131406Z","iopub.status.idle":"2023-10-11T09:56:04.150089Z","shell.execute_reply.started":"2023-10-11T09:56:04.131374Z","shell.execute_reply":"2023-10-11T09:56:04.148722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T09:56:06.461515Z","iopub.execute_input":"2023-10-11T09:56:06.461877Z","iopub.status.idle":"2023-10-11T09:56:06.475666Z","shell.execute_reply.started":"2023-10-11T09:56:06.461849Z","shell.execute_reply":"2023-10-11T09:56:06.47476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}