{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\n\nimport numpy as np\nimport pandas as pd\n\nimport os\n\nfrom fastai import *\nfrom fastai.vision import *\n\nfrom fastai import __version__ as fastai_version\nprint(f\"fastai version: {fastai_version}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb172db4a62a5f927835c9640308cf7edf381857"},"cell_type":"code","source":"# input paths\ndata_path = Path(\"../input/train\")\nlabel_path =Path(\"../input/train_labels.csv\")\ntest_path = Path(\"../input/test\")\nmodels_path = Path(\"../models\")\n\n#output path\nsubmission_path = Path(\"submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7eeef8b6e78c3c5e353cd6acb46be95e85bb30e"},"cell_type":"code","source":"models_path.mkdir(exist_ok=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"911f3e81451eb605f7ab8b5d630057ce35665a69"},"cell_type":"code","source":"input_paths = [data_path, label_path, models_path,test_path]\nfor p in input_paths:\n    assert p.exists()\nelse:\n    print(\"OK, all input paths exist\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef669113dd4a2e4e10995bafddbe6f63ce8aff5c"},"cell_type":"code","source":"size = 32 #96 #224 \n\ntfms = get_transforms(flip_vert=True, max_rotate=180, max_lighting=0.1)\ndef crop_transform(img):\n    return crop(img, size)\ncrop_transform = Transform(crop_transform, order=99)\n# tfms = ([crop_transform()], [crop_transform()])\n# tfms[0][0]\n\ntfms = ([], []) # for debugging\n\n# tfms[0].append(crop_transform())\n# tfms[1].append(crop_transform())\ntfms","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06cf8796e31c6762b456f55ff3fd467bdd208f48"},"cell_type":"code","source":"import torch\nd = torch.cuda.current_device()\ntorch.cuda.get_device_name(d)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8276e604d53c2447441c72e90b7a4c6ddd2daaeb"},"cell_type":"code","source":"np.random.seed(42)\nimage_lists = (ImageList.from_csv(label_path.parent, folder=data_path, csv_name=label_path.name, suffix=\".tif\")\n    .random_split_by_pct(0.2)\n    .label_from_df(\"label\")\n    .add_test(ImageList.from_folder(test_path))\n    .transform(tfms)\n    )\nimage_lists","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"4c1e86d1b3b8361817299e5ec3ce09e1f8b5ba40"},"cell_type":"code","source":"bs = 2**6\nprint(f\"batch size: {bs}\")\n\nnp.random.seed(42)\ndata = (image_lists.databunch(bs=bs)\n    .normalize(imagenet_stats)\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e76152239bee339afd708cb175209dcb8ad9eaa"},"cell_type":"code","source":"# data.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bf6c47729b4dfea1cb4100c013584319e8b756d7"},"cell_type":"code","source":"# arch = models.resnet34\narch = models.resnet50\narch_name = arch.__name__\ndisplay(arch_name)\n\nlearn = create_cnn(data, arch, metrics=accuracy, path=models_path)\nlearn.save(f\"{arch_name}-stage-0\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3022b3c170fec55abec4fb502f978fad1601705","scrolled":false},"cell_type":"code","source":"learn.fit_one_cycle(4, 3e-3)\nlearn.save(f\"{arch_name}-stage-1\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74f42aeecbe5ac848d2b1d0e94b9231012740d26","scrolled":false},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07fd8e1468b8d5082bf4b44c9c251fec85b8a798"},"cell_type":"code","source":"# learn.load(\"resnet34-stage-1\")\nlearn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb3b5a5fd5ee12a174b4d00d7562694626d4ed0a"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07fd8e1468b8d5082bf4b44c9c251fec85b8a798"},"cell_type":"code","source":"learn.fit_one_cycle(2, slice(2e-6, 5e-5))\nlearn.save(f\"{arch_name}-stage-2\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"46c1a97ffd39454d8e6c6f4fe5abee8c8f3c2cf4"},"cell_type":"code","source":"learn.load(f\"{arch_name}-stage-2\")\nlearn.fit_one_cycle(2, slice(2e-6, 5e-5))\nlearn.save(f\"{arch_name}-stage-3\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9783023d9b07fff0d0fe0ad308c6b9ddd386af21"},"cell_type":"code","source":"learn.load(f\"{arch_name}-stage-3\")\nlearn.fit_one_cycle(2, slice(2e-6, 5e-5))\nlearn.save(f\"{arch_name}-stage-4\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f1d76a7ac4aad61b52068e1b6bf3a84cc86a569"},"cell_type":"code","source":"learn.load(f\"{arch_name}-stage-4\");\np_valid, r_valid = learn.get_preds(DatasetType.Valid)\nl_valid = p_valid.argmax(dim=1)\nacc = float((l_valid == r_valid).sum()) / len(l_valid)\nprint(f\"final accuracy: {acc}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"507e99769ab3a398dbb70ed07482ab4005d2e889"},"cell_type":"code","source":"data.classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"73f85cbc10b93eb7acb56d1ab73687dd5ae099e4"},"cell_type":"code","source":"probabilities, _ = learn.get_preds(DatasetType.Test)\nlabels = probabilities.argmax(dim=1)\n\nids = list(map(lambda p: p.stem, test_path.ls()))\n\nsubmission_df= pd.DataFrame(dict(id=ids, label=labels))\ndisplay(submission_df.head(20))\n\ndisplay(submission_df.describe())\n\nsubmission_df.to_csv(submission_path, index=False)\n\n%cat $submission_path | wc -l","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a529df4e300225a084814e93c2da79c5e494358b"},"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}