{"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":"markdown","source":"<div style=\"border-radius:10px; border:#DEB887 solid; padding: 15px; background-color: #FFFAF0; font-size:100%; text-align:left;\">\n    <h2 style = \"color : green;\"><b>🌍Google Research: Identify Contrails to Reduce Global Warming⚠️ </b></h2>\n    </div>","metadata":{}},{"cell_type":"code","source":"import os\nimport gc\nimport numpy as np\nimport pandas as pd\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-05T04:25:17.895623Z","iopub.execute_input":"2023-06-05T04:25:17.896062Z","iopub.status.idle":"2023-06-05T04:25:17.900833Z","shell.execute_reply.started":"2023-06-05T04:25:17.896032Z","shell.execute_reply":"2023-06-05T04:25:17.899895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from 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":{"execution":{"iopub.status.busy":"2023-06-05T04:25:17.902488Z","iopub.execute_input":"2023-06-05T04:25:17.904355Z","iopub.status.idle":"2023-06-05T04:25:19.611403Z","shell.execute_reply.started":"2023-06-05T04:25:17.904323Z","shell.execute_reply":"2023-06-05T04:25:19.610399Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T04:25:19.616975Z","iopub.execute_input":"2023-06-05T04:25:19.620378Z","iopub.status.idle":"2023-06-05T04:25:19.630766Z","shell.execute_reply.started":"2023-06-05T04:25:19.62034Z","shell.execute_reply":"2023-06-05T04:25:19.629677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class 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-06-05T04:25:19.633715Z","iopub.execute_input":"2023-06-05T04:25:19.634638Z","iopub.status.idle":"2023-06-05T04:25:19.65295Z","shell.execute_reply.started":"2023-06-05T04:25:19.634594Z","shell.execute_reply":"2023-06-05T04:25:19.65194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = U_Net(3, 2)\nckpt_path = \"/kaggle/input/u-net-pth/u-net (4).pth\"\nmodel.load_state_dict(torch.load(ckpt_path))\nmodel.to(\"cuda\")","metadata":{"execution":{"iopub.status.busy":"2023-06-05T04:25:19.654519Z","iopub.execute_input":"2023-06-05T04:25:19.655193Z","iopub.status.idle":"2023-06-05T04:25:25.580046Z","shell.execute_reply.started":"2023-06-05T04:25:19.655158Z","shell.execute_reply":"2023-06-05T04:25:25.578946Z"},"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-06-05T04:25:25.582152Z","iopub.execute_input":"2023-06-05T04:25:25.58274Z","iopub.status.idle":"2023-06-05T04:25:25.590309Z","shell.execute_reply.started":"2023-06-05T04:25:25.582705Z","shell.execute_reply":"2023-06-05T04:25:25.589302Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T04:25:25.591442Z","iopub.execute_input":"2023-06-05T04:25:25.591938Z","iopub.status.idle":"2023-06-05T04:25:25.60576Z","shell.execute_reply.started":"2023-06-05T04:25:25.5919Z","shell.execute_reply":"2023-06-05T04:25:25.604868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class 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]","metadata":{"execution":{"iopub.status.busy":"2023-06-05T04:25:25.608713Z","iopub.execute_input":"2023-06-05T04:25:25.609098Z","iopub.status.idle":"2023-06-05T04:25:25.618474Z","shell.execute_reply.started":"2023-06-05T04:25:25.609068Z","shell.execute_reply":"2023-06-05T04:25:25.6175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tar_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-06-05T04:25:25.620224Z","iopub.execute_input":"2023-06-05T04:25:25.620607Z","iopub.status.idle":"2023-06-05T04:25:25.631177Z","shell.execute_reply.started":"2023-06-05T04:25:25.620575Z","shell.execute_reply":"2023-06-05T04:25:25.630215Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2023-06-05T04:25:25.63538Z","iopub.execute_input":"2023-06-05T04:25:25.635687Z","iopub.status.idle":"2023-06-05T04:25:25.659979Z","shell.execute_reply.started":"2023-06-05T04:25:25.635664Z","shell.execute_reply":"2023-06-05T04:25:25.659176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for 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 = 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-06-05T04:25:25.661274Z","iopub.execute_input":"2023-06-05T04:25:25.661668Z","iopub.status.idle":"2023-06-05T04:25:28.519222Z","shell.execute_reply.started":"2023-06-05T04:25:25.661637Z","shell.execute_reply":"2023-06-05T04:25:28.518151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-05T04:25:28.520851Z","iopub.execute_input":"2023-06-05T04:25:28.524045Z","iopub.status.idle":"2023-06-05T04:25:28.531592Z","shell.execute_reply.started":"2023-06-05T04:25:28.524014Z","shell.execute_reply":"2023-06-05T04:25:28.530657Z"},"trusted":true},"execution_count":null,"outputs":[]}]}