{"cells":[{"metadata":{"_uuid":"7b83ca930291a8df2f58d2b25833b3bd27445a40"},"cell_type":"markdown","source":"[reference]\n* https://course.fast.ai/lessons/lesson1.html\n* https://github.com/fastai/course-v3/blob/master/nbs/dl1/lesson1-pets.ipynb"},{"metadata":{"_uuid":"9f5fd4115b6b2cf16bdeae5ce72037a3da80a1c7"},"cell_type":"markdown","source":"# Import fast.ai libraries\n* Turn on GPU and Internet (connected)"},{"metadata":{"trusted":true,"_uuid":"9292e3a05dc6e19c0ef11a76d7e99e8b5f56c9e2"},"cell_type":"code","source":"import fastai\nfrom fastai.imports import *\nfrom fastai.vision import *\nfrom fastai.metrics import *\nfrom fastai.gen_doc.nbdoc import *\nprint('fast.ai version:{}'.format(fastai.__version__))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9697de7092c289d9dbcef194f1c199e8573bad74"},"cell_type":"markdown","source":"# Datas"},{"metadata":{"trusted":true,"_uuid":"d6dddae1dd601f41e65821628e14dc65494ec281"},"cell_type":"code","source":"model = models.resnet152#resnet34\nWORK_DIR = os.getcwd()\nIMAGE_DIR = Path('../input/')\nimage_size=224\nbatch_size=32","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"584df1daa5f2b0ba93f621ffecb9efcf18fff3bc"},"cell_type":"code","source":"fnames = get_image_files(IMAGE_DIR/'train')\nfnames[:5]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"82cc6b3f893791ae9c25920c4b20e62f1515b722"},"cell_type":"markdown","source":"`get_transforms` is a data augmentation function, with good initial settings."},{"metadata":{"trusted":true,"_uuid":"62216f5eb4e52ec9bf00697b317c9ea05e00772b"},"cell_type":"code","source":"show_doc(get_transforms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07174a462f4d06cc5b4fdacf66a1187a525c3cc6"},"cell_type":"code","source":"show_doc(ImageDataBunch.from_name_re)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ff97f9a8202d7a49ce4d23f8fee97254f709e153"},"cell_type":"code","source":"pattern_get_class = re.compile(r'/([^/]+)\\.\\d+\\.jpg$')\ndata = ImageDataBunch.from_name_re(path = IMAGE_DIR,\n                                   fnames = fnames, \n                                   pat = pattern_get_class, \n                                   ds_tfms=get_transforms(), \n                                   test ='test',\n                                   size=image_size, \n                                   bs=batch_size,\n                                   num_workers=0).normalize(imagenet_stats)\ndata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4402188f7aefd131985ec418be2a544fc451dc91"},"cell_type":"code","source":"data.show_batch(rows=3)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a4a777c5ba20a124e41aa72ce01f9210a3ccbddd"},"cell_type":"markdown","source":"# Train"},{"metadata":{"trusted":true,"_uuid":"3a0face680bbbbf345c186f885730d70041225c8"},"cell_type":"code","source":"show_doc(create_cnn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7323e94f94a19475e9543b81a5c8ae2bda978c42"},"cell_type":"code","source":"learn = create_cnn(data, model, metrics=accuracy, model_dir=WORK_DIR)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbca33108079df6f4ffcf0cb6b8b6d01b974a17f"},"cell_type":"code","source":"show_doc(learn.fit_one_cycle)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"63c5e2728bcdd67958bb061653c517aed1200419"},"cell_type":"code","source":"learn.fit_one_cycle(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"877c1828edbea71ca088ae41d10e823214659d02"},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07e9e82c9cc2d17db95e844ef58ffb201816729d"},"cell_type":"code","source":"learn.recorder.plot_lr(show_moms=True)\n# Left plot is Learning-Rate vs itteration, Right one is momentams vs itteration.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06335fdcde73e888efa3b8058d83581ffe4a34c7"},"cell_type":"code","source":"learn.save('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f2a83ef1380d7e5d5f6be9c6f93679bbae466e2f"},"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4de87ae1c72a704f2c7a0f796d3684d6dff6435b"},"cell_type":"code","source":"learn.load('stage-1')\nlearn.unfreeze()\nlearn.fit_one_cycle(4, max_lr=slice(1e-6,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"877c1828edbea71ca088ae41d10e823214659d02"},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"926bde271ca4ea7c1cd2abba424c2671bf9f089d"},"cell_type":"code","source":"learn.save('stage-2')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9e6b365542c43bcea907a98d417929dc418387c4"},"cell_type":"markdown","source":"# Classification Interpretation"},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"fb3a4759172b6edd04aff54cba23ffcfd60bbb22"},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b668f8cd80ad84eb43505d387301eb2ff2638ed"},"cell_type":"code","source":"interp.plot_top_losses(9)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eec8f98c958101bbd110004d14818633dc163fd7"},"cell_type":"code","source":"interp.plot_confusion_matrix()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"61d703bcff96aa5c226db0d54cbd7c72d886336c"},"cell_type":"markdown","source":"# Predict"},{"metadata":{"trusted":true,"_uuid":"8ce0c7e574133151487c164f327b820cd6bd4314"},"cell_type":"code","source":"# Check if DatasetType loaded\n# It's depend on difference between fast.ai version v1.0.39/v1.0.36.\nif 'DatasetType' not in sys.modules:\n    from fastai import DatasetType","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed4f3be010d61be9207e1b7918f3f480fc7c3323","_kg_hide-output":false},"cell_type":"code","source":"try:\n    # TTA cause error @fast.ai v1.0.39\n    preds, _ = learn.TTA(ds_type=DatasetType.Test)\nexcept:\n    preds, _ = learn.get_preds(DatasetType.Test)\nelse:\n    print('Predict with TTA done.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2e970a00c7fcfeb4631ff3324fbc222ae7f451b"},"cell_type":"code","source":"preds","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a8a7c08283031fdd9e7bdc25fb6b1c5c1dc38998"},"cell_type":"markdown","source":"> For each image in the test set, you must submit a probability that image is a dog. "},{"metadata":{"trusted":true,"_uuid":"37f0810e022a296bd465d7397a2c8bdda546c863"},"cell_type":"code","source":"# from here we know that 'cats' is label 0 and 'dogs' is label 1.\nprint(data.classes)\ndict_label_order = {label:order for order,label in enumerate(data.classes)}\nprint(dict_label_order)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee3b0b3be5a164054fc4ea7dcf46fc8f4d6fe726"},"cell_type":"code","source":"n_dogs = dict_label_order['dog']\nn_dogs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b27cad94c22cf40209ab48a49775f3241f2f23e7"},"cell_type":"code","source":"prob_dogs = preds[:,n_dogs].numpy()\nprob_dogs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a949f529da7712296b6ddbe102443904096225ff"},"cell_type":"code","source":"plt.hist(prob_dogs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2fe39ef9480a8f6f0556b1a45d048281341393cf"},"cell_type":"code","source":"ids = [int(file.stem) for file in data.test_ds.x.items]\nids[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b34da031e926adadc01851bc9c8666e6791b912"},"cell_type":"code","source":"import pandas as pd\nsubmission = pd.DataFrame({'id':ids,'label':prob_dogs})\nsubmission = submission.sort_values(by=['id'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df1440a2a8919fa71d8c578be468575b832885d7"},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db6d4f88a47cc96beea36e74888716e66c77761a"},"cell_type":"code","source":"submission.label.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a4ae2313f109fbe1ee9db099636fb167c5c4af7f"},"cell_type":"code","source":"submission.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}