{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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 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","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# This file contains all the main external libs we'll use\nfrom fastai.imports import *\n\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 *\n\nfrom PIL import Image\n\nPATH = \"./data/\"\nsz=299 #initial training image size\narch = resnet34 #image architecture developed by Facebook AI team #https://github.com/facebookresearch/ResNeXt\nbs = 28 #batch size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ca13e0e89ac81d873d6c1612a32e626f22b7801"},"cell_type":"code","source":"!cp -r ../input/dogsandcats/dogscats_small/dogscats_small ./data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbb8b1d8605c8c3d37d5c2acff084142fb72955c"},"cell_type":"code","source":"!ls {PATH}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"082906a6acccb2a6c1e900eec2bb177d6de51a03"},"cell_type":"code","source":"torch.cuda.is_available()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eac6de81de760afb44072692d3d3ce7f28af4c9b"},"cell_type":"code","source":"torch.backends.cudnn.enabled","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ebefd57585c488cdece7d80b95c14bbd0e2e0ac"},"cell_type":"code","source":"!ls {PATH}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50693c931c8265329d2ba568de17ef374d3ebdd6"},"cell_type":"code","source":"#Setup image transformation\ntfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)\n\n#Transform data\ndata = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=bs, num_workers=4)\n\n#Load pretrain model\nlearn = ConvLearner.pretrained(arch, data, precompute=True, ps=0.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10f2436d035a12facdcb9de9971c130a97d7208c"},"cell_type":"code","source":"#training with 0.001 as learning rate and 1 cycle\nlearn.fit(1e-2, 1)\n\n#trun off precompute\nlearn.precompute=False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15d029017576dc440e9ada0142411d416d0ab4c4"},"cell_type":"code","source":"#cycle_len?\nlearn.fit(1e-2, 2, cycle_len=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbdd82fd78ae3acf1e65ee0be05f3364dda79673"},"cell_type":"code","source":"#unfreeze pretrained layers so that can be trained. Explanation?\nlearn.unfreeze()\n\n#setting different learning rates\nlr=np.array([1e-4,1e-3,1e-2])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"2ca2fb90de1cb49f8584674c7367ee2f3d4ca96d"},"cell_type":"code","source":"#More learning(training), this time allow pretrained layers to be trained\nlearn.fit(lr, 3, cycle_len=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8fcc12d6cff8087c44e6872a9a4803c00ef622e2"},"cell_type":"code","source":"#Explain TTA time test augmentation\n\n#preicted results are provided as log of the probablity of each, y is the actual label of the images\nlog_preds,y = learn.TTA()\n\n#taking the averge of predictionas generated by TTA\nprobs = np.mean(np.exp(log_preds),0)\n\n#calculate accuracy\naccuracy_np(probs,y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"196ac476b4305fc785ac56fb78753d6d7b922806"},"cell_type":"code","source":"preds = np.argmax(probs, axis=1)\nprobs = probs[:,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ea04c4f0f479facd649e338031b8d4b383c0843"},"cell_type":"code","source":"def rand_by_mask(mask): return np.random.choice(np.where(mask)[0], 4, replace=False)\ndef rand_by_correct(is_correct): return rand_by_mask((preds == data.val_y)==is_correct)\n\ndef plot_val_with_title(idxs, title):\n    imgs = np.stack([data.val_ds[x][0] for x in idxs])\n    title_probs = [probs[x] for x in idxs]\n    print(title)\n    return plots(data.val_ds.denorm(imgs), rows=1, titles=title_probs)\n\ndef plots(ims, figsize=(12,6), rows=1, titles=None):\n    f = plt.figure(figsize=figsize)\n    for i in range(len(ims)):\n        sp = f.add_subplot(rows, len(ims)//rows, i+1)\n        sp.axis('Off')\n        if titles is not None: sp.set_title(titles[i], fontsize=16)\n        plt.imshow(ims[i])\n\n# def load_img_id(ds, idx, path = PATH): return np.array(PIL.Image.open(PATH+ds.fnames[idx]))\ndef load_img_id(ds, idx, path = PATH): return np.array(Image.open(PATH+ds.fnames[idx]))\n\ndef plot_val_with_title(idxs, title):\n    imgs = [load_img_id(data.val_ds,x) for x in idxs]\n    title_probs = [probs[x] for x in idxs]\n    print(title)\n    return plots(imgs, rows=1, titles=title_probs, figsize=(16,8))\n\ndef most_by_mask(mask, mult):\n    idxs = np.where(mask)[0]\n    return idxs[np.argsort(mult * probs[idxs])[:4]]\n\ndef most_by_correct(y, is_correct): \n    mult = -1 if (y==1)==is_correct else 1\n    return most_by_mask((preds == data.val_y)==is_correct & (data.val_y == y), mult)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8450a7d097d997107bd65837265c6f701b9c3cd"},"cell_type":"code","source":"plot_val_with_title(rand_by_correct(True),\"Correctly classified\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2931fb43de9eacd8beb3c568405e472bd117d7b9"},"cell_type":"code","source":"ls data/test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3498373033eb43e51762c5a1d4f434b5e4e0f207"},"cell_type":"code","source":"!wget https://images.freeimages.com/images/large-previews/466/cat-1401781.jpg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35a320103ae3295f5c1050bdb8ca249e61494d44"},"cell_type":"code","source":"test_file = f'{PATH}test/20.jpg'\nImage.open(test_file)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"223b8d4891a539cc363ec7631a5d651915c25eb3"},"cell_type":"code","source":"trn_tfms, val_tfms = tfms_from_model(arch, sz) #get transformations\nim = open_image(test_file) #f'{PATH}test/35.jpg'\nim = val_tfms(im)\nlearn.precompute=False \npreds = learn.predict_array(im[None])\nnp.argmax(preds)\n# 0 for cat, 1 for dog\n","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}