{"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":"486ec2b69626046abfca524fe51c428343dce408"},"cell_type":"code","source":"import pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac8bd5ac2296b29cd471bf70388e18b9e1c092a9"},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import * \nfrom fastai.datasets import *  # this is needed for untar_data\nfrom fastai.metrics import * # for accuracy\nfrom fastai.vision.data import * \nfrom fastai.vision.image import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"262db63ef5fac8cf0f68b368febc8da2f7086aa7"},"cell_type":"code","source":"path = untar_data(URLs.DOGS)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1b6dfc278b42b592559c305b83a840975c1f4b7d"},"cell_type":"code","source":"path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"43de6c0ce29d401c7c7ddf66657f1c6d9b8f98e1"},"cell_type":"code","source":"os.listdir(path)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# This file contains all the main external libs we'll use\nfrom fastai.imports import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3247cf213dfb2bdadbcd56dd2310a269732b9509"},"cell_type":"code","source":"torch.cuda.is_available()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"47760e4afd157b14d8ec2107e1498dad24083022"},"cell_type":"code","source":"torch.backends.cudnn.enabled","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"999638b5bf210056f5938de4aeb97488f3064943"},"cell_type":"code","source":"sz=224","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1dc68b11ffc2f2db2b886229ee3d12610165e584"},"cell_type":"code","source":"arch=models.resnet34","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ffe5005b23c9c0b1cae7e9a51a313575a04f7dc2"},"cell_type":"code","source":"data = ImageDataBunch.from_folder(path,test='test1',\n                                  ds_tfms=get_transforms(), \n                                  size=sz,\n                                  num_workers=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0d413821946f8ccd8ab0e4d278d6117afb9e451d"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet34, metrics=accuracy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54b5b99838f0479821153798e96b67442d0c9e66"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee16926a427985e1a942a31b4f03119682bcf916"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"306b15df668d66e078ce55327b3eb59ebc5a92a0"},"cell_type":"code","source":"learn.fit(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"451327e4986b95a48971e375174391caabf766d0"},"cell_type":"code","source":"PATH = '../input/'\nos.listdir(PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d63cae4acbe3074cbadad5aa508c8e2c73e09f2e"},"cell_type":"code","source":"os.listdir(\"../input/test\")[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f803efcf9224d4ff6d24b145e337a773a55e246b"},"cell_type":"code","source":"submission = pd.DataFrame(os.listdir(\"../input/test\"),columns = ['FileName'])\nsubmission['id']= submission['FileName'].apply(lambda s: s[:len(s)-4])\nsubmission['label']=0.5\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07c80137638b4ec6cfc75722f46cfc45081a3dac"},"cell_type":"code","source":"len(submission)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e536b1f4f95cba6190941691c67fb53bb203bf9c"},"cell_type":"markdown","source":"### test 10 images"},{"metadata":{"trusted":true,"_uuid":"c784eaa07bff1d9558f15b63ea1ad2b82eec8152"},"cell_type":"code","source":"test_path = PATH+'test/'\ntest_path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2256f6f09060372529917191f3bc43242e30980","scrolled":true},"cell_type":"code","source":"for i in range(10):\n    img = open_image(test_path+submission['FileName'][i])\n    result = learn.predict(img)\n    print(submission['FileName'][i],result[0],result[1])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0a7482421256e0e586d98f2c9bd5c76babe3ace3"},"cell_type":"markdown","source":"### Predictions"},{"metadata":{"trusted":true,"_uuid":"a6a06c7754ddc46893474ac41b8e5daceffc722a"},"cell_type":"code","source":"submission.loc[0,'label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e318a8a0b36bf39b6e60b29280baee9532b0a66"},"cell_type":"code","source":"for i in range(len(submission)):\n    img = open_image(test_path+submission['FileName'][i])\n    result = learn.predict(img)\n    submission.loc[i,'label']=float(result[2][1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"639b207f4d9e9b6413205d2f456d42ad168c54c9"},"cell_type":"code","source":"submission[:100]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c52f93b6fbbbe6d2ec820e1184998fb12cd6ed72"},"cell_type":"code","source":"submission[['id','label']].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.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}