{"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":"markdown","source":"# Imports ","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt \nimport pytorch_lightning as pl \nimport torch \nfrom torch.utils.data import Dataset, DataLoader\n\n# setting up timm models \nimport sys \nsys.path.append(\"../input/timm-pytorch-image-models/pytorch-image-models-master/\")\nimport timm\nimport torch.nn as nn ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-05T10:51:56.192184Z","iopub.execute_input":"2022-08-05T10:51:56.193535Z","iopub.status.idle":"2022-08-05T10:52:00.520464Z","shell.execute_reply.started":"2022-08-05T10:51:56.192862Z","shell.execute_reply":"2022-08-05T10:52:00.519017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/mayo-clinic-strip-ai/train.csv\")\ntest = pd.read_csv(\"../input/mayo-clinic-strip-ai/test.csv\")\nother = pd.read_csv(\"../input/mayo-clinic-strip-ai/other.csv\")\nss = pd.read_csv(\"../input/mayo-clinic-strip-ai/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-05T10:52:00.525369Z","iopub.execute_input":"2022-08-05T10:52:00.526408Z","iopub.status.idle":"2022-08-05T10:52:00.568693Z","shell.execute_reply.started":"2022-08-05T10:52:00.526367Z","shell.execute_reply":"2022-08-05T10:52:00.567032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generate_new = False \nclass ImgDataset(Dataset):\n    def __init__(self, df):\n        self.df = df \n        self.train = 'label' in df.columns\n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        if(generate_new):\n            paths = [\"../test/\", \"../train/\"]\n        else:\n            paths = [\"../input/jpg-images-strip-ai/test/\", \"../input/jpg-images-strip-ai/train/\"]\n        try:\n            image = cv2.imread(paths[self.train] + self.df.iloc[index].image_id + \".jpg\")\n        except:\n            image = np.zeros((512,512,3), np.uint8)\n        label = 0\n        try:\n            if len(image.shape) == 5:\n                image = image.squeeze().transpose(1, 2, 0)\n            image = cv2.resize(image, (512, 512)).transpose(2, 0, 1)\n        except:\n            image = np.zeros((3, 512, 512))\n        if(self.train):\n            label = {\"CE\" : 0, \"LAA\": 1}[self.df.iloc[index].label]\n        patient_id = self.df.iloc[index].patient_id\n        image = torch.Tensor(image)\n        return image, label, patient_id","metadata":{"execution":{"iopub.status.busy":"2022-08-05T10:52:00.57345Z","iopub.execute_input":"2022-08-05T10:52:00.573972Z","iopub.status.idle":"2022-08-05T10:52:00.590862Z","shell.execute_reply.started":"2022-08-05T10:52:00.573928Z","shell.execute_reply":"2022-08-05T10:52:00.589471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = ImgDataset(train)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-05T10:52:00.598016Z","iopub.execute_input":"2022-08-05T10:52:00.598394Z","iopub.status.idle":"2022-08-05T10:52:00.608301Z","shell.execute_reply.started":"2022-08-05T10:52:00.598356Z","shell.execute_reply":"2022-08-05T10:52:00.606996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:06:01.043265Z","iopub.execute_input":"2022-08-05T11:06:01.044008Z","iopub.status.idle":"2022-08-05T11:06:01.04903Z","shell.execute_reply.started":"2022-08-05T11:06:01.04397Z","shell.execute_reply":"2022-08-05T11:06:01.047882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MayoModel(pl.LightningModule):\n    def __init__(self,backbone):\n        super().__init__()\n        self.backbone = backbone \n        self.l1 = nn.Linear(1000,2)\n        \n    def forward(self, x):\n        out = self.backbone(x)\n        out = self.l1(out)\n        return out \n        \n    def train_dataloader(self):\n        return DataLoader(ImgDataset(train), batch_size = 32, shuffle = True)\n    \n    def training_step(self, batch):\n        x, y, patient_id = batch \n        out  = self(x)\n        loss = F.cross_entropy(out, y)\n        return loss \n    def test_step(self,batch):\n        x, y, patient_id = batch \n        out  = self(x)\n        return out\n    \n    def configure_optimizers(self):\n        return torch.optim.Adam(self.parameters(), lr = 0.02)\n    \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:06:01.15337Z","iopub.execute_input":"2022-08-05T11:06:01.154016Z","iopub.status.idle":"2022-08-05T11:06:01.163251Z","shell.execute_reply.started":"2022-08-05T11:06:01.153983Z","shell.execute_reply":"2022-08-05T11:06:01.162303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = pl.callbacks.ModelCheckpoint(dirpath='./')\ntrainer = pl.Trainer(\n    callbacks=[checkpoint],\n    weights_save_path='./',\n    max_epochs=10 ,\n    accelerator='gpu', \n    devices=1\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:06:01.346989Z","iopub.execute_input":"2022-08-05T11:06:01.347315Z","iopub.status.idle":"2022-08-05T11:06:01.355823Z","shell.execute_reply.started":"2022-08-05T11:06:01.347285Z","shell.execute_reply":"2022-08-05T11:06:01.354862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"backbone = timm.create_model(\"resnet34\")","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:06:01.449859Z","iopub.execute_input":"2022-08-05T11:06:01.450389Z","iopub.status.idle":"2022-08-05T11:06:01.813068Z","shell.execute_reply.started":"2022-08-05T11:06:01.450358Z","shell.execute_reply":"2022-08-05T11:06:01.811967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = MayoModel(backbone)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:06:01.815177Z","iopub.execute_input":"2022-08-05T11:06:01.815686Z","iopub.status.idle":"2022-08-05T11:06:01.823403Z","shell.execute_reply.started":"2022-08-05T11:06:01.815628Z","shell.execute_reply":"2022-08-05T11:06:01.822212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.fit(model)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:06:02.904939Z","iopub.execute_input":"2022-08-05T11:06:02.905577Z","iopub.status.idle":"2022-08-05T11:08:33.3485Z","shell.execute_reply.started":"2022-08-05T11:06:02.905519Z","shell.execute_reply":"2022-08-05T11:08:33.347337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making predictions ","metadata":{}},{"cell_type":"code","source":"test_data = DataLoader(ImgDataset(test), batch_size = 32, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:08:33.35077Z","iopub.execute_input":"2022-08-05T11:08:33.351158Z","iopub.status.idle":"2022-08-05T11:08:33.358955Z","shell.execute_reply.started":"2022-08-05T11:08:33.351119Z","shell.execute_reply":"2022-08-05T11:08:33.357601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs = []\nfor batch in test_data:\n        x, y, patient_id = batch \n        out  = model(x)\n        outputs.append(out)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:11:11.871255Z","iopub.execute_input":"2022-08-05T11:11:11.871686Z","iopub.status.idle":"2022-08-05T11:11:14.182931Z","shell.execute_reply.started":"2022-08-05T11:11:11.87165Z","shell.execute_reply":"2022-08-05T11:11:14.181295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:14:25.79022Z","iopub.execute_input":"2022-08-05T11:14:25.790703Z","iopub.status.idle":"2022-08-05T11:14:25.810862Z","shell.execute_reply.started":"2022-08-05T11:14:25.790662Z","shell.execute_reply":"2022-08-05T11:14:25.809785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ce_arr = []\nlaa_arr = []\nfor x in outputs[0]:\n    ce_arr.append(x[0].detach().numpy())\n    laa_arr.append(x[1].detach().numpy())","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:15:16.537055Z","iopub.execute_input":"2022-08-05T11:15:16.537446Z","iopub.status.idle":"2022-08-05T11:15:16.544335Z","shell.execute_reply.started":"2022-08-05T11:15:16.537414Z","shell.execute_reply":"2022-08-05T11:15:16.543027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss[\"CE\"] = ce_arr","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:15:23.840192Z","iopub.execute_input":"2022-08-05T11:15:23.84058Z","iopub.status.idle":"2022-08-05T11:15:23.846631Z","shell.execute_reply.started":"2022-08-05T11:15:23.840524Z","shell.execute_reply":"2022-08-05T11:15:23.845454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss[\"LAA\"] = laa_arr","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:15:33.749753Z","iopub.execute_input":"2022-08-05T11:15:33.751075Z","iopub.status.idle":"2022-08-05T11:15:33.756978Z","shell.execute_reply.started":"2022-08-05T11:15:33.751028Z","shell.execute_reply":"2022-08-05T11:15:33.755663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:15:37.111817Z","iopub.execute_input":"2022-08-05T11:15:37.112174Z","iopub.status.idle":"2022-08-05T11:15:37.123094Z","shell.execute_reply.started":"2022-08-05T11:15:37.112143Z","shell.execute_reply":"2022-08-05T11:15:37.121855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss.to_csv(\"submission.csv\", index = False )","metadata":{"execution":{"iopub.status.busy":"2022-08-05T11:15:53.961047Z","iopub.execute_input":"2022-08-05T11:15:53.96173Z","iopub.status.idle":"2022-08-05T11:15:53.969782Z","shell.execute_reply.started":"2022-08-05T11:15:53.961692Z","shell.execute_reply":"2022-08-05T11:15:53.968846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}