{"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":13336653,"sourceType":"datasetVersion","datasetId":8456474}],"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(\"/kaggle/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\nfrom sklearn import metrics\nimport torch\nfrom tez.callbacks import EarlyStopping\nfrom tqdm import tqdm\nimport math","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:55:53.122697Z","iopub.execute_input":"2025-10-11T16:55:53.123433Z","iopub.status.idle":"2025-10-11T16:56:36.067707Z","shell.execute_reply.started":"2025-10-11T16:55:53.123408Z","shell.execute_reply":"2025-10-11T16:56:36.067166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class args:\n    batch_size = 10\n    image_size = 384\n\ndef sigmoid(x):\n    return 1 / (1 + math.exp(-x))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:56:36.068796Z","iopub.execute_input":"2025-10-11T16:56:36.069248Z","iopub.status.idle":"2025-10-11T16:56:36.073393Z","shell.execute_reply.started":"2025-10-11T16:56:36.069217Z","shell.execute_reply":"2025-10-11T16:56:36.072585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class MelanomaDataset:\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":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:56:36.074031Z","iopub.execute_input":"2025-10-11T16:56:36.074286Z","iopub.status.idle":"2025-10-11T16:56:36.093760Z","shell.execute_reply.started":"2025-10-11T16:56:36.074260Z","shell.execute_reply":"2025-10-11T16:56:36.093260Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class MelanomaModel(nn.Module):\n    def __init__(self):\n        super().__init__()        \n        self.model = timm.create_model(\"resnext50_32x4d\", pretrained=False, in_chans=3)\n        self.dropout = nn.Dropout(0.4)\n        self.out = nn.Linear(1000, 1)\n        \n        self.step_scheduler_after = \"epoch\"\n\n    def monitor_metrics(self, outputs, targets, loss):\n        valid_binaryloss = loss\n        if str(valid_binaryloss) == 'nan':\n            valid_binaryloss = float('inf')\n        return {\"binaryloss\": valid_binaryloss}\n\n    def optimizer_scheduler(self):\n        opt = torch.optim.AdamW(self.parameters(), lr=3e-05, weight_decay=0.015)\n        sch = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(\n            opt, T_0=10, T_mult=1, eta_min=5e-7, last_epoch=-1\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.BCEWithLogitsLoss()(x, targets.view(-1, 1).type_as(x))\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:56:36.095628Z","iopub.execute_input":"2025-10-11T16:56:36.095896Z","iopub.status.idle":"2025-10-11T16:56:36.111485Z","shell.execute_reply.started":"2025-10-11T16:56:36.095879Z","shell.execute_reply":"2025-10-11T16:56:36.110860Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_aug = albumentations.Compose(\n    [\n        albumentations.LongestMaxSize(args.image_size, p=1),\n        albumentations.PadIfNeeded(args.image_size, args.image_size, p=1, border_mode=0),\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":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:56:36.112243Z","iopub.execute_input":"2025-10-11T16:56:36.112494Z","iopub.status.idle":"2025-10-11T16:56:36.131137Z","shell.execute_reply.started":"2025-10-11T16:56:36.112467Z","shell.execute_reply":"2025-10-11T16:56:36.130442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"super_final_predictions = []\ni = 0\nmodel = MelanomaModel()\nmodel = Tez(model)\nmodel.load(f\"/kaggle/input/fold-5-data/model_f{i} (1).bin\", weights_only=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:56:36.131904Z","iopub.execute_input":"2025-10-11T16:56:36.132179Z","iopub.status.idle":"2025-10-11T16:56:39.179560Z","shell.execute_reply.started":"2025-10-11T16:56:36.132152Z","shell.execute_reply":"2025-10-11T16:56:39.178844Z"}},"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]\n\n# Create dummy column since model doesn't use it\ndf_test['age_approx'] = 0\ndense_features = ['age_approx']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T17:09:38.625329Z","iopub.execute_input":"2025-10-11T17:09:38.626051Z","iopub.status.idle":"2025-10-11T17:09:38.646665Z","shell.execute_reply.started":"2025-10-11T17:09:38.626024Z","shell.execute_reply":"2025-10-11T17:09:38.645941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dataset = MelanomaDataset(\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:56:39.239335Z","iopub.execute_input":"2025-10-11T16:56:39.239590Z","iopub.status.idle":"2025-10-11T16:56:39.249583Z","shell.execute_reply.started":"2025-10-11T16:56:39.239563Z","shell.execute_reply":"2025-10-11T16:56:39.248943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions = model.predict(test_dataset, batch_size=2*args.batch_size, n_jobs=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:56:39.250218Z","iopub.execute_input":"2025-10-11T16:56:39.250456Z","iopub.status.idle":"2025-10-11T16:56:39.265123Z","shell.execute_reply.started":"2025-10-11T16:56:39.250441Z","shell.execute_reply":"2025-10-11T16:56:39.264515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_test_predictions = []\nfor preds in tqdm(test_predictions):\n    final_test_predictions.extend(preds.ravel().tolist())\n\nsuper_final_predictions.append(final_test_predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T16:56:39.267034Z","iopub.execute_input":"2025-10-11T16:56:39.267219Z","iopub.status.idle":"2025-10-11T17:04:47.503549Z","shell.execute_reply.started":"2025-10-11T16:56:39.267204Z","shell.execute_reply":"2025-10-11T17:04:47.502666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"super_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":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T17:04:47.504647Z","iopub.execute_input":"2025-10-11T17:04:47.505077Z","iopub.status.idle":"2025-10-11T17:04:47.544538Z","shell.execute_reply.started":"2025-10-11T17:04:47.505040Z","shell.execute_reply":"2025-10-11T17:04:47.544048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T17:04:47.545251Z","iopub.execute_input":"2025-10-11T17:04:47.545530Z","iopub.status.idle":"2025-10-11T17:04:47.562489Z","shell.execute_reply.started":"2025-10-11T17:04:47.545505Z","shell.execute_reply":"2025-10-11T17:04:47.561723Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}