{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n!pip install pretrainedmodels\n!pip install albumentations\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nimport torch\nfrom torch import nn\nfrom torch.nn import functional as F\nfrom tqdm import tqdm\n\nimport pretrainedmodels\nimport albumentations\nfrom PIL import Image\nfrom PIL import ImageFile\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-05T15:01:41.554570Z","iopub.execute_input":"2022-09-05T15:01:41.555687Z","iopub.status.idle":"2022-09-05T15:02:04.717808Z","shell.execute_reply.started":"2022-09-05T15:01:41.555629Z","shell.execute_reply":"2022-09-05T15:02:04.716791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SEResnext50_32x4d(nn.Module):\n    def __init__(self, pretrained='imagenet'):\n        super(SEResnext50_32x4d, self).__init__()\n        \n        self.model = pretrainedmodels.__dict__[\n            \"se_resnext50_32x4d\"\n        ](pretrained=None)\n        if pretrained is not None:\n            self.model.load_state_dict(\n                torch.load(\n                    \"../input/trained-model/final_model.pt\"\n                ), strict=False\n            )\n\n        self.l0 = nn.Linear(2048, 1)\n    \n    def forward(self, image, targets):\n        batch_size, channel, height, width = image.shape\n        \n        x = self.model.features(image)\n        x = F.adaptive_avg_pool2d(x, 1).reshape(batch_size, -1)\n        \n        out = self.l0(x)\n        loss = nn.BCEWithLogitsLoss()(out, targets.view(-1, 1).type_as(x))\n\n        return out, loss","metadata":{"execution":{"iopub.status.busy":"2022-09-05T15:02:04.719870Z","iopub.execute_input":"2022-09-05T15:02:04.720161Z","iopub.status.idle":"2022-09-05T15:02:04.728755Z","shell.execute_reply.started":"2022-09-05T15:02:04.720133Z","shell.execute_reply":"2022-09-05T15:02:04.727808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ClassificationDataset:\n    def __init__(\n        self,\n        image_paths,\n        targets,\n        resize,\n        augmentations=None,\n        backend=\"pil\",\n        channel_first=True,\n    ):\n        \"\"\"\n        :param image_paths: list of paths to images\n        :param targets: numpy array\n        :param resize: tuple or None\n        :param augmentations: albumentations augmentations\n        \"\"\"\n        self.image_paths = image_paths\n        self.targets = targets\n        self.resize = resize\n        self.augmentations = augmentations\n        self.backend = backend\n        self.channel_first = channel_first\n\n\n    def __len__(self):\n        return len(self.image_paths)\n\n    def __getitem__(self, item):\n        targets = self.targets[item]\n        if self.backend == \"pil\":\n            image = Image.open(self.image_paths[item])\n            if self.resize is not None:\n                image = image.resize(\n                    (self.resize[1], self.resize[0]), resample=Image.BILINEAR\n                )\n            image = np.array(image)\n            if self.augmentations is not None:\n                augmented = self.augmentations(image=image)\n                image = augmented[\"image\"]\n        elif self.backend == \"cv2\":\n            image = cv2.imread(self.image_paths[item])\n            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n            if self.resize is not None:\n                image = cv2.resize(\n                    image,\n                    (self.resize[1], self.resize[0]),\n                    interpolation=cv2.INTER_CUBIC,\n                )\n            if self.augmentations is not None:\n                augmented = self.augmentations(image=image)\n            image = augmented[\"image\"]\n        else:\n            raise Exception(\"Backend not implemented\")\n        if self.channel_first:\n            image = np.transpose(image, (2, 0, 1)).astype(np.float32)\n        return {\n            \"image\": torch.tensor(image),\n            \"targets\": torch.tensor(targets),\n        }\n","metadata":{"execution":{"iopub.status.busy":"2022-09-05T15:02:04.730694Z","iopub.execute_input":"2022-09-05T15:02:04.731398Z","iopub.status.idle":"2022-09-05T15:02:04.752202Z","shell.execute_reply.started":"2022-09-05T15:02:04.731361Z","shell.execute_reply":"2022-09-05T15:02:04.750839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Engine:\n    def predict(data_loader, model, device):\n        model.eval()\n        final_predictions = []\n        with torch.no_grad():\n            tk0 = tqdm(data_loader, total=len(data_loader))\n            for data in tk0:\n                for key, value in data.items():\n                    data[key] = value.to(device)\n                predictions, _ = model(**data)\n                predictions = predictions.cpu()\n                final_predictions.append(predictions)\n        return final_predictions","metadata":{"execution":{"iopub.status.busy":"2022-09-05T15:02:04.754640Z","iopub.execute_input":"2022-09-05T15:02:04.755076Z","iopub.status.idle":"2022-09-05T15:02:04.774031Z","shell.execute_reply.started":"2022-09-05T15:02:04.755028Z","shell.execute_reply":"2022-09-05T15:02:04.772924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict():\n    test_data_path = \"../input/siim-isic-melanoma-classification/jpeg/test/\"\n    df = pd.read_csv(\"../input/siim-isic-melanoma-classification/test.csv\")\n    device = \"cuda\"\n\n\n    mean = (0.485, 0.456, 0.406)\n    std = (0.229, 0.224, 0.225)\n    aug = albumentations.Compose(\n        [\n            albumentations.Normalize(mean, std, max_pixel_value=255.0, always_apply=True)\n        ]\n    )\n\n    images = df.image_name.values.tolist()\n    images = [os.path.join(test_data_path, i + \".jpg\") for i in images]\n    targets = np.zeros(len(images))\n\n    test_dataset = ClassificationDataset(\n        image_paths=images,\n        targets=targets,\n        resize=(512,512),\n        augmentations=aug,\n    )\n\n    test_loader = torch.utils.data.DataLoader(\n        test_dataset, batch_size=16, shuffle=False, num_workers=2\n    )\n\n    model = SEResnext50_32x4d()\n    model.load_state_dict(torch.load(\"../input/trained-model/final_model.pt\"))\n    model.to(device)\n    \n    \n    predictions = Engine.predict(test_loader,model, device)\n    predictions = np.vstack((predictions)).ravel()\n\n    return predictions","metadata":{"execution":{"iopub.status.busy":"2022-09-05T15:02:04.780033Z","iopub.execute_input":"2022-09-05T15:02:04.780708Z","iopub.status.idle":"2022-09-05T15:02:04.801003Z","shell.execute_reply.started":"2022-09-05T15:02:04.780660Z","shell.execute_reply":"2022-09-05T15:02:04.799834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = predict()\nsample = pd.read_csv(\"../input/siim-isic-melanoma-classification/sample_submission.csv\")\nsample.loc[:, \"target\"] = predictions","metadata":{"execution":{"iopub.status.busy":"2022-09-05T15:02:04.803820Z","iopub.execute_input":"2022-09-05T15:02:04.804341Z","iopub.status.idle":"2022-09-05T15:40:07.404939Z","shell.execute_reply.started":"2022-09-05T15:02:04.804292Z","shell.execute_reply":"2022-09-05T15:40:07.403779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min_max_scaled = sample.copy()\ncolumn = 'target'\nmin_max_scaled[column] = (min_max_scaled[column]-min_max_scaled[column].min()) / (min_max_scaled[column].max() - min_max_scaled[column].min())\nprint(min_max_scaled.loc[:,\"target\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-05T15:56:02.815844Z","iopub.execute_input":"2022-09-05T15:56:02.816464Z","iopub.status.idle":"2022-09-05T15:56:02.828218Z","shell.execute_reply.started":"2022-09-05T15:56:02.816405Z","shell.execute_reply":"2022-09-05T15:56:02.827126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min_max_scaled.to_csv(\"min_max_sub.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T15:57:37.847196Z","iopub.execute_input":"2022-09-05T15:57:37.847582Z","iopub.status.idle":"2022-09-05T15:57:37.872730Z","shell.execute_reply.started":"2022-09-05T15:57:37.847544Z","shell.execute_reply":"2022-09-05T15:57:37.871828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}