{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Attempting the Cassava Leaf Disease Prediction with FastAI using Resnet34 Model."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom fastai.vision.all import *\nimport fastai as fa\nimport os\nfrom fastai import losses","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fa.__version__","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Loading the dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/cassava-leaf-disease-classification')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(path/'train.csv')\ntrain_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Removing the Mislabeled and Duplicates as per a discussion\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = train_df[~train_df['image_id'].isin(['1562043567.jpg', '3551135685.jpg', '2252529694.jpg'])]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Labeling "},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_x(r):\n    return path/'train_images'/r['image_id']\n\ndef get_y(r):\n    return r['label']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Generators with Augmentations "},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_data(size=224,bs=128,data_df=train_df):\n    dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                       splitter=RandomSplitter(seed=42),\n                       get_x=get_x, \n                       get_y=get_y,\n                       item_tfms = RandomResizedCrop(460),\n                       batch_tfms = [*aug_transforms(size=size),Normalize.from_stats(*imagenet_stats)]\n                      )\n    return dblock.dataloaders(data_df,bs=bs)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## View of a batch of images "},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = get_data()\ndls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Moving resnet34 pretrained model to checkpoints "},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists('/root/.cache/torch/hub/checkpoints/'):\n        os.makedirs('/root/.cache/torch/hub/checkpoints/')\n!cp '../input/resnet34/resnet34.pth' '/root/.cache/torch/hub/checkpoints/resnet34-333f7ec4.pth'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Using LabelSmoothingCE & MixUp Augmentations in training loop"},{"metadata":{"trusted":true},"cell_type":"code","source":"loss_func = losses.LabelSmoothingCrossEntropy()\ncbs = MixUp","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, loss_func=loss_func, metrics=accuracy, cbs=cbs).to_native_fp16()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Smart LRFind to predict perfect LR to start"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Fine Tuning with EarlyStopping"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fine_tune(50, freeze_epochs=8, cbs=EarlyStoppingCallback(monitor='valid_loss', min_delta=0.003, patience=3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = learn.to_native_fp32()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv(path/'sample_submission.csv')\nlen(submission_df)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Remapping the labels for test and Making Predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data_path = submission_df['image_id'].apply(lambda x: path/'test_images'/x)\ntst_dl = learn.dls.test_dl(test_data_path)\npredictions = learn.tta(dl = tst_dl, n=10)\n\nsubmission_df['label'] = np.argmax(predictions[0],axis=1)\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Preparing Submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.to_csv('submission.csv',index=False)","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}