{"cells":[{"metadata":{"_uuid":"bc5790f6497637329435f6f8e2c3d3fac55034ec"},"cell_type":"markdown","source":"Load libraries"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom fastai.vision import *","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0fcf95009b32238123e2054fa83c134204423fb9"},"cell_type":"markdown","source":"Look at dataset"},{"metadata":{"trusted":true,"_uuid":"5227060f82ae990e1a08f9e9a876335a5d159795"},"cell_type":"code","source":"path = Path('../input')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c098a826c4db566fff757bdb489765707fd0609a"},"cell_type":"code","source":"path.ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4674673b54bb26b84078ae4f4540c626c37ad56c"},"cell_type":"code","source":"path_img = path/'train'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af0ab89536a47666504c607ec848f581488ae007"},"cell_type":"code","source":"get_image_files(path_img)[:5]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a9795ae580a7ba65e271bcd31bb699c19853572e"},"cell_type":"markdown","source":"Create datablock"},{"metadata":{"trusted":true,"_uuid":"bf1f21bfec2320cdd601af9c23ce290a8df9d570"},"cell_type":"code","source":"np.random.seed(42)\nsize = 224\nbs = 64\nnum_workers = 0  # set this to 0 to prevent kernel from crashing\npat = r'/([^/.]+).\\d+.jpg$'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35c817b06a3060a49f85cee0590915288efe4508"},"cell_type":"code","source":"tfms = get_transforms()                              #Do standard data augmentation\ndata = (ImageItemList.from_folder(path_img)          #Get the training images from the train dir\n        .random_split_by_pct()                       #Randomly split off 20% of the images to form validation set\n        .label_from_re(pat)                          #Label by applying the regex to the filenames\n        .add_test_folder('../test')                  #Add a test set using the test dir\n        .transform(tfms, size=size)                  #Pass in data augmentation\n        .databunch(bs=bs, num_workers=num_workers)   #Create ImageDataBunch\n        .normalize(imagenet_stats))                  #Normalize using imagenet stats","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f548b35f17986a916a656235071ba1fe9a522c4b"},"cell_type":"markdown","source":"Verify datasets loaded properly.  We should have the following:\n* train: 20,000\n* valid: 5,000\n* test: 12,500"},{"metadata":{"trusted":true,"_uuid":"87917cc445baa2e7809bbec9e9737f426acc02bc"},"cell_type":"code","source":"print(len(data.train_ds))\nprint(len(data.valid_ds))\nprint(len(data.test_ds))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7adebb077bc97d8493e864a515deb66e14169f5a"},"cell_type":"code","source":"data.classes","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fa7878cb334d1a53dcc73faf53fe978dc40e9be1"},"cell_type":"markdown","source":"Visualize data"},{"metadata":{"trusted":true,"_uuid":"1ff1232900ccc09cd93b82b51faca0cf6f947258"},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(7,6))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d3c563852e91e41ee9e8362f542956d8ff1b8663"},"cell_type":"markdown","source":"Create learner using pre-trained model"},{"metadata":{"trusted":true,"_uuid":"21209008a029b7e48ddb80743b4b847518b15b8d"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet50, metrics=accuracy, model_dir='/tmp/models')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9b2d2f6858c190a042450ea554a08880cf9357b0"},"cell_type":"markdown","source":"Fit the model with most layers frozen"},{"metadata":{"trusted":true,"_uuid":"3c39f2511ce7dd8a1eaef3547223f064d21da0a2"},"cell_type":"code","source":"learn.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bfd773658287694cbc22a3aaa6154019f06ed3d3"},"cell_type":"code","source":"learn.save('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2bf02197d016ab089cb16070caabf86c63a0f0fe"},"cell_type":"markdown","source":"Unfreeze all layers, run learning rate finder, fit some more"},{"metadata":{"trusted":true,"_uuid":"ffdd1a3c6b16ecd9ae349cf27accd0f158d7bb93"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d4207526c8593f08b493fa03b2cbf25e2c846b3"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"841f66640fc6429d67e84600f067311cd64defee"},"cell_type":"code","source":"learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0995049a986864e711602f26d3b3cd7f331d1d25"},"cell_type":"code","source":"learn.save('stage-2')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ee565eeddb460f05ed119a92a1bbac7e99f92e46"},"cell_type":"markdown","source":"Examine prediction errors in validation set"},{"metadata":{"trusted":true,"_uuid":"ee0d1a1b17b2369d2970574b4e99410de1247dcf"},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\nlosses,idxs = interp.top_losses()\nlen(data.valid_ds)==len(losses)==len(idxs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3550fde7d4b54c2e7e1b00e1c270eea79a4ccca3"},"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c4ff95b1d1543764691142c3b09f026b429338b2"},"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(6,6), dpi=60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"21b7cd80a0d60558f1c71a60d8de05cfed79eb4e"},"cell_type":"code","source":"interp.most_confused(min_val=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"897d5a6e2de8440d65e01670382bde0884737b1a"},"cell_type":"markdown","source":"Create predictions using test set"},{"metadata":{"trusted":true,"_uuid":"82405baa7de93f04e86d9d07ac518ec57acbc616"},"cell_type":"code","source":"preds, y = learn.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58168d5577abf41adc5a9a7d6d73046d4bf67d9c"},"cell_type":"code","source":"dog_preds = preds[:,1]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a8a2c8b85f4cac25c5d382af727dea6a1529c9bd"},"cell_type":"markdown","source":"Generate competition submission using predictions"},{"metadata":{"trusted":true,"_uuid":"1e99f274d00562321f593f7dffa5edbb52cdf61f"},"cell_type":"code","source":"submission = pd.DataFrame({'id':os.listdir('../input/test'), 'label':dog_preds})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0fac3e616b242093f2c08719fb272ce043b5994f"},"cell_type":"code","source":"submission['id'] = submission['id'].map(lambda x: x.split('.')[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"152cb94442841d5976342bf9b05009c396e1d848"},"cell_type":"code","source":"submission['id'] = submission['id'].astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec04a147300c19d3f1afe9e35025b635a426f25e"},"cell_type":"code","source":"submission = submission.sort_values('id')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a1ae9614ff93c68cdc8ed9e57231bf758337bb0"},"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}