{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Now let's get serious \n\nIn this version of the notebook I will try out: \n* using a larger image size (in the DataLoader) \n* flip_vert in the data transforms \n* Mixup\n* Early stopping"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Imports and loading data "},{"metadata":{"trusted":true},"cell_type":"code","source":"# importing Pytorch and fastai libraries\nimport torch\nfrom fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('/kaggle/input/cassava-leaf-disease-classification')\ntrain_df = pd.read_csv(path/'train.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Remove some duplicate image \ntrain_df = train_df[~train_df['image_id'].isin(['1562043567.jpg', '3551135685.jpg', '2252529694.jpg'])]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# A wee bit more of EDA "},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check the distribution of the different classes\ntrain_df['label'].hist(figsize=(10,5)); ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**These are the labels**\n* 0 = Cassava Bacterial Blight (CBB)\n* 1 = Cassava Brown Streak Disease (CBSD)\n* 2 = Cassava Green Mottle (CGM)\n* 3 = Cassava Mosaic Disease (CMD)\n* 4 = Healthy\n\n\nThe most common label is 3 - Cassava Mosaic Disease. Take note of the imbalance for now. May address it later if it causes a problem (although knowing what that problem potentiall could be would be helpful). "},{"metadata":{},"cell_type":"markdown","source":"See how many samples we have: "},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Shape of df: {train_df.shape}')\nprint(f'Distribution of labels: \\n {train_df.label.value_counts()}')\nprint(f'Percentages of classes: \\n {train_df.label.value_counts(1)}')\nprint(f'There are {len(train_df)} samples.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Class 3\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Choose a small sample of images to experiment on \n#sample_df = train_df[:3000]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check distribution for sample_df to see it roughly looks like train_df (our full dataset)\n#sample_df['label'].hist(figsize=(10,6));","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#print(f'Shape of df: {sample_df.shape}')\n#print(f'Distribution of classes: \\n {sample_df.label.value_counts()}')\n#print(f'Percentages of classes: \\n {sample_df.label.value_counts(1)}')\n#print(f'There are {len(sample_df)} samples.')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The distribution of the classes is similar, so we will go ahead and create a baseline model with our sample of the first 3000 images. "},{"metadata":{},"cell_type":"markdown","source":"### Define a function to get x (images/ independent variables) and y (labels/dependent variables)"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Use train images to create x and ['label'] from the train_df to create y\n\ntrain_path = '/kaggle/input/cassava-leaf-disease-classification/train_images/'\n# r refers to the train_df\ndef get_x(r): \n    return train_path + r['image_id']\n\ndef get_y(r): \n    return r['label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Set up datablock\ndblock = DataBlock(blocks = (ImageBlock, CategoryBlock),\n                   splitter=RandomSplitter(seed=42),\n                   get_x = get_x, \n                   get_y = get_y, \n                   item_tfms=Resize(460), # increasing size for better accuracy\n                   batch_tfms=[*aug_transforms(size=224, min_scale=0.75, flip_vert=True), Normalize.from_stats(*imagenet_stats) ]\n                  )\ndsets = dblock.datasets(train_df)\ndsets.train[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = dblock.dataloaders(train_df, bs=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch(nrows=4, ncols=4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Loading the pretrained weights for use \nif not os.path.exists('/root/.cache/torch/hub/checkpoints/'):\n        os.makedirs('/root/.cache/torch/hub/checkpoints/')\n!cp '../input/pretrained-model-weights-pytorch/resnet50-19c8e357.pth' '/root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# cnn_learner, pretrained model\nlearn = cnn_learner(dls, resnet50, cbs=MixUp, metrics=accuracy).to_native_fp16()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fine_tune(30, freeze_epochs=8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.recorder.plot_loss()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Export model so we can use it to make predictions \nlearn.export()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# need to make predictions on the sample test image before submitting baseline to the competition\n# Format submission df in same way as training df\nsample_sub = pd.read_csv(path/'sample_submission.csv')\nsample_sub","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_copy = sample_sub.copy()\nsub_copy['image_id'] = sub_copy['image_id'].apply(lambda x: f'../test_images/{x}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_copy","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the model so we can use it to make predictions\nlearn = learn.to_native_fp32()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load test set into dataloaders \ntest_dl = learn.dls.test_dl(sub_copy)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# make sure our test set looks like we expect \ntest_dl.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# make predictions!\n# test-time augmentation to hopefully improve accuracy\npreds, _ = learn.tta(dl=test_dl) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Put the predictions into a csv so we can submit it\nsample_sub['label'] = preds.argmax(dim=-1).numpy()\nsample_sub.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}