{"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)\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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-07T18:31:08.383598Z","iopub.execute_input":"2023-08-07T18:31:08.384028Z","iopub.status.idle":"2023-08-07T18:31:27.000665Z","shell.execute_reply.started":"2023-08-07T18:31:08.383990Z","shell.execute_reply":"2023-08-07T18:31:26.999040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install tez","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:12:04.840702Z","iopub.execute_input":"2023-08-07T19:12:04.841361Z","iopub.status.idle":"2023-08-07T19:12:19.306936Z","shell.execute_reply.started":"2023-08-07T19:12:04.841325Z","shell.execute_reply":"2023-08-07T19:12:19.305829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tez_path=\"..input/tez_lib/ \"\nimport sys\nsys.path.append(tez_path)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:25:19.424713Z","iopub.execute_input":"2023-08-07T19:25:19.425395Z","iopub.status.idle":"2023-08-07T19:25:19.429701Z","shell.execute_reply.started":"2023-08-07T19:25:19.425361Z","shell.execute_reply":"2023-08-07T19:25:19.428797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \nimport matplotlib.pyplot as plt\nimport pandas as pd \nimport numpy as np \nimport albumentations","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:25:20.130233Z","iopub.execute_input":"2023-08-07T19:25:20.131151Z","iopub.status.idle":"2023-08-07T19:25:20.136277Z","shell.execute_reply.started":"2023-08-07T19:25:20.131109Z","shell.execute_reply":"2023-08-07T19:25:20.135025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tez\nfrom tez.datasets import ImageDataset \nfrom tez.callbacks import EarlyStopping\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:25:41.840007Z","iopub.execute_input":"2023-08-07T19:25:41.840360Z","iopub.status.idle":"2023-08-07T19:25:41.847944Z","shell.execute_reply.started":"2023-08-07T19:25:41.840333Z","shell.execute_reply":"2023-08-07T19:25:41.846816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch \nimport torch.nn as nn \nimport torchvision \nfrom sklearn import metrics,model_selection\n%matplotlib inline ","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:28:12.817780Z","iopub.execute_input":"2023-08-07T19:28:12.818937Z","iopub.status.idle":"2023-08-07T19:28:12.825559Z","shell.execute_reply.started":"2023-08-07T19:28:12.818898Z","shell.execute_reply":"2023-08-07T19:28:12.824175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfx=pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:31:29.623890Z","iopub.execute_input":"2023-08-07T19:31:29.624815Z","iopub.status.idle":"2023-08-07T19:31:29.654425Z","shell.execute_reply.started":"2023-08-07T19:31:29.624778Z","shell.execute_reply":"2023-08-07T19:31:29.653496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfx.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:31:46.016970Z","iopub.execute_input":"2023-08-07T19:31:46.017350Z","iopub.status.idle":"2023-08-07T19:31:46.034561Z","shell.execute_reply.started":"2023-08-07T19:31:46.017317Z","shell.execute_reply":"2023-08-07T19:31:46.033493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfx.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:32:41.367331Z","iopub.execute_input":"2023-08-07T19:32:41.368448Z","iopub.status.idle":"2023-08-07T19:32:41.382271Z","shell.execute_reply.started":"2023-08-07T19:32:41.368405Z","shell.execute_reply":"2023-08-07T19:32:41.380805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train  , df_valid=model_selection.train_test_split(\n\n    dfx,\n    test_size=0.2,\n    random_state=42,\n    stratify=dfx.label.values\n)\ndf_train=df_train.reset_index(drop=True)\ndf_valid=df_valid.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:38:55.839791Z","iopub.execute_input":"2023-08-07T19:38:55.842445Z","iopub.status.idle":"2023-08-07T19:38:55.860762Z","shell.execute_reply.started":"2023-08-07T19:38:55.842409Z","shell.execute_reply":"2023-08-07T19:38:55.859773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:39:22.390071Z","iopub.execute_input":"2023-08-07T19:39:22.390454Z","iopub.status.idle":"2023-08-07T19:39:22.397510Z","shell.execute_reply.started":"2023-08-07T19:39:22.390423Z","shell.execute_reply":"2023-08-07T19:39:22.396362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_valid.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:39:31.534482Z","iopub.execute_input":"2023-08-07T19:39:31.535084Z","iopub.status.idle":"2023-08-07T19:39:31.542033Z","shell.execute_reply.started":"2023-08-07T19:39:31.535050Z","shell.execute_reply":"2023-08-07T19:39:31.540970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path=\"/kaggle/input/cassava-leaf-disease-classification/train_images\"\n\ntrain_image_paths=[\n    os.path.join(image_path, x) for x in df_train.image_id.values\n]\nvalid_image_paths=[\n    os.path.join(image_path, x) for x in df_valid.image_id.values\n]","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:43:20.908256Z","iopub.execute_input":"2023-08-07T19:43:20.908640Z","iopub.status.idle":"2023-08-07T19:43:20.958496Z","shell.execute_reply.started":"2023-08-07T19:43:20.908609Z","shell.execute_reply":"2023-08-07T19:43:20.957608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_paths[:5]\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:43:47.856620Z","iopub.execute_input":"2023-08-07T19:43:47.856989Z","iopub.status.idle":"2023-08-07T19:43:47.863358Z","shell.execute_reply.started":"2023-08-07T19:43:47.856959Z","shell.execute_reply":"2023-08-07T19:43:47.862459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_targets=df_train.label.values\nvalid_targets=df_valid.label.values","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:52:01.486102Z","iopub.execute_input":"2023-08-07T19:52:01.487430Z","iopub.status.idle":"2023-08-07T19:52:01.492287Z","shell.execute_reply.started":"2023-08-07T19:52:01.487386Z","shell.execute_reply":"2023-08-07T19:52:01.491104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = ImageDataset(\n    image_paths=train_image_paths,\n    targets=train_targets,\n    augmentations=None,\n)\n\nvalid_dataset = ImageDataset(\n    image_paths=valid_image_paths,\n    targets=valid_targets,\n    augmentations=None,\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:56:05.174382Z","iopub.execute_input":"2023-08-07T19:56:05.174765Z","iopub.status.idle":"2023-08-07T19:56:05.182445Z","shell.execute_reply.started":"2023-08-07T19:56:05.174734Z","shell.execute_reply":"2023-08-07T19:56:05.181490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[100]","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:57:05.576518Z","iopub.execute_input":"2023-08-07T19:57:05.576937Z","iopub.status.idle":"2023-08-07T19:57:05.604415Z","shell.execute_reply.started":"2023-08-07T19:57:05.576903Z","shell.execute_reply":"2023-08-07T19:57:05.603347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef plot_image(img_dict):\n    image_tensor = img_dict[\"image\"]\n    target = img_dict[\"targets\"]\n    print(target)\n    plt.figure(figsize=(10, 10))\n    image = image_tensor.permute(1, 2, 0) / 255\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:57:29.753362Z","iopub.execute_input":"2023-08-07T19:57:29.753735Z","iopub.status.idle":"2023-08-07T19:57:29.759904Z","shell.execute_reply.started":"2023-08-07T19:57:29.753705Z","shell.execute_reply":"2023-08-07T19:57:29.758701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image(train_dataset[100])\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:58:01.770154Z","iopub.execute_input":"2023-08-07T19:58:01.770516Z","iopub.status.idle":"2023-08-07T19:58:02.473322Z","shell.execute_reply.started":"2023-08-07T19:58:01.770486Z","shell.execute_reply":"2023-08-07T19:58:02.472463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now, lets add some augmentations using one of the best\n# augmentations library: albumentations\n# Tez supports albumentations exclusively\n\ntrain_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        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        #)\n    ]\n)\n\n\n# now, we set resize to None as we are doing \n# resizing via augmentations\ntrain_dataset = ImageDataset(\n    image_paths=train_image_paths,\n    targets=train_targets,\n    augmentations=train_aug,\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:58:58.554489Z","iopub.execute_input":"2023-08-07T19:58:58.555056Z","iopub.status.idle":"2023-08-07T19:58:58.561533Z","shell.execute_reply.started":"2023-08-07T19:58:58.555020Z","shell.execute_reply":"2023-08-07T19:58:58.560602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image(train_dataset[100])\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:59:14.665054Z","iopub.execute_input":"2023-08-07T19:59:14.665436Z","iopub.status.idle":"2023-08-07T19:59:15.044890Z","shell.execute_reply.started":"2023-08-07T19:59:14.665406Z","shell.execute_reply":"2023-08-07T19:59:15.042353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's define a model now\n# We inherit from tez.Model instead of nn.Module\n# we have monitor_metrics if we want to monitor any metrics\n# except the loss\n# and we return 3 values in forward function.\n\nclass LeafModel(tez.Model):\n    def __init__(self, num_classes):\n        super().__init__()\n\n        self.convnet = torchvision.models.resnet18(pretrained=True)\n        self.convnet.fc = nn.Linear(512, num_classes)\n        self.step_scheduler_after = \"epoch\"\n        \n    def monitor_metrics(self, outputs, targets):\n        if targets is None:\n            return {}\n        outputs = torch.argmax(outputs, dim=1).cpu().detach().numpy()\n        targets = targets.cpu().detach().numpy()\n        accuracy = metrics.accuracy_score(targets, outputs)\n        return {\"accuracy\": accuracy}\n    \n    def fetch_optimizer(self):\n        opt = torch.optim.Adam(self.parameters(), lr=1e-3)\n        return opt\n    \n    def fetch_scheduler(self):\n        sch = torch.optim.lr_scheduler.StepLR(self.optimizer, step_size=0.7)\n        return sch\n\n    def forward(self, image, targets=None):\n        batch_size, _, _, _ = image.shape\n\n        outputs = self.convnet(image)\n        \n        if targets is not None:\n            loss = nn.CrossEntropyLoss()(outputs, targets)\n            metrics = self.monitor_metrics(outputs, targets)\n            return outputs, loss, metrics\n        return outputs, None, None","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:59:32.885599Z","iopub.execute_input":"2023-08-07T19:59:32.885987Z","iopub.status.idle":"2023-08-07T19:59:32.897453Z","shell.execute_reply.started":"2023-08-07T19:59:32.885956Z","shell.execute_reply":"2023-08-07T19:59:32.896449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LeafModel(num_classes=5)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:59:45.804642Z","iopub.execute_input":"2023-08-07T19:59:45.805020Z","iopub.status.idle":"2023-08-07T19:59:46.327297Z","shell.execute_reply.started":"2023-08-07T19:59:45.804991Z","shell.execute_reply":"2023-08-07T19:59:46.326364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimage = train_dataset[0][\"image\"].unsqueeze(0)\ntarget = train_dataset[0][\"targets\"].unsqueeze(0)\n\n\nmodel(image, target)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T19:59:54.447603Z","iopub.execute_input":"2023-08-07T19:59:54.448708Z","iopub.status.idle":"2023-08-07T19:59:54.671057Z","shell.execute_reply.started":"2023-08-07T19:59:54.448663Z","shell.execute_reply":"2023-08-07T19:59:54.669861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\ntrain_dataset = ImageDataset(\n    image_paths=train_image_paths,\n    targets=train_targets,\n    augmentations=train_aug,\n)\n\nvalid_dataset = ImageDataset(\n    image_paths=valid_image_paths,\n    targets=valid_targets,\n    augmentations=valid_aug,\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T20:00:19.578737Z","iopub.execute_input":"2023-08-07T20:00:19.579748Z","iopub.status.idle":"2023-08-07T20:00:19.591029Z","shell.execute_reply.started":"2023-08-07T20:00:19.579711Z","shell.execute_reply":"2023-08-07T20:00:19.588373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = EarlyStopping(\n    monitor=\"valid_accuracy\", model_path=\"model.bin\", patience=2, mode=\"max\"\n)\nmodel.fit(\n    train_dataset,\n    valid_dataset=valid_dataset,\n    train_bs=32,\n    valid_bs=64,\n    epochs=1\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.history.keys()","metadata":{},"execution_count":null,"outputs":[]}]}