{"cells":[{"metadata":{},"cell_type":"markdown","source":"Add learn.export() to the end of your training kernal. This is necessary as this competition does not allow internet access. \nOnce exported download the exported file ending with .pkl and upload it as dataset to your inference kernal. \nYou can now use your trained model and weights with inference kernal."},{"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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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(\"/kaggle/input/export-fork1of4\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.vision import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import cohen_kappa_score\ndef quadratic_kappa(y_hat, y):\n    return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.callbacks import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_image_dir = os.path.join('../', 'input/aptos2019-blindness-detection')\ntrain_dir = os.path.join(base_image_dir,'train_images/')\ndf = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))\ndf['path'] = df['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))\ndf = df.drop(columns=['id_code'])\ndf = df.sample(frac=1).reset_index(drop=True) #shuffle dataframe\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tfms = get_transforms(do_flip=True,\n                      flip_vert=True,\n                      max_rotate=360,\n                      max_warp=0.,\n                      max_zoom=1.05,\n                      max_lighting=0.1,\n                      p_lighting=0.5\n                     )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bs = 32 \nsz=320\ntfms = get_transforms()\nsrc = (ImageList.from_df(df=df\n                         ,path=''\n                         ,cols='path'\n                        ) \n        .split_by_rand_pct(0.20) \n        .label_from_df(cols='diagnosis',label_cls=FloatList) \n      )\ndata= (src.transform(tfms,size=sz,resize_method=ResizeMethod.SQUISH,padding_mode='zeros') \n        .databunch(bs=bs,num_workers=4) \n        .normalize(imagenet_stats)      \n       )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn1 = load_learner('../input/pretrainblindness2/','final (11).pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn1.data = data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn2 = load_learner('../input/pretrainblindness1/','final (10).pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn2.data = data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn3 = load_learner('../input/exportdense/','final (8).pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn3.data = data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn4 = load_learner('../input/exportmodel/','final.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn4.data = data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp1 = ClassificationInterpretation.from_learner(learn1)\nlosses1,idxs1 = interp.top_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp2 = ClassificationInterpretation.from_learner(learn2)\nlosses2,idxs2 = interp.top_losses()\ninterp3 = ClassificationInterpretation.from_learner(learn3)\nlosses3,idxs3 = interp.top_losses()\ninterp4 = ClassificationInterpretation.from_learner(learn4)\nlosses4,idxs4 = interp.top_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_preds1 = learn1.get_preds(ds_type=DatasetType.Valid)\nvalid_preds2 = learn2.get_preds(ds_type=DatasetType.Valid)\nvalid_preds3 = learn3.get_preds(ds_type=DatasetType.Valid)\nvalid_preds4 = learn4.get_preds(ds_type=DatasetType.Valid)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport scipy as sp\nfrom functools import partial\nfrom sklearn import metrics\nfrom collections import Counter\nfrom fastai.callbacks import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class OptimizedRounder(object):\n    def __init__(self):\n        self.coef_ = 0\n\n    def _kappa_loss(self, coef, X, y):\n        X_p = np.copy(X)\n        for i, pred in enumerate(X_p):\n            if pred < coef[0]:\n                X_p[i] = 0\n            elif pred >= coef[0] and pred < coef[1]:\n                X_p[i] = 1\n            elif pred >= coef[1] and pred < coef[2]:\n                X_p[i] = 2\n            elif pred >= coef[2] and pred < coef[3]:\n                X_p[i] = 3\n            else:\n                X_p[i] = 4\n\n        ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')\n        return -ll\n\n    def fit(self, X, y):\n        loss_partial = partial(self._kappa_loss, X=X, y=y)\n        initial_coef = [0.5, 1.5, 2.5, 3.5]\n        self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')\n        print(-loss_partial(self.coef_['x']))\n\n    def predict(self, X, coef):\n        X_p = np.copy(X)\n        for i, pred in enumerate(X_p):\n            if pred < coef[0]:\n                X_p[i] = 0\n            elif pred >= coef[0] and pred < coef[1]:\n                X_p[i] = 1\n            elif pred >= coef[1] and pred < coef[2]:\n                X_p[i] = 2\n            elif pred >= coef[2] and pred < coef[3]:\n                X_p[i] = 3\n            else:\n                X_p[i] = 4\n        return X_p\n\n    def coefficients(self):\n        return self.coef_['x']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"optR1 = OptimizedRounder()\noptR1.fit(valid_preds1[0],valid_preds1[1])\noptR2 = OptimizedRounder()\noptR2.fit(valid_preds2[0],valid_preds2[1])\noptR3 = OptimizedRounder()\noptR3.fit(valid_preds3[0],valid_preds3[1])\noptR4 = OptimizedRounder()\noptR4.fit(valid_preds4[0],valid_preds4[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"coefficients1 = optR1.coefficients()\ncoefficients2 = optR2.coefficients()\ncoefficients3 = optR3.coefficients()\ncoefficients4 = optR4.coefficients()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(coefficients1)\nprint(coefficients2)\nprint(coefficients3)\nprint(coefficients4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df = pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')\nsample_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn1.data.add_test(ImageList.from_df(sample_df,'../input/aptos2019-blindness-detection',folder='test_images',suffix='.png'))\nlearn2.data.add_test(ImageList.from_df(sample_df,'../input/aptos2019-blindness-detection',folder='test_images',suffix='.png'))\nlearn3.data.add_test(ImageList.from_df(sample_df,'../input/aptos2019-blindness-detection',folder='test_images',suffix='.png'))\nlearn4.data.add_test(ImageList.from_df(sample_df,'../input/aptos2019-blindness-detection',folder='test_images',suffix='.png'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds1,_ = learn1.TTA(ds_type=DatasetType.Test)\npreds2,_ = learn2.TTA(ds_type=DatasetType.Test)\npreds3,_ = learn3.TTA(ds_type=DatasetType.Test)\npreds4,_ = learn4.TTA(ds_type=DatasetType.Test)\n\nlabelled_preds = []\npred11 = preds4 + preds1 + preds2 + preds3\nfor pred in pred11:\n    labelled_preds.append(int(np.argmax(pred))+1)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_predictions = optR1.predict(labelled_preds, coefficients1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df.diagnosis = test_predictions.astype(int)\nsample_df.groupby('diagnosis').count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]}],"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}