{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport sys\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\n\nimport albumentations as A\nimport tqdm\nimport cv2\nimport numpy as np\nimport pandas as pd\n\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader, Dataset, Subset\n\n\nbatch_size, num_workers = 16, 8\ndebug = False\n\n\nresnet_fns = [\n    '../input/ranzcr-clip-models/resnet200d_640_1000_fold_0_model-018-0.961305.pth',\n    '../input/ranzcr-clip-models/resnet200d_640_1000_fold_1_model-015-0.958331.pth',\n    '../input/ranzcr-clip-models/resnet200d_640_1000_fold_2_model-018-0.956810.pth',\n    '../input/ranzcr-clip-models/resnet200d_640_1000_fold_3_model-017-0.958175.pth',\n    '../input/ranzcr-clip-models/resnet200d_640_1000_fold_4_model-010-0.957087.pth',\n]\n\n\neffnet_fns = [\n    '../input/ranzcr-clip-models/tf_efficientnet_b7_ns_1000_fold_0_model-005-0.958068.pth',\n    '../input/ranzcr-clip-models/tf_efficientnet_b7_ns_1000_fold_1_model-005-0.955992.pth',\n    '../input/ranzcr-clip-models/tf_efficientnet_b7_ns_1000_fold_2_model-004-0.954233.pth',\n    '../input/ranzcr-clip-models/tf_efficientnet_b7_ns_1000_fold_3_model-009-0.957107.pth',\n    '../input/ranzcr-clip-models/tf_efficientnet_b7_ns_1000_fold_4_model-008-0.955761.pth',\n]\n\n\nclass RANZCRDatasetInference(Dataset):\n    def __init__(self, df, image_size):\n        self.df = df\n        self.transform = A.Compose([\n            A.Resize(image_size, image_size),\n            A.Normalize(\n                mean=(0.485),\n                std=(0.229),\n            ),\n        ])\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, cv2.IMREAD_GRAYSCALE)\n        mask = img > 0\n        img = img[np.ix_(mask.any(1), mask.any(0))]\n\n        img = np.expand_dims(img, -1)\n\n        res = self.transform(image=img)\n\n        img = res['image']\n        img = torch.from_numpy(img.transpose(2, 0, 1))\n\n        return img\n\n\ndef inference(models, test_loader, device='cuda'):\n    probs = []\n    for images in tqdm.tqdm(test_loader):\n        images = images.to(device)\n        y_preds = []\n        for model in models:\n            batch_preds = []\n            for tta in [lambda x: x, lambda x: x.flip(-1)]:\n                with torch.no_grad():\n                    batch_preds.append(model(tta(images)).sigmoid().to('cpu').numpy())\n            batch_preds = np.mean(batch_preds, axis=0)\n            y_preds.append(batch_preds)\n        y_preds = np.mean(y_preds, axis=0)\n        probs.append(y_preds)\n    probs = np.concatenate(probs)\n    return probs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/sample_submission.csv')\ntest['file_path'] = test.StudyInstanceUID.apply(lambda x: os.path.join('../input/ranzcr-clip-catheter-line-classification/test', f'{x}.jpg'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"models = []\n\nfor fn in effnet_fns:\n    model = torch.load(fn, map_location={'cuda:1':'cuda:0'})\n    model.eval()\n    models.append(model)\n\ntest_dataset = RANZCRDatasetInference(test, 1000)\nif debug:\n    test_dataset = Subset(test_dataset, list(range(batch_size)))\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, num_workers=num_workers, pin_memory=True)\n\neffnet_predictions = inference(models, test_loader)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"models = []\n\nfor fn in resnet_fns:\n    model = torch.load(fn, map_location={'cuda:1':'cuda:0'})\n    model.eval()\n    models.append(model)\n\ntest_dataset = RANZCRDatasetInference(test, 640)\nif debug:\n    test_dataset = Subset(test_dataset, list(range(batch_size)))\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, num_workers=num_workers, pin_memory=True)\n\nresnet_predictions = inference(models, test_loader)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = (effnet_predictions * 0.971 + resnet_predictions * 0.965) / (0.971 + 0.965)\n\ntest = test.drop('file_path', axis=1)\n\nif debug:\n    test.iloc[:batch_size, 1:] = predictions\nelse:\n    test.iloc[:, 1:] = predictions\n\ntest.to_csv('submission.csv', index=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}