{"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/effnetpytorchmodel/efficientnet_pytorch-0.1.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:40:02.859542Z","iopub.execute_input":"2022-01-12T14:40:02.859905Z","iopub.status.idle":"2022-01-12T14:40:33.302669Z","shell.execute_reply.started":"2022-01-12T14:40:02.859812Z","shell.execute_reply":"2022-01-12T14:40:33.301937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Importing Libraries</center></h1>","metadata":{"execution":{"iopub.status.busy":"2022-01-12T10:54:06.695838Z","iopub.execute_input":"2022-01-12T10:54:06.696288Z","iopub.status.idle":"2022-01-12T10:54:06.704146Z","shell.execute_reply.started":"2022-01-12T10:54:06.696234Z","shell.execute_reply":"2022-01-12T10:54:06.702738Z"}}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport cv2 as cv\n\nimport torch\nimport torch.nn as nn\nimport efficientnet_pytorch\n\nfrom torch.utils.data import DataLoader, Dataset\n\nfrom pathlib import Path\n\nfrom albumentations import (\n    Compose, Normalize\n)\nfrom albumentations.pytorch import ToTensorV2","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:40:33.304706Z","iopub.execute_input":"2022-01-12T14:40:33.304983Z","iopub.status.idle":"2022-01-12T14:40:37.364182Z","shell.execute_reply.started":"2022-01-12T14:40:33.304936Z","shell.execute_reply":"2022-01-12T14:40:37.363299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set device\ndevice = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:40:37.365943Z","iopub.execute_input":"2022-01-12T14:40:37.366304Z","iopub.status.idle":"2022-01-12T14:40:37.372061Z","shell.execute_reply.started":"2022-01-12T14:40:37.36626Z","shell.execute_reply":"2022-01-12T14:40:37.371425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set configuration\nclass Config:\n    cfg = {\n        'batch_size': 32,\n        'num_workers': 8,\n        'image_size': (512, 512),\n        'num_classes': 5,\n        'model_path': '../input/effnetb0f3/3_fold_model_effnet_b0_best.torch'\n    }","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:40:37.374145Z","iopub.execute_input":"2022-01-12T14:40:37.374603Z","iopub.status.idle":"2022-01-12T14:40:37.387247Z","shell.execute_reply.started":"2022-01-12T14:40:37.374568Z","shell.execute_reply":"2022-01-12T14:40:37.386567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Paths, files</center></h1>","metadata":{}},{"cell_type":"code","source":"base_dir = Path('/kaggle/input/cassava-leaf-disease-classification')\ntest_img_dir = f'{base_dir}/test_images'\ntest_df = pd.read_csv(f'{base_dir}/sample_submission.csv', index_col=0)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:40:37.388606Z","iopub.execute_input":"2022-01-12T14:40:37.389084Z","iopub.status.idle":"2022-01-12T14:40:37.417365Z","shell.execute_reply.started":"2022-01-12T14:40:37.389053Z","shell.execute_reply":"2022-01-12T14:40:37.416668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:01.154721Z","iopub.execute_input":"2022-01-12T14:41:01.155199Z","iopub.status.idle":"2022-01-12T14:41:01.174147Z","shell.execute_reply.started":"2022-01-12T14:41:01.155162Z","shell.execute_reply":"2022-01-12T14:41:01.173317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Data Preparation</center></h1>","metadata":{}},{"cell_type":"code","source":"class CassavaDataset(Dataset):\n    def __init__(self, df, image_size, augments=None):\n        self.df = df.index.tolist()\n        self.image_size = image_size\n        self.augments = augments\n        \n    def __getitem__(self, idx):\n        image = cv.imread(os.path.join(test_img_dir, self.df[idx]))\n        image = cv.resize(image, self.image_size)\n        image = cv.cvtColor(image, cv.COLOR_BGR2RGB)\n        \n        if self.augments:\n            image = self.augments(image=image)['image']\n        \n        return {'X': image}\n    \n    def __len__(self):\n        return len(self.df)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:25.242633Z","iopub.execute_input":"2022-01-12T14:41:25.243307Z","iopub.status.idle":"2022-01-12T14:41:25.251838Z","shell.execute_reply.started":"2022-01-12T14:41:25.243255Z","shell.execute_reply":"2022-01-12T14:41:25.250916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Augments:\n    test_augments = Compose([\n        Normalize(mean=[.485, .456, .406],\n                  std=[.229, .224, .225],\n                  p=1.),\n        ToTensorV2(p=1.),\n    ],\n    p=1.,\n    )","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:25.523812Z","iopub.execute_input":"2022-01-12T14:41:25.524138Z","iopub.status.idle":"2022-01-12T14:41:25.528762Z","shell.execute_reply.started":"2022-01-12T14:41:25.524102Z","shell.execute_reply":"2022-01-12T14:41:25.528052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = CassavaDataset(df=test_df, image_size=Config.cfg['image_size'], augments=Augments.test_augments)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:25.765363Z","iopub.execute_input":"2022-01-12T14:41:25.766021Z","iopub.status.idle":"2022-01-12T14:41:25.770306Z","shell.execute_reply.started":"2022-01-12T14:41:25.765983Z","shell.execute_reply":"2022-01-12T14:41:25.769511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataloader = DataLoader(\n    test_dataset,\n    batch_size=Config.cfg['batch_size'],\n    shuffle=False,\n    num_workers=Config.cfg['num_workers']\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:26.554848Z","iopub.execute_input":"2022-01-12T14:41:26.555591Z","iopub.status.idle":"2022-01-12T14:41:26.563511Z","shell.execute_reply.started":"2022-01-12T14:41:26.555549Z","shell.execute_reply":"2022-01-12T14:41:26.562538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Model Initialization</center></h1>","metadata":{}},{"cell_type":"code","source":"def efficientnet_b0(num_classes):\n    model = efficientnet_pytorch.EfficientNet.from_name('efficientnet-b0')\n    model._fc = nn.Linear(in_features=1280,\n                          out_features=num_classes,\n                          bias=True)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:28.258768Z","iopub.execute_input":"2022-01-12T14:41:28.259254Z","iopub.status.idle":"2022-01-12T14:41:28.263988Z","shell.execute_reply.started":"2022-01-12T14:41:28.259216Z","shell.execute_reply":"2022-01-12T14:41:28.263393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = efficientnet_b0(Config.cfg['num_classes']).to(device)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:28.517291Z","iopub.execute_input":"2022-01-12T14:41:28.517755Z","iopub.status.idle":"2022-01-12T14:41:28.638613Z","shell.execute_reply.started":"2022-01-12T14:41:28.517707Z","shell.execute_reply":"2022-01-12T14:41:28.63761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = torch.load(Config.cfg['model_path'], map_location=device)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:28.897406Z","iopub.execute_input":"2022-01-12T14:41:28.897701Z","iopub.status.idle":"2022-01-12T14:41:30.269309Z","shell.execute_reply.started":"2022-01-12T14:41:28.897667Z","shell.execute_reply":"2022-01-12T14:41:30.268385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(checkpoint['model_state_dict'])\n\nbest_score = checkpoint['best_valid_score']\nepoch_num = checkpoint['num_epoch']\n\nprint(f'Best validation score: {round(best_score, 4)} in {epoch_num} epoch.')","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:30.272063Z","iopub.execute_input":"2022-01-12T14:41:30.272716Z","iopub.status.idle":"2022-01-12T14:41:30.306425Z","shell.execute_reply.started":"2022-01-12T14:41:30.272675Z","shell.execute_reply":"2022-01-12T14:41:30.305591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Evaluation</center></h1>","metadata":{}},{"cell_type":"code","source":"model.eval()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:32.245133Z","iopub.execute_input":"2022-01-12T14:41:32.24542Z","iopub.status.idle":"2022-01-12T14:41:32.25505Z","shell.execute_reply.started":"2022-01-12T14:41:32.245392Z","shell.execute_reply":"2022-01-12T14:41:32.254354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prediction = []\n\nfor batch in test_dataloader:\n    with torch.no_grad():\n        X_test = batch['X'].to(device)\n        \n        y_prediction.extend(model(X_test).argmax(axis=-1).cpu().numpy())\n        \nprint(y_prediction)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:34.244985Z","iopub.execute_input":"2022-01-12T14:41:34.24587Z","iopub.status.idle":"2022-01-12T14:41:34.92361Z","shell.execute_reply.started":"2022-01-12T14:41:34.24582Z","shell.execute_reply":"2022-01-12T14:41:34.922902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Submission</center></h1>","metadata":{}},{"cell_type":"code","source":"test_df['label'] = y_prediction\ntest_df.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-12T14:41:36.34115Z","iopub.execute_input":"2022-01-12T14:41:36.342021Z","iopub.status.idle":"2022-01-12T14:41:36.352323Z","shell.execute_reply.started":"2022-01-12T14:41:36.341987Z","shell.execute_reply":"2022-01-12T14:41:36.351504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}