{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom PIL import Image\nimport cv2\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import StratifiedKFold\nfrom glob import glob\n\nimport pytorch_lightning as pl\nfrom pytorch_lightning import Trainer, seed_everything\nfrom pytorch_lightning import Callback\nfrom pytorch_lightning.loggers import CSVLogger\nfrom pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping\n\nimport torch\nfrom torch.utils.data import DataLoader, Dataset\nfrom torchvision.models import resnet18\n\nimport albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2\n\n\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\n\nrandom_seed = 42\ntrain_batch = 16\nbatch_size = 32\nworking_dir = \"/kaggle/input/plant-pathology-2021-fgvc8/\"\nDIR_MODELS = '/kaggle/working'\nk_fold_number = 0\nnum_classes = 6\n\nepoch_num = 20\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# label_name = \"scab frog_eye_leaf_spot complex\"\n# train_img_names = train_df.loc[train_df[\"labels\"] == label_name].head().image\ntest_df = pd.DataFrame(columns=[\"image\"])\n# subplot_num = 511\n(path, _, img_names) = next(os.walk(working_dir + \"test_images/\"))\n\ntest_df.loc[:,\"image\"] = img_names\n# for img_name in img_names:\n#     print(img_name)\n    \n\n#     im = cv2.imread(path + img_name) \n# #     plt.subplot(subplot_num)\n# #     plt.title(name + \" label: \" + label_name)\n#     plt.imshow(im)\n#     plt.show()\n#     subplot_num += 1\n#     break\nprint(len(img_names))\n    \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PlantDataset(Dataset):\n    def __init__(self, df, dir_path):\n        \n        self.dir_path = dir_path\n        self.df = df\n        self.img_ids = self.df.image.unique()\n        \n    def __getitem__(self, index):\n        img_id = self.img_ids[index]\n#         target = self.df[self.df[\"image\"] == img_id].labels.iloc[0]\n#         target = self.encode_target(target)\n        \n#         image = cv2.imread(self.dir_path + img_id, cv2.IMREAD_COLOR)\n        image = cv2.imread(working_dir + \"test_images/\" + img_id, cv2.IMREAD_COLOR)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n        \n#         image /= 255.0\n        \n        image = self.transform()(image=image)[\"image\"]\n        \n        return {\"image\": image, \"img_id\": img_id}\n    \n    def get_by_id(self,img_id):\n        index = np.where(self.img_ids == img_id)[0][0]\n        return self.__getitem__(index)\n    \n    def transform(self):\n        return A.Compose([\n            A.Resize(512,512),\n            A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n            ToTensorV2(p=1.0)\n        ])\n    \n    def encode_target(self, target):\n#         scab healthy frog_eye_leaf_spot rust complex powdery_mildew    \n        encoded = list(map(int, ['scab' in target, \"healthy\" in target, \"frog_eye_leaf_spot\" in target, \"rust\" in target,  \"complex\" in target, \"powdery_mildew\" in target]))\n        \n        return encoded\n    \n    def __len__(self):\n        return self.img_ids.shape[0]\n       ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df.count()\ntest_path = working_dir + \"test_images/\"\ntest_dataset = PlantDataset(test_df, test_path)\n\nprint(test_dataset[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def collate_fn(batch):\n#     return tuple(zip(*batch))\n\ntest_data_loader = DataLoader(\n    test_dataset,\n    batch_size=batch_size,\n    shuffle=False,\n    num_workers=4,\n#     collate_fn=collate_fn\n)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = resnet18(pretrained=False)\n# dir(model)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add_module(name=\"fc\", module=torch.nn.Linear(in_features=512, out_features=6, bias=True))\n# # model.add_module(name=\"Sigmoid\", module=torch.nn.Sigmoid())\n# print(next(model.modules()))\n\n# model.to(device)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Lightning usage\n\nclass LitModel(pl.LightningModule):\n    def __init__(self, model):\n        super(LitModel, self).__init__()\n        self.model = model\n        self.metric = pl.metrics.F1(num_classes=num_classes)\n        self.criterion = torch.nn.BCEWithLogitsLoss()\n        self.lr = 5e-3\n        \n    def forward(self, x, *args, **kwargs):\n        output = self.model(x)        \n        return torch.nn.Sigmoid()(output.detach()).apply_(lambda x: x > 0.5)\n    \n    def configure_optimizers(self):\n        self.optimizer = torch.optim.Adam(self.parameters(), lr=self.lr)\n        self.scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(self.optimizer, T_max=20, eta_min=1e-6)\n        \n        return {\"optimizer\": self.optimizer, \"lr_scheduler\": self.scheduler}\n    \n    def training_step(self, batch, batch_idx):\n        image = batch[\"image\"]\n        target = batch[\"target\"]\n        \n        output = self.model(image)\n        loss = self.criterion(output, target)\n        metric = self.metric(output, target)\n        \n        logs = {\"training_loss\": loss, \"train_f1\": metric, \"lr\": self.optimizer.param_groups[0][\"lr\"]}\n        \n        self.log_dict(logs, on_step=False, on_epoch=True, prog_bar=True, logger=True)\n        return loss\n    \n    def validation_step(self, batch, batch_idx):\n        image = batch[\"image\"]\n        target = batch[\"target\"]\n        \n        output = self.model(image)\n        loss = self.criterion(output, target)\n        metric = self.metric(output, target)\n        \n        logs = {\"valid_loss\": loss, \"valid_f1\": metric}\n        \n        self.log_dict(logs, on_step=False, on_epoch=True, prog_bar=True, logger=True)\n        return loss","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"../input/plant-pathology-2021/logs/Resnet/version_0/checkpoints/checkpoint/epoch=10-valid_loss=0.1101-valid_f1=0.8661.ckpt\"\n# path = \"../input/plant-pathology-2021/logs/Resnet/version_0/checkpoints/last.ckpt\"\nlit_model = LitModel(model)\nlit_model = lit_model.load_from_checkpoint(path, model=model)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img = next(iter(test_data_loader))\n# img[\"image\"]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_target(target):\n#         scab healthy frog_eye_leaf_spot rust complex powdery_mildew \n    labels = [\"scab\", \"healthy\", \"frog_eye_leaf_spot\", \"rust\", \"complex\", \"powdery_mildew\"]\n    decoded = []\n    for key, x in enumerate(target):\n        if x:\n            decoded.append(labels[key])\n#     decoded = [labels[key] if x else]\n#     encoded = list(map(int, ['scab' if target[0], \"healthy\" in target, \"frog_eye_leaf_spot\" in target, \"rust\" in target,  \"complex\" in target, \"powdery_mildew\" in target]))\n\n    return \" \".join(decoded)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# target = [1, 0, 1, 0, 1, 0]\n# decoded = decode_target(target)\n# decoded","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lit_model.eval()\n\noutput_df = pd.DataFrame(columns=[\"image\", \"labels\"])\nfor batch in test_data_loader:\n    with torch.no_grad():\n        img = batch[\"image\"]\n        output = lit_model(img)\n        preds = list(map(decode_target, output.tolist()))\n        to_df = [pd.Series(x, index=output_df.columns) for x in zip(batch[\"img_id\"],preds)]    \n        output_df = output_df.append(to_df, ignore_index=True)\n#         print(batch[\"img_id\"], output, preds)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# output_df.loc[:, \"image\"] = batch[\"img_id\"]\n# output_df.loc[:, \"labels\"] = preds\noutput_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# list(zip(batch[\"img_id\"],preds))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_df.to_csv(\"./submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}