{"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 cv2\nimport gc\nimport glob\nimport json\nimport time\nimport math\nimport torch\nimport torchvision.transforms.functional as F\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport random as rn\nfrom tqdm import tqdm\n\n# import segmentation_models as sm\n# sm.set_framework('tf.keras')\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\nprint(tf.__version__)","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":14.178206,"end_time":"2023-06-15T16:53:54.124047","exception":false,"start_time":"2023-06-15T16:53:39.945841","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-08T17:03:20.062892Z","iopub.execute_input":"2023-08-08T17:03:20.064039Z","iopub.status.idle":"2023-08-08T17:03:32.699394Z","shell.execute_reply.started":"2023-08-08T17:03:20.064Z","shell.execute_reply":"2023-08-08T17:03:32.697625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def clone_function(layer):\n#     c = layer.get_config()\n#     s = json.dumps(c)\n#     s = s.replace('mixed_bfloat16', 'float32')\n#     c = json.loads(s)\n#     return layer.__class__.from_config(c)","metadata":{"papermill":{"duration":0.017808,"end_time":"2023-06-15T16:53:54.149048","exception":false,"start_time":"2023-06-15T16:53:54.13124","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-08T17:03:32.701367Z","iopub.execute_input":"2023-08-08T17:03:32.702141Z","iopub.status.idle":"2023-08-08T17:03:32.708772Z","shell.execute_reply.started":"2023-08-08T17:03:32.702105Z","shell.execute_reply":"2023-08-08T17:03:32.707824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m_cv_6959 = tf.keras.models.load_model('/kaggle/input/icrgw-valid-multi-512-480-f5-efv2l-3d-ups-0706-s3/m.h5')\nm_cv_6957 = tf.keras.models.load_model('/kaggle/input/icrgw-valid-multi-512-480-f5-efv2l-3d-ups-0706-s2/m.h5')\nm_cv_6851 = tf.keras.models.load_model('/kaggle/input/icrgw-valid-single-ash-768-512-efv2l-0617-s3/m.h5')\nm_cv_693 = tf.keras.models.load_model('/kaggle/input/icrgw-valid-multi-512-480-f5-efv2l-3d-mul64-0722/m.h5')\nm_cv_6852 = tf.keras.models.load_model('/kaggle/input/icrgw-valid-single-256-efv2l-3d-0620-t3/m.h5')\nm_cv_6878 = tf.keras.models.load_model('/kaggle/input/icrgw-valid-single-ash-512-480-efv2l-0804/m.h5')\n\nm_cv_6959._name = 'm_cv_6959'\nm_cv_6957._name = 'm_cv_6957'\nm_cv_6851._name = 'm_cv_6851'\nm_cv_693._name = 'm_cv_693'\nm_cv_6852._name = 'm_cv_6852'\nm_cv_6878._name = 'm_cv_6878'","metadata":{"papermill":{"duration":120.836915,"end_time":"2023-06-15T16:55:54.993316","exception":false,"start_time":"2023-06-15T16:53:54.156401","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-08T17:05:40.14073Z","iopub.execute_input":"2023-08-08T17:05:40.141912Z","iopub.status.idle":"2023-08-08T17:09:12.999174Z","shell.execute_reply.started":"2023-08-08T17:05:40.141855Z","shell.execute_reply":"2023-08-08T17:09:12.998129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_kernel(kernel):\n    kernel = tf.constant(kernel)\n    kernel = kernel / tf.reduce_sum(kernel)\n\n    kernel = tf.expand_dims(kernel, -1)\n    kernel = tf.expand_dims(kernel, -1)\n    kernel = tf.cast(kernel, tf.float32)\n    return kernel\n\ndef align(xs, x, y):\n    print(f'align xs:{xs.shape}  x: {x}, y: {y}')\n\n    kernel = [\n        [x * y, (1 - x) * y],\n        [x * (1 - y), (1 - x) * (1 - y)],\n    ]\n    kernel = create_kernel(kernel)\n    xs1 = tf.pad(xs, [[0,0],[1,0],[1,0],[0,0]])\n    xs2 = tf.pad(xs, [[0,0],[0,1],[0,1],[0,0]])\n    mask = tf.ones((1, 256, 256, 1), dtype=tf.float32)\n    mask = tf.pad(mask, [[0,0],[1,0],[1,0],[0,0]])\n    xs = xs1 * mask + xs2 * (1 - mask)\n    \n    xs = tf.nn.conv2d(xs, kernel, strides=1, padding='VALID')\n    \n    return xs\ndef create_stack(kernel, idx, length):\n    zero = tf.zeros((2, 2))\n    ts = []\n    for i in range(idx):\n        ts.append(zero)\n    ts.append(kernel)\n    for i in range(length - idx - 1):\n        ts.append(zero)\n    kernel = tf.stack(ts, axis=-1)\n    return kernel\n\ndef create_kernel_2(kernel):\n    kernel = tf.constant(kernel)\n    kernel = kernel / tf.reduce_sum(kernel)\n\n    length = 24\n    ts = []\n    for i in range(length):\n        ts.append(create_stack(kernel, i, length))\n    kernel = tf.stack(ts, axis=-1)\n    kernel = tf.cast(kernel, tf.float32)\n    return kernel\n\ndef align_2(xs, x, y):\n    print(f'align xs:{xs.shape}  x: {x}, y: {y}')\n\n    kernel = [\n        [x * y, (1 - x) * y],\n        [x * (1 - y), (1 - x) * (1 - y)],\n    ]\n    kernel = create_kernel_2(kernel)\n    xs1 = tf.pad(xs, [[0,0],[1,0],[1,0],[0,0]])\n    xs2 = tf.pad(xs, [[0,0],[0,1],[0,1],[0,0]])\n    mask = tf.ones((1, 256, 256, 1), dtype=tf.float32)\n    mask = tf.pad(mask, [[0,0],[1,0],[1,0],[0,0]])\n    xs = xs1 * mask + xs2 * (1 - mask)\n    \n    xs = tf.nn.conv2d(xs, kernel, strides=1, padding='VALID')\n    \n    return xs","metadata":{"execution":{"iopub.status.busy":"2023-08-08T17:09:13.001242Z","iopub.execute_input":"2023-08-08T17:09:13.001604Z","iopub.status.idle":"2023-08-08T17:09:13.021168Z","shell.execute_reply.started":"2023-08-08T17:09:13.001572Z","shell.execute_reply":"2023-08-08T17:09:13.020177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/code/wuliaokaola/icrgw-pp-ensemble-0808\n# ['0.004', '0.275', '0.014', '0.270', '0.132', '0.306'] \n# CV: 0.6993397882339618\n\nrs = [0., 0.275, 0.014, 0.270, 0.132, 0.310]\n\nxi = tf.keras.layers.Input((256, 256, 8, 3))\nxs = xi\nxs = tf.reshape(xs, [-1, 256, 256, 24])\nxs = align_2(xs, 0.408, 0.453)\nxs = tf.reshape(xs, [-1, 256, 256, 8, 3])\n\n\nps_693 = m_cv_693(xs)\nps_6957 = m_cv_6957(xs)\nps_6878 = m_cv_6878(xs)\nps_6852 = m_cv_6852(xs)\nps_6851 = m_cv_6851(xs)\nps_6959 = m_cv_6959(xs)\n# ps = ps_693 * rs[0] + ps_6957 * rs[1] + ps_6878 * rs[2] + ps_6851 * rs[3] + ps_6852 * rs[4]\nps = ps_6957 * rs[1] + ps_6878 * rs[2] + ps_6851 * rs[3] + ps_6852 * rs[4] + ps_6959 * rs[5]\n\nmodel = tf.keras.models.Model(xi, ps)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T17:09:13.022794Z","iopub.execute_input":"2023-08-08T17:09:13.023351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = np.zeros([1,256,256,8,3])\nps = model.predict(image)\nps.shape","metadata":{"papermill":{"duration":111.696212,"end_time":"2023-06-15T16:58:31.248055","exception":false,"start_time":"2023-06-15T16:56:39.551843","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nps = model.predict(image)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.summary()","metadata":{"papermill":{"duration":0.016968,"end_time":"2023-06-15T16:58:31.273114","exception":false,"start_time":"2023-06-15T16:58:31.256146","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:49:41.350746Z","iopub.execute_input":"2023-07-28T02:49:41.351302Z","iopub.status.idle":"2023-07-28T02:49:41.356235Z","shell.execute_reply.started":"2023-07-28T02:49:41.351266Z","shell.execute_reply":"2023-07-28T02:49:41.354895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_fs = glob.glob('/kaggle/input/google-research-identify-contrails-reduce-global-warming/test/*')\n\nif len(test_fs) == 2:\n    CONFIRM = True\n    test_fs = glob.glob('/kaggle/input/google-research-identify-contrails-reduce-global-warming/validation/*')[:20]\nelse:\n    CONFIRM = False","metadata":{"papermill":{"duration":0.147122,"end_time":"2023-06-15T16:58:31.427449","exception":false,"start_time":"2023-06-15T16:58:31.280327","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:49:41.357887Z","iopub.execute_input":"2023-07-28T02:49:41.358348Z","iopub.status.idle":"2023-07-28T02:49:41.434883Z","shell.execute_reply.started":"2023-07-28T02:49:41.358316Z","shell.execute_reply":"2023-07-28T02:49:41.433698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 to_ash(band11, band14, band15):\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    false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n    return false_color","metadata":{"papermill":{"duration":0.01921,"end_time":"2023-06-15T16:58:31.454522","exception":false,"start_time":"2023-06-15T16:58:31.435312","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:49:41.436479Z","iopub.execute_input":"2023-07-28T02:49:41.436837Z","iopub.status.idle":"2023-07-28T02:49:41.446283Z","shell.execute_reply.started":"2023-07-28T02:49:41.436803Z","shell.execute_reply":"2023-07-28T02:49:41.444302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_band(path):\n#     with open(f'{path}/band_11.npy', 'rb') as f:\n#         band11 = np.load(f)\n#     with open(f'{path}/band_14.npy', 'rb') as f:\n#         band14 = np.load(f)\n#     with open(f'{path}/band_15.npy', 'rb') as f:\n#         band15 = np.load(f)\n    \n    band11 = np.load(f'{path}/band_11.npy', mmap_mode='r').astype('float32')\n    band14 = np.load(f'{path}/band_14.npy', mmap_mode='r').astype('float32')\n    band15 = np.load(f'{path}/band_15.npy', mmap_mode='r').astype('float32')\n    \n    ash = to_ash(band11, band14, band15) \n    ash =  np.transpose(ash, [0, 1, 3, 2])\n    del band11, band14, band15\n    return torch.tensor(ash)","metadata":{"papermill":{"duration":0.017974,"end_time":"2023-06-15T16:58:31.479631","exception":false,"start_time":"2023-06-15T16:58:31.461657","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:49:41.447915Z","iopub.execute_input":"2023-07-28T02:49:41.448298Z","iopub.status.idle":"2023-07-28T02:49:41.456834Z","shell.execute_reply.started":"2023-07-28T02:49:41.448266Z","shell.execute_reply":"2023-07-28T02:49:41.455719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = load_band(test_fs[0])\nb.shape","metadata":{"papermill":{"duration":0.2689,"end_time":"2023-06-15T16:58:31.755828","exception":false,"start_time":"2023-06-15T16:58:31.486928","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:49:41.458518Z","iopub.execute_input":"2023-07-28T02:49:41.45908Z","iopub.status.idle":"2023-07-28T02:49:41.570725Z","shell.execute_reply.started":"2023-07-28T02:49:41.459049Z","shell.execute_reply":"2023-07-28T02:49:41.569657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(torch.utils.data.Dataset):\n    def __init__(self, fs):\n        self.fs = fs\n\n    def __len__(self):\n        return len(self.fs)      \n\n    def __getitem__(self, idx):\n        f = self.fs[idx]\n        name = os.path.basename(f)\n        image = load_band(f)\n#         image = cv2.resize(image, [512, 512])\n        return image, name\n\nds = Dataset(test_fs)\ndl = torch.utils.data.DataLoader(ds, batch_size=1, num_workers=2)","metadata":{"papermill":{"duration":0.018799,"end_time":"2023-06-15T16:58:31.782075","exception":false,"start_time":"2023-06-15T16:58:31.763276","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:49:41.572187Z","iopub.execute_input":"2023-07-28T02:49:41.572537Z","iopub.status.idle":"2023-07-28T02:49:41.57978Z","shell.execute_reply.started":"2023-07-28T02:49:41.572503Z","shell.execute_reply":"2023-07-28T02:49:41.578621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"papermill":{"duration":0.023121,"end_time":"2023-06-15T16:58:31.81248","exception":false,"start_time":"2023-06-15T16:58:31.789359","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:49:41.581479Z","iopub.execute_input":"2023-07-28T02:49:41.581907Z","iopub.status.idle":"2023-07-28T02:49:41.596512Z","shell.execute_reply.started":"2023-07-28T02:49:41.581875Z","shell.execute_reply":"2023-07-28T02:49:41.595504Z"},"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":{"papermill":{"duration":0.034506,"end_time":"2023-06-15T16:58:31.854674","exception":false,"start_time":"2023-06-15T16:58:31.820168","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:49:41.598582Z","iopub.execute_input":"2023-07-28T02:49:41.599288Z","iopub.status.idle":"2023-07-28T02:49:41.622616Z","shell.execute_reply.started":"2023-07-28T02:49:41.599255Z","shell.execute_reply":"2023-07-28T02:49:41.621715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_mask(path):\n    with open(f'/kaggle/input/google-research-identify-contrails-reduce-global-warming/validation/{path}/human_pixel_masks.npy', 'rb') as f:\n        mask = np.load(f)\n    return mask\n# b = load_mask('4167468572886092711')\n# b.shape","metadata":{"papermill":{"duration":0.017013,"end_time":"2023-06-15T16:58:31.879562","exception":false,"start_time":"2023-06-15T16:58:31.862549","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:49:41.625849Z","iopub.execute_input":"2023-07-28T02:49:41.626108Z","iopub.status.idle":"2023-07-28T02:49:41.633748Z","shell.execute_reply.started":"2023-07-28T02:49:41.626085Z","shell.execute_reply":"2023-07-28T02:49:41.63277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = 0\n# for f in tqdm(test_fs):\nfor image, name in tqdm(dl):\n# for f in test_fs:\n    xs = image.numpy()\n    \n    ps = model.predict(xs, verbose=0)[0]\n    \n    submission.loc[int(name[0]), 'encoded_pixels'] = list_to_string(rle_encode(ps > 0.5))\n    \n    if CONFIRM == True:\n        mask = load_mask(name[0])\n        fig, ax = plt.subplots(1, 3, figsize=(10, 10))\n        ax[0].imshow(image[0,:,:,4])\n        ax[1].imshow(ps, vmax=1)\n        ax[2].imshow(mask )\n        plt.show()\n        \n    del image, xs, ps\n    n = n + 1\n    if n % 10 == 0:\n        gc.collect()","metadata":{"papermill":{"duration":126.547038,"end_time":"2023-06-15T17:00:38.459776","exception":false,"start_time":"2023-06-15T16:58:31.912738","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:51:48.465778Z","iopub.execute_input":"2023-07-28T02:51:48.466149Z","iopub.status.idle":"2023-07-28T02:53:24.627186Z","shell.execute_reply.started":"2023-07-28T02:51:48.466118Z","shell.execute_reply":"2023-07-28T02:53:24.625598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"papermill":{"duration":0.069567,"end_time":"2023-06-15T17:00:38.579622","exception":false,"start_time":"2023-06-15T17:00:38.510055","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:51:11.356574Z","iopub.execute_input":"2023-07-28T02:51:11.356919Z","iopub.status.idle":"2023-07-28T02:51:11.374104Z","shell.execute_reply.started":"2023-07-28T02:51:11.356888Z","shell.execute_reply":"2023-07-28T02:51:11.373091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"papermill":{"duration":0.056673,"end_time":"2023-06-15T17:00:38.681477","exception":false,"start_time":"2023-06-15T17:00:38.624804","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-28T02:51:11.375401Z","iopub.execute_input":"2023-07-28T02:51:11.379372Z","iopub.status.idle":"2023-07-28T02:51:11.388882Z","shell.execute_reply.started":"2023-07-28T02:51:11.379342Z","shell.execute_reply":"2023-07-28T02:51:11.38775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.046568,"end_time":"2023-06-15T17:00:38.771723","exception":false,"start_time":"2023-06-15T17:00:38.725155","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}