{"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":"markdown","source":"## A look at the data","metadata":{}},{"cell_type":"markdown","source":"Let's start out by setting up our environment by importing the required modules and setting a random seed:","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport os\nimport pandas as pd\nfrom fastai.vision.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-21T20:25:00.622607Z","iopub.execute_input":"2021-05-21T20:25:00.622962Z","iopub.status.idle":"2021-05-21T20:25:02.996323Z","shell.execute_reply.started":"2021-05-21T20:25:00.622914Z","shell.execute_reply":"2021-05-21T20:25:02.995536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set_seed(999)","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:03.000576Z","iopub.execute_input":"2021-05-21T20:25:03.000832Z","iopub.status.idle":"2021-05-21T20:25:03.006795Z","shell.execute_reply.started":"2021-05-21T20:25:03.000806Z","shell.execute_reply":"2021-05-21T20:25:03.005858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_path = Path('../input/cassava-leaf-disease-classification')\nos.listdir(dataset_path)","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-05-21T20:25:03.010172Z","iopub.execute_input":"2021-05-21T20:25:03.010414Z","iopub.status.idle":"2021-05-21T20:25:03.021861Z","shell.execute_reply.started":"2021-05-21T20:25:03.010390Z","shell.execute_reply":"2021-05-21T20:25:03.021057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(dataset_path/'train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:03.023341Z","iopub.execute_input":"2021-05-21T20:25:03.023699Z","iopub.status.idle":"2021-05-21T20:25:03.048865Z","shell.execute_reply.started":"2021-05-21T20:25:03.023661Z","shell.execute_reply":"2021-05-21T20:25:03.048221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:03.052810Z","iopub.execute_input":"2021-05-21T20:25:03.053056Z","iopub.status.idle":"2021-05-21T20:25:03.069925Z","shell.execute_reply.started":"2021-05-21T20:25:03.053033Z","shell.execute_reply":"2021-05-21T20:25:03.068967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['path'] = train_df['image_id'].map(lambda x:dataset_path/'train_images'/x)\ntrain_df = train_df.drop(columns=['image_id'])\ntrain_df = train_df.sample(frac=1).reset_index(drop=True) #shuffle dataframe\ntrain_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:03.071366Z","iopub.execute_input":"2021-05-21T20:25:03.071758Z","iopub.status.idle":"2021-05-21T20:25:03.305549Z","shell.execute_reply.started":"2021-05-21T20:25:03.071709Z","shell.execute_reply":"2021-05-21T20:25:03.304507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len_df = len(train_df)\nprint(f\"There are {len_df} images\")\n","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:03.306922Z","iopub.execute_input":"2021-05-21T20:25:03.307293Z","iopub.status.idle":"2021-05-21T20:25:03.312460Z","shell.execute_reply.started":"2021-05-21T20:25:03.307255Z","shell.execute_reply":"2021-05-21T20:25:03.311369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Categories\n\n0. Cassava Bacterial Blight (CBB)\n1. Cassava Brown Streak Disease (CBSD)\n2. Cassava Green Mottle (CGM)\n3. Cassava Mosaic Disease (CMD)\n4. Healthy","metadata":{}},{"cell_type":"code","source":"#create different dataframes with disease and healthy binary clasification\n\nlabel0_df = train_df[train_df['label'] == 0]\nlabel1_df = train_df[train_df['label'] == 1]\nlabel2_df = train_df[train_df['label'] == 2]\nlabel3_df = train_df[train_df['label'] == 3]\nlabel4_df = train_df[train_df['label'] == 4]\n\nresult = pd.concat([label0_df,label4_df])\ntrain_df = result.sample(frac=1).reset_index(drop=True) #shuffle dataframe\n","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:03.314168Z","iopub.execute_input":"2021-05-21T20:25:03.314634Z","iopub.status.idle":"2021-05-21T20:25:03.332507Z","shell.execute_reply.started":"2021-05-21T20:25:03.314598Z","shell.execute_reply":"2021-05-21T20:25:03.331652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['label'].hist(figsize = (10, 5))\n","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:22.865868Z","iopub.execute_input":"2021-05-21T20:25:22.866208Z","iopub.status.idle":"2021-05-21T20:25:23.045462Z","shell.execute_reply.started":"2021-05-21T20:25:22.866176Z","shell.execute_reply":"2021-05-21T20:25:23.044584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Disease 0\n* The item transforms performs a large crop on each of the images.\n* The batch transforms performs random resized crop to 224 and also apply other standard augmentations (in `aug_tranforms`) at the batch level on the GPU.\n* The batch size is set to 256 here.\n","metadata":{}},{"cell_type":"code","source":"item_tfms = RandomResizedCrop(460, min_scale=0.75, ratio=(1.,1.))\nbatch_tfms = [*aug_transforms(size=224, max_warp=0), Normalize.from_stats(*imagenet_stats)]\nbs=128","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:29.440026Z","iopub.execute_input":"2021-05-21T20:25:29.440354Z","iopub.status.idle":"2021-05-21T20:25:33.152213Z","shell.execute_reply.started":"2021-05-21T20:25:29.440323Z","shell.execute_reply":"2021-05-21T20:25:33.151271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(train_df, #pass in train DataFrame\n                               valid_pct=0.2, #80-20 train-validation random split\n                               seed=999, #seed\n                               label_col=0, #label is in the first column of the DataFrame\n                               fn_col=1, #filename/path is in the second column of the DataFrame\n                               bs=bs, #pass in batch size\n                               item_tfms=item_tfms, #pass in item_tfms\n                               batch_tfms=batch_tfms) #pass in batch_tfms","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:33.153790Z","iopub.execute_input":"2021-05-21T20:25:33.154127Z","iopub.status.idle":"2021-05-21T20:25:34.326761Z","shell.execute_reply.started":"2021-05-21T20:25:33.154091Z","shell.execute_reply":"2021-05-21T20:25:34.325846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:36.504485Z","iopub.execute_input":"2021-05-21T20:25:36.504812Z","iopub.status.idle":"2021-05-21T20:25:39.342886Z","shell.execute_reply.started":"2021-05-21T20:25:36.504778Z","shell.execute_reply":"2021-05-21T20:25:39.341862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model training:","metadata":{}},{"cell_type":"code","source":"# Making pretrained weights work without needing to find the default filename\nif not os.path.exists('/root/.cache/torch/hub/checkpoints/'):\n        os.makedirs('/root/.cache/torch/hub/checkpoints/')\n!cp '../input/resnet50/resnet50.pth' '/root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth'","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:43.048170Z","iopub.execute_input":"2021-05-21T20:25:43.048502Z","iopub.status.idle":"2021-05-21T20:25:44.895312Z","shell.execute_reply.started":"2021-05-21T20:25:43.048471Z","shell.execute_reply":"2021-05-21T20:25:44.894359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, \n                    resnet50,\n                    metrics = [accuracy]).to_native_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:25:59.297417Z","iopub.execute_input":"2021-05-21T20:25:59.297768Z","iopub.status.idle":"2021-05-21T20:26:00.138620Z","shell.execute_reply.started":"2021-05-21T20:25:59.297727Z","shell.execute_reply":"2021-05-21T20:26:00.137876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:07:06.103180Z","iopub.execute_input":"2021-05-21T20:07:06.103497Z","iopub.status.idle":"2021-05-21T20:12:36.458424Z","shell.execute_reply.started":"2021-05-21T20:07:06.103466Z","shell.execute_reply":"2021-05-21T20:12:36.456501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(5,base_lr=1e-2)","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:26:15.489851Z","iopub.execute_input":"2021-05-21T20:26:15.490235Z","iopub.status.idle":"2021-05-21T20:31:09.841491Z","shell.execute_reply.started":"2021-05-21T20:26:15.490202Z","shell.execute_reply":"2021-05-21T20:31:09.840635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.loss_func","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:32:07.069548Z","iopub.execute_input":"2021-05-21T20:32:07.069900Z","iopub.status.idle":"2021-05-21T20:32:07.075855Z","shell.execute_reply.started":"2021-05-21T20:32:07.069865Z","shell.execute_reply":"2021-05-21T20:32:07.074956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Disease 1","metadata":{}},{"cell_type":"code","source":"result = pd.concat([label1_df,label4_df])\ntrain_df = result.sample(frac=1).reset_index(drop=True) #shuffle dataframe","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:34:39.840109Z","iopub.execute_input":"2021-05-21T20:34:39.840545Z","iopub.status.idle":"2021-05-21T20:34:39.860394Z","shell.execute_reply.started":"2021-05-21T20:34:39.840508Z","shell.execute_reply":"2021-05-21T20:34:39.859415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(train_df, #pass in train DataFrame\n                               valid_pct=0.2, #80-20 train-validation random split\n                               seed=999, #seed\n                               label_col=0, #label is in the first column of the DataFrame\n                               fn_col=1, #filename/path is in the second column of the DataFrame\n                               bs=bs, #pass in batch size\n                               item_tfms=item_tfms, #pass in item_tfms\n                               batch_tfms=batch_tfms) #pass in batch_tfms","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:34:42.175649Z","iopub.execute_input":"2021-05-21T20:34:42.175986Z","iopub.status.idle":"2021-05-21T20:34:42.751109Z","shell.execute_reply.started":"2021-05-21T20:34:42.175934Z","shell.execute_reply":"2021-05-21T20:34:42.750323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:34:45.046694Z","iopub.execute_input":"2021-05-21T20:34:45.047051Z","iopub.status.idle":"2021-05-21T20:34:47.884802Z","shell.execute_reply.started":"2021-05-21T20:34:45.047014Z","shell.execute_reply":"2021-05-21T20:34:47.884016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, \n                    resnet50, \n                    metrics = [accuracy]).to_native_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:34:52.091035Z","iopub.execute_input":"2021-05-21T20:34:52.091385Z","iopub.status.idle":"2021-05-21T20:34:52.808673Z","shell.execute_reply.started":"2021-05-21T20:34:52.091352Z","shell.execute_reply":"2021-05-21T20:34:52.807935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:34:56.425418Z","iopub.execute_input":"2021-05-21T20:34:56.425742Z","iopub.status.idle":"2021-05-21T20:37:37.834194Z","shell.execute_reply.started":"2021-05-21T20:34:56.425710Z","shell.execute_reply":"2021-05-21T20:37:37.833297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(5,base_lr=1e-2)","metadata":{"execution":{"iopub.status.busy":"2021-05-21T20:37:49.515814Z","iopub.execute_input":"2021-05-21T20:37:49.516165Z","iopub.status.idle":"2021-05-21T20:44:09.308550Z","shell.execute_reply.started":"2021-05-21T20:37:49.516130Z","shell.execute_reply":"2021-05-21T20:44:09.307805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Disease 2","metadata":{}},{"cell_type":"code","source":"result = pd.concat([label2_df,label4_df])\ntrain_df = result.sample(frac=1).reset_index(drop=True) #shuffle dataframe","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(train_df, #pass in train DataFrame\n                               valid_pct=0.2, #80-20 train-validation random split\n                               seed=999, #seed\n                               label_col=0, #label is in the first column of the DataFrame\n                               fn_col=1, #filename/path is in the second column of the DataFrame\n                               bs=bs, #pass in batch size\n                               item_tfms=item_tfms, #pass in item_tfms\n                               batch_tfms=batch_tfms) #pass in batch_tfms","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, \n                    resnet50, \n                    metrics = [accuracy]).to_native_fp16()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(5,base_lr=1e-2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Disease 3","metadata":{}},{"cell_type":"code","source":"result = pd.concat([label3_df,label4_df])\ntrain_df = result.sample(frac=1).reset_index(drop=True) #shuffle dataframe","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(train_df, #pass in train DataFrame\n                               valid_pct=0.2, #80-20 train-validation random split\n                               seed=999, #seed\n                               label_col=0, #label is in the first column of the DataFrame\n                               fn_col=1, #filename/path is in the second column of the DataFrame\n                               bs=bs, #pass in batch size\n                               item_tfms=item_tfms, #pass in item_tfms\n                               batch_tfms=batch_tfms) #pass in batch_tfms","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, \n                    resnet50, \n                    metrics = [accuracy]).to_native_fp16()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(5,base_lr=1e-2)","metadata":{},"execution_count":null,"outputs":[]}]}