{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from 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 *\n\nimport numpy as np\nimport pandas as pd\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8846a72a2b0f2b7a092b103b3d526e40132fe737"},"cell_type":"code","source":"os.listdir(\"../working\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f438c1c56a77d880d36987ab488c3c8f1234c38"},"cell_type":"code","source":"torch.cuda.set_device(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e379507f9cea5d1c09b9975ef9b8501d3c5b106a"},"cell_type":"code","source":"comp_name = \"dog_breed\"\ninput_path = \"../input/\"\nwd = \"/kaggle/working/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e6f8d8007625f014bdc54dee4142c8ab23b4d856"},"cell_type":"code","source":"def create_symlnk(src_dir, src_name, dst_name, dst_dir=wd, target_is_dir=False):\n    \"\"\"\n    If symbolic link does not already exist, create it by pointing dst_dir/lnk_name to src_dir/lnk_name\n    \"\"\"\n    if not os.path.exists(dst_dir + dst_name):\n        os.symlink(src=src_dir + src_name, dst = dst_dir + src_name, target_is_directory=target_is_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"522d8b0e0da450bb440d79cd578dab65bec7bcbd"},"cell_type":"code","source":"PATH = wd\nsz = 224\narch = resnext101_64\nbs = 58","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dde84820a8616145ba5a7a89f8ed34bbfb1cf711"},"cell_type":"code","source":"def clean_up(wd=wd):\n    \"\"\"\n    Delete all temporary directories and symlinks in working directory (wd)\n    \"\"\"\n    for root, dirs, files in os.walk(wd):\n        try:\n            for d in dirs:\n                if os.path.islink(d):\n                    os.unlink(d)\n                else:\n                    shutil.rmtree(d)\n            for f in files:\n                if os.path.islink(f):\n                    os.unlink(f)\n                else:\n                    print(f)\n        except FileNotFoundError as e:\n            print(e)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b43d8bf1fee4f4787d56efc2855a041216ee223b"},"cell_type":"code","source":"create_symlnk(input_path, \"train\", \"train\", target_is_dir=True)\ncreate_symlnk(input_path, \"test\", \"test\", target_is_dir=True)\ncreate_symlnk(input_path, \"labels.csv\", \"labels.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9471c68d944e0f29a18c85fcfa0837c943de6c31"},"cell_type":"code","source":"label_df = pd.read_csv(f\"{wd}labels.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8cea3253cc6c5f145fb3ed6e2055eca2f5efc7a9"},"cell_type":"code","source":"val_idxs = get_cv_idxs(label_df.shape[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3de08b35342fe82b36ca62468d48ab0eb2a84cd"},"cell_type":"code","source":"arch = resnet101\nsz = 224\nbs = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf5662e190c43a94c88ed89e9b70f3de6d3a6856"},"cell_type":"code","source":"val_idxs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"20dd07ccfadd91667c598dac8a16d477177d981d"},"cell_type":"code","source":"tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)\ndata = ImageClassifierData.from_csv(path=wd, folder=\"train\", csv_fname=f\"{wd}labels.csv\", tfms=tfms, val_idxs=val_idxs, suffix=\".jpg\", test_name=\"test\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b0110ca89a4b168764f8b417a367060759b7676"},"cell_type":"code","source":"learn = ConvLearner.pretrained(arch, data, precompute=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99bdae81bb26df54cdfa25519564d388bb39c0f7"},"cell_type":"code","source":"learn.fit(1e-2, 3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7e4ff40b69849c60d5126b729829d9e6dd36355"},"cell_type":"code","source":"from sklearn import metrics","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed294132eaa7d897c29574bf6933d56c2b14722a"},"cell_type":"code","source":"log_preds, y = learn.TTA(is_test=True) # use test dataset rather than validation dataset\nprobs = np.mean(np.exp(log_preds),0)\n#accuracy_np(probs, y), metrcs.log_loss(y, probs) # This does not make sense since test dataset has no labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa20f022744be87698eea660f807d6c467c2d065"},"cell_type":"code","source":"df = pd.DataFrame(probs)\ndf.columns = data.classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1181f432669b23eb57c3a01c1b431194fc426cd9"},"cell_type":"code","source":"df.insert(0, \"id\", [e[5:-4] for e in data.test_ds.fnames])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b546b64962cd6e3cd8639faf7c5e67e28c7b5e8"},"cell_type":"code","source":"df.to_csv(f\"sub_{comp_name}_{str(arch.__name__)}.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ad8197b50b03f366e71b477252155b7c6ab9dd0"},"cell_type":"code","source":"clean_up()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5d90580691e11c5ac8326d96ba263b22934d404"},"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.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}