{"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":"!pip install ../input/pytorch-modules/einops-0.4.1-py3-none-any.whl","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('../input/pytorch-modules/segmentation_models.pytorch-master/segmentation_models.pytorch-master/')\nsys.path.append('../input/pytorch-modules/pytorch-image-models-master/pytorch-image-models-master/')\nsys.path.append('../input/pytorch-modules/pretrained-models.pytorch-master/pretrained-models.pytorch-master/')\nsys.path.append('../input/pytorch-modules/EfficientNet-PyTorch-master/EfficientNet-PyTorch-master/')\nsys.path.append('../input/pytorch-modules/lightning-master/lightning-master/')\nimport cv2\nimport os \nsys.path.append('../input/pytorch-modules') \nimport pandas as pd\nimport numpy as np\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nimport importlib\nfrom timeit import default_timer as timer\nimport torch\nimport pytorch_lightning as pl\nimport timm\nimport segmentation_models_pytorch as smp\nimport torch.cuda.amp as amp\nimport torch.nn.functional as F\nfrom torch import nn\nfrom torch.utils import data as torch_data\nfrom torch.utils.data import DataLoader\nimport joblib\nimport gc\nfrom coat import *","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:05:53.044673Z","iopub.execute_input":"2022-09-27T10:05:53.045174Z","iopub.status.idle":"2022-09-27T10:05:57.447448Z","shell.execute_reply.started":"2022-09-27T10:05:53.045073Z","shell.execute_reply":"2022-09-27T10:05:57.446401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MayoModel(pl.LightningModule):\n    \n    def __init__(self, model):\n        super().__init__()\n        self.model = model\n        self.register_buffer(\"std\", torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1))\n        self.register_buffer(\"mean\", torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1))\n\n    def forward(self, image):\n        image = (image - self.mean) / self.std\n        logit = self.model(image)\n        return logit    \n    \nclass DataGenerator(torch_data.Dataset):\n    \n    def __init__(self, df):\n        super().__init__()\n        self.df = df\n        self.indexes = np.arange(len(df))\n        \n    def __len__(self):\n        return int(np.floor(len(self.df) / 1))\n    \n    def __getitem__(self, index):\n        \n        img = self.__load_image(self.df['path'].iloc[index])  \n        return img.T\n        \n    def __load_image(self, img_path):\n        try:\n            img = cv2.imread(img_path)\n        except:\n            img = np.zeros((384,384,3), np.uint8)\n        img = cv2.resize(img, dsize=(384,384))\n        img = img.astype(np.float32)/255\n        return img","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:05:57.449434Z","iopub.execute_input":"2022-09-27T10:05:57.450062Z","iopub.status.idle":"2022-09-27T10:05:57.461258Z","shell.execute_reply.started":"2022-09-27T10:05:57.450019Z","shell.execute_reply":"2022-09-27T10:05:57.459854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/mayo-clinic-strip-ai/test.csv')\ntest['path'] = ['./test/'+str(x)+'.tif' for x in test.image_id]\ntest.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:05:57.462796Z","iopub.execute_input":"2022-09-27T10:05:57.463453Z","iopub.status.idle":"2022-09-27T10:05:57.489605Z","shell.execute_reply.started":"2022-09-27T10:05:57.463415Z","shell.execute_reply":"2022-09-27T10:05:57.488549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = glob('../input/mayo-clinic-strip-ai/test/*')\nos.makedirs('test', exist_ok=True)\n\nfor f in tqdm(files):\n    path = './test/'+f.split('/')[-1]\n    try:\n        size = os.path.getsize(f)\n    except:\n        size = 1000000000\n    if(size > 8e8):\n        img = np.zeros((384,384,3), np.uint8)\n        cv2.imwrite(path, img)\n    else:\n        try:\n            img = cv2.imread(f)\n            img = cv2.resize(img, (384,384))\n            cv2.imwrite(path, img)\n        except:\n            img = np.zeros((384,384,3), np.uint8)\n            cv2.imwrite(path, img)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:05:57.492738Z","iopub.execute_input":"2022-09-27T10:05:57.493577Z","iopub.status.idle":"2022-09-27T10:06:23.4191Z","shell.execute_reply.started":"2022-09-27T10:05:57.493548Z","shell.execute_reply":"2022-09-27T10:06:23.418087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = DataGenerator(test)  \ndataloader = DataLoader(dataset, batch_size=1, num_workers=os.cpu_count())","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:06:23.420798Z","iopub.execute_input":"2022-09-27T10:06:23.421795Z","iopub.status.idle":"2022-09-27T10:06:23.427263Z","shell.execute_reply.started":"2022-09-27T10:06:23.421753Z","shell.execute_reply":"2022-09-27T10:06:23.426308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths = [\n        '../input/hubmap-weights/mayo_10.pth',\n        '../input/hubmap-weights/mayo_20.pth',\n              ]\n\nmodel = timm.create_model('swin_large_patch4_window12_384', pretrained=False)\nfc_features = model.head.in_features\nmodel.head = nn.Linear(fc_features, 2, bias=True)\nmodel = MayoModel(model)\ntorch.set_grad_enabled(False)\npreds = []\n\nfor p in paths:\n    model.load_state_dict(torch.load(p))\n    model.eval()\n    p = []\n    for batch_idx, batch in tqdm(enumerate(dataloader)):\n        pred = model.predict_step(batch, batch_idx).sigmoid()\n        p.append(pred.numpy()[0])\n    preds.append(p)\n\npreds = np.mean(preds, axis=0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('../input/mayo-clinic-strip-ai/sample_submission.csv')\ntest[['CE', 'LAA']] = preds\ntest = test[['CE', 'LAA', 'patient_id']].groupby(\"patient_id\").mean()\nsample = sample[['patient_id']].merge(test[['CE', 'LAA']], on='patient_id',how='left')\nsample.to_csv('submission.csv', index=False)\nsample.head(4)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}