{"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":"embed_train = False\nembed_test = True","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:38:56.550263Z","iopub.execute_input":"2023-08-10T06:38:56.55127Z","iopub.status.idle":"2023-08-10T06:38:56.566421Z","shell.execute_reply.started":"2023-08-10T06:38:56.551237Z","shell.execute_reply":"2023-08-10T06:38:56.565504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wheels_path = \"/kaggle/input/open-clip-wheels/open_clip_wheels\"\nopen_clip_whl_path = f\"{wheels_path}/open_clip_torch-2.14.0-py3-none-any.whl\"","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:38:56.57535Z","iopub.execute_input":"2023-08-10T06:38:56.576603Z","iopub.status.idle":"2023-08-10T06:38:56.583309Z","shell.execute_reply.started":"2023-08-10T06:38:56.57657Z","shell.execute_reply":"2023-08-10T06:38:56.581831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --no-index --find-links $wheels_path $open_clip_whl_path -qq","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:38:56.584713Z","iopub.execute_input":"2023-08-10T06:38:56.585354Z","iopub.status.idle":"2023-08-10T06:39:11.508246Z","shell.execute_reply.started":"2023-08-10T06:38:56.58532Z","shell.execute_reply":"2023-08-10T06:39:11.506995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\n\nimport open_clip\n\nimport torch\nfrom torch.utils.data import DataLoader, Dataset\nimport torchvision.transforms as T\n\nfrom PIL import Image\n\nfrom tqdm import tqdm\n\nfrom sklearn.cluster import DBSCAN, AgglomerativeClustering\nfrom sklearn.metrics.pairwise import cosine_similarity\nfrom os import listdir\nimport os\n\nfrom sklearn.neighbors import NearestNeighbors","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:11.511961Z","iopub.execute_input":"2023-08-10T06:39:11.512986Z","iopub.status.idle":"2023-08-10T06:39:19.367959Z","shell.execute_reply.started":"2023-08-10T06:39:11.512944Z","shell.execute_reply":"2023-08-10T06:39:19.366868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_records = listdir('/kaggle/input/google-research-identify-contrails-reduce-global-warming/test/')","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:19.369211Z","iopub.execute_input":"2023-08-10T06:39:19.369601Z","iopub.status.idle":"2023-08-10T06:39:19.376632Z","shell.execute_reply.started":"2023-08-10T06:39:19.369564Z","shell.execute_reply":"2023-08-10T06:39:19.375508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(test_records, columns=[\"record_id\"])\ntest_df[\"train\"] = \"test\"\ntest_df[\"path\"] = '/kaggle/input/google-research-identify-contrails-reduce-global-warming/test/' + test_df.record_id.astype(str).values\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:19.378401Z","iopub.execute_input":"2023-08-10T06:39:19.37958Z","iopub.status.idle":"2023-08-10T06:39:19.407756Z","shell.execute_reply.started":"2023-08-10T06:39:19.379507Z","shell.execute_reply":"2023-08-10T06:39:19.406517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/contrails-images-ash-color/train_df.csv\")\ntrain_df[\"path\"] = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/train/\" + train_df.record_id.astype(str).values\n\nval_df = pd.read_csv(\"/kaggle/input/contrails-images-ash-color/valid_df.csv\")\nval_df[\"path\"] = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/validation/\" + val_df.record_id.astype(str).values","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:19.409079Z","iopub.execute_input":"2023-08-10T06:39:19.40944Z","iopub.status.idle":"2023-08-10T06:39:19.474903Z","shell.execute_reply.started":"2023-08-10T06:39:19.409404Z","shell.execute_reply":"2023-08-10T06:39:19.473996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.concat([train_df, val_df], axis=0).reset_index(drop=True)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:19.47636Z","iopub.execute_input":"2023-08-10T06:39:19.47674Z","iopub.status.idle":"2023-08-10T06:39:19.492691Z","shell.execute_reply.started":"2023-08-10T06:39:19.476708Z","shell.execute_reply":"2023-08-10T06:39:19.49152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:19.494387Z","iopub.execute_input":"2023-08-10T06:39:19.494806Z","iopub.status.idle":"2023-08-10T06:39:19.502556Z","shell.execute_reply.started":"2023-08-10T06:39:19.494771Z","shell.execute_reply":"2023-08-10T06:39:19.501521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_types = {\"record_id\": str}\n\ntr_json = pd.read_json(\"/kaggle/input/google-research-identify-contrails-reduce-global-warming/train_metadata.json\", dtype=data_types)\nval_json = pd.read_json(\"/kaggle/input/google-research-identify-contrails-reduce-global-warming/validation_metadata.json\", dtype=data_types)","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:19.508219Z","iopub.execute_input":"2023-08-10T06:39:19.509472Z","iopub.status.idle":"2023-08-10T06:39:19.979271Z","shell.execute_reply.started":"2023-08-10T06:39:19.509414Z","shell.execute_reply":"2023-08-10T06:39:19.978108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_json = pd.concat([tr_json, val_json], axis=0)\ndf_json.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:19.981699Z","iopub.execute_input":"2023-08-10T06:39:19.982022Z","iopub.status.idle":"2023-08-10T06:39:19.992005Z","shell.execute_reply.started":"2023-08-10T06:39:19.981992Z","shell.execute_reply":"2023-08-10T06:39:19.991036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.record_id = train_df.record_id.astype(\"str\")\ndf_json.record_id = df_json.record_id.astype(\"str\")","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:19.993706Z","iopub.execute_input":"2023-08-10T06:39:19.994411Z","iopub.status.idle":"2023-08-10T06:39:20.022739Z","shell.execute_reply.started":"2023-08-10T06:39:19.994375Z","shell.execute_reply":"2023-08-10T06:39:20.021536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.merge(train_df, df_json, on='record_id')","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:20.024232Z","iopub.execute_input":"2023-08-10T06:39:20.027853Z","iopub.status.idle":"2023-08-10T06:39:20.066052Z","shell.execute_reply.started":"2023-08-10T06:39:20.027817Z","shell.execute_reply":"2023-08-10T06:39:20.064951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compute embeddings","metadata":{}},{"cell_type":"code","source":"class EmbeddingDataset(Dataset):\n    \n    def __init__(self, df):\n        self.df = df\n        self.device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n        self.model, _, self.preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='laion2b_s34b_b79k')\n        self.model.to(self.device)\n        #self.model, self.preprocess = clip.load(\"ViT-B/32\", device=self.device)\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 read_record(self, directory):\n        record_data = {}\n        for x in [\n            \"band_11\", \n            \"band_14\", \n            \"band_15\"\n        ]:\n            record_data[x] = np.load(os.path.join(directory, x + \".npy\"))\n\n        return record_data\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        N_TIMES_BEFORE = 4\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        img = false_color[..., N_TIMES_BEFORE]\n\n        return img\n    \n    def __getitem__(self, index):\n        row = self.df.iloc[index]\n        path = row.path\n        data = self.read_record(path)    \n        \n        img = self.get_false_color(data) * 255\n        img = Image.fromarray(img.astype(np.uint8))\n        img = torch.tensor(self.preprocess(img)).float().unsqueeze(0)\n        img = img.to(self.device)\n        \n        \n        if row[\"train\"] != \"test\":\n            mask = np.load(path + \"/human_pixel_masks.npy\").squeeze() * 255\n            mask = Image.fromarray(mask.astype(np.uint8))\n            mask = torch.tensor(self.preprocess(mask)).float().unsqueeze(0)\n            mask = mask.to(self.device)\n        \n        with torch.no_grad():\n            img_features = self.model.encode_image(img)\n            img_features = img_features.detach().cpu().numpy()\n            \n            if row[\"train\"] != \"test\":\n                mask_features = self.model.encode_image(mask)\n                mask_features = mask_features.detach().cpu().numpy()\n        \n        if row[\"train\"] != \"test\":\n            return img_features, mask_features\n        \n        return img_features\n    \n    def __len__(self):\n        return len(self.df)","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:20.06793Z","iopub.execute_input":"2023-08-10T06:39:20.068374Z","iopub.status.idle":"2023-08-10T06:39:20.086772Z","shell.execute_reply.started":"2023-08-10T06:39:20.068335Z","shell.execute_reply":"2023-08-10T06:39:20.085646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if embed_train:\n    dataset = EmbeddingDataset(train_df)\n    loader = DataLoader(dataset, batch_size=1, shuffle=False, drop_last=False, num_workers=8)\n    \n    all_img_features = []\n    all_mask_features = []\n    for _, (img_features, mask_features) in enumerate(tqdm(loader)):\n        all_img_features.append(img_features)\n        all_mask_features.append(mask_features)\n        \n    img_features = np.concatenate(all_img_features, axis=0).squeeze()\n    mask_features = np.concatenate(all_mask_features, axis=0).squeeze()\n    np.save(\"image_features.npy\", img_features)\n    np.save(\"mask_features.npy\", mask_features)\nelse:\n    img_features = np.load(\"/kaggle/input/ic23-contrails-metadata/image_features.npy\")\n    mask_features = np.load(\"/kaggle/input/ic23-contrails-metadata/mask_features.npy\")","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:20.088526Z","iopub.execute_input":"2023-08-10T06:39:20.089416Z","iopub.status.idle":"2023-08-10T06:39:21.854265Z","shell.execute_reply.started":"2023-08-10T06:39:20.08938Z","shell.execute_reply":"2023-08-10T06:39:21.853276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(img_features.shape, mask_features.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:21.858031Z","iopub.execute_input":"2023-08-10T06:39:21.858327Z","iopub.status.idle":"2023-08-10T06:39:21.866774Z","shell.execute_reply.started":"2023-08-10T06:39:21.858302Z","shell.execute_reply":"2023-08-10T06:39:21.86584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = EmbeddingDataset(test_df)\nloader = DataLoader(test_dataset, batch_size=1, shuffle=False, drop_last=False)\n    \nall_test_img_features = []\nfor _, test_img_features in enumerate(tqdm(loader)):\n    all_test_img_features.append(test_img_features)","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:21.870325Z","iopub.execute_input":"2023-08-10T06:39:21.870607Z","iopub.status.idle":"2023-08-10T06:39:37.898766Z","shell.execute_reply.started":"2023-08-10T06:39:21.870583Z","shell.execute_reply":"2023-08-10T06:39:37.897791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img_features = np.concatenate(all_test_img_features, axis=0).squeeze()\ntest_img_features.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:37.90181Z","iopub.execute_input":"2023-08-10T06:39:37.902123Z","iopub.status.idle":"2023-08-10T06:39:37.908856Z","shell.execute_reply.started":"2023-08-10T06:39:37.902095Z","shell.execute_reply":"2023-08-10T06:39:37.907886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Finding neighbors","metadata":{}},{"cell_type":"code","source":"df = pd.concat([df, test_df], axis=0).reset_index(drop=True)\nfeatures = np.concatenate([img_features, test_img_features], axis=0)\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:37.910033Z","iopub.execute_input":"2023-08-10T06:39:37.911671Z","iopub.status.idle":"2023-08-10T06:39:37.986517Z","shell.execute_reply.started":"2023-08-10T06:39:37.911644Z","shell.execute_reply":"2023-08-10T06:39:37.98552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cuml\nimport cudf, cuml\nfrom cuml.neighbors import NearestNeighbors as CuNearest","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:37.987713Z","iopub.execute_input":"2023-08-10T06:39:37.988141Z","iopub.status.idle":"2023-08-10T06:39:44.187372Z","shell.execute_reply.started":"2023-08-10T06:39:37.988106Z","shell.execute_reply":"2023-08-10T06:39:44.18619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.record_id = df.record_id.astype(np.str)","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:44.192342Z","iopub.execute_input":"2023-08-10T06:39:44.193838Z","iopub.status.idle":"2023-08-10T06:39:44.207336Z","shell.execute_reply.started":"2023-08-10T06:39:44.193798Z","shell.execute_reply":"2023-08-10T06:39:44.206108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cu_df = cudf.from_pandas(df)\nknn = CuNearest(n_neighbors=3, metric=\"cosine\")\nknn.fit(features)\ndistances, nbors = knn.kneighbors(features)","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:44.211884Z","iopub.execute_input":"2023-08-10T06:39:44.214236Z","iopub.status.idle":"2023-08-10T06:39:52.033287Z","shell.execute_reply.started":"2023-08-10T06:39:44.214201Z","shell.execute_reply":"2023-08-10T06:39:52.032227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(distances[21170,:])\nprint(nbors[21170,:])","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:52.034917Z","iopub.execute_input":"2023-08-10T06:39:52.035312Z","iopub.status.idle":"2023-08-10T06:39:52.042904Z","shell.execute_reply.started":"2023-08-10T06:39:52.035277Z","shell.execute_reply":"2023-08-10T06:39:52.041927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[:, \"distance\"] = distances[:,1]\ndf.loc[:, \"nbor\"] = nbors[:,1]\ndf.loc[:, \"distance_2\"] = distances[:,2]\ndf.loc[:, \"nbor_2\"] = nbors[:,2]","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:52.044266Z","iopub.execute_input":"2023-08-10T06:39:52.045222Z","iopub.status.idle":"2023-08-10T06:39:52.057039Z","shell.execute_reply.started":"2023-08-10T06:39:52.045189Z","shell.execute_reply":"2023-08-10T06:39:52.056071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_range(data, bounds):\n        \"\"\"Maps data to the range [0, 1].\"\"\"\n        return (data - bounds[0]) / (bounds[1] - bounds[0])\n\ndef read_record(directory):\n    record_data = {}\n    for x in [\n        \"band_11\", \n        \"band_14\", \n        \"band_15\"\n    ]:\n        record_data[x] = np.load(os.path.join(directory, x + \".npy\"))\n\n    return record_data\n\ndef get_false_color(record_data):\n    _T11_BOUNDS = (243, 303)\n    _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n    _TDIFF_BOUNDS = (-4, 2)\n\n    N_TIMES_BEFORE = 4\n\n    r = normalize_range(record_data[\"band_15\"] - record_data[\"band_14\"], _TDIFF_BOUNDS)\n    g = normalize_range(record_data[\"band_14\"] - record_data[\"band_11\"], _CLOUD_TOP_TDIFF_BOUNDS)\n    b = normalize_range(record_data[\"band_14\"], _T11_BOUNDS)\n    false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n    img = false_color[..., N_TIMES_BEFORE]\n\n    return img\n","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:41:02.335245Z","iopub.execute_input":"2023-08-10T06:41:02.335636Z","iopub.status.idle":"2023-08-10T06:41:02.346841Z","shell.execute_reply.started":"2023-08-10T06:41:02.335604Z","shell.execute_reply":"2023-08-10T06:41:02.343956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = read_record(df.loc[21170].path)\nimg = get_false_color(data)\nimg = img*255\nplt.imshow(img.astype(np.uint8))\nplt.title(df.loc[21170].record_id)","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:41:02.811801Z","iopub.execute_input":"2023-08-10T06:41:02.812973Z","iopub.status.idle":"2023-08-10T06:41:03.484471Z","shell.execute_reply.started":"2023-08-10T06:41:02.812933Z","shell.execute_reply":"2023-08-10T06:41:03.483511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[21170]","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:42:43.560057Z","iopub.execute_input":"2023-08-10T06:42:43.560432Z","iopub.status.idle":"2023-08-10T06:42:43.569098Z","shell.execute_reply.started":"2023-08-10T06:42:43.5604Z","shell.execute_reply":"2023-08-10T06:42:43.567995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = read_record(df.loc[22386].path)\nimg = get_false_color(data)\nimg = img*255\nplt.imshow(img.astype(np.uint8))\nplt.title(df.loc[22386].record_id)","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:42:11.642351Z","iopub.execute_input":"2023-08-10T06:42:11.643364Z","iopub.status.idle":"2023-08-10T06:42:12.140797Z","shell.execute_reply.started":"2023-08-10T06:42:11.643329Z","shell.execute_reply":"2023-08-10T06:42:12.139747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[22386]","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:42:55.008295Z","iopub.execute_input":"2023-08-10T06:42:55.009436Z","iopub.status.idle":"2023-08-10T06:42:55.018468Z","shell.execute_reply.started":"2023-08-10T06:42:55.009392Z","shell.execute_reply":"2023-08-10T06:42:55.017392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The test images we have given as examples are from the validation dataset. ","metadata":{}},{"cell_type":"markdown","source":"# Do we have more duplicates?","metadata":{}},{"cell_type":"code","source":"df = df.sort_values(by='distance')\ndf.head(20)","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:39:52.058468Z","iopub.execute_input":"2023-08-10T06:39:52.058802Z","iopub.status.idle":"2023-08-10T06:39:52.097043Z","shell.execute_reply.started":"2023-08-10T06:39:52.05877Z","shell.execute_reply":"2023-08-10T06:39:52.09614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Yes! Just looking at the distances and the neighbors, we definitely have more duplicates. And interestingly the timestamps are either the same or very close. Same holds for row and column features. But they are not exactly the same! Why?","metadata":{}},{"cell_type":"markdown","source":"# Looking a bit deeper into similarities","metadata":{}},{"cell_type":"code","source":"bp = '/kaggle/input/contrails-images-ash-color/contrails/'\ncp = '/kaggle/input/google-research-identify-contrails-reduce-global-warming/'\n\ndata = np.load(bp + df.loc[5047].record_id + \".npy\")\nind_mask = np.load(cp + df.loc[5047].train + \"/\" + df.loc[5047].record_id + \"/human_individual_masks.npy\")\n\nfig, ax = plt.subplots(1,7,figsize=(20,4))\nax[0].imshow((255*data[...,0:3]).astype(np.uint8))\nax[1].imshow((data[...,3]).astype(np.uint8))\nax[1].set_title(\"Ground truth\")\nfor k in range(ind_mask.shape[3]):\n    ax[k+2].imshow(ind_mask[...,k])\nax[ind_mask.shape[3]+2].imshow(ind_mask.mean(axis=3).squeeze())\nplt.show()\n\ndata = np.load(bp + df.loc[df.loc[5047].nbor].record_id + \".npy\")\nind_mask = np.load(cp + df.loc[df.loc[5047].nbor].train + \"/\" + df.loc[df.loc[5047].nbor].record_id + \"/human_individual_masks.npy\")\n\nfig, ax = plt.subplots(1,7,figsize=(20,4))\nax[0].imshow((255*data[...,0:3]).astype(np.uint8))\nax[1].imshow((data[...,3]).astype(np.uint8))\nax[1].set_title(\"Ground truth\")\nax[0].set_title(\"First neighbor (pair)\")\nfor k in range(ind_mask.shape[3]):\n    ax[k+2].imshow(ind_mask[...,k])\nax[ind_mask.shape[3]+2].imshow(ind_mask.mean(axis=3).squeeze())\nplt.show()\n\ndata = np.load(bp + df.loc[df.loc[5047].nbor_2].record_id + \".npy\")\nind_mask = np.load(cp + df.loc[df.loc[5047].nbor_2].train + \"/\" + df.loc[df.loc[5047].nbor_2].record_id + \"/human_individual_masks.npy\")\n\nfig, ax = plt.subplots(1,7,figsize=(20,4))\nax[0].imshow((255*data[...,0:3]).astype(np.uint8))\nax[1].imshow((data[...,3]).astype(np.uint8))\nax[1].set_title(\"Ground truth\")\nax[0].set_title(\"Second neighbor\")\nfor k in range(ind_mask.shape[3]):\n    ax[k+2].imshow(ind_mask[...,k])\nax[ind_mask.shape[3]+2].imshow(ind_mask.mean(axis=3).squeeze())\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-10T06:45:26.755131Z","iopub.execute_input":"2023-08-10T06:45:26.755564Z","iopub.status.idle":"2023-08-10T06:45:29.94733Z","shell.execute_reply.started":"2023-08-10T06:45:26.755529Z","shell.execute_reply":"2023-08-10T06:45:29.946391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There is a lot noise in the labels as well. ","metadata":{}}]}