{"cells":[{"metadata":{"_uuid":"57ae45a6f217dc08aa03d59e105ae64c816331d8"},"cell_type":"markdown","source":"**Dog breed competition...**"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"e353fb4337cd1b7bd18fe50a2b93c636fe7223df"},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"20d0094bc69b501ae5cb7b1848190a4a25e6bf6e"},"cell_type":"markdown","source":"**Loading libraries**"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nfrom fastai.imports import *\nfrom fastai.torch_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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# here we specify  the path for our dataset and our global variables!! \nPATH = '../input/'\nsz = 224 \nbs = 58\narch = resnet50\nprint(os.listdir(PATH))\n# os.makedirs('../work')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24f2d0bcf407939b73a5c940266895b5e3a569fb"},"cell_type":"code","source":"#labels.. \nlabels_dir = f'{PATH}labels.csv'\nn = len(list(open(labels_dir))) -1 \nval_idxs = get_cv_idxs(n)\nlen(val_idxs)\nn","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"336d7735d3a2912f582d7dc38f64ab18e8b2e00c"},"cell_type":"markdown","source":"*now we read the csv file and then see what in in, and the count of each class*"},{"metadata":{"trusted":true,"_uuid":"e4b5de959cccd50dc3dffaecfc2e69ada2953001"},"cell_type":"code","source":"labels = pd.read_csv(labels_dir)\nlabels.head()\nlabels['breed'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3f8da5c83e084a1ae6829908d0212974209b498f"},"cell_type":"markdown","source":"***In order to visualize our data we need to do some preprocessing first ***"},{"metadata":{"trusted":true,"_uuid":"6d97f1e91a4f26ea304974320c1b2db37602d175","collapsed":true},"cell_type":"code","source":"tfms = tfms_from_model(arch, sz, aug_tfms = transforms_side_on, max_zoom = 1.1)\ndata = ImageClassifierData.from_csv(PATH, 'train', csv_fname = labels_dir,test_name = 'test', suffix = '.jpg',val_idxs = val_idxs,tfms=tfms, bs =bs)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa7f007f8dec123c9dbe905e556e3115d2ab0ba6"},"cell_type":"code","source":"file = PATH + data.trn_ds.fnames[1000]\nfile","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"526eeab705f2f46ed4fd286f8c8e653de2cbd52f"},"cell_type":"code","source":"img = PIL.Image.open(file) \nimg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"548056c5a6580677b41af81c7d09f73fdbe8b2d6"},"cell_type":"markdown","source":"**Now we create our model**\n   what we will do : \n        1. create the get data function\n        2. instantiate a learner object \n        3. find a good learning rate \n        4. train for 2-3 epochs \n        5. unfreeze and retrain with cycle_mult = 2 \n        6. increase size, freeze, step4,step5 \n        "},{"metadata":{"trusted":true,"_uuid":"a10c12b966e678394aef6d3c775f1233f77f2069","collapsed":true},"cell_type":"code","source":"def get_data(sz, bs):\n    tfms = tfms_from_model(arch, sz, aug_tfms = transforms_side_on, max_zoom = 1.1)\n    data = ImageClassifierData.from_csv(PATH, 'train', csv_fname = labels_dir,test_name = 'test', suffix = '.jpg',val_idxs = val_idxs,tfms=tfms, bs =bs)\n    if sz > 300:\n        return data\n    else:\n        data.resize(340, '/tmp')\n        return data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8e5553d5593a6aa1db2ee5d6d6e54cea8a106129"},"cell_type":"code","source":"TMP_PATH = \"/tmp/tmp\"\nMODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"91c9d41233d5bcb1f5d40d15bbf5b4078de00039","scrolled":true},"cell_type":"code","source":"data = get_data(sz, bs)\n# data = data.resize(int(sz*1.3), '/tmp')\nlearn = ConvLearner.pretrained(arch, data,tmp_name = TMP_PATH, models_name = MODEL_PATH, precompute = True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5080f00438f7abe56905ef6ce40de39aff68f6b4"},"cell_type":"code","source":"lrf = learn.lr_find()\nlearn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"002a0a30aa0353c4458ea98624ebc361c683184e"},"cell_type":"code","source":"%time learn.fit(1e-1,5,cycle_len=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5947abbdfa38f516e87a0f33f1a4f4756b49f40c"},"cell_type":"code","source":"learn.precompute = False\n%time learn.fit(1e-2,5,cycle_len=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"4e9694f3feca42c6c08eef50fce05b88c4d14621"},"cell_type":"code","source":"learn.save(\"224_pre\")\nlearn.load(\"224_pre\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f352435872c659be26fdaabffd8134ac9c49403"},"cell_type":"code","source":"sz = 299 \nlearn.set_data(get_data(sz,bs))\nlearn.freeze()\nlearn.fit(1e-2,3,cycle_len=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d72a74a1578bfe34e51ae1c092662b19d0c682b"},"cell_type":"code","source":"learn.fit(1e-2, 3, cycle_len = 1, cycle_mult = 2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca88a8a6036c6df56bffb4333e12fd6d961cafd1"},"cell_type":"code","source":"from sklearn import metrics\nlog_preds, y = learn.TTA() \nprobs = np.mean(np.exp(log_preds), 0)\naccuracy_np(probs, y), metrics.log_loss(y, probs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"23f1897896a1e42d2bc337a5a9e7df102998aadd"},"cell_type":"code","source":"learn.save(\"229_pre\")\nlearn.load(\"229_pre\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60da42a568e3fcb1d6d2212ff5984290fa719c7a"},"cell_type":"code","source":"log_preds, y = learn.TTA(is_test= True) \nprobs = np.mean(np.exp(log_preds), 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59bdaab5fd22ef5fd600b8a5482db1656083d2cf"},"cell_type":"code","source":"data.classes ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"293d97a9b8f6dd67513d8d3dac156d8c79f7946c"},"cell_type":"code","source":"import pandas as pd\ndf = pd.DataFrame(probs)\ndf.columns = data.classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"ced8afaf0e75f24e4c323ddd30be3909a384c154"},"cell_type":"code","source":"df.insert(0, 'id', [o[5:-4] for o in data.test_ds.fnames])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6dedebd26e08a193c68b09148b044be2cdddd2e6"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0b67d0e1a6b776d5cae36e336b7f5533a943d6d"},"cell_type":"code","source":"df.to_csv(\"Submit.csv\",index = False)\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bec54855e36519d89718ffb5c202950dbc16760c"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b4073ec910bff9132077316905ebdbe2282a069d"},"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}