{"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":"image_size = 512\nbatch_size = 32\nnum_workers = 4","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-17T09:09:06.439961Z","iopub.execute_input":"2021-07-17T09:09:06.440349Z","iopub.status.idle":"2021-07-17T09:09:06.444845Z","shell.execute_reply.started":"2021-07-17T09:09:06.440242Z","shell.execute_reply":"2021-07-17T09:09:06.443922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport sys\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\nimport os\nimport sys\nimport time\nimport cv2\nimport PIL.Image\nimport random\nfrom sklearn.metrics import accuracy_score\nfrom tqdm.notebook import tqdm\nimport torch\nfrom torch.utils.data import DataLoader, Dataset\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torchvision\nimport torchvision.transforms as transforms\nimport torch.optim as optim\nfrom torch.optim.lr_scheduler import CosineAnnealingLR\nimport albumentations\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nimport gc\nfrom sklearn.metrics import roc_auc_score\n%matplotlib inline\nimport seaborn as sns\nfrom pylab import rcParams\nimport timm\nfrom warnings import filterwarnings\nfrom sklearn.preprocessing import LabelEncoder\nimport math\nimport glob\nfilterwarnings(\"ignore\")\n\ndevice = torch.device('cuda') ","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:09:13.829098Z","iopub.execute_input":"2021-07-17T09:09:13.829468Z","iopub.status.idle":"2021-07-17T09:09:13.844748Z","shell.execute_reply.started":"2021-07-17T09:09:13.829432Z","shell.execute_reply":"2021-07-17T09:09:13.843641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    print(f'Setting all seeds to be {seed} to reproduce...')\nseed_everything(42)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:09:59.494714Z","iopub.execute_input":"2021-07-17T09:09:59.495052Z","iopub.status.idle":"2021-07-17T09:09:59.507090Z","shell.execute_reply.started":"2021-07-17T09:09:59.495023Z","shell.execute_reply":"2021-07-17T09:09:59.505849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transforms_valid = albumentations.Compose([\n    albumentations.Resize(image_size, image_size),\n    albumentations.Normalize()\n])","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:10:00.146087Z","iopub.execute_input":"2021-07-17T09:10:00.146447Z","iopub.status.idle":"2021-07-17T09:10:00.150713Z","shell.execute_reply.started":"2021-07-17T09:10:00.146414Z","shell.execute_reply":"2021-07-17T09:10:00.149858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class RANZCRDataset(Dataset):\n    def __init__(self, df, mode, transform=None):\n        \n        self.df = df.reset_index(drop=True)\n        self.mode = mode\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        row = self.df.loc[index]\n        img = cv2.imread(row.file_path)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        \n        if self.transform is not None:\n            res = self.transform(image=img)\n            img = res['image']\n                \n        img = img.astype(np.float32)\n        img = img.transpose(2,0,1)\n        \n        if self.mode == 'test':\n            return torch.tensor(img).float()\n        else:\n            return torch.tensor(img).float(), torch.tensor(row.PatientID).float()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:10:03.457384Z","iopub.execute_input":"2021-07-17T09:10:03.457753Z","iopub.status.idle":"2021-07-17T09:10:03.466686Z","shell.execute_reply.started":"2021-07-17T09:10:03.457721Z","shell.execute_reply":"2021-07-17T09:10:03.465612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ArcModule(nn.Module):\n    def __init__(self, in_features, out_features, s=10, m=0):\n        super().__init__()\n        self.in_features = in_features\n        self.out_features = out_features\n        self.s = s\n        self.m = m\n        self.weight = nn.Parameter(torch.FloatTensor(out_features, in_features))\n        nn.init.xavier_normal_(self.weight)\n\n        self.cos_m = math.cos(m)\n        self.sin_m = math.sin(m)\n        self.th = torch.tensor(math.cos(math.pi - m))\n        self.mm = torch.tensor(math.sin(math.pi - m) * m)\n\n    def forward(self, inputs, labels):\n        cos_th = F.linear(inputs, F.normalize(self.weight))\n        cos_th = cos_th.clamp(-1, 1)\n        sin_th = torch.sqrt(1.0 - torch.pow(cos_th, 2))\n        cos_th_m = cos_th * self.cos_m - sin_th * self.sin_m\n        # print(type(cos_th), type(self.th), type(cos_th_m), type(self.mm))\n        cos_th_m = torch.where(cos_th > self.th, cos_th_m, cos_th - self.mm)\n\n        cond_v = cos_th - self.th\n        cond = cond_v <= 0\n        cos_th_m[cond] = (cos_th - self.mm)[cond]\n\n        if labels.dim() == 1:\n            labels = labels.unsqueeze(-1)\n        onehot = torch.zeros(cos_th.size()).cuda()\n        labels = labels.type(torch.LongTensor).cuda()\n        onehot.scatter_(1, labels, 1.0)\n        outputs = onehot * cos_th_m + (1.0 - onehot) * cos_th\n        outputs = outputs * self.s\n        return outputs","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:10:06.278118Z","iopub.execute_input":"2021-07-17T09:10:06.278479Z","iopub.status.idle":"2021-07-17T09:10:06.299272Z","shell.execute_reply.started":"2021-07-17T09:10:06.278449Z","shell.execute_reply":"2021-07-17T09:10:06.295526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MetricLearningModel(nn.Module):\n\n    def __init__(self, channel_size, out_feature, dropout=0.5, backbone='densenet121', pretrained=False):\n        super(MetricLearningModel, self).__init__()\n        self.backbone = timm.create_model(backbone, pretrained=pretrained)\n        self.channel_size = channel_size\n        self.out_feature = out_feature\n        self.in_features = self.backbone.classifier.in_features\n        self.margin = ArcModule(in_features=self.channel_size, out_features = self.out_feature)\n        self.bn1 = nn.BatchNorm2d(self.in_features)\n        self.dropout = nn.Dropout2d(dropout, inplace=True)\n        self.fc1 = nn.Linear(self.in_features * 16 * 16 , self.channel_size)\n        self.bn2 = nn.BatchNorm1d(self.channel_size)\n        \n    def forward(self, x, labels=None):\n        features = self.backbone.features(x)\n        features = self.bn1(features)\n        features = self.dropout(features)\n        features = features.view(features.size(0), -1)\n        features = self.fc1(features)\n        features = self.bn2(features)\n        features = F.normalize(features)\n        if labels is not None:\n            return self.margin(features, labels)\n        return features\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:10:09.109086Z","iopub.execute_input":"2021-07-17T09:10:09.109465Z","iopub.status.idle":"2021-07-17T09:10:09.122700Z","shell.execute_reply.started":"2021-07-17T09:10:09.109430Z","shell.execute_reply":"2021-07-17T09:10:09.118532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = MetricLearningModel(image_size, 30805)\nmodel.load_state_dict(torch.load('../input/feature-extractor/dense121_feature_extractor.pth', map_location='cuda:0'))\nmodel.to(device);","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:10:11.821033Z","iopub.execute_input":"2021-07-17T09:10:11.821385Z","iopub.status.idle":"2021-07-17T09:10:23.611262Z","shell.execute_reply.started":"2021-07-17T09:10:11.821351Z","shell.execute_reply":"2021-07-17T09:10:23.610386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir external","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:10:31.454742Z","iopub.execute_input":"2021-07-17T09:10:31.455061Z","iopub.status.idle":"2021-07-17T09:10:32.125281Z","shell.execute_reply.started":"2021-07-17T09:10:31.455033Z","shell.execute_reply":"2021-07-17T09:10:32.124297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp ../input/bimcv-all-images-512-scale/scale_512/* external","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:10:45.669720Z","iopub.execute_input":"2021-07-17T09:10:45.670063Z","iopub.status.idle":"2021-07-17T09:11:56.833546Z","shell.execute_reply.started":"2021-07-17T09:10:45.670031Z","shell.execute_reply":"2021-07-17T09:11:56.832411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q -d . ../input/ricord-eda/train.zip","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:12:43.784274Z","iopub.execute_input":"2021-07-17T09:12:43.784618Z","iopub.status.idle":"2021-07-17T09:12:55.018044Z","shell.execute_reply.started":"2021-07-17T09:12:43.784589Z","shell.execute_reply":"2021-07-17T09:12:55.016974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp ./train/* external","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:13:14.852122Z","iopub.execute_input":"2021-07-17T09:13:14.852606Z","iopub.status.idle":"2021-07-17T09:13:16.190935Z","shell.execute_reply.started":"2021-07-17T09:13:14.852559Z","shell.execute_reply":"2021-07-17T09:13:16.189946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('./external'))","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:13:19.776899Z","iopub.execute_input":"2021-07-17T09:13:19.779733Z","iopub.status.idle":"2021-07-17T09:13:19.833359Z","shell.execute_reply.started":"2021-07-17T09:13:19.779684Z","shell.execute_reply":"2021-07-17T09:13:19.832443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = glob.glob('./external/*')\ndf = pd.DataFrame(files)\ndf.columns = ['file_path']\ndf['StudyInstanceUID'] = df['file_path'].str.split('/', expand=True)[2]\ndataset_ext = RANZCRDataset(df, 'test', transform=transforms_valid)\next_loader = torch.utils.data.DataLoader(dataset_ext, batch_size=batch_size, shuffle=False, num_workers=num_workers, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:13:32.442175Z","iopub.execute_input":"2021-07-17T09:13:32.442556Z","iopub.status.idle":"2021-07-17T09:13:32.687995Z","shell.execute_reply.started":"2021-07-17T09:13:32.442526Z","shell.execute_reply":"2021-07-17T09:13:32.686905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_test_features(test_loader):\n    model.eval()\n    bar = tqdm(test_loader)\n    \n    FEAS = []\n    TARGETS = []\n\n    with torch.no_grad():\n        for batch_idx, (images) in enumerate(bar):\n\n            images = images.to(device)\n\n            features = model(images)\n\n            FEAS += [features.detach().cpu()]\n\n    FEAS = torch.cat(FEAS).cpu().numpy()\n    \n    return FEAS","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:13:44.528985Z","iopub.execute_input":"2021-07-17T09:13:44.529335Z","iopub.status.idle":"2021-07-17T09:13:44.536547Z","shell.execute_reply.started":"2021-07-17T09:13:44.529287Z","shell.execute_reply":"2021-07-17T09:13:44.535375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FEAS = generate_test_features(ext_loader)\nFEAS = torch.tensor(FEAS).cuda()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:13:48.892869Z","iopub.execute_input":"2021-07-17T09:13:48.893193Z","iopub.status.idle":"2021-07-17T09:17:47.866668Z","shell.execute_reply.started":"2021-07-17T09:13:48.893162Z","shell.execute_reply":"2021-07-17T09:17:47.865624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"./external./external./external./external## The train features","metadata":{"execution":{"iopub.status.busy":"2021-07-05T14:50:06.532224Z","iopub.execute_input":"2021-07-05T14:50:06.532781Z","iopub.status.idle":"2021-07-05T14:50:06.551957Z","shell.execute_reply.started":"2021-07-05T14:50:06.532736Z","shell.execute_reply":"2021-07-05T14:50:06.550774Z"}}},{"cell_type":"code","source":"!rm -r train","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:17:57.845343Z","iopub.execute_input":"2021-07-17T09:17:57.845692Z","iopub.status.idle":"2021-07-17T09:17:58.654876Z","shell.execute_reply.started":"2021-07-17T09:17:57.845660Z","shell.execute_reply":"2021-07-17T09:17:58.653789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train\n!tar -xf ../input/siim-covid-19-convert-to-jpg-256px/train.tar.gz -C train","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:17:59.559534Z","iopub.execute_input":"2021-07-17T09:17:59.559872Z","iopub.status.idle":"2021-07-17T09:18:43.525479Z","shell.execute_reply.started":"2021-07-17T09:17:59.559841Z","shell.execute_reply":"2021-07-17T09:18:43.524480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_files = glob.glob('./train/*')\ntrn_df = pd.DataFrame(trn_files)\ntrn_df.columns = ['file_path']\ntrn_df['StudyInstanceUID'] = trn_df['file_path'].str.split('/', expand=True)[2]\ndataset_trn = RANZCRDataset(trn_df, 'test', transform=transforms_valid)\ntrn_loader = torch.utils.data.DataLoader(dataset_trn, batch_size=batch_size, shuffle=False, num_workers=num_workers, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:18:46.281076Z","iopub.execute_input":"2021-07-17T09:18:46.281434Z","iopub.status.idle":"2021-07-17T09:18:46.324530Z","shell.execute_reply.started":"2021-07-17T09:18:46.281401Z","shell.execute_reply":"2021-07-17T09:18:46.323825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FEAS_trn = generate_test_features(trn_loader)\nFEAS_trn = torch.tensor(FEAS_trn).cuda()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:18:46.966894Z","iopub.execute_input":"2021-07-17T09:18:46.967562Z","iopub.status.idle":"2021-07-17T09:21:10.799694Z","shell.execute_reply.started":"2021-07-17T09:18:46.967518Z","shell.execute_reply":"2021-07-17T09:21:10.798572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# chestx_df = pd.read_csv('../input/data/Data_Entry_2017.csv')\n# chestx_df['file_path'] = sorted(glob.glob('../input/data/images_*/*/*'))","metadata":{"execution":{"iopub.status.busy":"2021-07-05T16:45:53.763271Z","iopub.execute_input":"2021-07-05T16:45:53.763617Z","iopub.status.idle":"2021-07-05T16:45:53.767753Z","shell.execute_reply.started":"2021-07-05T16:45:53.763586Z","shell.execute_reply":"2021-07-05T16:45:53.766579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# chestx_features = np.load('../input/chest-x-features/chest_x_features.npy')\n# chestx_features = torch.tensor(chestx_features).cuda()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-05T16:45:54.444138Z","iopub.execute_input":"2021-07-05T16:45:54.444442Z","iopub.status.idle":"2021-07-05T16:45:54.448016Z","shell.execute_reply.started":"2021-07-05T16:45:54.444413Z","shell.execute_reply":"2021-07-05T16:45:54.446971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"product = (FEAS@FEAS_trn.T)\n# test['chest_x_image'] = chestx_df.loc[idx.detach().cpu()]['Image Index'].values\n# test['chest_x_file_path'] = chestx_df.loc[idx.detach().cpu()]['file_path'].values","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:21:43.734054Z","iopub.execute_input":"2021-07-17T09:21:43.734426Z","iopub.status.idle":"2021-07-17T09:21:43.742982Z","shell.execute_reply.started":"2021-07-17T09:21:43.734388Z","shell.execute_reply":"2021-07-17T09:21:43.741883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sim_df = pd.DataFrame(product.cpu().numpy(), index=df['StudyInstanceUID'], columns=trn_df['StudyInstanceUID'])","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:21:44.498008Z","iopub.execute_input":"2021-07-17T09:21:44.498378Z","iopub.status.idle":"2021-07-17T09:21:44.830009Z","shell.execute_reply.started":"2021-07-17T09:21:44.498334Z","shell.execute_reply":"2021-07-17T09:21:44.829252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ext_prefix = './external/'\ntrn_prefix = './train/'","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:21:56.831664Z","iopub.execute_input":"2021-07-17T09:21:56.831987Z","iopub.status.idle":"2021-07-17T09:21:56.836091Z","shell.execute_reply.started":"2021-07-17T09:21:56.831957Z","shell.execute_reply":"2021-07-17T09:21:56.834794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(sim_df.max(1) < 0.90).sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:22:33.892639Z","iopub.execute_input":"2021-07-17T09:22:33.892959Z","iopub.status.idle":"2021-07-17T09:22:34.005101Z","shell.execute_reply.started":"2021-07-17T09:22:33.892930Z","shell.execute_reply":"2021-07-17T09:22:34.004042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"se = sim_df[(sim_df.max(1) > 0.95) & (sim_df.max(1) < 0.97)].idxmax(1).sample(1)\nx = se.index[0]\ny = se.values[0]\n\nimg1 = cv2.imread(ext_prefix+x)\nimg2 = cv2.imread(trn_prefix+y)\n\nf, ax = plt.subplots(1, 2, figsize=(16, 8))\nax[0].imshow(img1)\nax[1].imshow(img2)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:21:57.880582Z","iopub.execute_input":"2021-07-17T09:21:57.881074Z","iopub.status.idle":"2021-07-17T09:21:58.741657Z","shell.execute_reply.started":"2021-07-17T09:21:57.881029Z","shell.execute_reply":"2021-07-17T09:21:58.740752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r train","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:25:53.046182Z","iopub.execute_input":"2021-07-17T09:25:53.046533Z","iopub.status.idle":"2021-07-17T09:25:54.351488Z","shell.execute_reply.started":"2021-07-17T09:25:53.046504Z","shell.execute_reply":"2021-07-17T09:25:54.350473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sim_df.to_pickle('sim.pkl')","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:25:54.355262Z","iopub.execute_input":"2021-07-17T09:25:54.355554Z","iopub.status.idle":"2021-07-17T09:25:55.737437Z","shell.execute_reply.started":"2021-07-17T09:25:54.355525Z","shell.execute_reply":"2021-07-17T09:25:55.733890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r external.zip external","metadata":{"execution":{"iopub.status.busy":"2021-07-17T09:26:09.458373Z","iopub.execute_input":"2021-07-17T09:26:09.458747Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r external","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}