{"cells":[{"metadata":{},"cell_type":"markdown","source":"Training with image of height and width of 800 saw a good increase in performance!"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n%matplotlib inline\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\nfrom fastai.metrics import accuracy, KappaScore\nfrom fastai.vision import *\nfrom fastai import *\nfrom fastai.callbacks import *\nfrom skimage import io\nfrom fastai.vision.image import *\nimport cv2\nfrom PIL import Image\nfrom imblearn import over_sampling\nimport shutil\n\n\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Dataset contains one train CSV file with ID and class"},{"metadata":{},"cell_type":"markdown","source":"# Lets see what we're working with"},{"metadata":{"trusted":true},"cell_type":"code","source":"data_dir = '../input/aptos2019-blindness-detection'\ntrain_df = pd.read_csv(os.path.join(data_dir,'train.csv'))\nprint('Train df: ')\nprint(train_df.head(4))\n\nadd_extension = lambda x: str(x) + '.png'\nadd_dir = lambda x: os.path.join('train_images', x)\n\ntrain_df['id_code'] = train_df['id_code'].apply(add_extension)\ntrain_df['id_code'] = train_df['id_code'].apply(add_dir)\n\nfname = train_df['id_code'].iloc[:4]\n\ndata_dir = Path(data_dir)\ntrain_dir = data_dir/'train_images'\nim1 = io.imread(str(data_dir/fname[0]))\nim2 = io.imread(str(data_dir/fname[1]))\nim3 = io.imread(str(data_dir/fname[2]))\nim4 = io.imread(str(data_dir/fname[3]))\n\nplt.subplot(2,2,1)\nplt.imshow(im1)\nplt.subplot(2,2,2)\nplt.imshow(im2)\nplt.subplot(2,2,3)\nplt.imshow(im3)\nplt.subplot(2,2,4)\nplt.imshow(im4)\nplt.show()\n\nprint(str(len(train_df)) + ' number of samples')\nval_counts = train_df['diagnosis'].value_counts()\nprint('Distribution of classes',val_counts)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('modified train df: ')\ntrain_df.head(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#def _preprocess(im):\n#    \"Flip `x` horizontally.\"\n    \n#    im = cv2.addWeighted (np.array(x),4, cv2.GaussianBlur(np.array(x) , (0,0) , 800/10) ,-4 ,128)\n#    #im = np.array(im) * 100\n#    return im\n\n#preprocess = TfmPixel(_preprocess)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dict_rs={0: 1805, 2: 1200, 1: 600, 3: 500, 4: 550}\nros = over_sampling.RandomOverSampler(dict_rs,random_state=42)\n\nX_res, y_res = ros.fit_resample(train_df['id_code'].values.reshape(-1,1), train_df['diagnosis'].values)\n\ndf_new = pd.DataFrame(columns=['id_code', 'diagnosis'])\ndf_new['diagnosis'] = y_res\ndf_new['id_code'] = X_res\ntrain_df = df_new.copy()\ntrain_df.diagnosis.value_counts() \n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":" # Creating databunch "},{"metadata":{"trusted":true},"cell_type":"code","source":"# data augmentation \ntfms = get_transforms(max_rotate=80, flip_vert=True)\nbs = 8\nPATH = Path('../input/aptos2019-blindness-detection')\n\ndata = (\n    ImageList.from_df(train_df,PATH)\n        .split_by_rand_pct(0.1, seed=42)\n        .label_from_df()\n        .transform(tfms,size=800)\n        .databunch(bs=8)\n        .normalize(imagenet_stats)\n    \n    )\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.classes ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train "},{"metadata":{"trusted":true},"cell_type":"code","source":"cwd = os.getcwd()\n# creating learner with resent architecture \n\nkappa = KappaScore()\nkappa.weights = \"quadratic\"\n\nPath('/tmp/.cache/torch/checkpoints/').mkdir(exist_ok=True, parents=True)\n!cp '../input/resnet34/resnet34.pth' '/tmp/.cache/torch/checkpoints/resnet34-333f7ec4.pth'\n\nlearn = cnn_learner(data, models.resnet34, metrics=[accuracy, kappa], pretrained=True, \n                    callback_fns=[partial(CSVLogger, append=True)], path='../tmp/model/')\n\nlearn.fit_one_cycle(5, slice(0.01))\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()\nlearn.recorder.plot()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(7, slice(1e-6, 0.01/6))\n\nlearn.save('model1')\n\nshutil.copy('../tmp/model/model1.pth', os.getcwd())\n#learn.fit_one_cycle(5, max_lr = 1.5e-6)\n\n#learn.freeze()\n#learn.fit_one_cycle(2, max_lr = 1.5e-6)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# prediction on test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df = pd.read_csv(PATH/'sample_submission.csv')\nlearn.data.add_test(ImageList.from_df(sample_df,PATH,folder='test_images',suffix='.png'))\n\npreds,y = learn.get_preds(DatasetType.Test)\n\nsample_df.diagnosis = preds.argmax(1)\nsample_df.head()\n\nsample_df.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# There we have it:\n\nThe fastai workflow, from data loading to prediction, was demstrated so you can go ahead and modify parts to quickly experiment with different techniques.\n\nHappy learning! \n"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}