{"cells":[{"metadata":{"_uuid":"67b1eb93d654f93e44d8f3db5482e0df0bff824f"},"cell_type":"markdown","source":"## Dog Breed classification with fast ai\n\nThe fastai library simplifies training fast and accurate neural nets using modern best practices. This library is built on top of Pytorch. You can more about fastai by visiting this [link](https://docs.fast.ai/index.html).  \n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom fastai.vision import *\nfrom fastai.metrics import error_rate, accuracy\n\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"627f59e6afdcd7a46421abe1bb62d886580715be"},"cell_type":"code","source":"PATH = \"../input/\"\nMODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3a7c648270c6671d81ae18e7b0ad1e4eb1f77b57"},"cell_type":"code","source":"df = pd.read_csv('../input/labels.csv')\ndf.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3924248a22ae70d4c56b164cfff5d3dfacf5bfb2"},"cell_type":"code","source":"print(df['breed'].value_counts().sort_values(ascending=False))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"724459a4b184f5d85af3e66d3252c48b82020fe6"},"cell_type":"code","source":"plt.figure(figsize=(12,8))\nplt.hist(df['breed'].value_counts().sort_values(ascending=False))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29624542611e585b5ec38eca990f803e50f59feb"},"cell_type":"code","source":"bs = 64","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"17af6b4db3d3d09d62de585baf5b81eb9745e2b6"},"cell_type":"markdown","source":"### Get the Data"},{"metadata":{"trusted":true,"_uuid":"aa0fb5865c44dced7c32832a10dfff61cdd35ed4"},"cell_type":"code","source":"tfms = get_transforms(max_rotate=25); len(tfms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b41eb76986ee49d7fb6e2ab7da4d52bf5de46cb4"},"cell_type":"code","source":"data = ImageDataBunch.from_csv(PATH, folder='train', test='test', suffix='.jpg', ds_tfms=tfms,\n                               csv_labels='labels.csv', fn_col=0, label_col=1, \n                               size=128, bs=bs).normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"edf8bfdd2de39cae832b2e3984e5759ef324fe9d"},"cell_type":"code","source":"data.show_batch(rows=4, figsize=(12,8))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"945182550651e925de5bd31e1c56cefc6ed2d7c9"},"cell_type":"code","source":"print(data.classes)\nlen(data.classes),data.c","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2361c2a2ad58139c9c8c66eec93cc20d1f4d09fb"},"cell_type":"markdown","source":"### Training with Resnet 34"},{"metadata":{"trusted":true,"_uuid":"c474bf5a57f6cad49165f65c9ed71c6efeefc5a6"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet34, metrics=accuracy, model_dir=MODEL_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58f1f0239695260e1588746e89835796c5304224"},"cell_type":"code","source":"learn.model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8f4c135aa0318cb74f3cf51fcc1f4f273723f69a"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ffb77dc83f13bcc1484d9ffa06bc280309530ab"},"cell_type":"code","source":"learn.fit_one_cycle(5, max_lr=slice(3e-2,1e-2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a4a5eee584f1c7f3bdefd67df15b10b19df9801f"},"cell_type":"code","source":"learn.save('/tmp/model/stage-1-34')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40b7009d4c274145eb19abd317d832ecaf0816ac"},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\nlosses,idxs = interp.top_losses()\nlen(data.valid_ds)==len(losses)==len(idxs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eae4b78880bd9ff42b7be8582ec97ccadba53a11"},"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d57465d096b3f9ebc005081812cb3703b0766a27"},"cell_type":"code","source":"interp.most_confused(min_val=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6c9e16c705c495d33f0d4cce818ba70ea3b73cf"},"cell_type":"code","source":"#learn.load('/tmp/model/stage-1')\nlearn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8249216161d819b05b81bcd00c9508e021519dc"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"327cbb303b78fbd80336dd87059953ad2f44201d"},"cell_type":"code","source":"learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd72fd12483667f3467bd7a19c9585e6b4242ea9"},"cell_type":"code","source":"learn.save('/tmp/model/stage-2-34')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"813990bf4a6b88fe6188ccf2e387a06e2f100451"},"cell_type":"markdown","source":"### Training with Rest Net 50"},{"metadata":{"trusted":true,"_uuid":"8391d9fff88147db7d323f84a32715e5bb0a7a2b"},"cell_type":"code","source":"data = ImageDataBunch.from_csv(PATH, folder='train', test='test', suffix='.jpg', ds_tfms=tfms,\n                               csv_labels='labels.csv', fn_col=0, label_col=1, \n                               size=224, bs=16).normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68f183f4162ba2ef98554cfb5c9ee13939c67fc6"},"cell_type":"code","source":"data.show_batch(rows=4, figsize=(12,8))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9b094d651fa7b238f35403be6bcc570319f7050"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet50, metrics=accuracy, model_dir=MODEL_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6287b8a0ce447b5331600f833f101361fe0c2512"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"724119dc0a714c8123041be66bcb00cc0c30d1e5"},"cell_type":"code","source":"learn.fit_one_cycle(3, max_lr=slice(1e-3,1e-2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"970290200c1f7695a7e07f43f83c38c0a691d7db"},"cell_type":"code","source":"learn.save('/tmp/model/stage-1-50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e1947ab35d7aea0dbae45c5aec5e462953960e1d"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8660d46df8fb7459c77a7810ddf047d8261cf6e"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4178284802fd98c43c1096754b3edf9942b5500c"},"cell_type":"code","source":"learn.fit_one_cycle(3, max_lr=slice(1e-5,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08448159b0d257c8d9b351f7b55c892bf64395af"},"cell_type":"code","source":"learn.save('/tmp/model/stage-2-50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0d1965d399561b405baf7e9cec579a9ad2ac61ff"},"cell_type":"code","source":"predictions = learn.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b7546aaec51e46c54478ab80a1786f0b5be8d8f"},"cell_type":"code","source":"predictions[0][0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de2a739061bef5a51c54b0eedd7979d2e2c6e0ca"},"cell_type":"code","source":"sample_submission = pd.read_csv('../input/sample_submission.csv')\nsample_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4bc7fcb06bfc0d4c41f867dbe717be317da4be8e"},"cell_type":"code","source":"submission = sample_submission.copy()\nfor i in range(len(submission)):\n    submission.iloc[i, 1:] = predictions[0][i].tolist()\nsubmission.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4de3255caca52722c2a32c7aa64fb7012b997534"},"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60c65690c593dc087e0f35675abb5ee80d8a658d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}