{"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 numpy as np\nimport torch\nimport torchvision.transforms as T\n\nfrom torch.utils.data import Dataset, DataLoader\n\nPATH = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/\"\n\nclass ContrailsDataset(Dataset):\n    def __init__(self, split=\"train\", mode=\"single\"):\n        self.split = split\n        self.mode = mode\n        self.path = f'{PATH}{split}/'\n        self.examples = os.listdir(self.path)\n        self.normalize_image = T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))\n    \n    def read_record(self, directory):\n        record_data = {}\n        for x in [\n            \"band_11\", \n            \"band_14\", \n            \"band_15\"\n        ]:\n\n            record_data[x] = np.load(os.path.join(directory, x + \".npy\"))\n\n        return record_data\n\n    def normalize_range(self, data, bounds):\n        \"\"\"Maps data to the range [0, 1].\"\"\"\n        return (data - bounds[0]) / (bounds[1] - bounds[0])\n    \n    def get_false_color(self, record_data):\n        _T11_BOUNDS = (243, 303)\n        _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n        _TDIFF_BOUNDS = (-4, 2)\n\n        r = self.normalize_range(record_data[\"band_15\"] - record_data[\"band_14\"], _TDIFF_BOUNDS)\n        g = self.normalize_range(record_data[\"band_14\"] - record_data[\"band_11\"], _CLOUD_TOP_TDIFF_BOUNDS)\n        b = self.normalize_range(record_data[\"band_14\"], _T11_BOUNDS)\n        false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n        if self.mode == \"single\":\n            return false_color[..., 4]\n        else:\n            return false_color\n    \n    def __getitem__(self, index):\n        path = f\"{self.path}{self.examples[index]}\"\n        data = self.read_record(path)    \n        \n        img = self.get_false_color(data)\n        \n        if self.mode == \"single\":\n            img = torch.tensor(np.reshape(img, (256, 256, 3, -1))).to(torch.float32).permute(3, 2, 0, 1).squeeze()\n        else:\n            img = torch.tensor(np.reshape(img, (256, 256, 3, -1))).to(torch.float32).permute(3, 2, 0, 1)\n        \n        img = self.normalize_image(img)\n        \n        if self.split in [\"train\", \"validation\"]:\n            label = np.load(os.path.join(path, \"human_pixel_masks.npy\")).squeeze()\n            label = torch.Tensor(label).to(torch.int64)\n            return img.float(), label\n        \n        return img.float()\n    \n    def __len__(self):\n        return len(self.examples)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:26.689039Z","iopub.execute_input":"2023-07-21T19:12:26.689464Z","iopub.status.idle":"2023-07-21T19:12:30.318918Z","shell.execute_reply.started":"2023-07-21T19:12:26.689432Z","shell.execute_reply":"2023-07-21T19:12:30.317965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append(\"/kaggle/input/smp-github/segmentation_models.pytorch-master\")\nsys.path.append(\"/kaggle/input/pretrained-models-pytorch\")\nsys.path.append(\"/kaggle/input/efficientnet-pytorch\")","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:30.321192Z","iopub.execute_input":"2023-07-21T19:12:30.321799Z","iopub.status.idle":"2023-07-21T19:12:30.328011Z","shell.execute_reply.started":"2023-07-21T19:12:30.321764Z","shell.execute_reply":"2023-07-21T19:12:30.326893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds_multi = ContrailsDataset(\"train\", \"multi\")\nval_ds_multi = ContrailsDataset(\"validation\", \"multi\")","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:30.329708Z","iopub.execute_input":"2023-07-21T19:12:30.330177Z","iopub.status.idle":"2023-07-21T19:12:30.583687Z","shell.execute_reply.started":"2023-07-21T19:12:30.330144Z","shell.execute_reply":"2023-07-21T19:12:30.582645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data: Timestep x Channel x Width x Height","metadata":{}},{"cell_type":"code","source":"train_ds_multi[0][0].shape","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:30.585129Z","iopub.execute_input":"2023-07-21T19:12:30.585714Z","iopub.status.idle":"2023-07-21T19:12:30.783561Z","shell.execute_reply.started":"2023-07-21T19:12:30.585678Z","shell.execute_reply":"2023-07-21T19:12:30.782579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Label: Width x Height","metadata":{}},{"cell_type":"code","source":"train_ds_multi[0][1].shape","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:30.786675Z","iopub.execute_input":"2023-07-21T19:12:30.786993Z","iopub.status.idle":"2023-07-21T19:12:30.824046Z","shell.execute_reply.started":"2023-07-21T19:12:30.786967Z","shell.execute_reply":"2023-07-21T19:12:30.823092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Copy from smp config:","metadata":{}},{"cell_type":"code","source":"# config\nconfig = {\n    \"data_path\": \"/kaggle/input/contrails-images-ash-color\",\n    \"model\": {\n        \"encoder_name\": \"timm-efficientnet-b3\",\n        \"loss_smooth\": 1.0,\n        \"optimizer_params\": {\"lr\": 0.003, \"weight_decay\": 0.01},\n        \"scheduler\": {\n            \"name\": \"CosineAnnealingLR\",\n            \"params\": {\n                \"CosineAnnealingLR\": {\"T_max\": 500, \"eta_min\": 1e-06, \"last_epoch\": -1},\n                \"ReduceLROnPlateau\": {\n                    \"factor\": 0.31622776601,\n                    \"mode\": \"min\",\n                    \"patience\": 4,\n                    \"verbose\": True,\n                },\n            },\n        },\n        \"seg_model\": \"Unet++\",\n    },\n    \"output_dir\": \"models\",\n    \"progress_bar_refresh_rate\": 50,\n    \"seed\": 42,\n    \"train_bs\": 32,\n    \"trainer\": {\n        \"enable_progress_bar\": True,\n        \"max_epochs\": 30,\n        \"min_epochs\": 30,\n    },\n    \"valid_bs\": 64,\n    \"workers\": 2,\n}","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:30.825585Z","iopub.execute_input":"2023-07-21T19:12:30.825955Z","iopub.status.idle":"2023-07-21T19:12:30.835214Z","shell.execute_reply.started":"2023-07-21T19:12:30.825921Z","shell.execute_reply":"2023-07-21T19:12:30.833203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Datasource for checkpoint:","metadata":{}},{"cell_type":"code","source":"chkpt_path = \"/kaggle/input/gr-icrgw-unet-efficientnet-ckpt/model.ckpt\"","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:30.836705Z","iopub.execute_input":"2023-07-21T19:12:30.837854Z","iopub.status.idle":"2023-07-21T19:12:30.844678Z","shell.execute_reply.started":"2023-07-21T19:12:30.837814Z","shell.execute_reply":"2023-07-21T19:12:30.843803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pytorch_lightning as pl\nimport segmentation_models_pytorch as smp\n\nseg_models = {\n    \"Unet\": smp.Unet,\n    \"Unet++\": smp.UnetPlusPlus,\n    \"MAnet\": smp.MAnet,\n    \"Linknet\": smp.Linknet,\n    \"FPN\": smp.FPN,\n    \"PSPNet\": smp.PSPNet,\n    \"PAN\": smp.PAN,\n    \"DeepLabV3\": smp.DeepLabV3,\n    \"DeepLabV3+\": smp.DeepLabV3Plus,\n}\n\nclass LitModel(pl.LightningModule):\n\n    def __init__(self):\n        super().__init__()\n        self.model = seg_models[config[\"model\"][\"seg_model\"]](\n            encoder_name=config[\"model\"][\"encoder_name\"],\n            encoder_weights=None,\n            in_channels=3,\n            classes=1,\n            activation=None,\n        )\n\n    def forward(self, batch):\n        return self.model(batch)\n\nmodel = LitModel().load_from_checkpoint(chkpt_path)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.to(device)\nmodel.eval()\nmodel.zero_grad()","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:30.846991Z","iopub.execute_input":"2023-07-21T19:12:30.847798Z","iopub.status.idle":"2023-07-21T19:12:50.419685Z","shell.execute_reply.started":"2023-07-21T19:12:30.84776Z","shell.execute_reply":"2023-07-21T19:12:50.418665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n    images, label = train_ds_multi[i]\n    path = train_ds_multi.examples[i]\n    print(f\"{path}: {images.shape}, {label.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:50.420892Z","iopub.execute_input":"2023-07-21T19:12:50.421275Z","iopub.status.idle":"2023-07-21T19:12:51.199287Z","shell.execute_reply.started":"2023-07-21T19:12:50.421242Z","shell.execute_reply":"2023-07-21T19:12:51.198281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nexamples_to_check = [100, 80, 64, 42, 3]\n\nnrows, ncols = (len(examples_to_check)*2,9)\n\nfig, axs = plt.subplots(nrows, ncols, figsize = (16,16))\n\nfor i, img in enumerate(examples_to_check):\n    label = train_ds_multi[img][1]\n    preds = model(train_ds_multi[img][0].to(device))\n    \n    axs[2*i, 0].axis('off')\n    \n    for j in range(8):\n        subimg = train_ds_multi[img][0][j].to(\"cpu\")\n        axs[2*i,j+1].imshow(subimg.to(\"cpu\").detach().squeeze().permute(1,2,0))\n        axs[2*i,j+1].axis('off')\n    \n    axs[2*i+1, 0].imshow(label)\n    axs[2*i+1, 0].axis('off')\n    \n    for j in range(8):\n        axs[2*i+1, j+1].imshow(preds[j].to(\"cpu\").detach().squeeze())\n        axs[2*i+1, j+1].axis('off')\n        \nplt.show()\n\ndel preds","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:12:51.200823Z","iopub.execute_input":"2023-07-21T19:12:51.201188Z","iopub.status.idle":"2023-07-21T19:13:01.232992Z","shell.execute_reply.started":"2023-07-21T19:12:51.201155Z","shell.execute_reply":"2023-07-21T19:13:01.231805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_dir = \"/kaggle/working/train\"\n!mkdir -p $out_dir\n\nfor i, (img, _) in enumerate(train_ds_multi):\n    name = train_ds_multi.examples[i]\n    preds = model(img.to(device))\n    preds = preds.to(\"cpu\").detach().squeeze()\n    preds = np.float16(preds)\n    np.savez_compressed(f\"{out_dir}/{name}.npz\", preds)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:13:01.234946Z","iopub.execute_input":"2023-07-21T19:13:01.235299Z","iopub.status.idle":"2023-07-21T19:13:28.581518Z","shell.execute_reply.started":"2023-07-21T19:13:01.23527Z","shell.execute_reply":"2023-07-21T19:13:28.58037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_dir = \"/kaggle/working/validation\"\n!mkdir -p $out_dir\n\nfor i, (img, _) in enumerate(val_ds_multi):\n    name = val_ds_multi.examples[i]\n    preds = model(img.to(device))\n    preds = preds.to(\"cpu\").detach().squeeze()\n    preds = np.float16(preds)\n    np.savez_compressed(f\"{out_dir}/{name}.npz\", preds)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:36:52.186253Z","iopub.execute_input":"2023-07-21T19:36:52.18667Z","iopub.status.idle":"2023-07-21T19:37:12.697052Z","shell.execute_reply.started":"2023-07-21T19:36:52.186636Z","shell.execute_reply":"2023-07-21T19:37:12.69589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"How to load files:","metadata":{}},{"cell_type":"code","source":"np.load(\"/kaggle/working/train/7457695218848685981.npz\")[\"arr_0\"]","metadata":{"execution":{"iopub.status.busy":"2023-07-21T19:40:23.825152Z","iopub.execute_input":"2023-07-21T19:40:23.825507Z","iopub.status.idle":"2023-07-21T19:40:23.86858Z","shell.execute_reply.started":"2023-07-21T19:40:23.82548Z","shell.execute_reply":"2023-07-21T19:40:23.865741Z"},"trusted":true},"execution_count":null,"outputs":[]}]}