{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"\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\nimport glob\nimport shutil\nimport os\nimport gc\nimport pathlib\nprint(os.listdir(\"../input/\"))\n%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# ! pip freeze\n# !pip install torchvision fastai==0.6","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"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},"cell_type":"code","source":"torch.cuda.is_available()\ntorch.backends.cudnn.enabled","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dog_indexes = []\ncat_indexes = []\ncount = 0\ncat_count = 1\ndog_count = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile\npath_to_zip_file=\"../input/dogs-vs-cats-redux-kernels-edition/test.zip\"\ndirectory_to_extract_to=\"../output/data\"\nwith zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:\n    zip_ref.extractall(directory_to_extract_to)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile\npath_to_zip_file=\"../input/dogs-vs-cats-redux-kernels-edition/train.zip\"\ndirectory_to_extract_to=\"../output/data\"\nwith zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:\n    zip_ref.extractall(directory_to_extract_to)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for name in os.listdir('../output/data/train/'):\n    if 'cat' in name:\n        cat_indexes.append(name.split('.')[1])\n    if 'dog' in name:\n        dog_indexes.append(name.split('.')[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.makedirs('../working/dogcats/valid/cats/')\nos.makedirs('../working/dogcats/valid/dogs/')\nos.makedirs('../working/dogcats/train/cats/')\nos.makedirs('../working/dogcats/train/dogs/')\nos.makedirs('../working/dogcats/test/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir = \"../output/data/train/\"\ntest_dir = \"../output/data/test/\"\ncat_train_dir = \"../working/dogcats/train/cats/\"\ncat_valid_dir = \"../working/dogcats/valid/cats/\"\ndog_train_dir = \"../working/dogcats/train/dogs/\"\ndog_valid_dir = \"../working/dogcats/valid/dogs/\"\ndogcats_test = \"../working/dogcats/test/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH = \"../working/dogcats/\"\nsz=224","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for jpgfile in iglob(os.path.join(train_dir, \"cat*.jpg\")):\n    if jpgfile.split('.')[3] in cat_val_list:\n        shutil.copy(jpgfile, cat_valid_dir)\n    else:\n        shutil.copy(jpgfile, cat_train_dir)\n\nfor jpgfile in iglob(os.path.join(train_dir, \"dog*.jpg\")):\n    if jpgfile.split('.')[3] in dog_val_list:\n        shutil.copy(jpgfile, dog_valid_dir)\n    else:\n        shutil.copy(jpgfile, dog_train_dir)\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for jpgfile in iglob(os.path.join(test_dir, \"*.jpg\")):\n    shutil.copy(jpgfile, dogcats_test)\n    \ncache_dir = os.path.expanduser(os.path.join('~', '.torch'))\nif not os.path.exists(cache_dir):\n    os.makedirs(cache_dir)\nmodels_dir = os.path.join(cache_dir, 'models')\nif not os.path.exists(models_dir):\n    os.makedirs(models_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nos.makedirs('/tmp/.torch/models/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp ../input/resnet34/resnet34.pth /tmp/.torch/models/resnet34-333f7ec4.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# data aaugmentation\ntfms = tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1)\n\n# transfer learning\n\narch=resnet34\ndata = ImageClassifierData.from_paths(PATH, tfms=tfms, test_name='test')\nlearn = ConvLearner.pretrained(arch, data, precompute=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# begin training\n\nlearn.fit(1e-2, 1)\nlearn.precompute=False\nlearn.fit(1e-2, 3, cycle_len=1)\nlearn.sched.plot_lr()\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.unfreeze()\nlr=np.array([1e-4,1e-3,1e-2])\nlearn.fit(lr, 3, cycle_len=1, cycle_mult=2)\nlearn.sched.plot_lr()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.sched.plot_lr()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"log_preds,y = learn.TTA()\nprobs = np.mean(np.exp(log_preds),0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"accuracy_np(probs, y)\npreds = np.argmax(probs, axis=1)\nprobs = probs[:,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test prediction\ntemp = learn.predict(is_test=True)\ntemp.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_test = np.argmax(temp, axis=1)\nprobs = np.exp(temp[:,1])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# submission file\nsubmission = pd.DataFrame({'id':os.listdir(f'{PATH}test'), 'label':probs})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! rm -rf ../working/dogcats/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission['id'] = submission['id'].map(lambda x: x.split('.')[0])\nsubmission['id'] = submission['id'].astype(int)\nsubmission = submission.sort_values('id')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('../working/output.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}