{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nfrom fastai.conv_learner import *\nimport os\n\n# Input data files are available in the \"../input/\" directory.\n# Any results you write to the current directory are saved as output.\n\nPATH = \"data/dogscats/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b960fcc6affa8d901b1df71eea0ada757ad0b0bb","collapsed":true},"cell_type":"code","source":"# Create FastAi friendly directory structure for training and test sets\n#\n# The Kaggle data looks like this:\n#\n# ../input/train/: 12500 cats.*.jpg images\n#                  12500 dogs.*.jpg images\n#  ../input/test/ : 12500 nnnnn.jpg images\n#\n# To keep it simple with the Fastai code below,\n# And also to add a Validation Set to the game (randomly chosen from the training set),\n# I'm creating the following data structure using symbolic links:\n#\n# data/dogscats/train/cats: 11500 cats.*.jpg images\n# data/dogscats/train/dogs: 11500 dogs.*.jpg images\n# data/dogscats/valid/cats: 1000 cats.*.jpg images\n# data/dogscats/valid/dogs: 1000 dogs.*.jpg images\n# data/dogscats/test1: 12500 nnnnn.jpg images\n#\n! rm -rf data\nos.makedirs(f'{PATH}train/cats')\nos.makedirs(f'{PATH}train/dogs')\nos.makedirs(f'{PATH}valid/cats')\nos.makedirs(f'{PATH}valid/dogs')\nos.makedirs(f'{PATH}test1')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"_uuid":"008e5af4fb269a7ee3f7ed865b729c26dde95a4c"},"cell_type":"code","source":"# Symbolic links for test images\nfor file in os.listdir('../input/test'):\n    os.symlink('/kaggle/input/test/' + file, '/kaggle/working/data/dogscats/test1/' + file)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"_uuid":"31d423c3cf593dc293de77e63619dacbd9a04d1f"},"cell_type":"code","source":"# Symbolic links for cats and dogs images from training set\nfor file in os.listdir('../input/train'):\n    if 'cat' in file:\n        os.symlink('/kaggle/input/train/' + file, '/kaggle/working/data/dogscats/train/cats/' + file)\n    elif 'dog' in file:\n        os.symlink('/kaggle/input/train/' + file, '/kaggle/working/data/dogscats/train/dogs/' + file)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c464c77ef587d4b255d66b0c2d01cb41c565f241"},"cell_type":"code","source":"# Need to move 1000 images from training set to validation set for cats and dogs\nimport random\nfor r in random.sample(range(12499), 1000):\n    os.rename(f'{PATH}train/cats/cat.{r}.jpg', f'{PATH}valid/cats/cat.{r}.jpg')\nfor r in random.sample(range(12499), 1000):\n    os.rename(f'{PATH}train/dogs/dog.{r}.jpg', f'{PATH}valid/dogs/dog.{r}.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9fb6737ec8c71091942a13c32739d1a067704534"},"cell_type":"code","source":"# Image size, batch size and pretrained model architecture\nsz=224\nbs=20\narch=resnet50","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d0af8db55408464e927a7a603a194e4b43ba32e"},"cell_type":"code","source":"tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)    # transformers of images (train and valid)\ndata = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=bs,                  # Read in images and their labels\n                                      trn_name='train', \n                                      test_name='test1', \n                                      val_name='valid')\nlearn = ConvLearner.pretrained(arch, data, precompute=False)                   # Prepare neural network layers","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80bcdd64276dffdd0d91bc368302c03d207cb9db"},"cell_type":"code","source":"learn.fit(0.01, 3, cycle_len=1)                                                # 3 training iterations with a fixed learning rate","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bcecbe82865cdad7cc284c0da4aae55fb46a34b4"},"cell_type":"code","source":"learn.unfreeze()                              # Unfreeze all layers\nlearn.bn_freeze(True)                         # Freeze batch normalization parameters (standard deviation & mean)\nlearn.fit(lrs=[1e-5,1e-4,1e-2], n_cycle=1,    # Stochastic gradient descent gradually changing learning rates\n          cycle_len=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6cbf8e9bda757dfcca72e6cebeabd6cddb2e7761"},"cell_type":"code","source":"log_preds,y = learn.TTA()                     # Predict with Test Time Augmentation (to validation images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"081959a2ac0785738dc848d1f22f64484c4dc271"},"cell_type":"code","source":"probs = np.mean(np.exp(log_preds),0)          # Probabilities, accurary and logistic loss\naccuracy_np(probs, y), metrics.log_loss(y, probs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b38dc89a116637b438ea2ea317dfc845e774f5e0"},"cell_type":"code","source":"# Get predictions from test set\nprediction = learn.predict(is_test=True)      # Get predictions from test set\npred_test = np.argmax(prediction, axis=1)     # From log scale to binary values\nlabel_probs = np.exp(prediction[:,1])         # From log scale to probabilities","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35cfcf95c247b1712f13d8a5965f9ceb9341f065"},"cell_type":"code","source":"# Create submission file: 2 colmuns with header id, label\nsubmission = pd.DataFrame({'id':os.listdir(f'{PATH}test1'), 'label':label_probs})\nsubmission['id'] = submission['id'].map(lambda x: x.split('.')[0])\nsubmission['id'] = submission['id'].astype(int)\nsubmission = submission.sort_values('id')\nsubmission.to_csv('../working/submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38b6a775362865960d0198ccf905442bb5ce2199"},"cell_type":"code","source":"# Clean up to prevent too many output files error message at commit\n! rm -rf data","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}