{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Importing necessary libraries, setting up document"},{"metadata":{},"cell_type":"markdown","source":"https://course.fast.ai/videos/?lesson=1"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\n# The notebook reloads automatically, have plots stored in document","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nfrom fastai.metrics import error_rate\n\nimport pandas as pd # Data processing\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\n# Import some libraries to use","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<font size=\"5\">**Preparing data for the model**"},{"metadata":{"trusted":true},"cell_type":"code","source":"data_folder = Path(\"../input/aptos2019-blindness-detection\")\ndata_folder.ls()\n# Allows for the manipulation of folders, see file structure used","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(data_folder/'train.csv')\ntest_df = pd.read_csv(data_folder/'test.csv')\n# Read data into the dataframes for training and testing, allows for easy manipulation of the data","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"https://blog.usejournal.com/finding-data-block-nirvana-a-journey-through-the-fastai-data-block-api-c38210537fe4\nhttps://docs.fast.ai/data_block.html"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data = ImageList.from_df(test_df, path=data_folder, folder='test_images', suffix='.png')\n# Object to represent test set, using data block API","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bs = 64\n#Set batch size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Data block API, prepares data for training, use in the model \ndata = (ImageList.from_df(train_df, path=data_folder, folder='train_images', suffix = '.png')\n                 .split_by_rand_pct(valid_pct=0.2) #split between training, validation sets\n                 .label_from_df()\n                 .add_test(test_data)              \n                 .transform(get_transforms(flip_vert=True), size=224)\n                 .databunch(path='.', bs=bs) #use bs from earlier\n                 .normalize(imagenet_stats)\n       )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(10,10))\n# See some of the images we are working with","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training, running the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create the learner for a convolutional neural network\nlearn = cnn_learner(data, models.resnet34, pretrained=False, model_dir = \"/output/kaggle/working\", metrics=error_rate)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"https://www.kaggle.com/tanlikesmath/oversampling-mnist-with-fastai\n\nhttps://docs.fast.ai/callbacks.lr_finder.html#LRFinder"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Find optimal learning rate to use for the model\nlearn.lr_find()\nlearn.recorder.plot(suggestion=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"min_grad_lr = learn.recorder.min_grad_lr\nmin_grad_lr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(10, min_grad_lr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Retrain model and fine-tune the learning rate\nlearn.save('initial') # Set of weights created with learn, saves the model for possible future use/access\nlearn.unfreeze()\nlearn.lr_find()\nlearn.recorder.plot(suggestion=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fine-tuning, new learning rate\nmin_grad_lr2 = learn.recorder.min_grad_lr\nmin_grad_lr2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Results"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(10, min_grad_lr2)\nlearn.save('final')","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}