{"cells":[{"metadata":{},"cell_type":"markdown","source":"**Training noteebook can found here https://www.kaggle.com/razatabish/ranzr-clip**"},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\nsys.path.append('../input/pytorch-image-models/pytorch-image-models-master')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport os\nimport time \nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom collections import defaultdict\nfrom tqdm import tqdm\nfrom sklearn import model_selection, preprocessing \nimport cv2\nfrom matplotlib import pyplot as plt\nfrom PIL import Image\nimport copy\n\nimport torch \nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.optim.lr_scheduler import CosineAnnealingWarmRestarts, CosineAnnealingLR, ReduceLROnPlateau\n\nfrom torch.utils.data import Dataset, DataLoader \nimport torchvision\nfrom torchvision import models, transforms \nfrom torch.cuda.amp import autocast, GradScaler\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"params = {\n    \"model\": \"efficientnet_b3\", #\"model\": \"resnet50\",\n    \"device\": torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\"),\n    \"lr\": 0.0001,\n    \"batch_size\": 4,\n    \"num_workers\": 16,\n    \"num_epochs\": 10,\n    \"T_0\":6, # CosineAnnealingWarmRestarts\n    \"min_lr\":1e-6\n}","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"ROOT_DIR = \"../input/ranzcr-clip-catheter-line-classification\"\ntest_dir = \"../input/ranzcr-clip-catheter-line-classification/test/\"\nmodel_path = \"../input/ranzr-clip-train-pytorch/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(ROOT_DIR)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(os.path.join(ROOT_DIR, \"sample_submission.csv\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(df['StudyInstanceUID'].unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes = df.columns[1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = df[classes]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#img_list = os.list_dir(os.path.join(ROOT_DIR, df[\"StudyInstanceUID\"]+\".jpg\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes = ['ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal', 'NGT - Abnormal',\n       'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal',\n       'CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal',\n       'Swan Ganz Catheter Present']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class RANZRDataset(Dataset):\n    def __init__(self, data_dir, df, transform=None):\n        self.data_dir = data_dir\n        self.df = df\n        self.files = df[\"StudyInstanceUID\"].values\n        self.transform = transform\n        \n    def __getitem__(self, idx):\n        img_name = self.files[idx]\n        file_path = f\"{self.data_dir}{img_name}\"+\".jpg\"\n        image = Image.open(file_path).convert('RGB')\n        image = np.array(image)\n#         image = cv2.imread(file_path)\n#         image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n        #print(file_path)\n        if self.transform:\n            augmented = self.transform(image = image)\n            image = augmented[\"image\"]\n        \n    \n        \n        return image\n    def __len__(self):\n        return len(self.df)\n        \n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = RANZRDataset(test_dir, df)\nimg = dataset[0]\nplt.imshow(img)\nplt.show()\nimg.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_valid_transform():\n    return A.Compose([\n        A.RandomResizedCrop(300, 300),\n        #A.Flip(0.5),\n        #A.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225]),\n        ToTensorV2(),\n\n    ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# import timm\n\n# class RANZRModel(nn.Module):\n#     def __init__(self, model_name=params[\"model\"], pretrained=False):\n#         super().__init__()\n#         self.model = timm.create_model(model_name, pretrained=pretrained)\n#         n_features = self.model.fc.in_features\n#         self.model.fc = nn.Linear(n_features, len(classes))\n\n#     def forward(self, x):\n#         x = self.model(x)\n#         return x\n#efficientnet\nimport timm\n#EFFICIENTNET-B7\nclass RANZRModel(nn.Module):\n    def __init__(self, model_name=params[\"model\"], pretrained=False):\n        super().__init__()\n        self.model = timm.create_model(model_name, pretrained=pretrained)\n        n_features = self.model.classifier.in_features\n        self.model.classifier = nn.Linear(n_features, len(classes))\n\n    def forward(self, x):\n        x = self.model(x)\n        return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def inference(model,states, test_loader):\n    preds = []\n    model.to(\"cpu\")\n    for image in tqdm(test_loader, total = len(test_loader)):\n        avg_preds = []\n        image = image.to(\"cpu\").float()\n        for state in states:\n            model.load_state_dict(state)\n            model.eval() \n            with torch.no_grad():\n                y_preds = model(image)\n                avg_preds.append(y_preds.sigmoid().to('cpu').numpy())\n        avg_preds = np.mean(avg_preds, axis=0)\n        preds.append(avg_preds)\n\n    preds = np.concatenate(preds)\n    return preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"models_weights = []\nfor file in os.listdir(model_path):\n    if file.endswith(\".pth\"):\n        models_weights.append(file)\nmodels_weights","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = RANZRModel(model_name=params[\"model\"], pretrained=False)\nmodel.to(\"cpu\")\nstates = [torch.load(f\"{model_path}{weights}\", map_location=torch.device('cpu')) for weights in models_weights] \n#states\ntest_dataset = RANZRDataset(test_dir, df, get_valid_transform())\ntest_loader = DataLoader(test_dataset, shuffle=False, pin_memory = True)\npredictions = inference(model, states, test_loader)\n\ndf[classes] = predictions\ndf[['StudyInstanceUID'] + classes].to_csv('./submission.csv', index=False)\ndf.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}