{"cells":[{"metadata":{"_uuid":"8052e47f-3e15-4b95-b4b3-87cc0f2bdd50","_cell_guid":"9a12d96b-b416-47c3-86dd-65b79297394e","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 all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d6465922-d263-4fbe-bdfd-5d2db68bf578","_cell_guid":"3361e2c2-f12d-46c2-985c-012311ffa7fb","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9c485a99-ab71-46da-97a8-fb751ecb4a10","_cell_guid":"145f694a-94b4-4e0c-8057-ce0673336075","trusted":true},"cell_type":"code","source":"from fastai.vision import *\nfrom fastai.metrics import error_rate\nfrom IPython.display import Image\n\nbs = 64\n# bs = 16   # uncomment this line if you run out of memory even after clicking Kernel->Restart","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"92686eb3-3754-49d6-9057-68548db9f8a2","_cell_guid":"57c30246-32cd-468b-a8e9-6469b1339e67","trusted":true},"cell_type":"code","source":"# ls /kaggle/input/train_images/","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"e256e6e3-7d44-42bb-b6fd-e94f0c7fac03","_cell_guid":"ed47514a-95d8-4939-b57c-81913c762bd6","trusted":true},"cell_type":"code","source":"# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"ac82d2cd-f1a0-4f53-8e5c-3de6faf47f3c","_cell_guid":"6cb713a0-02b2-4061-9d26-8e94afd50d32","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/train.csv\")\ntest_df = pd.read_csv(\"../input/test.csv\")\n\nsample_submission_df = pd.read_csv(\"../input/sample_submission.csv\")","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"d7672a78-1ded-40e0-b4a7-6326bd6f16df","_cell_guid":"0e51393d-21a9-4110-aa79-8886d8851679","trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"4725adb8-fb79-4351-83ec-f0b7d57807b5","_cell_guid":"ec9f5a7d-b47b-44a6-a580-955100f9d10e","trusted":true},"cell_type":"code","source":"tfms = get_transforms(do_flip=False)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"4cb7f27f-81c2-4688-8132-c44b68579081","_cell_guid":"51f03d8b-c50c-471c-a9cd-470c585009d6","trusted":true},"cell_type":"code","source":"data = ImageDataBunch.from_csv(path=\"../input/train_images/\", csv_labels=\"../train.csv\", fn_col=\"file_name\", label_col=\"category_id\", ds_tfms=tfms, size=32)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"8ea94cee-d52f-4e18-9c50-d356c20dcb03","_cell_guid":"6d99b502-7182-406a-8077-dbeca80583ed","trusted":true},"cell_type":"code","source":"data.show_batch(rows=3)\n# data.classes","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"20bd4b29-9475-4602-a1bb-f99cafec2276","_cell_guid":"e284a5e0-6b4e-4e43-968d-e4e34b784a49","trusted":true},"cell_type":"code","source":"# train_df[\"height\"].value_counts()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"a798e32e-a109-486f-85b3-62540d6fa178","_cell_guid":"b2e0639e-45c7-4a74-aa2a-26b21e30dd30","trusted":true},"cell_type":"code","source":"learn = cnn_learner(data, models.resnet34, metrics=error_rate)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"c176ccd8-2093-450b-8dbe-55d8daa58c90","_cell_guid":"17c25a75-c06b-4373-8de3-922105c204fe","trusted":true},"cell_type":"code","source":"help(cnn_learner)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"f928aa86-3873-4fb0-8729-62058b71ae1b","_cell_guid":"3cb6bb50-2cb9-46de-9e02-b7366e11a70b","trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(1)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"e593e8ac-e5b5-4c4d-a806-10dc691bf9a3","_cell_guid":"fd45b8ef-9042-4494-89a4-54f3490fad05","trusted":true},"cell_type":"code","source":"learn.model_dir='/kaggle/working/'\nlearn.save('stage-1')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"1a45a61a-268e-4812-abad-410050c8cca6","_cell_guid":"8c6ff019-5f7a-4472-9eb4-8a8f4885210d","trusted":true},"cell_type":"code","source":"learn.unfreeze()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"a65d3b8f-5f3a-4ae0-9826-b2c99ecef287","_cell_guid":"badb4151-3b80-46ed-8612-bca0e364f83b","trusted":true},"cell_type":"code","source":"# learn.fit_one_cycle(1)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"b664434b-eb9d-4953-9aa3-9276acd81469","_cell_guid":"455e28ad-4b26-4a41-ab9b-fb598c9eac29","trusted":true},"cell_type":"code","source":"learn.load('stage-1');","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"4c7fe1d0-c19e-494c-85a6-77972277464e","_cell_guid":"fdf31684-bf3c-4a64-97df-34ae18a0b12a","trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"85ee36cb-08cb-498e-a755-32f37e114f93","_cell_guid":"286a7e2a-7bfa-4a58-bd17-0ead8f6d415e","trusted":true},"cell_type":"code","source":"# learn.recorder.plot()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"7dd5532d-74f5-4b0f-baab-5bb83e51d19a","_cell_guid":"1b9f685d-bfc5-44f8-8d89-43b22bf9c539","trusted":true},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(2, max_lr=slice(1e-6,1e-3))","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"f0569156-4923-465b-9177-f301260f6fca","_cell_guid":"fcd5b240-7dc4-4a1b-806c-988cfffe1449","trusted":true},"cell_type":"code","source":"img = test_df['file_name'][23678]\nimgpath = '../input/test_images/' + img\n# from IPython.display import Image\n# Image(filename=imgpath)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"d202356c-e3ad-449b-b8b5-f42331d3c1a1","_cell_guid":"ad3b4703-5e99-4ae2-ab51-f2c203e2b04a","trusted":true},"cell_type":"code","source":"# data_test = ImageDataBunch.from_csv(path=\"../input/test_images/\", csv_labels=\"../test.csv\", fn_col=\"file_name\", ds_tfms=tfms, size=32)\n# test_df","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"25faccf5-daf4-4371-a906-c5871d0dbe45","_cell_guid":"1b42138b-45a5-4098-91b2-a5cdb69c1c57","trusted":true},"cell_type":"code","source":"learn.predict(open_image(fn=imgpath))","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"85c67203-32bc-46ee-97ac-51449ec5ae98","_cell_guid":"f8c8cd84-b8a3-4214-bd4d-e927920671db","trusted":true},"cell_type":"code","source":"data_test = ImageDataBunch.from_df(path=\"../input/test_images/\", df=test_df, fn_col=\"file_name\", ds_tfms=tfms, size=32)\n# learn.data.add_test(data_test)\n# test_df.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"b7ff536e-0432-4109-a28a-4b0a6fec0b8e","_cell_guid":"0f8fcd7e-0f00-42d0-a6c2-e874462944a3","trusted":true},"cell_type":"code","source":"# learn.data.add_test(data_test) \ndata_test","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"be8c8b3f-3ea8-412a-881a-c71a8ad8c6ac","_cell_guid":"b6860cd2-ec5e-4e5b-aa56-c27c222b6bcc","trusted":true},"cell_type":"code","source":"img_paths = test_df['file_name'].tolist()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"bc6e0450-db04-4809-947b-6907ba1bd4e4","_cell_guid":"c7c26be6-8703-465a-966b-2032c4671a7a","trusted":true},"cell_type":"code","source":"preds = []\n\nfor i in range(0, len(img_paths)):\n    file_name = img_paths[i];\n    tens = learn.predict(open_image(fn='../input/test_images/'+ file_name))\n    preds.append((file_name[:-4], int(tens[0]) ))\nresult = pd.DataFrame(preds, columns=['Id', 'Predicted'])","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"993a2fc8-4a59-4687-a6ba-bb7c9cedfeb5","_cell_guid":"34b56d0f-0d50-429a-a71a-5bc9df13192a","trusted":true},"cell_type":"code","source":"result.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]}],"metadata":{"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"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}