{"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_minor":4,"nbformat":4,"cells":[{"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.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"../input\")","metadata":{"_uuid":"2839456fafc0b8faab80065e0590455ad72758cb","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs=os.listdir(\"../input/train/train/\")","metadata":{"_uuid":"33c4bce79b115d013a05c4e2d2e47f9d57894d5a","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs[:5]","metadata":{"_uuid":"83fdea05675637ae2f5ad557b05ad7d86b8844b4","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pathlib","metadata":{"_uuid":"eb6898f2dd64ab0b6a5b4004ee3f9bd6bc118825","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=pathlib.Path(\"../input/train/train/\")","metadata":{"_uuid":"2f93b08b47ec95e7b666bc1b95c2894ec0921ed0","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames=get_image_files(path)\nfnames[:5]","metadata":{"_uuid":"f70495498ca336a0896936f95805e3eea17a46a3","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def return_label(path):\n    if str(path).find('dog')==-1:\n        return \"cat\"\n    else:\n        return \"dog\"","metadata":{"_uuid":"6f56201d724e68fbf2c6c72dccd8c491a63ae291","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"return_label(pathlib.Path(\"../input/train/train/cat.1.jpg\"))","metadata":{"_uuid":"e8fb824ce3c39796064a5ed35fabd18cd5d3f93f","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = ImageDataBunch.from_name_func(path, fnames, return_label, ds_tfms=get_transforms(), size=224,bs=8)\ndata.normalize(imagenet_stats)","metadata":{"scrolled":true,"_uuid":"8959dcde29a642190aa6bbb8b196ba99f637ab5c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.train_ds.y.classes","metadata":{"_uuid":"6c4b0f55423c8a2742f420fd513a906f1ecc67cb","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TMP_PATH = \"/tmp/tmp\"\nMODEL_PATH = \"/tmp/model/\"\n\nlearn=create_cnn(data,models.resnet34,metrics=[accuracy,error_rate],path=TMP_PATH,model_dir=MODEL_PATH)","metadata":{"_uuid":"9c647769316baa87b1746f33d010f898b2a799ff","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(1)","metadata":{"_uuid":"26df2cf16fa7805c035bb3837e64a0687bc124ac","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('model_224')","metadata":{"_uuid":"80d81ab45eca3798db70c047dca2261ed154f68c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"../../tmp/model/\")","metadata":{"_uuid":"0e05b334f4e0a96b42168ee41c4c35252a378e00","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"_uuid":"b38e1a923c60d9caef5d986cebb116e5631a6a73","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot()","metadata":{"_uuid":"10a72beec1cc997eb54f5f5f34e95aa355dc7721","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp=ClassificationInterpretation.from_learner(learn)","metadata":{"_uuid":"b070ece5d35db2f222f6ffca0b7c2c643749b47d","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix()","metadata":{"_uuid":"b61b6d361225dc18814ad33b22c13c0a1f1ae8c5","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dog=1 and cat=0\ntest_path=pathlib.Path(\"../input/test1/test1/\")\ntest_images=get_image_files(test_path)\ntest_images[:5]","metadata":{"_uuid":"17f9966fb963ba8d8130c0b718f2ba0b20276d4c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list=[]\nlabels=[]\nimport re\nfor img_path in test_images:\n    #print(img)\n    img_id=re.findall('\\d+',str(img_path))\n    id_list.append(int(img_id[2]))\n    img=open_image(img_path)\n    label=learn.predict(img)\n    if str(label[0])=='cat':\n        labels.append(0)\n    else:\n        labels.append(1)\n    print(img_id,\"  \",label)","metadata":{"_uuid":"3e945729688f910e5f4d1cbc2bba279a3f8ef815","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels)","metadata":{"_kg_hide-output":true,"_uuid":"3562f7d5f4d0db0bd9753e15bc2be592920a4820","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(id_list)","metadata":{"_uuid":"a0bb90e26d444b4af3f4bb16754a8b127d014901","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"../input\")","metadata":{"_uuid":"e6f82189b77a5cb8c4dcdb7a824b7b3b6e321588","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission=pd.DataFrame({\"id\":id_list,\"label\":labels})","metadata":{"_uuid":"3dca2501e728400faecfbd34a5aed562405f1845","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission.to_csv('submission.csv', index=False)","metadata":{"_uuid":"bb1a0a90688ace668fbdda817c15ff1d1e9abef7","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir()","metadata":{"_uuid":"846b163fe6a6875962f4871439b389db67191d9e","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=pd.read_csv(\"submission.csv\")","metadata":{"_uuid":"3f6a9865ef4334cf18fe02b5b74fc5c5de02b932","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"_uuid":"a9ee5d35bd2e6f7ee4f0aec869b79606a6291463","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"fcaf4f70fc420168ff357bd7dfccaf80f9986a6a","trusted":true},"execution_count":null,"outputs":[]}]}