{"cells":[{"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\n\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\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":"import numpy as np\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.mkdir(\"../train_images\")\nos.mkdir(\"../test_images\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# for x in os.listdir(\"../input/aptos2019-blindness-detection/train_images/\"):\n\n#     img = cv2.imread(\"../input/aptos2019-blindness-detection/train_images/\"+x,0)\n#     clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))\n#     cl1 = clahe.apply(img)\n#     cv2.imwrite(\"../train_images/\"+x,cl1)\n    \n# for x in os.listdir(\"../input/aptos2019-blindness-detection/test_images/\"):\n\n#     img = cv2.imread(\"../input/aptos2019-blindness-detection/test_images/\"+x,0)\n#     clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))\n#     cl1 = clahe.apply(img)\n#     cv2.imwrite(\"../test_images/\"+x,cl1)\n\nfor x in os.listdir(\"../input/aptos2019-blindness-detection/train_images/\"):\n    img = cv2.imread(\"../input/aptos2019-blindness-detection/train_images/\"+x,0)\n    equ = cv2.equalizeHist(img)\n    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))\n    cl1 = clahe.apply(equ)\n    cv2.imwrite(\"../train_images/\"+x,cl1)\n    \nfor x in os.listdir(\"../input/aptos2019-blindness-detection/test_images/\"):\n    img = cv2.imread(\"../input/aptos2019-blindness-detection/test_images/\"+x,0)\n    equ = cv2.equalizeHist(img)\n    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))\n    cl1 = clahe.apply(equ)\n    cv2.imwrite(\"../test_images/\"+x,cl1)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nfrom fastai.metrics import accuracy, error_rate","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")\ntest_img = ImageList.from_df(test_df, path=\"..\", folder='/test_images',suffix='.png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\ntfms = get_transforms(do_flip=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(5)\ndata = (ImageList.from_df(train_df,path=\"..\",folder=\"/train_images\",suffix='.png')\n        .split_by_rand_pct()\n        .label_from_df(cols='diagnosis')\n        .add_test(test_img)\n        .transform(tfms,size = 256)\n        .databunch(bs=64)    \n        .normalize(imagenet_stats)\n       )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(rows=3,figsize = (5,5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.valid_ds.classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Path('/tmp/.cache/torch/checkpoints/').mkdir(exist_ok=True, parents=True)\n!cp ../input/resnet50/resnet50.pth /tmp/.cache/torch/checkpoints/resnet50.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mv  /tmp/.cache/torch/checkpoints/resnet50.pth /tmp/.cache/torch/checkpoints/resnet50-19c8e357.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = cnn_learner(data,models.resnet50, metrics = [accuracy,error_rate],callback_fns=ShowGraph)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.model_dir = '../kaggle/working/models/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model.lr_find()\n# model.recorder.plot(suggestion = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit_one_cycle(5,1e-2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.unfreeze()\n# model.lr_find()\n# model.recorder.plot(suggestion = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit_one_cycle(10,max_lr = slice(1e-6,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interpreter = ClassificationInterpretation.from_learner(model)\ninterpreter.plot_confusion_matrix()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds, _ = model.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df[\"diagnosis\"] = preds.argmax(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_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}