{"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":"!git clone https://github.com/mahmoodlab/MI-Zero.git\n!git clone https://github.com/mahmoodlab/CLAM.git\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-11T06:51:55.54844Z","iopub.execute_input":"2023-07-11T06:51:55.549818Z","iopub.status.idle":"2023-07-11T06:52:00.500373Z","shell.execute_reply.started":"2023-07-11T06:51:55.549766Z","shell.execute_reply":"2023-07-11T06:52:00.49922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd MI-Zero\n!pip install assets/timm_ctp.tar --no-deps","metadata":{"execution":{"iopub.status.busy":"2023-07-11T06:52:00.504182Z","iopub.execute_input":"2023-07-11T06:52:00.504655Z","iopub.status.idle":"2023-07-11T06:52:06.757697Z","shell.execute_reply.started":"2023-07-11T06:52:00.504615Z","shell.execute_reply":"2023-07-11T06:52:06.756175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2023-07-11T06:52:06.759937Z","iopub.execute_input":"2023-07-11T06:52:06.760815Z","iopub.status.idle":"2023-07-11T06:52:07.855778Z","shell.execute_reply.started":"2023-07-11T06:52:06.76076Z","shell.execute_reply":"2023-07-11T06:52:07.854392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport torch\nimport torch.nn as nn\nimport h5py\nfrom torch.utils.data import DataLoader, Dataset\nfrom torchvision import transforms\nimport torch.nn.functional as F\nimport sys\nimport math\n\nfrom src.models.ctran import ctranspath","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:12.032436Z","iopub.execute_input":"2023-07-11T07:01:12.032932Z","iopub.status.idle":"2023-07-11T07:01:12.040109Z","shell.execute_reply.started":"2023-07-11T07:01:12.032899Z","shell.execute_reply":"2023-07-11T07:01:12.038851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ctranspath(img_size = 224)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:12.412144Z","iopub.execute_input":"2023-07-11T07:01:12.412996Z","iopub.status.idle":"2023-07-11T07:01:13.003771Z","shell.execute_reply.started":"2023-07-11T07:01:12.412952Z","shell.execute_reply":"2023-07-11T07:01:13.00259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:13.005649Z","iopub.execute_input":"2023-07-11T07:01:13.006319Z","iopub.status.idle":"2023-07-11T07:01:13.017464Z","shell.execute_reply.started":"2023-07-11T07:01:13.006282Z","shell.execute_reply":"2023-07-11T07:01:13.01623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_ctranspath_clip(ckpt_path, img_size = 224, return_trsforms = True):\n    def clean_state_dict_clip(state_dict):\n        new_state_dict = {}\n        for k, v in state_dict.items():\n            if 'attn_mask' in k:\n                continue\n            if 'visual.trunk.' in k:\n                new_state_dict[k.replace('module.visual.trunk.', '')] = v\n        return new_state_dict\n    \n    model = ctranspath(img_size = img_size)\n    model.head = nn.Identity()\n    state_dict = torch.load(ckpt_path, map_location=\"cpu\")['state_dict']\n    state_dict = clean_state_dict_clip(state_dict)\n    missing_keys, unexpected_keys = model.load_state_dict(state_dict, strict=False)\n    print('missing keys: ', missing_keys)\n    print('unexpected keys: ', unexpected_keys)\n    \n    if return_trsforms:\n        trsforms = get_transforms_ctranspath(img_size = img_size)\n        return model, trsforms\n    return model\ndef get_transforms_ctranspath(img_size=224, \n                              mean = (0.485, 0.456, 0.406), \n                              std = (0.229, 0.224, 0.225)):\n    trnsfrms = transforms.Compose(\n                    [\n                     transforms.Resize(img_size),\n                     transforms.ToTensor(),\n                     transforms.Normalize(mean = mean, std = std)\n                    ]\n                )\n    return trnsfrms\nckpt_path = \"/kaggle/input/mizerow/ctranspath_448_bioclinicalbert/ctranspath_448_bioclinicalbert/checkpoints/epoch_50.pt\"\nmodel, trsforms = load_ctranspath_clip(ckpt_path, img_size = 224)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:13.135145Z","iopub.execute_input":"2023-07-11T07:01:13.135834Z","iopub.status.idle":"2023-07-11T07:01:15.33686Z","shell.execute_reply.started":"2023-07-11T07:01:13.135795Z","shell.execute_reply":"2023-07-11T07:01:15.335732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import openslide\nwsi_path = \"/kaggle/input/prostate-cancer-grade-assessment/train_images/0005f7aaab2800f6170c399693a96917.tiff\"\nwsi = openslide.open_slide(wsi_path)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:15.339016Z","iopub.execute_input":"2023-07-11T07:01:15.339394Z","iopub.status.idle":"2023-07-11T07:01:15.413747Z","shell.execute_reply.started":"2023-07-11T07:01:15.339362Z","shell.execute_reply":"2023-07-11T07:01:15.412736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir ../debug \n!mkdir ../PATCH \n!cp /kaggle/input/prostate-cancer-grade-assessment/train_images/0005f7aaab2800f6170c399693a96917.tiff ../debug/","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:15.41556Z","iopub.execute_input":"2023-07-11T07:01:15.416308Z","iopub.status.idle":"2023-07-11T07:01:18.879175Z","shell.execute_reply.started":"2023-07-11T07:01:15.416266Z","shell.execute_reply":"2023-07-11T07:01:18.877493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python ../CLAM/create_patches_fp.py --source ../debug --save_dir ../PATCH --patch_size 256 --seg --patch --stitch ","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:18.883478Z","iopub.execute_input":"2023-07-11T07:01:18.884754Z","iopub.status.idle":"2023-07-11T07:01:21.518756Z","shell.execute_reply.started":"2023-07-11T07:01:18.884673Z","shell.execute_reply":"2023-07-11T07:01:21.517308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../PATCH/","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:21.520797Z","iopub.execute_input":"2023-07-11T07:01:21.521229Z","iopub.status.idle":"2023-07-11T07:01:22.639088Z","shell.execute_reply.started":"2023-07-11T07:01:21.521192Z","shell.execute_reply":"2023-07-11T07:01:22.637498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\nmask = cv2.imread(\"../PATCH/stitches/0005f7aaab2800f6170c399693a96917.jpg\")\nplt.imshow(mask)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:22.641509Z","iopub.execute_input":"2023-07-11T07:01:22.641901Z","iopub.status.idle":"2023-07-11T07:01:23.353754Z","shell.execute_reply.started":"2023-07-11T07:01:22.641866Z","shell.execute_reply":"2023-07-11T07:01:23.352627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(\"../PATCH/masks/0005f7aaab2800f6170c399693a96917.jpg\")\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:23.355309Z","iopub.execute_input":"2023-07-11T07:01:23.356425Z","iopub.status.idle":"2023-07-11T07:01:24.035334Z","shell.execute_reply.started":"2023-07-11T07:01:23.35639Z","shell.execute_reply":"2023-07-11T07:01:24.034176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_assets_from_h5(h5_path):\n    assets = {}\n    attrs = {}\n    with h5py.File(h5_path, 'r') as f:\n        for key in f.keys():\n            assets[key] = f[key][:]\n            if f[key].attrs is not None:\n                attrs[key] = dict(f[key].attrs)\n    return assets, attrs\n\nassets, _ = read_assets_from_h5(\"../PATCH/patches/0005f7aaab2800f6170c399693a96917.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:24.036854Z","iopub.execute_input":"2023-07-11T07:01:24.037216Z","iopub.status.idle":"2023-07-11T07:01:24.050833Z","shell.execute_reply.started":"2023-07-11T07:01:24.037187Z","shell.execute_reply":"2023-07-11T07:01:24.049744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Whole_Slide_Bag_FP(Dataset):\n    def __init__(self,\n        coords,\n        wsi,\n        patch_level,\n        patch_size,\n        custom_transforms=None,\n        target_patch_size=-1):\n        \"\"\"\n        Args:\n            coords (string): coordinates to extract patches from w.r.t. level 0.\n            custom_transforms (callable, optional): Optional transform to be applied on a sample\n            target_patch_size (int): Custom defined image size before embedding\n        \"\"\"\n        self.coords = coords\n        self.patch_level = patch_level\n        self.patch_size = patch_size\n        self.wsi = wsi\n        self.roi_transforms = custom_transforms\n        self.length = len(coords)\n        if target_patch_size > 0:\n            self.target_patch_size = (target_patch_size, ) * 2\n        else:\n            self.target_patch_size = None\n\n    def __len__(self):\n        return self.length\n    \n    def __getitem__(self, idx):\n        coord = self.coords[idx]\n        img = self.wsi.read_region(coord, self.patch_level, (self.patch_size, self.patch_size)).convert('RGB')\n        if self.target_patch_size is not None:\n            img = img.resize(self.target_patch_size)\n        img = self.roi_transforms(img)\n        return {'img': img, 'coords': coord}","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:24.052752Z","iopub.execute_input":"2023-07-11T07:01:24.053174Z","iopub.status.idle":"2023-07-11T07:01:24.063657Z","shell.execute_reply.started":"2023-07-11T07:01:24.053143Z","shell.execute_reply":"2023-07-11T07:01:24.062628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = Whole_Slide_Bag_FP(assets[\"coords\"],\n                                 wsi,\n                                 0,\n                                 patch_size=256,\n                                 custom_transforms = trsforms,\n                                 target_patch_size = 256)\ndataloader = DataLoader(dataset, shuffle=False, batch_size=32)\nfeatures_ = []\ndevice = \"cpu\"\ncoords_ = []\nwith torch.inference_mode():\n    for batch in dataloader:\n        imgs = batch['img'].to(device)\n        coords = np.array(batch['coords'])\n        features = model(imgs)\n        features_.append(features)\n        coords_.append(coords)\n        \n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:01:24.067608Z","iopub.execute_input":"2023-07-11T07:01:24.067991Z","iopub.status.idle":"2023-07-11T07:02:50.003226Z","shell.execute_reply.started":"2023-07-11T07:01:24.067961Z","shell.execute_reply":"2023-07-11T07:02:50.002129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coords_ = np.concatenate(coords_)\nfeatures_ = torch.cat(features_)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:02:50.005013Z","iopub.execute_input":"2023-07-11T07:02:50.005671Z","iopub.status.idle":"2023-07-11T07:02:50.011255Z","shell.execute_reply.started":"2023-07-11T07:02:50.005638Z","shell.execute_reply":"2023-07-11T07:02:50.009937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tokenize(tokenizer, texts=[\"a histopathological image of adipocytes\"]):\n    tokens = tokenizer.batch_encode_plus(texts, \n                                         max_length = 64,\n                                         add_special_tokens=True, # Add '[CLS]' and '[SEP]'\n                                         return_token_type_ids=False,\n                                         truncation = True,\n                                         padding = 'max_length',\n                                         return_attention_mask=True)\n    return tokens['input_ids'], tokens['attention_mask']\nfrom transformers import AutoTokenizer\nmodel_name = 'emilyalsentzer/Bio_ClinicalBERT'\ntokenizer = AutoTokenizer.from_pretrained(model_name, fast=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:02:50.012885Z","iopub.execute_input":"2023-07-11T07:02:50.013246Z","iopub.status.idle":"2023-07-11T07:02:52.791755Z","shell.execute_reply.started":"2023-07-11T07:02:50.013215Z","shell.execute_reply":"2023-07-11T07:02:52.790397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from src.models.model import CLIP\nimport json\nIMAGENET_COLOR_MEAN = (0.485, 0.456, 0.406)\nIMAGENET_COLOR_STD = (0.229, 0.224, 0.225)\n\ndef clean_state_dict_ctranspath(state_dict):\n    new_state_dict = {}\n    for k, v in state_dict.items():\n        if 'attn_mask' in k:\n            continue\n        new_state_dict[k.replace('module.', '')] = v\n    return new_state_dict\n\ndef create_model(\n        device: torch.device = torch.device('cpu'),\n        override_image_size = None,\n        model_checkpoint = None,\n):\n    with open(\"./src/model_configs/ctranspath_448_bioclinicalbert.json\") as f:\n        model_cfg = json.load(f)\n    \n    if override_image_size:\n        model_cfg['vision_cfg']['image_size'] = override_image_size\n        logging.info(f'Created model {model_name} with image size of {override_image_size} instead')\n        \n    model = CLIP(**model_cfg)\n    \n    model.to(device=device)\n    model.visual.image_mean = IMAGENET_COLOR_MEAN\n    model.visual.image_std = IMAGENET_COLOR_STD\n    state_dict = torch.load(model_checkpoint, map_location='cpu')['state_dict']\n    state_dict = clean_state_dict_ctranspath(state_dict)\n    missing_keys, _ = model.load_state_dict(state_dict, strict=False)\n    print(missing_keys)\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:04:30.208015Z","iopub.execute_input":"2023-07-11T07:04:30.208517Z","iopub.status.idle":"2023-07-11T07:04:30.220541Z","shell.execute_reply.started":"2023-07-11T07:04:30.208482Z","shell.execute_reply":"2023-07-11T07:04:30.219269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclip_model = create_model(model_checkpoint = ckpt_path)\nimage_features = clip_model.visual.head(features_) # Project to desired dimensions num_patches x 512\nimage_features = F.normalize(image_features, dim=-1) \n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:04:31.471004Z","iopub.execute_input":"2023-07-11T07:04:31.472178Z","iopub.status.idle":"2023-07-11T07:04:35.463381Z","shell.execute_reply.started":"2023-07-11T07:04:31.472133Z","shell.execute_reply":"2023-07-11T07:04:35.462135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"texts, attention_mask = tokenize(tokenizer,texts=[\"a histopathological image of prostate cancer\"]) # Tokenize with custom tokenizer\ntexts = torch.from_numpy(np.array(texts))#.to(device)\nattention_mask = torch.from_numpy(np.array(attention_mask)).to(device)\nclass_embeddings = clip_model.encode_text(texts, attention_mask=attention_mask)\n\nclass_embedding = F.normalize(class_embeddings, dim=-1)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:04:52.823601Z","iopub.execute_input":"2023-07-11T07:04:52.824034Z","iopub.status.idle":"2023-07-11T07:04:53.007819Z","shell.execute_reply.started":"2023-07-11T07:04:52.824003Z","shell.execute_reply":"2023-07-11T07:04:53.006773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"texts, attention_mask = tokenize(tokenizer,texts=[\"a histopathological image of adipose tissue\"]) # Tokenize with custom tokenizer\ntexts = torch.from_numpy(np.array(texts))#.to(device)\nattention_mask = torch.from_numpy(np.array(attention_mask)).to(device)\nclass_embeddings2 = clip_model.encode_text(texts, attention_mask=attention_mask)\n\nclass_embedding2 = F.normalize(class_embeddings2, dim=-1)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:04:55.750114Z","iopub.execute_input":"2023-07-11T07:04:55.75057Z","iopub.status.idle":"2023-07-11T07:04:55.923051Z","shell.execute_reply.started":"2023-07-11T07:04:55.750528Z","shell.execute_reply":"2023-07-11T07:04:55.921999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"texts, attention_mask = tokenize(tokenizer,texts=[\"adipose\"]) # Tokenize with custom tokenizer\ntexts = torch.from_numpy(np.array(texts))#.to(device)\nattention_mask = torch.from_numpy(np.array(attention_mask)).to(device)\nclass_embedding3 = clip_model.encode_text(texts, attention_mask=attention_mask)\n\nclass_embedding3 = F.normalize(class_embedding3, dim=-1)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:04:56.619423Z","iopub.execute_input":"2023-07-11T07:04:56.619862Z","iopub.status.idle":"2023-07-11T07:04:56.79018Z","shell.execute_reply.started":"2023-07-11T07:04:56.619826Z","shell.execute_reply":"2023-07-11T07:04:56.789076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"texts, attention_mask = tokenize(tokenizer,texts=[\"stromal\"]) # Tokenize with custom tokenizer\ntexts = torch.from_numpy(np.array(texts))#.to(device)\nattention_mask = torch.from_numpy(np.array(attention_mask)).to(device)\nclass_embedding_stromal = clip_model.encode_text(texts, attention_mask=attention_mask)\n\nclass_embedding_stromal = F.normalize(class_embedding_stromal, dim=-1)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:04:57.405697Z","iopub.execute_input":"2023-07-11T07:04:57.406111Z","iopub.status.idle":"2023-07-11T07:04:57.570683Z","shell.execute_reply.started":"2023-07-11T07:04:57.406081Z","shell.execute_reply":"2023-07-11T07:04:57.569541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_features.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:04:57.961666Z","iopub.execute_input":"2023-07-11T07:04:57.962101Z","iopub.status.idle":"2023-07-11T07:04:57.969939Z","shell.execute_reply.started":"2023-07-11T07:04:57.96207Z","shell.execute_reply":"2023-07-11T07:04:57.968654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_embedding.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:04:58.52345Z","iopub.execute_input":"2023-07-11T07:04:58.524204Z","iopub.status.idle":"2023-07-11T07:04:58.5316Z","shell.execute_reply.started":"2023-07-11T07:04:58.524157Z","shell.execute_reply":"2023-07-11T07:04:58.530542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport torch.nn as nn\ncos = nn.CosineSimilarity(dim=1, eps=1e-6)\noutput = cos(image_features, class_embedding)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:04:59.144447Z","iopub.execute_input":"2023-07-11T07:04:59.145257Z","iopub.status.idle":"2023-07-11T07:04:59.158114Z","shell.execute_reply.started":"2023-07-11T07:04:59.145212Z","shell.execute_reply":"2023-07-11T07:04:59.157127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_patch_img(wsi,coords,cos_sim,index):\n    coord = coords[index]\n    sim = cos_sim[index]\n    img = wsi.read_region(coord, 0, (256,256)).convert('RGB')\n    plt.title(f\"cos sim: {sim}\")\n    plt.imshow(img)\n    plt.show()\n    plt.close()","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:05:00.062387Z","iopub.execute_input":"2023-07-11T07:05:00.063235Z","iopub.status.idle":"2023-07-11T07:05:00.071163Z","shell.execute_reply.started":"2023-07-11T07:05:00.063184Z","shell.execute_reply":"2023-07-11T07:05:00.069915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx in torch.argsort(output).numpy()[::-1][:10]:\n    get_patch_img(wsi,assets[\"coords\"],output.detach().numpy(),idx)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:05:01.535539Z","iopub.execute_input":"2023-07-11T07:05:01.535954Z","iopub.status.idle":"2023-07-11T07:05:06.06354Z","shell.execute_reply.started":"2023-07-11T07:05:01.535923Z","shell.execute_reply":"2023-07-11T07:05:06.06227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cos = nn.CosineSimilarity(dim=1, eps=1e-6)\noutput2 = cos(image_features, class_embedding2)\n\nfor idx in torch.argsort(output2).numpy()[::-1][:10]:\n    get_patch_img(wsi,assets[\"coords\"],output2.detach().numpy(),idx)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:05:32.626502Z","iopub.execute_input":"2023-07-11T07:05:32.626932Z","iopub.status.idle":"2023-07-11T07:05:36.490772Z","shell.execute_reply.started":"2023-07-11T07:05:32.626901Z","shell.execute_reply":"2023-07-11T07:05:36.489196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cos = nn.CosineSimilarity(dim=1, eps=1e-6)\noutput3 = cos(image_features, class_embedding3)\n\nfor idx in torch.argsort(output3).numpy()[::-1][:10]:\n    get_patch_img(wsi,assets[\"coords\"],output3.detach().numpy(),idx)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:05:48.446988Z","iopub.execute_input":"2023-07-11T07:05:48.447466Z","iopub.status.idle":"2023-07-11T07:05:52.210469Z","shell.execute_reply.started":"2023-07-11T07:05:48.447431Z","shell.execute_reply":"2023-07-11T07:05:52.209292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cos = nn.CosineSimilarity(dim=1, eps=1e-6)\noutput_stromal = cos(image_features, class_embedding_stromal)\n\nfor idx in torch.argsort(output_stromal).numpy()[::-1][:10]:\n    get_patch_img(wsi,assets[\"coords\"],output_stromal.detach().numpy(),idx)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T07:05:52.212552Z","iopub.execute_input":"2023-07-11T07:05:52.21315Z","iopub.status.idle":"2023-07-11T07:05:56.011797Z","shell.execute_reply.started":"2023-07-11T07:05:52.213118Z","shell.execute_reply":"2023-07-11T07:05:56.010684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}