{"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":267253460,"sourceType":"kernelVersion"}],"dockerImageVersionId":31090,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import sys\nsys.path.append(\"../input/tez-lib/\")","metadata":{"_uuid":"04e122ab-309e-4cc4-8ece-414e995ef880","_cell_guid":"068c5953-f68e-45da-a2b4-b7fee65eee6e","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-10-11T11:52:50.014870Z","iopub.execute_input":"2025-10-11T11:52:50.015180Z","iopub.status.idle":"2025-10-11T11:52:50.021798Z","shell.execute_reply.started":"2025-10-11T11:52:50.015157Z","shell.execute_reply":"2025-10-11T11:52:50.021125Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import 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\nfrom sklearn import metrics\nimport torch\nfrom tez.callbacks import EarlyStopping\nfrom tqdm import tqdm\nimport math","metadata":{"_uuid":"fb090d7a-6b97-4272-9134-312ba1b7e519","_cell_guid":"b27b84e2-ebab-4d4e-8d64-839485530354","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-10-11T11:52:50.022885Z","iopub.execute_input":"2025-10-11T11:52:50.023091Z","iopub.status.idle":"2025-10-11T11:53:34.720330Z","shell.execute_reply.started":"2025-10-11T11:52:50.023075Z","shell.execute_reply":"2025-10-11T11:53:34.719749Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class args:\n    batch_size = 64#16\n    image_size = 384 #64","metadata":{"_uuid":"4850796c-c26e-40f5-8095-988ca911d1a5","_cell_guid":"5bcf8b38-f1e4-48a1-b68c-d002ab115190","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-10-11T11:53:34.720962Z","iopub.execute_input":"2025-10-11T11:53:34.721326Z","iopub.status.idle":"2025-10-11T11:53:34.724906Z","shell.execute_reply.started":"2025-10-11T11:53:34.721308Z","shell.execute_reply":"2025-10-11T11:53:34.724391Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def sigmoid(x):\n    return 1 / (1 + math.exp(-x))","metadata":{"_uuid":"287bc3ef-ff2f-4e97-826a-5965c47972ce","_cell_guid":"19dec7d9-4347-4161-a2ba-5698d23cf704","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-10-11T11:53:34.726712Z","iopub.execute_input":"2025-10-11T11:53:34.726942Z","iopub.status.idle":"2025-10-11T11:53:34.742182Z","shell.execute_reply.started":"2025-10-11T11:53:34.726920Z","shell.execute_reply":"2025-10-11T11:53:34.741502Z"},"jupyter":{"outputs_hidden":false}},"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        \n        if self.augmentations is not None:\n            augmented = self.augmentations(image=image)\n            image = augmented[\"image\"]\n            \n        image = np.transpose(image, (2, 0, 1)).astype(np.float32)\n        \n        features = self.dense_features[item, :]\n        targets = self.targets[item]\n        \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        }","metadata":{"_uuid":"a035f49f-5fc1-4c68-a697-ed4b2c66d9d0","_cell_guid":"bb2449c7-e985-4534-bc28-27fb4e521e60","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-10-11T11:53:34.742788Z","iopub.execute_input":"2025-10-11T11:53:34.742967Z","iopub.status.idle":"2025-10-11T11:53:34.756432Z","shell.execute_reply.started":"2025-10-11T11:53:34.742946Z","shell.execute_reply":"2025-10-11T11:53:34.755767Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# class PawpularModel(tez.Model):\nclass CustomModel(nn.Module):\n    def __init__(self):\n        super().__init__()        \n        self.model = timm.create_model(\"resnet50\", pretrained=False, in_chans=3)#resnet50#resnet101#eca_nfnet_l1#resnest101e\n\n        \n        self.dropout = nn.Dropout(0.5)# increase dropout\n        # self.out = nn.Linear(1280+12, 1)\n        self.out = nn.Linear(1000, 1)\n        # self.out_final = nn.Linear(512, 1)\n        \n        self.step_scheduler_after = \"epoch\"\n\n\n    def monitor_metrics(self, outputs, targets):\n        # rmse = torch.sqrt(loss).cpu().detach().numpy()\n        # rmse = loss\n        # if str(rmse) == 'nan':\n        #     rmse = float('inf')\n        # # return {\"rmse\": rmse}\n        # return {\"rmse\": rmse}\n        outputs_np = outputs.cpu().detach().numpy()\n        targets_np = targets.cpu().detach().numpy()\n    \n        try:\n            # Calculate AUC score using the numpy arrays\n            auc_score = metrics.roc_auc_score(targets_np, outputs_np)\n        except ValueError:\n            # If the batch has only one class, AUC is not defined.\n            auc_score = 0.5\n        \n        # 2. Create the return tensor using the .device from the ORIGINAL 'outputs' tensor.\n        return {\"auc\": torch.tensor(auc_score, device=outputs.device)}\n\n    def optimizer_scheduler(self):\n        opt = torch.optim.AdamW(self.parameters(), lr=2.5e-05, weight_decay=0.01)\n        sch = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(\n            opt, T_0=10, T_mult=1, eta_min=1e-6, last_epoch=-1\n        )\n        return opt,sch\n\n    def forward(self, image, features, targets=None):\n\n        x = self.model(image)\n        x = self.dropout(x)\n        # x = torch.cat([x, features], dim=1)\n        # x = self.dropout(x)\n        x = self.out(x)\n        # x = self.dropout(x)\n        # x = self.out_final(x)\n\n        if targets is not None:\n            loss = nn.BCEWithLogitsLoss()(x, targets.view(-1, 1))\n            metrics = self.monitor_metrics(x, targets)\n            return x, loss, metrics\n        return x, 0, {}\n\n# PawpularModel()","metadata":{"_uuid":"ec562db8-202b-4782-a13d-50c9916a11ce","_cell_guid":"c907f3f7-40b0-4cac-912a-b8e82bb80736","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-10-11T11:53:34.757152Z","iopub.execute_input":"2025-10-11T11:53:34.757383Z","iopub.status.idle":"2025-10-11T11:53:34.771187Z","shell.execute_reply.started":"2025-10-11T11:53:34.757345Z","shell.execute_reply":"2025-10-11T11:53:34.770696Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_aug = albumentations.Compose(\n    [\n        albumentations.Resize(args.image_size, args.image_size, p=1),\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    ],\n    p=1.0,\n)","metadata":{"_uuid":"2298fc11-0545-4005-bf0d-5d8d90df50a5","_cell_guid":"37f051d7-b741-4fbb-b282-20426a3a262a","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-10-11T11:53:34.771781Z","iopub.execute_input":"2025-10-11T11:53:34.771960Z","iopub.status.idle":"2025-10-11T11:53:34.790694Z","shell.execute_reply.started":"2025-10-11T11:53:34.771946Z","shell.execute_reply":"2025-10-11T11:53:34.790029Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"super_final_predictions = []\ni=0\nmodel = CustomModel()\nmodel = Tez(model)\nmodel.load(f\"/kaggle/input/medha-shree-10049-training-melanoma/model_f{i}.bin\", weights_only=True)\n\ndf_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]\n\ndense_features = [\n    \n]\n\ntest_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)\ntest_predictions = model.predict(test_dataset, batch_size=2*args.batch_size, n_jobs=-1)\n\nfinal_test_predictions = []\nfor preds in tqdm(test_predictions):\n    final_test_predictions.extend(preds.ravel().tolist())\n\nfinal_test_predictions = [sigmoid(x) * 100 for x in final_test_predictions]\nsuper_final_predictions.append(final_test_predictions)\n\nsuper_final_predictions = np.mean(np.column_stack(super_final_predictions), axis=1)\ndf_test[\"target\"] = super_final_predictions\ndf_test = df_test[[\"image_name\", \"target\"]]\ndf_test.to_csv(\"submission.csv\", index=False)","metadata":{"_uuid":"a4df60f8-ceaf-4908-8d18-999fcd04c882","_cell_guid":"64cf4e6f-18b3-4ee4-885b-c33cdebcf186","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-10-11T11:53:34.791311Z","iopub.execute_input":"2025-10-11T11:53:34.791644Z","iopub.status.idle":"2025-10-11T11:59:26.884151Z","shell.execute_reply.started":"2025-10-11T11:53:34.791627Z","shell.execute_reply":"2025-10-11T11:59:26.883424Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.head()","metadata":{"_uuid":"3f68650c-1f87-4edc-afff-1f611e717de5","_cell_guid":"f858caf2-9a4d-4030-ad42-262b5d656b94","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-10-11T11:59:26.885227Z","iopub.execute_input":"2025-10-11T11:59:26.885486Z","iopub.status.idle":"2025-10-11T11:59:26.902616Z","shell.execute_reply.started":"2025-10-11T11:59:26.885462Z","shell.execute_reply":"2025-10-11T11:59:26.902015Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"_uuid":"e9b232c5-ac3f-492b-8ed6-a3cab2340ab1","_cell_guid":"4a66b6ea-6893-4d35-ad2d-e1043ddd8a89","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null}]}