{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\nfrom albumentations import Resize\nfrom albumentations.pytorch import ToTensorV2\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\ndf = pd.read_csv(\"../input/ranzcr-clip-catheter-line-classification/train.csv\")\n\nLABELS = [\n    'ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal',\n    'NGT - Abnormal', 'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal', \n    'CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal',\n    'Swan Ganz Catheter Present'\n]\n\ndf.head()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEBUG = False\nif DEBUG:\n    df = df.sample(frac = 0.01).reset_index(drop = True)\n    print(df.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain, valid = train_test_split(df, test_size = 0.1)\nprint(train.shape, valid.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset\n\nclass TrainDataset(Dataset):\n    def __init__(self, df):\n        self.df = df\n        self.studyuid = df[\"StudyInstanceUID\"].values\n        self.labels = df[LABELS].values\n        \n    def __len__(self):\n        return self.df.shape[0]\n    \n    def __getitem__(self, idx):\n        path = self.studyuid[idx]\n        path = \"../input/ranzcr-clip-catheter-line-classification/train\" + \"/\" + path + \".jpg\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        image = Resize(512, 512)(image = image)[\"image\"]\n        image = ToTensorV2()(image = image)[\"image\"]\n        labels = self.labels[idx]\n        return image, labels","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\ntrain_dataset = TrainDataset(train)\ntrain_loader = DataLoader(train_dataset, batch_size = 16, shuffle = True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_dataset = TrainDataset(valid)\nvalid_loader = DataLoader(valid_dataset, batch_size = 16, shuffle = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('../input/pytorch-image-models/pytorch-image-models-master')\nimport timm\nfrom pprint import pprint\npprint(timm.list_models(pretrained = True))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nclass ResNet200D(nn.Module):\n    def __init__(self, model_name='resnet200d'):\n        super().__init__()\n        self.model = timm.create_model(model_name, pretrained=False)\n        n_features = self.model.fc.in_features\n        self.model.global_pool = nn.Identity()\n        self.model.fc = nn.Identity()\n        self.pooling = nn.AdaptiveAvgPool2d(1)\n        self.fc = nn.Linear(n_features, 11)\n\n    def forward(self, x):\n        bs = x.size(0)\n        features = self.model(x)\n        pooled_features = self.pooling(features).view(bs, -1)\n        output = self.fc(pooled_features)\n        return output","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nDEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n\n\nmodel = ResNet200D().to(DEVICE)\n\ncriterion = nn.BCEWithLogitsLoss()\noptimizer = torch.optim.Adam(model.parameters())\n\nbest_loss = np.inf\nfor epoch in range(6):\n    model.train()\n    for X, y in train_loader:\n        optimizer.zero_grad()\n        X = X.float().to(DEVICE)\n        y = y.float().to(DEVICE)\n        pred = model(X)\n        loss = criterion(pred, y)\n        loss.backward()\n        optimizer.step()\n    model.eval()\n    valid_loss = 0\n    with torch.no_grad():\n        for X, y in valid_loader:\n            X = X.float().to(DEVICE)\n            y = y.float().to(DEVICE)\n            pred = model(X)\n            loss = criterion(pred, y)\n            valid_loss += loss.item()\n    valid_loss /= len(valid_loader)\n    print(f\"EPOCH:{epoch}, Loss:{valid_loss}\")\n    if valid_loss < best_loss:\n        best_loss = valid_loss\n        torch.save(model.state_dict(), \"resnet200d.pth\")\n        print(\"saved...\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self, df):\n        self.df = df\n        self.studyuid = df[\"StudyInstanceUID\"].values\n        \n    def __len__(self):\n        return self.df.shape[0]\n\n    def __getitem__(self, idx):\n        path = self.studyuid[idx]\n        path = \"../input/ranzcr-clip-catheter-line-classification/test\" + \"/\" + path + \".jpg\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        image = Resize(300, 300)(image = image)[\"image\"]\n        image = ToTensorV2()(image = image)[\"image\"]\n        return image","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/ranzcr-clip-catheter-line-classification/sample_submission.csv\")\ntest_dataset = TestDataset(test)\ntest_loader = DataLoader(test_dataset, batch_size = 32, shuffle = False)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = ResNet200D().to(DEVICE)\nmodel.load_state_dict(torch.load(\"./resnet200d.pth\"))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_preds = []\n\nmodel.eval()\nwith torch.no_grad():\n    for X in test_loader:\n        X = X.float().to(DEVICE)\n        submit_preds.append(model(X).sigmoid().to(\"cpu\"))\n    submit_preds = np.concatenate([p.numpy() for p in submit_preds], axis = 0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.DataFrame(submit_preds, columns = LABELS)\nsubmit.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit[\"StudyInstanceUID\"] = test[\"StudyInstanceUID\"]\nsubmit = pd.concat([submit.iloc[:, -1], submit.iloc[:, :-1]], axis = 1)\nsubmit.to_csv(\"submission.csv\", index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}