{"cells":[{"metadata":{"_uuid":"3319353664822ff78324c3b119d64a8665ccb0fd"},"cell_type":"markdown","source":"# Building an image classifier\nMaterial from fast.ai lesson 1 (https://github.com/fastai/fastai/blob/master/courses/dl1/lesson1.ipynb), adapted to run on Kaggle kernels"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport torch\n\nfrom fastai.transforms import tfms_from_model\nfrom fastai.conv_learner import ConvLearner\nfrom fastai.model import resnet34\nfrom fastai.dataset import ImageClassifierData\nfrom fastai.plots import ImageModelResults","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"473a016f6fd0016b48905e2c72aa84af10dc88d6"},"cell_type":"code","source":"PATH = \"../input/\"\nTMP_PATH = \"/tmp/tmp\"\nMODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d33b91e1a2138654ae72116e869ca8152b633480","collapsed":true},"cell_type":"code","source":"torch.cuda.is_available()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2a61511bf12e1eb9e3858c16d82864783677441e","collapsed":true},"cell_type":"code","source":"fnames = np.array([f'train/{f}' for f in sorted(os.listdir(f'{PATH}train'))])\nlabels = np.array([(0 if 'cat' in fname else 1) for fname in fnames])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06694bacdac111a501875fd15aeaa0da5c5c5765","collapsed":true},"cell_type":"code","source":"labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5fcaf1f56e9c4714ac26656bbf698f26123cc8f2","collapsed":true},"cell_type":"code","source":"fnames","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bbb5fc768a4eff94a071dbb2de8f52feebac8fe2","collapsed":true},"cell_type":"code","source":"arch=resnet34\nsz=224","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"2b92fafdac903d5e018635e2eba5e310eb55b53c"},"cell_type":"code","source":"learning_rate = 0.01\ntraining_length = 2 # epochs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1c6352a42ce59f0dc3c118ddad28dfb5306d7e26"},"cell_type":"code","source":"data = ImageClassifierData.from_names_and_array(\n    path=PATH, \n    fnames=fnames, \n    y=labels, \n    classes=['dogs', 'cats'], \n    test_name='test', \n    tfms=tfms_from_model(arch, sz)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ed525ceb5ae4cafaaa0f6636843ff6affdd33ed","collapsed":true},"cell_type":"code","source":"learner = ConvLearner.pretrained(arch, data, precompute=True, tmp_name=TMP_PATH, models_name=MODEL_PATH)\nlearner.fit(learning_rate, training_length)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dc3c1cbab00e64b84b09784ae9611d7eed42a613"},"cell_type":"markdown","source":"# Checking our model's predictions"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"eab1a0b8febb19591871956a9a8bcee0cae2b16e"},"cell_type":"code","source":"# predictions for the validation set\nlog_preds = learner.predict()\n\n# a class that will help us plot our results\nresults = ImageModelResults(data.val_ds, log_preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"ae01453fe3618bab9fa1385228711190a792ade6"},"cell_type":"code","source":"cats = 0\ndogs = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9152dd4e82d8ae282d1ce74c1eed4853eb290a60","collapsed":true},"cell_type":"code","source":"results.plot_most_correct(cats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90a0aa3f3ecad6dbe1071d92c8635ed5f07e9bb7","collapsed":true},"cell_type":"code","source":"results.plot_most_correct(dogs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f63bcae037dfb4ac59c56ae56bd1de6f63d6258","collapsed":true},"cell_type":"code","source":"results.plot_most_incorrect(cats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29d196be37b84df05b3f40081622b75011b4c505","collapsed":true},"cell_type":"code","source":"results.plot_most_incorrect(dogs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de9445b4ae161db30e2e4ff41ac0b428a5198e5f","collapsed":true},"cell_type":"code","source":"results.plot_most_uncertain(cats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"2d35a653d61f6dca8851dfacedcc0f79ab4826e2"},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}