{"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":"# Aditya attempts","metadata":{"_uuid":"b07ea2e6-cbfb-445e-8d3f-90727ac0d652","_cell_guid":"c44d641b-8999-4de2-8fe2-cdae203e76bb","trusted":true}},{"cell_type":"code","source":"TRAIN_MODE = True","metadata":{"_uuid":"205147fa-b98e-4c72-a352-39af6c8400b4","_cell_guid":"c3387ca3-703d-4617-90c6-f1b62129232e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T10:41:36.768136Z","iopub.execute_input":"2023-07-02T10:41:36.768556Z","iopub.status.idle":"2023-07-02T10:41:36.775924Z","shell.execute_reply.started":"2023-07-02T10:41:36.768526Z","shell.execute_reply":"2023-07-02T10:41:36.774972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Libraries","metadata":{"_uuid":"d84b7987-0bac-4bf9-b622-c0be1238a4e1","_cell_guid":"b2e95362-5497-4bc0-8c8f-2bbb546aeb82","trusted":true}},{"cell_type":"code","source":"!pip install -q -U segmentation-models-pytorch","metadata":{"execution":{"iopub.status.busy":"2023-07-02T10:41:40.97143Z","iopub.execute_input":"2023-07-02T10:41:40.971817Z","iopub.status.idle":"2023-07-02T10:41:55.222267Z","shell.execute_reply.started":"2023-07-02T10:41:40.971789Z","shell.execute_reply":"2023-07-02T10:41:55.220895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm\n\nimport torch\nimport torch.nn as nn\n\nimport cv2\nimport random\nimport seaborn as sns\n\nfrom torch.utils.data import DataLoader\n\nimport segmentation_models_pytorch as smp","metadata":{"_uuid":"fefcab31-d558-4ce0-becf-0f7cba356964","_cell_guid":"48202975-c027-46e3-81f1-0f6457d9de34","collapsed":false,"papermill":{"duration":3.421775,"end_time":"2023-05-15T00:35:16.571754","exception":false,"start_time":"2023-05-15T00:35:13.149979","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T10:41:55.226952Z","iopub.execute_input":"2023-07-02T10:41:55.227298Z","iopub.status.idle":"2023-07-02T10:41:59.913206Z","shell.execute_reply.started":"2023-07-02T10:41:55.22727Z","shell.execute_reply":"2023-07-02T10:41:59.912216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utilities","metadata":{"_uuid":"3a2f32b7-93db-48b5-9629-c2cbc186cd42","_cell_guid":"9e20e421-c1a0-4073-8b01-ce394cfd8ecf","trusted":true}},{"cell_type":"code","source":"def get_device():\n    device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n    print(f\"Using {device} device\")\n    return device\n\ndevice = get_device()","metadata":{"_uuid":"f0b6ce90-af84-436a-a2f7-2603ca0e3cae","_cell_guid":"f80df3b6-8249-431d-9153-f07eed995f82","collapsed":false,"papermill":{"duration":0.064303,"end_time":"2023-05-15T00:35:16.6412","exception":false,"start_time":"2023-05-15T00:35:16.576897","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T10:41:59.915782Z","iopub.execute_input":"2023-07-02T10:41:59.916675Z","iopub.status.idle":"2023-07-02T10:41:59.942237Z","shell.execute_reply.started":"2023-07-02T10:41:59.91664Z","shell.execute_reply":"2023-07-02T10:41:59.941299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Config","metadata":{"_uuid":"3c6fd9fa-571f-42a6-b27d-23656cc9b357","_cell_guid":"50fadcad-f832-452b-b2c3-32f495ed279a","trusted":true}},{"cell_type":"code","source":"class CFG:\n    # Data\n    base_dir = \"../input/google-research-identify-contrails-reduce-global-warming\"\n    train_path = os.path.join(base_dir,\"train\")\n    val_path = os.path.join(base_dir,\"train\")\n    \n    # Train\n    num_epochs = 1\n    batch_size = 24\n    num_workers = 2\n    \n    # Optimizer\n    lr = 0.00001","metadata":{"_uuid":"430ced6e-5564-46d3-b376-c81e24240f12","_cell_guid":"8e6782e3-ea79-4100-af88-91b26d223af4","collapsed":false,"papermill":{"duration":0.012225,"end_time":"2023-05-15T00:35:16.658379","exception":false,"start_time":"2023-05-15T00:35:16.646154","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T10:41:59.945919Z","iopub.execute_input":"2023-07-02T10:41:59.946888Z","iopub.status.idle":"2023-07-02T10:41:59.960198Z","shell.execute_reply.started":"2023-07-02T10:41:59.946853Z","shell.execute_reply":"2023-07-02T10:41:59.959224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset","metadata":{"_uuid":"031b6c4a-f01d-46c5-9024-f37fcbd3547e","_cell_guid":"9d40ec3d-3baf-4df3-bf88-2f3516ae3a61","trusted":true}},{"cell_type":"markdown","source":"### Reference\n[Visualizing Contrails](https://www.kaggle.com/code/inversion/visualizing-contrails)","metadata":{"_uuid":"ae34f558-ab47-4b40-84c4-dda5035c948b","_cell_guid":"8d3a0ecc-bf2c-4da8-a1b1-8e1d8167a936","trusted":true}},{"cell_type":"code","source":"_T11_BOUNDS = (243, 303)\n_CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n_TDIFF_BOUNDS = (-4, 2)\n\ndef normalize_range(data, bounds):\n    \"\"\"Maps data to the range [0, 1].\"\"\"\n    return (data - bounds[0]) / (bounds[1] - bounds[0])\n\ndef normalize_std(spec):\n    return (spec- np.mean(spec))/np.std(spec)\n\nclass Dataset(torch.utils.data.Dataset):\n    def __init__(self, data_path, mode='train'):\n        self.data_path = data_path\n        self.file_name = os.listdir(data_path)\n        self.mode = mode\n        \n\n    def __len__(self):\n        return len(self.file_name)\n\n    def __getitem__(self, i):\n        \n        band11 = np.load(os.path.join(self.data_path, self.file_name[i], 'band_11.npy'))\n        band14 = np.load(os.path.join(self.data_path, self.file_name[i], 'band_14.npy'))\n        band15 = np.load(os.path.join(self.data_path, self.file_name[i], 'band_15.npy'))\n        \n        r = normalize_range(band15 - band14, _TDIFF_BOUNDS)\n        g = normalize_range(band14 - band11, _CLOUD_TOP_TDIFF_BOUNDS)\n        b = normalize_range(band14, _T11_BOUNDS)\n        x = np.transpose(np.clip(np.stack([r, g, b], axis=2), 0, 1)[:,:,:,4],(2,0,1))\n        x = normalize_std(x)\n        \n        if self.mode == 'train':\n            y = np.load(os.path.join(self.data_path, self.file_name[i], 'human_pixel_masks.npy')).astype(np.float32).transpose(2,0,1)\n        elif self.mode == 'test':\n            y = self.file_name[i]\n        else:\n            y = None\n        \n        return x, y","metadata":{"_uuid":"dc174e0e-2b31-4b0a-8c3c-36afb448e21a","_cell_guid":"ca1bdb47-b7d9-4649-8af4-838c49ba080f","collapsed":false,"papermill":{"duration":0.017522,"end_time":"2023-05-15T00:35:16.699306","exception":false,"start_time":"2023-05-15T00:35:16.681784","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T10:41:59.961613Z","iopub.execute_input":"2023-07-02T10:41:59.962533Z","iopub.status.idle":"2023-07-02T10:41:59.976Z","shell.execute_reply.started":"2023-07-02T10:41:59.962499Z","shell.execute_reply":"2023-07-02T10:41:59.974949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = Dataset(CFG.train_path)\na, b = dataset[5]\n\nplt.figure(figsize=(12, 6))\nax = plt.subplot(1, 2, 1)\nax.imshow(np.transpose(a,(1,2,0)))\nax = plt.subplot(1, 2, 2)\nax.imshow(np.transpose(b,(1,2,0)), interpolation='none') \nplt.show()","metadata":{"_uuid":"3391fd20-e9aa-4ea2-a314-2190e86a8dd3","_cell_guid":"09794994-b385-4e9e-bb38-bd83b8b66670","collapsed":false,"papermill":{"duration":0.911202,"end_time":"2023-05-15T00:35:17.615302","exception":false,"start_time":"2023-05-15T00:35:16.7041","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T10:41:59.977489Z","iopub.execute_input":"2023-07-02T10:41:59.977871Z","iopub.status.idle":"2023-07-02T10:42:00.466161Z","shell.execute_reply.started":"2023-07-02T10:41:59.977837Z","shell.execute_reply":"2023-07-02T10:42:00.46526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"_uuid":"881103e8-f2d3-4c2f-b5b9-b6baf5e4813b","_cell_guid":"08aaaaf1-64c3-4cc0-a6f1-9d81c329539e","trusted":true}},{"cell_type":"code","source":"# class Model:\n#     Insert neural network here","metadata":{"_uuid":"1f8bfce1-3c5e-4981-a382-f6a325ab2d50","_cell_guid":"d419f71d-ea2a-4541-a4df-68a2aab93f0d","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ENCODER = 'resnet101'\nENCODER_WEIGHTS = 'imagenet'\nACTIVATION = 'softmax' # could be None for logits or 'softmax2d' for multiclass segmentation\n\n# create segmentation model with pretrained encoder\nmodel = smp.DeepLabV3Plus(\n    encoder_name=ENCODER, \n    encoder_weights=ENCODER_WEIGHTS, \n    activation=ACTIVATION,\n).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T10:44:27.044832Z","iopub.execute_input":"2023-07-02T10:44:27.045891Z","iopub.status.idle":"2023-07-02T10:44:42.410709Z","shell.execute_reply.started":"2023-07-02T10:44:27.045832Z","shell.execute_reply":"2023-07-02T10:44:42.409635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = torch.load('/kaggle/input/ic2rgw-pytorch-samplemodel/sample.pth').to(device);","metadata":{"_uuid":"d08ebc93-56f0-46a6-ae0b-3f25cfc8b57d","_cell_guid":"a2b1e8cd-fc59-4850-a54b-bca3445a3a20","collapsed":false,"papermill":{"duration":6.104629,"end_time":"2023-05-15T00:35:23.727688","exception":false,"start_time":"2023-05-15T00:35:17.623059","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Metric","metadata":{"_uuid":"9542eabb-56b4-41f4-b25d-f985fbe66239","_cell_guid":"39696601-b3de-4979-b45b-b33d68ac244c","trusted":true}},{"cell_type":"code","source":"class Dice(nn.Module):\n    def __init__(self, weight=None, size_average=True):\n        super(Dice, self).__init__()\n        self.sigmoid = nn.Sigmoid()\n\n    def forward(self, inputs, targets, smooth=1):\n        \n        inputs = self.sigmoid(inputs)       \n        \n        inputs = inputs.view(-1)\n        targets = targets.view(-1)\n        \n        intersection = (inputs * targets).sum()                            \n        dice = (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth)  \n        \n        return dice\n    \ndice = Dice()","metadata":{"_uuid":"0e8d2066-dc8e-412e-b3d6-47a1b600873d","_cell_guid":"3f2d38ce-722d-494f-806f-0a102c5696d7","collapsed":false,"papermill":{"duration":0.018966,"end_time":"2023-05-15T00:35:23.758237","exception":false,"start_time":"2023-05-15T00:35:23.739271","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T10:44:45.007883Z","iopub.execute_input":"2023-07-02T10:44:45.008907Z","iopub.status.idle":"2023-07-02T10:44:45.016882Z","shell.execute_reply.started":"2023-07-02T10:44:45.008869Z","shell.execute_reply":"2023-07-02T10:44:45.01586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train","metadata":{"_uuid":"a832fdc8-f1f5-4498-9e3e-c558751a2d51","_cell_guid":"10b3f809-6026-44af-86bf-9df47f1dd939","trusted":true}},{"cell_type":"code","source":"if TRAIN_MODE == True:\n    num_epochs = CFG.num_epochs\n    batch_size = CFG.batch_size\n    num_workers = CFG.num_workers\n\n    train_dataset = Dataset(CFG.train_path, mode='train')\n    val_dataset = Dataset(CFG.val_path, mode='train')\n\n    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers, drop_last=True, pin_memory=True)\n    val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers, drop_last=True, pin_memory=True)\n\n    optimizer = torch.optim.AdamW(model.parameters(), lr=CFG.lr)\n    criterion = nn.BCEWithLogitsLoss()\n    \n    for epoch in range(num_epochs):\n        train_loss, val_loss = 0, 0\n        train_dice, val_dice = 0, 0\n        n_train, n_val = 0, 0\n\n        model.train()\n        \n        for i_train, (X, y) in enumerate(tqdm(train_loader)):   \n            n_train += len(y)\n            X = X.to(device)\n            y = y.to(device)\n            \n            \n            optimizer.zero_grad()\n            pred = model(X)\n            loss = criterion(torch.flatten(pred), torch.flatten(y))\n            dice_temp = dice(torch.flatten(pred), torch.flatten(y))\n\n\n            loss.backward()\n\n            optimizer.step()\n            train_loss += loss.item()\n            train_dice += dice_temp.item()\n\n        model.eval()\n        \n        with torch.no_grad():\n            for i_val, (X, y) in enumerate(val_loader):   \n                n_val += len(y)\n                X = X.to(device)\n                y = y.to(device)\n        \n                pred = model(X)\n                loss = criterion(torch.flatten(pred), torch.flatten(y))\n                dice_temp = dice(torch.flatten(pred), torch.flatten(y))\n\n                val_loss += loss.item()\n                val_dice += dice_temp.item()\n        \n        print (f'Epoch [{(epoch+1)}/{num_epochs}], loss: {train_loss/n_train:.5f}, dice: {(train_dice+1e-23)/i_train:.5f}, val_loss: {val_loss/n_val:.5f}, val_dice: {(val_dice+1e-23)/i_val:.5f}')\n        \n\nprint('Done')","metadata":{"_uuid":"095b81a6-63f7-4e07-a68b-ffee777dda85","_cell_guid":"e8d3278a-f04f-4546-bbb4-75b8287fa831","collapsed":false,"papermill":{"duration":22180.769991,"end_time":"2023-05-15T06:45:04.728989","exception":false,"start_time":"2023-05-15T00:35:23.958998","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"scrolled":true,"execution":{"iopub.status.busy":"2023-07-02T10:53:55.155183Z","iopub.execute_input":"2023-07-02T10:53:55.155601Z","iopub.status.idle":"2023-07-02T11:23:23.667367Z","shell.execute_reply.started":"2023-07-02T10:53:55.155565Z","shell.execute_reply":"2023-07-02T11:23:23.666028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict and submit","metadata":{"_uuid":"fbc956c3-d2ee-466c-b6a7-a189af57e2a7","_cell_guid":"51cbb460-d6e5-48a5-95fd-c52e7605f44a","trusted":true}},{"cell_type":"markdown","source":"### Reference\n[Contrails - RLE Submission](https://www.kaggle.com/code/inversion/contrails-rle-submission)","metadata":{"_uuid":"2a3c3c65-aa03-42d0-a401-df03d20d1d39","_cell_guid":"43b49cdd-3d8d-4696-bd83-e796d59c167f","trusted":true}},{"cell_type":"code","source":"def rle_encode(x, fg_val=1):\n    \"\"\"\n    Args:\n        x:  numpy array of shape (height, width), 1 - mask, 0 - background\n    Returns: run length encoding as list\n    \"\"\"\n\n    dots = np.where(\n        x.T.flatten() == fg_val)[0]  # .T sets Fortran order down-then-right\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 run_lengths\n\n\ndef list_to_string(x):\n    \"\"\"\n    Converts list to a string representation\n    Empty list returns '-'\n    \"\"\"\n    if x: # non-empty list\n        s = str(x).replace(\"[\", \"\").replace(\"]\", \"\").replace(\",\", \"\")\n    else:\n        s = '-'\n    return s\n\n\ndef rle_decode(mask_rle, shape=(256, 256)):\n    '''\n    mask_rle: run-length as string formatted (start length)\n              empty predictions need to be encoded with '-'\n    shape: (height, width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    '''\n\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    if mask_rle != '-': \n        s = mask_rle.split()\n        starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n        starts -= 1\n        ends = starts + lengths\n        for lo, hi in zip(starts, ends):\n            img[lo:hi] = 1\n    return img.reshape(shape, order='F')  # Needed to align to RLE direction","metadata":{"_uuid":"aa81d10a-4bca-45fe-b9c3-0c3080857f05","_cell_guid":"78fa67bf-54ce-4588-9fb6-397509bfe2b0","collapsed":false,"_kg_hide-input":true,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T13:17:04.911086Z","iopub.execute_input":"2023-07-02T13:17:04.91145Z","iopub.status.idle":"2023-07-02T13:17:04.92853Z","shell.execute_reply.started":"2023-07-02T13:17:04.911414Z","shell.execute_reply":"2023-07-02T13:17:04.927375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_recs = os.listdir(os.path.join(CFG.base_dir,\"test\"))\nprint(test_recs)","metadata":{"_uuid":"6bf1aa6b-2f2a-4c5d-a3e2-8153653db2d2","_cell_guid":"ebd790d0-a951-4616-b6b7-a57730624a5b","collapsed":false,"_kg_hide-input":true,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T13:17:14.484478Z","iopub.execute_input":"2023-07-02T13:17:14.485433Z","iopub.status.idle":"2023-07-02T13:17:14.849889Z","shell.execute_reply.started":"2023-07-02T13:17:14.485374Z","shell.execute_reply":"2023-07-02T13:17:14.84847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 1\nnum_workers = 2\n\ntest_dataset = Dataset('/kaggle/input/google-research-identify-contrails-reduce-global-warming/test', mode='test')\n\ntest_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers, drop_last=True, pin_memory=True)","metadata":{"_uuid":"6a7ab253-4e0f-4776-862b-551c8488180e","_cell_guid":"b009c195-4523-4b83-b8f7-847c4aad0047","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T11:23:24.563385Z","iopub.status.idle":"2023-07-02T11:23:24.564239Z","shell.execute_reply.started":"2023-07-02T11:23:24.563959Z","shell.execute_reply":"2023-07-02T11:23:24.563987Z"},"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\nmodel.eval()\n\nwith torch.no_grad():\n    for X, rec in test_loader:\n        X = X.to(device)\n        pred = (model(X)['out']).cpu().detach().numpy().copy()[0,0,:,:] \n        mask = np.zeros((256, 256))\n        mask[pred<0.5] = 0\n        mask[pred>0.5] = 1\n        \n        submission.loc[int(rec[0]), 'encoded_pixels'] = list_to_string(rle_encode(mask))\n\n\nsubmission.head()","metadata":{"_uuid":"2f49219b-1d94-438c-8e52-913e00693123","_cell_guid":"2ce33af9-3a52-4fbe-802c-8c247ab8f3af","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T11:23:24.565607Z","iopub.status.idle":"2023-07-02T11:23:24.566359Z","shell.execute_reply.started":"2023-07-02T11:23:24.566115Z","shell.execute_reply":"2023-07-02T11:23:24.566137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"_uuid":"ae1a2c4c-2b2d-464a-b761-619ea2cd039e","_cell_guid":"09b71563-c7f6-48fc-aded-b723c8ae961b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-02T11:23:24.567691Z","iopub.status.idle":"2023-07-02T11:23:24.568465Z","shell.execute_reply.started":"2023-07-02T11:23:24.568211Z","shell.execute_reply":"2023-07-02T11:23:24.568234Z"},"trusted":true},"execution_count":null,"outputs":[]}]}