{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!pip install tez","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 tez\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n\nfrom tez.datasets import ImageDataset\nfrom tez.callbacks import EarlyStopping\n\nimport torch\nimport torch.nn as nn\n\nimport torchvision\n\nfrom sklearn import metrics, model_selection\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfx = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfx.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfx.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train, df_valid = model_selection.train_test_split(dfx, test_size=0.1,random_state=42, stratify=dfx.label.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = df_train.reset_index(drop=True)\ndf_valid = df_valid.reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print (df_train.shape)\nprint (df_valid.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_path = '../input/cassava-leaf-disease-classification/train_images/'\n\ntrain_images_path = [os.path.join(image_path, x) for x in df_train.image_id.values]\nvalid_images_path = [os.path.join(image_path, x) for x in df_valid.image_id.values]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print (train_images_path[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_targets = df_train.label.values\nvalid_targets = df_valid.label.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_aug = albumentations.Compose([\n            albumentations.RandomResizedCrop(256, 256),\n            albumentations.Transpose(p=0.5),\n            albumentations.HorizontalFlip(p=0.5),\n            albumentations.VerticalFlip(p=0.5),\n            albumentations.ShiftScaleRotate(p=0.5),\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            )], p=1.)\n      \nvalid_aug = albumentations.Compose([\n            albumentations.CenterCrop(256, 256, p=1.),\n            albumentations.Resize(256, 256),\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            )], p=1.)\n\n\ntrain_dataset = ImageDataset(image_paths=train_images_path, \n                             targets=train_targets, resize=None, augmentations=train_aug)\n\nvalid_dataset = ImageDataset(image_paths=valid_images_path, \n                             targets=valid_targets, resize=None, augmentations=valid_aug)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dataset[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class LeafModel(tez.Model):\n    def __init__(self, num_classes, pretrained=True):\n        super().__init__()\n        print (num_classes)\n        self.convnet = torchvision.models.resnet18(pretrained=pretrained)\n        self.convnet.fc = nn.Linear(512, num_classes)\n        self.step_scheduler_after = \"epoch\"\n        \n    def loss(self, output, target=None):\n        if target is None:\n            return None\n        return nn.CrossEntropyLoss()(output, target)\n    \n    def monitor_metrics(self, output, targets):\n        output = torch.argmax(output, dim=1).cpu().detach().numpy()\n        target = targets.cpu().detach().numpy()\n        acc = metrics.accuracy_score(output, target)\n        return {\n            \"accuracy\":acc\n        }\n    \n    def fetch_optimizer(self):\n        opt = torch.optim.Adam(self.convnet.parameters(), lr=1e-3)\n        return opt\n    \n    def fetch_scheduler(self):\n        sch = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(\n            self.optimizer, T_0=10, T_mult=1, eta_min=1e-6, last_epoch=-1\n        )\n        return sch\n        \n    def forward(self, image, targets=None):\n        output = self.convnet(image)\n        if targets is not None:\n            loss = self.loss(output, targets)\n            mon_metrics = self.monitor_metrics(output, targets)\n            return output, loss, mon_metrics\n        return output, None, None","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = LeafModel(num_classes=dfx.label.nunique(), pretrained=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = train_dataset[0][\"image\"]\ny = train_dataset[0][\"targets\"]\nmodel(x.unsqueeze(0), y.unsqueeze(0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"es = EarlyStopping(\n        monitor=\"valid_accuracy\",\n        model_path = \"model.bin\",\n        patience = 2,\n        mode = \"max\"\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(train_dataset, \n          valid_dataset=valid_dataset, \n          train_bs=32, \n          valid_bs=64,\n          device = \"cuda\",\n          callbacks = [es],\n          fp16 = True,\n          epochs = 15\n         )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save(\"model.bin\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#final_model = model.load('model.bin')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test_aug = albumentations.Compose(\n#     [\n#             albumentations.RandomResizedCrop(256,256),\n#             albumentations.Transpose(p=0.5),\n#             albumentations.HorizontalFlip(p=0.5),\n#             albumentations.VerticalFlip(p=0.5)\n#     ]\n# )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# df_test = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test_path = '../input/cassava-leaf-disease-classification/test_images'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test_image_paths = [os.path.join(test_path, x) for x in df_test.image_id]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# print(test_image_paths)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test_aug = albumentations.Compose(\n#     [\n        \n#         albumentations.CenterCrop(256,256, p=1.0),\n#         albumentations.Resize(256,256),\n#         albumentations.Transpose(p=0.5),\n#         albumentations.HorizontalFlip(p=0.5),\n#         albumentations.VerticalFlip(p=0.5)\n        \n#     ]\n\n# )\n# test_targets = df_test.label.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test_dataset = ImageDataset(image_paths=test_image_paths, \n#                              targets=test_targets, resize=None, augmentations=test_aug)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test_dataset[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# preds = model.predict(test_dataset, batch_size=32, n_jobs=-1, device=\"cuda\")\n# final_preds = None\n# for p in preds:\n#     if final_preds is None:\n#         final_preds = p\n#     else:\n#         final_preds = np.vstack((final_preds, p))\n# final_preds = final_preds.argmax(axis=1)\n# df_test.label = final_preds\n# df_test.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# df_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Autoencoder -- commit\n### Commit this notebook as well\n","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}