{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Introduction:\nIn this notebook we intend to use Fastai, for the purpose of image classification, our score in the competiton was ~75 %\nlet's see if we can improve this score."},{"metadata":{"trusted":true},"cell_type":"code","source":"# do this to make sure no error creeps in\n#! [ -e /content ] && pip install -Uqq fastai \n\n# basic fastai import \nfrom fastai.basics import *\nfrom fastai.vision.all import *\nfrom fastai.callback.all import *\n\n#rest of the imports\nimport os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# loading data \npath = Path('../input/cassava-leaf-disease-classification')\ntrainFrame = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now the very first thing that we have to do is to add the path to every image name in the dataFrame"},{"metadata":{"trusted":true},"cell_type":"code","source":"trainFrame['path'] = trainFrame['image_id'].map(lambda x:path/'train_images'/x)\ntrainFrame = trainFrame.sample(frac=1).reset_index(drop=True)\ntrainFrame = trainFrame.drop(columns=['image_id'])\n\ntrainFrame[0:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# with this done let load our data\nitem_tfms = RandomResizedCrop(460)\nbatch_tfms = [*aug_transforms(size = 224, max_warp =0), Normalize.from_stats(*imagenet_stats)]\n\ndls = ImageDataLoaders.from_df(trainFrame, valid_pct=0.2, seed = 42, label_col = 0,\n                              fn_col = 1, bs = 32, item_tfms = item_tfms,\n                              batch_tfms = batch_tfms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"is_pretrained = True\n\nif is_pretrained == False:\n    # let's define and train our model\n    learn = cnn_learner(dls, models.resnet101, pretrained = True, metrics = [error_rate, accuracy])\n    learn = learn.load('/kaggle/input/cassavafastaimodel-test/cassava_pred')\n    # lets train our leaner for 5 cycles\n    learn.fit_one_cycle(3)\n    learn.save('cassava_pred')\nelse:\n    learn = load_learner('/kaggle/input/cassavafastaimodel-test/cassava.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sampleData = pd.read_csv(path/'sample_submission.csv')\ncopyFrame = sampleData.copy()\ncopyFrame['path'] = copyFrame['image_id'].map(lambda x:path/'test_images'/x)\ncopyFrame = copyFrame.drop(columns=['image_id'])\n\ntest_dl = dls.test_dl(copyFrame)\npreds, _ = learn.tta(dl=test_dl, n=15, beta=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sampleData['label'] = preds.argmax(dim=-1).numpy()\nsampleData.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sampleData","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}