{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#################################################################\n#  Importing the libraries \n#################################################################\nfrom fastai import * \nfrom fastai.vision import * \nfrom fastai.callbacks import *\n\nfrom sklearn.metrics import roc_auc_score,f1_score\nimport cv2 as cv\nimport numpy as np\nimport pandas as pd \nimport os\n\nimport matplotlib.pyplot as plt\n%matplotlib inline ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  adjusting the path and displaying the files \n#################################################################\n\nprint(os.listdir((\"../input/aptos2019-blindness-detection\")))\nkaggle_path = '../input/aptos2019-blindness-detection'\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  Reading the csv files  \n#################################################################\nlabels = pd.read_csv(kaggle_path+'/train.csv')\ntest_labels = pd.read_csv(kaggle_path+'/sample_submission.csv')\ntest = ImageList.from_df(test_labels, path = kaggle_path+'/test_images', suffix = '.png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  For Debugging and understanding the data \n#################################################################\nlabels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  Transforming parameters for augmentation \n#################################################################\ntfms = get_transforms(\n    do_flip=True,\n    flip_vert=False,\n    max_warp=0.15,\n    max_rotate=360.,\n    max_zoom=1.1,\n    max_lighting=0.1,\n    p_lighting=0.5\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  For Debugging and understanding the data \n#################################################################\nlabels['diagnosis'].value_counts().plot(kind = 'bar', title='Distribution of diagnosis categories')\nplt.show()\nlabels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  For Debugging and understanding the data \n#################################################################\nimg = open_image(kaggle_path+'/train_images/002c21358ce6.png')\nimg.show(figsize = (5,5))\nprint(img.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  loading arranging and splitting the data  \n#################################################################\nsrc = (ImageList.from_df(labels, path = kaggle_path+'/train_images', suffix = '.png')\n       .split_by_rand_pct(0.2) # slpliting 20 -80\n       .label_from_df(cols = 'diagnosis')\n       .add_test(test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  adjusting data, image size , batch sizes and normalization  \n#################################################################\ndata = (\n    src.transform(\n        tfms,\n        size = 224, \n        resize_method=ResizeMethod.SQUISH,\n        padding_mode='zeros'\n    )\n    .databunch(bs=32)\n    .normalize(imagenet_stats))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  For Debugging and understanding the data \n#################################################################\nprint(data.classes)\ndata.show_batch(rows=3, figsize=(10,6), hide_axis=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  Kappa score matrix  \n#################################################################\nkappa = KappaScore()\nkappa.weights = \"quadratic\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  making CNN resnet 34  \n#################################################################\nlearn = cnn_learner(\n    data, \n    models.resnet34, \n    metrics = [accuracy, kappa], \n    model_dir = Path(kaggle_path+'/working'),\n    path = Path(\".\")\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  For learning  \n#################################################################\nlearn.fit_one_cycle(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  For readjusting the weights if needed\n#################################################################\n# MODEL_PATH = str(arch).split()[1]\nlearn.model_dir='/kaggle/working/'\n\n# learner.save(MODEL_PATH + '_stage1')\nlearn.save('model-34')\n\n# learn.unfreeze()\n# learn.lr_find()\n# learn.recorder.plot(suggestion=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"######################################################################\n#  Finding the best results base upon kappa and saving the best model\n######################################################################\nlearn.model_dir='/kaggle/working/'\nlr = slice(1e-4,1e-3)\nlearn.fit_one_cycle(5,lr, callbacks=[SaveModelCallback(learn, every='imrpovement', monitor='kappa_score', name='bestmodel')])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  For submitting the result\n#################################################################\n\nlearn.load('bestmodel')\npreds, _ = learn.get_preds(ds_type=DatasetType.Test)\n\npreds = np.array(preds.argmax(1)).astype(int).tolist()\nsubmission = pd.read_csv(kaggle_path+'/sample_submission.csv')\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  Saving our results on the submission file \n#################################################################\nsubmission['diagnosis'] = preds\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  Generating the output file \n#################################################################\n# submission.to_csv('submission.csv', index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################################################\n#  For debugging \n#################################################################\ninterp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","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":1}