{"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\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(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8ed456f786756e9da8481e5e3e502b8f153d148"},"cell_type":"code","source":"!rm -rf input","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"!cp -r ../input .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bdb469b4b6c16330db5aa19d2b61fe019d771151"},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec59ccd651fab27a95e7998c8dd985a9ce9c6fca"},"cell_type":"code","source":"path = Path('input/train/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1772cbf5a21e68c238b14780a326c3e75d88017c"},"cell_type":"code","source":"np.random.seed(42)\ndata = ImageDataBunch.from_folder(path,test='../test', ds_tfms=get_transforms(),valid_pct=0.25,size=299,bs=32,num_workers=0)\ndata.normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72abfa1476f75e410d9f1a049b15c557247fc222"},"cell_type":"code","source":"print(data.classes)\nlen(data.classes),data.c","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4da04b7819cae4b13230a194a7f45906381eb929"},"cell_type":"code","source":"data.show_batch(rows=3,figsize=(7,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62cceab0e239fb63e338dfb0da079523f183c70a"},"cell_type":"code","source":"learn = create_cnn(data,models.resnet50,metrics=error_rate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a778db09806ff70d2d93c34697828dbd6e765397"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0805b0971d2c2f1858a0733734e518a020b53723"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1ba352389342107525eca2723911d0b1d820296"},"cell_type":"code","source":"learn.fit_one_cycle(6,slice(1e-2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cac3f373e3d63fda9b8b6e79cbdfc03c9edb57f2"},"cell_type":"code","source":"learn.save('stg-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cfbf0da83a0ba5cecc35991a3bb58c9fac838fd3"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fca0562d97f90577ee0986498d54b735d95faaa1"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ae76f990886285f7a47d4364bdc24eda4f5fac4"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a5b150780dc51d5454564ac643d16a0af4a8c92f"},"cell_type":"code","source":"#learn.load('stg-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04b955da5809cb350b8a7f427ce6ea8096d91e72"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(4,max_lr=slice(1e-4,1e-3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d6e02b01aaa24318f5abe1e60677117a104f4189"},"cell_type":"code","source":"data = ImageDataBunch.from_folder(path,test='../test', ds_tfms=get_transforms(),valid_pct=0.25,size=350,bs=32,num_workers=0)\ndata.normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7531b1703f575dd830a5c0520127f48a7c9d86e"},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\nfrom sklearn import metrics\nprint(metrics.classification_report(interp.y_true.numpy(), interp.pred_class.numpy(),target_names =data.classes))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4241e21599fc6bd183bb3133e378061f6663128b"},"cell_type":"code","source":"learn.save('stg-2')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"86cc38718cc2c998e933b0bd7e4854402cd2927e"},"cell_type":"code","source":"learn.data=data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db9396096ba0e8216443b68868377db22303eca1"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(4,max_lr=slice(1e-5,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"21682a5f0b3168a37edb205bb8c0568bf2691150"},"cell_type":"code","source":"preds,y=learn.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5bd822d5a1812c6706853407c4d655a9cb678b53"},"cell_type":"code","source":"preds = np.argmax(preds, axis = 1)\npreds_classes = [data.classes[i] for i in preds]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6e13f6e0c8f3b40329919638288b1b9705b4acf9"},"cell_type":"code","source":"submission = pd.DataFrame({ 'file': os.listdir('input/test'), 'species': preds_classes })\nsubmission.to_csv('test_classification_results.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb86d43fb765a2697355cfe08d5b9a967071ff3b"},"cell_type":"code","source":"submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"687ae759dbc629bcb919e2afa885e0797164c0f9"},"cell_type":"code","source":"!rm -rf input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b878c50d2b7b868268b52ae63db3c75c422d14b"},"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}