{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"tez_path = '../input/tez-lib/'\neffnet_path = '../input/efficientnet-pytorch/'\ntimm_path = '../input/timm-pytorch-image-models/pytorch-image-models-master'\nimport sys\nsys.path.append(tez_path)\nsys.path.append(effnet_path)\nsys.path.append(timm_path)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nimport albumentations\nimport pandas as pd\nimport numpy as np\nimport timm\nimport tez\nfrom tez.datasets import ImageDataset\n\nimport torch\nimport torch.nn as nn\nfrom torch.nn import functional as F\nfrom tqdm import tqdm\nfrom efficientnet_pytorch import EfficientNet","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class EfficientnetModel(tez.Model):\n    def __init__(self, num_classes):\n        super().__init__()\n\n        self.effnet = EfficientNet.from_name(\"efficientnet-b4\")\n        self.dropout = nn.Dropout(0.1)\n        self.out = nn.Linear(1792, num_classes)\n        self.step_scheduler_after = \"epoch\"\n\n    def forward(self, image, targets=None):\n        batch_size, _, _, _ = image.shape\n\n        x = self.effnet.extract_features(image)\n        x = F.adaptive_avg_pool2d(x, 1).reshape(batch_size, -1)\n        outputs = self.out(self.dropout(x))\n        return outputs, None, None\n    \n    def load(self, model_path, device=\"cuda\"):\n        self.device = device\n        if next(self.parameters()).device != self.device:\n            self.to(self.device)\n        model_dict = torch.load(model_path, map_location=device)\n        self.load_state_dict(model_dict[\"state_dict\"])\n        \n    def predict(self, dataset, sampler=None, batch_size=16, n_jobs=1, collate_fn=None):\n        if next(self.parameters()).device != self.device:\n            self.to(self.device)\n\n        if batch_size == 1:\n            n_jobs = 0\n        data_loader = torch.utils.data.DataLoader(\n            dataset, batch_size=batch_size, num_workers=4, sampler=sampler, collate_fn=collate_fn, pin_memory=True\n        )\n\n        if self.training:\n            self.eval()\n\n        final_output = []\n        tk0 = tqdm(data_loader, total=len(data_loader))\n\n        for b_idx, data in enumerate(tk0):\n            with torch.no_grad():\n                out = self.predict_one_step(data)\n                out = out.cpu().detach().numpy()\n                yield out\n\n            tk0.set_postfix(stage=\"test\")\n        tk0.close()\n        \n    def model_fn(self, data):\n        for key, value in data.items():\n            data[key] = value.to(self.device)\n        #if self.fp16:\n        #    with torch.cuda.amp.autocast():\n        #        output, loss, metrics = self(**data)\n        #else:\n        output, loss, metrics = self(**data)\n        return output, loss, metrics\n    \n    def predict_one_step(self, data):\n        output, _, _ = self.model_fn(data)\n        return output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class CustomResNext(tez.Model):\n    def __init__(self, num_classes, model_name='resnext50_32x4d', pretrained=False):\n        super().__init__()\n\n        self.model = timm.create_model(model_name, pretrained=pretrained)\n        n_features = self.model.fc.in_features\n        self.out = nn.Linear(n_features, num_classes)\n        self.step_scheduler_after = \"epoch\"\n\n    def forward(self, image, targets=None):\n        batch_size, _, _, _ = image.shape\n        outputs = self.model(image)\n        return outputs, None, None\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# augmentations taken from: https://www.kaggle.com/khyeh0719/pytorch-efficientnet-baseline-inference-tta\nimg_size = 512\ntest_aug = albumentations.Compose([\n    albumentations.RandomResizedCrop(img_size, img_size),\n    albumentations.Transpose(p=0.5),\n    albumentations.HorizontalFlip(p=0.5),\n    albumentations.VerticalFlip(p=0.5),\n    albumentations.HueSaturationValue(\n        hue_shift_limit=0.2, \n        sat_shift_limit=0.2,\n        val_shift_limit=0.2, \n        p=0.5\n    ),\n    albumentations.RandomBrightnessContrast(\n        brightness_limit=(-0.1,0.1), \n        contrast_limit=(-0.1, 0.1), \n        p=0.5\n    ),\n    albumentations.Normalize(\n        mean=[0.485, 0.456, 0.406], \n        std=[0.229, 0.224, 0.225], \n        max_pixel_value=255.0, \n        p=1.0\n    )\n], p=1.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfx = pd.read_csv(\"../input/cassava-leaf-disease-classification/sample_submission.csv\")\nimage_path = \"../input/cassava-leaf-disease-classification/test_images/\"\ntest_image_paths = [os.path.join(image_path, x) for x in dfx.image_id.values]\n# fake targets\ntest_targets = dfx.label.values\ntest_dataset = ImageDataset(\n    image_paths=test_image_paths,\n    targets=test_targets,\n    #resize=None,\n    augmentations=test_aug,\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dfx = pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")\nmodel_path = \"../input/cassava-model-3\"\n\nmodel0 = EfficientnetModel(num_classes=train_dfx.label.nunique())\nmodel0.load(f\"{model_path}/efficentnet_model_fold0.bin\", device='cuda')\n\nmodel1 = EfficientnetModel(num_classes=train_dfx.label.nunique())\nmodel1.load(f\"{model_path}/efficentnet_model_fold1.bin\", device='cuda')\n\nmodel2 = EfficientnetModel(num_classes=train_dfx.label.nunique())\nmodel2.load(f\"{model_path}/efficentnet_model_fold2.bin\", device='cuda')\n\nmodel3 = EfficientnetModel(num_classes=train_dfx.label.nunique())\nmodel3.load(f\"{model_path}/efficentnet_model_fold3.bin\", device='cuda')\n\nmodel4 = EfficientnetModel(num_classes=train_dfx.label.nunique())\nmodel4.load(f\"{model_path}/efficentnet_model_fold4.bin\", device='cuda')\n\n\nmodel5= CustomResNext(num_classes=train_dfx.label.nunique())\nmodel5.load(f\"{model_path}/resnet_model_fold0.bin\", device='cuda')\n\nmodel6= CustomResNext(num_classes=train_dfx.label.nunique())\nmodel6.load(f\"{model_path}/resnet_model_fold1.bin\", device='cuda')\n\nmodel7= CustomResNext(num_classes=train_dfx.label.nunique())\nmodel7.load(f\"{model_path}/resnet_model_fold2.bin\", device='cuda')\n\nmodel8= CustomResNext(num_classes=train_dfx.label.nunique())\nmodel8.load(f\"{model_path}/resnet_model_fold3.bin\", device='cuda')\n\nmodel9= CustomResNext(num_classes=train_dfx.label.nunique())\nmodel9.load(f'{model_path}/resnet_model_fold4.bin', device='cuda')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from scipy.special import softmax\nmodel_list = [model0, model1, model2, model3, model4, model5, model6, model7, model8, model9]\n\ndef run_inference(model):\n    final_preds = None\n    for j in range(7):\n        preds = model.predict(test_dataset, batch_size=64, n_jobs=-1)\n        temp_preds = None\n        for p in preds:\n            if temp_preds is None:\n                temp_preds = p\n            else:\n                temp_preds = np.vstack((temp_preds, p))\n        if final_preds is None:\n            final_preds = temp_preds\n        else:\n            final_preds += temp_preds\n    final_preds /= 7\n    final_prob = softmax(final_preds)\n    final_preds = final_preds.argmax(axis=1)\n    return final_preds, final_prob[0][final_preds]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_df = pd.DataFrame()\nnew_df['model0'], new_df['model0_prob'] = run_inference(model_list[0])\nnew_df['model1'], new_df['model1_prob'] = run_inference(model_list[1])\nnew_df['model2'], new_df['model2_prob'] = run_inference(model_list[2])\nnew_df['model3'], new_df['model3_prob'] = run_inference(model_list[3])\nnew_df['model4'], new_df['model4_prob'] = run_inference(model_list[4])\nnew_df['model5'], new_df['model5_prob'] = run_inference(model_list[5])\nnew_df['model6'], new_df['model6_prob'] = run_inference(model_list[6])\nnew_df['model7'], new_df['model7_prob'] = run_inference(model_list[7])\nnew_df['model8'], new_df['model8_prob'] = run_inference(model_list[8])\nnew_df['model9'], new_df['model9_prob'] = run_inference(model_list[9])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from collections import Counter\ndef most_common(lst):\n    data = Counter(lst)\n    return max(lst, key=data.get)\n\nfinal_preds = list()\nfor index, row in new_df.iterrows():\n    out_list = [row['model0'], row['model1'], row['model2'], row['model3'], row['model4'],row['model5'], row['model6'], row['model7'], row['model8'], row['model9'] ]\n    final_preds.append(most_common(out_list))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"\n#Approach 3 for assemble\nfrom statistics import mode\nfinal_preds = list()\n\nfor index, row in new_df.iterrows():\n    temp_df = pd.DataFrame()\n    temp_df['model'] = [row['model0'], row['model1'], row['model2'], row['model3'], row['model4'],row['model5'], row['model6'], row['model7'], row['model8'], row['model9'] ]\n    temp_df['model_prob'] = [row['model0_prob'], row['model1_prob'], row['model2_prob'], row['model3_prob'], row['model4_prob'],row['model5_prob'], row['model6_prob'], row['model7_prob'], row['model8_prob'], row['model9_prob'] ]\n    out_df = temp_df.groupby('model').sum().sort_values('model_prob', ascending=False).reset_index()\n    pred = out_df['model'].tolist()[0]\n    final_preds.append(pred)\n    \n\"\"\"\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"\nApproach 2\nfrom scipy.special import softmax\ndef run_inference(model):\n    final_preds = None\n    for j in range(5):\n        preds = model.predict(test_dataset, batch_size=64, n_jobs=-1)\n        temp_preds = None\n        for p in preds:\n            if temp_preds is None:\n                temp_preds = p\n            else:\n                temp_preds = np.vstack((temp_preds, p))\n        if final_preds is None:\n            final_preds = temp_preds\n        else:\n            final_preds += temp_preds\n    final_preds /= 5\n    return final_preds\n    \n    \nout_preds1 = np.empty([1,5])\nout_preds2 = np.empty([1,1000])\nfor model in model_list:\n    try:\n        out_preds1 = np.add(out_preds1, run_inference(model))\n    except Exception as ex:\n        out_preds2 = np.add(out_preds2, run_inference(model))\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#softmax(out_preds1).argmax(axis=1)\n#out = 0.5*out_preds1 + 0.5*out_preds2[:,:5]\n#final_preds = softmax(out).argmax(axis=1)\n#out_predsoft1 = softmax(out_preds1)\n#out_predsoft2 = softmax(out_preds2[:,:5])\n\n#out = np.add(out_predsoft1, out_predsoft2)\n#final_preds = softmax(out).argmax(axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfx.label = final_preds\ndfx.to_csv(\"submission.csv\", index=False)","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}