{"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 os\nimport gc\nimport numpy as np\nimport pandas as pd\nfrom tqdm.notebook import tqdm\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-25T15:11:04.580647Z","iopub.execute_input":"2023-05-25T15:11:04.581103Z","iopub.status.idle":"2023-05-25T15:11:07.843947Z","shell.execute_reply.started":"2023-05-25T15:11:04.581058Z","shell.execute_reply":"2023-05-25T15:11:07.842887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Conv(nn.Module):\n    def __init__(self, in_channels, out_channels):\n        super(Conv, self).__init__()\n        self.layers = nn.Sequential(\n            nn.Conv2d(in_channels, out_channels, 3, bias=False),\n            nn.BatchNorm2d(out_channels),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(out_channels, out_channels, 3, bias=False),\n            nn.BatchNorm2d(out_channels),\n            nn.ReLU(inplace=True)\n        )\n    def forward(self, x):\n        return self.layers(x)\n\n\nclass U_Net(nn.Module):\n    def __init__(self, n_channels, n_classes):\n        super(U_Net, self).__init__()\n        self.conv0 = Conv(n_channels, 64)\n        self.conv1 = Conv(64, 128)\n        self.conv2 = Conv(128, 256)\n        self.conv3 = Conv(256, 512)\n        self.conv4 = Conv(512, 1024)\n        self.conv5 = Conv(1024, 512)\n        self.conv6 = Conv(512, 256)\n        self.conv7 = Conv(256, 128)\n        self.conv8 = Conv(128, 64)\n        self.maxpool = nn.MaxPool2d(2)\n        self.convT0 = nn.ConvTranspose2d(1024, 512, kernel_size=2, stride=2)\n        self.convT1 = nn.ConvTranspose2d(512, 256, kernel_size=2, stride=2)\n        self.convT2 = nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2)\n        self.convT3 = nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2)\n        self.outconv = nn.Conv2d(64, n_classes, 1)\n        \n        \n    def forward(self, x):\n        # contracting path\n        x0 = self.conv0(x)\n        x1 = self.conv1(self.maxpool(x0))\n        x2 = self.conv2(self.maxpool(x1))\n        x3 = self.conv3(self.maxpool(x2))\n        x = self.conv4(self.maxpool(x3))\n        # expanding path\n        x = self.conv5(self.concat(self.convT0(x), x3))\n        x = self.conv6(self.concat(self.convT1(x), x2))\n        x = self.conv7(self.concat(self.convT2(x), x1))\n        x = self.conv8(self.concat(self.convT3(x), x0))\n        return self.outconv(x)\n    \n    \n    @staticmethod\n    def concat(x_e, x_c):\n        diff_h = x_c.size()[2] - x_e.size()[2]\n        diff_w = x_c.size()[3] - x_e.size()[3]\n        x_c = x_c[:, :, diff_h//2:-(diff_h - diff_h//2), diff_w//2:-(diff_w - diff_w//2)]\n        return torch.cat([x_c, x_e], dim=1)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-05-25T15:11:07.84594Z","iopub.execute_input":"2023-05-25T15:11:07.846724Z","iopub.status.idle":"2023-05-25T15:11:07.872652Z","shell.execute_reply.started":"2023-05-25T15:11:07.84668Z","shell.execute_reply":"2023-05-25T15:11:07.869141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = U_Net(3, 2)\nckpt_path = \"/kaggle/input/u-net-pth/u-net (5).pth\"\nmodel.load_state_dict(torch.load(ckpt_path))\nmodel.to(\"cuda\")","metadata":{"execution":{"iopub.status.busy":"2023-05-25T15:11:07.875604Z","iopub.execute_input":"2023-05-25T15:11:07.877228Z","iopub.status.idle":"2023-05-25T15:11:13.19378Z","shell.execute_reply.started":"2023-05-25T15:11:07.877177Z","shell.execute_reply":"2023-05-25T15:11:13.192838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(y_pred, fg_val=1):\n    def list_to_string(x):\n        if x:\n            s = str(x).replace(\"[\", \"\").replace(\"]\", \"\").replace(\",\", \"\")\n        else:\n            s = '-'\n        return s\n    dots = np.where(\n        y_pred.T.flatten() == fg_val)[0]\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if b > prev + 1:\n            run_lengths.extend((b + 1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return list_to_string(run_lengths)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T15:11:13.196597Z","iopub.execute_input":"2023-05-25T15:11:13.199151Z","iopub.status.idle":"2023-05-25T15:11:13.206736Z","shell.execute_reply.started":"2023-05-25T15:11:13.199114Z","shell.execute_reply":"2023-05-25T15:11:13.205438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def false_color(band11, band14, band15):\n    def normalize(band, bounds):\n        return (band - bounds[0]) / (bounds[1] - bounds[0])    \n    _T11_BOUNDS = (243, 303)\n    _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n    _TDIFF_BOUNDS = (-4, 2)\n    r = normalize(band15 - band14, _TDIFF_BOUNDS)\n    g = normalize(band14 - band11, _CLOUD_TOP_TDIFF_BOUNDS)\n    b = normalize(band14, _T11_BOUNDS)\n    return np.clip(np.stack([r, g, b], axis=2), 0, 1)\n\n\nclass ICRGWDataset(Dataset):\n    def __init__(self, tar_path, ids, padding_size):\n        self.tar_path = tar_path\n        self.ids = ids\n        self.padding_size = padding_size\n    def __len__(self):\n        return len(self.ids)\n    def __getitem__(self, idx):\n        N_TIMES_BEFORE = 4\n        sample_path = f\"{tar_path}/{self.ids[idx]}\"\n        band11 = np.load(f\"{sample_path}/band_11.npy\")[..., N_TIMES_BEFORE]\n        band14 = np.load(f\"{sample_path}/band_14.npy\")[..., N_TIMES_BEFORE]\n        band15 = np.load(f\"{sample_path}/band_15.npy\")[..., N_TIMES_BEFORE]\n        image = false_color(band11, band14, band15)\n        image = torch.Tensor(image)\n        image = image.permute(2, 0, 1)\n        padding_size = self.padding_size\n        image = F.pad(image, (padding_size, padding_size, padding_size, padding_size), mode='reflect')\n        return image, self.ids[idx]\n    \n    \ntar_path = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/test\"\nids = os.listdir(tar_path)\ndataloader = DataLoader(ICRGWDataset(tar_path, ids, 100), 1, shuffle=False, num_workers=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T15:11:13.208807Z","iopub.execute_input":"2023-05-25T15:11:13.209157Z","iopub.status.idle":"2023-05-25T15:11:13.227496Z","shell.execute_reply.started":"2023-05-25T15:11:13.209111Z","shell.execute_reply":"2023-05-25T15:11:13.226562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/google-research-identify-contrails-reduce-global-warming/sample_submission.csv', index_col='record_id')\n\nfor X, img_id in tqdm(dataloader):\n    X = X.to(\"cuda\")\n    pred = model(X)\n    pred = pred[:, :, 2:-2, 2:-2]\n    pred = F.softmax(pred, dim=1)\n    pred[:, 1, :, :] += 0.1\n    pred = F.softmax(pred, dim=1)\n    pred = torch.argmax(pred, dim=1)\n    pred = pred.cpu().squeeze().numpy()\n    submission.loc[int(img_id[0]), 'encoded_pixels'] = rle_encode(pred)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T15:11:13.229647Z","iopub.execute_input":"2023-05-25T15:11:13.230676Z","iopub.status.idle":"2023-05-25T15:11:15.951687Z","shell.execute_reply.started":"2023-05-25T15:11:13.230642Z","shell.execute_reply":"2023-05-25T15:11:15.9506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-25T15:11:15.953822Z","iopub.execute_input":"2023-05-25T15:11:15.95461Z","iopub.status.idle":"2023-05-25T15:11:15.970928Z","shell.execute_reply.started":"2023-05-25T15:11:15.954569Z","shell.execute_reply":"2023-05-25T15:11:15.969898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-25T15:11:15.972384Z","iopub.execute_input":"2023-05-25T15:11:15.972998Z","iopub.status.idle":"2023-05-25T15:11:15.98244Z","shell.execute_reply.started":"2023-05-25T15:11:15.972964Z","shell.execute_reply":"2023-05-25T15:11:15.981548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}