{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":20270,"databundleVersionId":1222630,"sourceType":"competition"},{"sourceId":9797223,"sourceType":"datasetVersion","datasetId":982170},{"sourceId":267306013,"sourceType":"kernelVersion"}],"dockerImageVersionId":31154,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import sys\nsys.path.append(\"../input/tez-lib/\")\nimport tez\nfrom tez import Tez, TezConfig\nimport albumentations\nimport pandas as pd\nimport cv2\nimport numpy as np\nimport timm\nimport torch.nn as nn\nimport torch\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:40.894614Z","iopub.execute_input":"2025-10-11T16:26:40.895140Z","iopub.status.idle":"2025-10-11T16:26:53.926320Z","shell.execute_reply.started":"2025-10-11T16:26:40.895117Z","shell.execute_reply":"2025-10-11T16:26:53.925588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class args:\n    batch_size = 32\n    image_size = 384","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:53.927622Z","iopub.execute_input":"2025-10-11T16:26:53.927868Z","iopub.status.idle":"2025-10-11T16:26:53.931518Z","shell.execute_reply.started":"2025-10-11T16:26:53.927842Z","shell.execute_reply":"2025-10-11T16:26:53.930728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CustomDataset:\n    def __init__(self, image_paths, dense_features, targets, augmentations):\n        self.image_paths = image_paths\n        self.dense_features = dense_features\n        self.targets = targets\n        self.augmentations = augmentations\n        \n    def __len__(self):\n        return len(self.image_paths)\n    \n    def __getitem__(self, item):\n        image = cv2.imread(self.image_paths[item])\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        if self.augmentations is not None:\n            augmented = self.augmentations(image=image)\n            image = augmented[\"image\"]\n        image = np.transpose(image, (2, 0, 1)).astype(np.float32)\n        features = self.dense_features[item, :]\n        targets = self.targets[item]\n        return {\n            \"image\": torch.tensor(image, dtype=torch.float),\n            \"features\": torch.tensor(features, dtype=torch.float),\n            \"targets\": torch.tensor(targets, dtype=torch.float),\n        }\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:53.932373Z","iopub.execute_input":"2025-10-11T16:26:53.932698Z","iopub.status.idle":"2025-10-11T16:26:53.955787Z","shell.execute_reply.started":"2025-10-11T16:26:53.932669Z","shell.execute_reply":"2025-10-11T16:26:53.955069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CustomModel(nn.Module):\n    def __init__(self):\n        super().__init__()        \n        self.model = timm.create_model(\"resnet50\", pretrained=False, in_chans=3)\n        self.dropout = nn.Dropout(0.5)\n        self.out = nn.Linear(1000, 1)\n        self.step_scheduler_after = \"epoch\"\n\n    def monitor_metrics(self, outputs, targets, loss):\n        # Keep metrics as tensor to avoid Tez float error\n        return {\"rmse\": loss.detach()}\n\n    def optimizer_scheduler(self):\n        opt = torch.optim.AdamW(self.parameters(), lr=2.5e-5, weight_decay=0.01)\n        sch = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(\n            opt, T_0=10, T_mult=1, eta_min=1e-6\n        )\n        return opt, sch\n\n    def forward(self, image, features, targets=None):\n        x = self.model(image)\n        x = self.dropout(x)\n        x = self.out(x)\n\n        if targets is not None:\n            loss = nn.MSELoss()(x, targets.view(-1, 1))\n            metrics = self.monitor_metrics(x, targets, loss)\n            return x, loss, metrics\n        return x, 0, {}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:53.957378Z","iopub.execute_input":"2025-10-11T16:26:53.957586Z","iopub.status.idle":"2025-10-11T16:26:53.973194Z","shell.execute_reply.started":"2025-10-11T16:26:53.957551Z","shell.execute_reply":"2025-10-11T16:26:53.972491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_aug = albumentations.Compose([\n    albumentations.LongestMaxSize(args.image_size, p=1),\n    albumentations.PadIfNeeded(args.image_size, args.image_size, p=1, border_mode=0),\n    albumentations.HorizontalFlip(p=0.5),\n    albumentations.Normalize(\n        mean=[0.485, 0.456, 0.406],\n        std=[0.229, 0.224, 0.225],\n        max_pixel_value=255.0,\n        p=1.0\n    ),\n], p=1.0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:53.973967Z","iopub.execute_input":"2025-10-11T16:26:53.974333Z","iopub.status.idle":"2025-10-11T16:26:53.997120Z","shell.execute_reply.started":"2025-10-11T16:26:53.974308Z","shell.execute_reply":"2025-10-11T16:26:53.996427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"i = 0  # fold number\nmodel = CustomModel()\nmodel = Tez(model)\nmodel.load(f\"/kaggle/input/10121-yash-agarwal-training-siim/model_f{i}.bin\", weights_only=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:53.997725Z","iopub.execute_input":"2025-10-11T16:26:53.997961Z","iopub.status.idle":"2025-10-11T16:26:55.481976Z","shell.execute_reply.started":"2025-10-11T16:26:53.997944Z","shell.execute_reply":"2025-10-11T16:26:55.481135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test = pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/test.csv\")\ntest_img_paths = [f\"/kaggle/input/siim-isic-melanoma-classification/jpeg/test/{x}.jpg\" for x in df_test[\"image_name\"].values]\ndense_features = []","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:55.482989Z","iopub.execute_input":"2025-10-11T16:26:55.483325Z","iopub.status.idle":"2025-10-11T16:26:55.514762Z","shell.execute_reply.started":"2025-10-11T16:26:55.483293Z","shell.execute_reply":"2025-10-11T16:26:55.513978Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dataset = CustomDataset(\n    image_paths=test_img_paths,\n    dense_features=df_test[dense_features].values,\n    targets=np.ones(len(test_img_paths)),\n    augmentations=test_aug,\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:55.515508Z","iopub.execute_input":"2025-10-11T16:26:55.515838Z","iopub.status.idle":"2025-10-11T16:26:55.521461Z","shell.execute_reply.started":"2025-10-11T16:26:55.515816Z","shell.execute_reply":"2025-10-11T16:26:55.520736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions = model.predict(test_dataset, batch_size=args.batch_size, n_jobs=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:55.522139Z","iopub.execute_input":"2025-10-11T16:26:55.522406Z","iopub.status.idle":"2025-10-11T16:26:55.537222Z","shell.execute_reply.started":"2025-10-11T16:26:55.522379Z","shell.execute_reply":"2025-10-11T16:26:55.536678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_test_predictions = []\nfor preds in tqdm(test_predictions):\n    final_test_predictions.extend(torch.sigmoid(torch.tensor(preds)).numpy().ravel().tolist())\n\n\ndf_test[\"target\"] = final_test_predictions\ndf_test = df_test[[\"image_name\", \"target\"]]\ndf_test.to_csv(\"submission.csv\", index=False)\ndf_test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:26:55.539050Z","iopub.execute_input":"2025-10-11T16:26:55.539235Z"}},"outputs":[],"execution_count":null}]}