{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import torch\nimport fastai\nfrom fastai.vision.all import *\nfrom fastai.callback import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(fastai.__version__)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reading train data\ndf = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# replacing the label values with names\nlabel = {0:'CBB', 1:'CBSD', 2:'CGM', 3:'CMD', 4:'Healthy'}\ndf['label'] = df.label.map(label)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# target label distribution\ndf.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndl = ImageDataLoaders.from_df(df, path = '../input/cassava-leaf-disease-classification', \n                              folder='train_images',\n                              #csv_fname='train_df.csv',delimeter=',',\n                               valid_pct=0.3,fn_col = 0,label_col=1,\n                               bs = 32,val_bs = 64,\n                               item_tfms = Resize(460), \n                               batch_tfms =aug_transforms(mult = 2,flip_vert = True,\n                                                          size = 224,max_zoom = 1.2,\n                                                         min_scale = 0.75))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dl.show_batch(max_n=12)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the  model\nmodel = cnn_learner(dls = dl, arch=resnet34, metrics = accuracy,\n                    path = './')\n                   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# find lr\nlr_min,lr_steep = model.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Minimum_lr :{lr_min:.2e},  Steepest_point: {lr_steep:.2e}')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model training\n\nmodel.fit_one_cycle(n_epoch=15, \n                    lr_max = 0.003)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Evaluation\ninterp = ClassificationInterpretation.from_learner(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_top_losses(k = 6)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_confusion_matrix(figsize = (8,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save('stage_1')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Fine Tuning"},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.callback.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# unfreeze the model\nmodel.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# find lr\nlr_min,lr_steep = model.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Minimum_lr :{lr_min:.2e},  Steepest_point: {lr_steep:.2e}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"callbacks = [ShowGraphCallback(),\n            EarlyStoppingCallback( monitor = 'accuracy', min_delta=0.001, patience=3)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit_one_cycle(n_epoch=10, lr_max=slice(1e-6, 1e-4),\n                   cbs = callbacks)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# evaluation\ninterp = ClassificationInterpretation.from_learner(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_confusion_matrix(figsize = (8,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save(\"stage_2\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load('stage_2')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.validate()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.export('./models/final_model.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = load_learner('./models/final_model.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# prediction\nimg = load_image('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img.resize((224,224))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label,index, prob =  model.predict('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Predicted label: {label} \\nProbability: {prob[index.item()]}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# creating submission\n#label = {0:'CBB', 1:'CBSD', 2:'CGM', 3:'CMD', 4:'Healthy'}\n\n# changing the label as seen from the prediction\nsubmission_df['label'] = 2\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# saving the submission\nsubmission_df.to_csv('./submission.csv', index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}