{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"PATH = \"../input/\"\nTMP_PATH = \"/tmp/tmp\"\nMODEL_PATH = \"/tmp/model/\"\nsz=224","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6e376aa50aa332233706403db495308ffa958bca","collapsed":true},"cell_type":"code","source":"fnames = np.array([f'train/{f}' for f in sorted(os.listdir(f'{PATH}train'))])\nlabels = np.array([(0 if 'cat' in fname else 1) for fname in fnames])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"747edfcb62475bae19eb6b07a21fd91f2d01ce6c","collapsed":true},"cell_type":"code","source":"print(fnames[-2],labels[-2])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3bf0bfec0a753ffea6299bc2f2ea18e2cce6b891"},"cell_type":"code","source":"from fastai.imports import *\nfrom fastai.transforms import *\nfrom fastai.conv_learner import *\nfrom fastai.model import *\nfrom fastai.dataset import *\nfrom fastai.sgdr import *\nfrom fastai.plots import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eef266ac656ba699631aa5094cb70a6c914a75ff","collapsed":true},"cell_type":"code","source":"arch=resnet50\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"558145e26e702aad4dc1e958088289f5276cc0d3"},"cell_type":"markdown","source":"**Model 1**"},{"metadata":{"trusted":true,"_uuid":"4e5aae092fd06f7fe453cbcb6878f3c4c1aacebd","collapsed":true},"cell_type":"markdown","source":"data = ImageClassifierData.from_names_and_array(\n    path=PATH, \n    fnames=fnames, \n    y=labels, \n    classes=['dogs', 'cats'], \n    test_name='test', \n    tfms= tfms_from_model(arch, sz)   \n)\nlearn = ConvLearner.pretrained(arch, data, precompute=True, tmp_name=TMP_PATH, models_name=MODEL_PATH)"},{"metadata":{"trusted":true,"_uuid":"e40286ac49a020ba1656ee797536cd0fc2632352","collapsed":true},"cell_type":"markdown","source":"%%time\nlearn.fit(0.01, 2)"},{"metadata":{"trusted":true,"_uuid":"08a8b7cc804910e7be8aa80f4d808e14f4794801","collapsed":true},"cell_type":"code","source":"#lrf= learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2c881aeb9bf6220b3661178a052a35f10fafcda","collapsed":true},"cell_type":"code","source":"#learn.sched.plot_lr()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"237cbc53fc77b834f1e1b7c5e7184e60968a3500","collapsed":true},"cell_type":"code","source":"#learn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"17ac21f41051241d96644bbf793b84cb5a39ea9e"},"cell_type":"code","source":"#learn.save('model1')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9cd7be95ff2de8d7421e0a8253c02e546a55a5e8"},"cell_type":"markdown","source":"This model got 0.06218 binary loss error on test set on Kaggle with resnet 34\nand 0.05806 with resnet 50 and 0.05876 with resnet101   and 0.06055 with TTA resnet50"},{"metadata":{"_uuid":"eb0c2ad51a7b2c574f6fc1093214c73cc8c7f510"},"cell_type":"markdown","source":"**Model 2**"},{"metadata":{"trusted":true,"_uuid":"fdd09f9f4620e3ca73b803e8a58f7c688dcacb2f","collapsed":true},"cell_type":"code","source":"data = ImageClassifierData.from_names_and_array(\n    path=PATH, \n    fnames=fnames, \n    y=labels, \n    classes=['dogs', 'cats'], \n    test_name='test', \n    tfms= tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1)   #data augmentation\n)\nlearn = ConvLearner.pretrained(arch, data, precompute=True, tmp_name=TMP_PATH, models_name=MODEL_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d16b971337252e36e66217003ac25c1d54102ab4","collapsed":true},"cell_type":"code","source":"learn.fit(0.01,4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a95b55ba187146039f6c4e3afefc6de50f2306d1","collapsed":true},"cell_type":"code","source":"learn.precompute=False\nlearn.fit(1e-2, 3, cycle_len=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec17fcc85b9ca15cfd336ef2c7443b5b84d73e04","collapsed":true},"cell_type":"code","source":"#learn.sched.plot_lr()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"7b3022f0c8d9e68bc0267e35762cb03a1a9e34b0"},"cell_type":"code","source":"#learn.save('model2')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd9259e1cbfdcf1421eb0d4937449182fe281e06","collapsed":true},"cell_type":"code","source":"#learn.load('model2')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e1c6351c6447869b5a202e3aabe3e934c267abcf"},"cell_type":"markdown","source":"This model got  0.06917 log loss error on test set on Kaggle using resnet50  and 0.05605 with TTA resent50"},{"metadata":{"_uuid":"b316febc782d7e28525c5116ec34d2c1af2e4b72"},"cell_type":"markdown","source":"**Model 3**"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6496eb4ad6e3f3e31c69fcc543634d5c7596b8d4"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c776f413478eef2f6391e7a006d1e072de9c4c06"},"cell_type":"code","source":"lr=np.array([1e-4,1e-3,1e-2])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d84a43992c4021a799dd0bafe72ef4c7fd6a872"},"cell_type":"code","source":"learn.fit(lr, 3, cycle_len=1, cycle_mult=2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a6ada18e435abc23d08e008cc54962c1f4ed9fdd"},"cell_type":"markdown","source":"0.07477 log losss model 3"},{"metadata":{"trusted":true,"_uuid":"1d45797b213a0680fa9a07523345701403509b41","collapsed":true},"cell_type":"code","source":"#learn.sched.plot_lr()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"41e163f6a50968dfa24870743ae682424e699a60"},"cell_type":"code","source":"#learn.save('model3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f2bcd0d5f11de0ebe949b991452b9f7e2c651495"},"cell_type":"code","source":"??learn.TTA","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2175546f790706b685405764dd4a4d2b8c9027de","collapsed":true},"cell_type":"code","source":"log_predictions,y = learn.TTA(is_test=True)\nprob_predictions = np.mean(np.exp(log_predictions),0)\nprobs = prob_predictions[:,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"57120ba13b8582566eeaefb7688fb122bd22c4d4"},"cell_type":"code","source":"valid_preds= np.argmax(prob_predictions, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8803f6cbda302a075d1720e2e9a91c22b33134c7","collapsed":true},"cell_type":"markdown","source":"log_preds = learn.predict(is_test=True)\npreds = np.argmax(log_preds, axis=1) \nprobs = np.exp(log_preds[:,1])\n#probs= probs[:,1]\n#probs = [max(np.exp(i)[0],np.exp(i)[1]) for i in log_preds]"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"727dce4c4a8ed297ea48444d76d4672616256b0d"},"cell_type":"code","source":"#tmpk= log_preds\n#tmpk= log_preds[:,:,0]\n#tmpk=tmpk.reshape(tmpk.shape[1],tmpk.shape[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"d4c959494d3a46d418aee93342ccb1f091287b4b"},"cell_type":"code","source":"#tmpk= [np.mean(i) for i in tmpk]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b531ae17ef7e826efc33a926de5d7f6a3834d235"},"cell_type":"code","source":"#tmpk= [ np.exp(i) for i in tmpk]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"92df9c3aacb90d286473cc47b150d4d3ae4a3351","collapsed":true},"cell_type":"code","source":"log_predictions.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"0d6067e794f5658ee4b677941cd44c65dec570e8"},"cell_type":"code","source":"ids= fnames = np.array([f'{f}' for f in os.listdir(f'{PATH}test')])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba2b108ccba88d5d5c1e9b297de89f51e310162c","collapsed":true},"cell_type":"code","source":"ids= [i.replace(\".jpg\",\"\") for i in ids]\nids[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"552e955c1066ef1e669bec6f4817a2df5ff284f6","collapsed":true},"cell_type":"code","source":"ans= pd.DataFrame({\"id\":ids,\"label\":probs})\nans= ans.sort_values('id')\nans.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8cf4ea9f25e5b7eee13e6eb080a62951f1f14bc","collapsed":true},"cell_type":"code","source":"ans.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f5112bf70cdba7682b41415b17fda1b5a7a9930","collapsed":true},"cell_type":"code","source":"cm = confusion_matrix(y, valid_preds)\nplot_confusion_matrix(cm, data.classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f78aff411ee29c8f6e84d4fbf853bbc3b9eab981"},"cell_type":"code","source":"# model 2 mean 5.006981e-01   std  4.964828e-01    min  1.536870e-09   [0.025943222, 0.9912974683544303]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"cd94172cec17a4c0837e4e99df1721c951de2c61"},"cell_type":"code","source":"#model 1 mean 5.014179e-01   std 4.955358e-01     min 2.701442e-08     [0.026336912, 0.991495253164557]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c14b05429baf7b0f5e7f3af26ad1372ea81c5845"},"cell_type":"code","source":"ans.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"9f732e2185675654930cc5deae077bd29f2cfede"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}