{"cells":[{"metadata":{"hide_input":false,"_uuid":"d3cc5f53d048a3f32d77dc2693165c4e936aaefb"},"cell_type":"markdown","source":"# Easy Classification Model using FastAI\n\n## Let's try to identify wolf or husky images in an easy way. "},{"metadata":{"hide_input":false,"trusted":true,"_uuid":"0fc8e02561129b1ad3ffaf340b1c2a77a4d8860f"},"cell_type":"code","source":"# We import required lib's\nfrom fastai.vision import *\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a1cd824b4815825a8bfa4a381776f44c3171a967"},"cell_type":"code","source":"# nothing in our folder yet\nprint(os.listdir(\"../working\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a467c83856a0b9c46522a006a53a6864fef6ab20"},"cell_type":"code","source":"# move images to working folder\n!mkdir ../working/dog\n!mkdir ../working/wolf\n!cp ../input/dog-v1/* ../working/dog\n!cp ../input/wolf-v1/* ../working/wolf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b950d0c5c0532780e21a11df44f712ff0101c467"},"cell_type":"code","source":"# let's check\nprint(os.listdir(\"../working\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9e5bb059812f4b1bca9a2b34e71258ddd2b556a2"},"cell_type":"markdown","source":"Then we can remove any images that can't be opened:"},{"metadata":{"trusted":true,"_uuid":"fbd717032e9e7e2c2585acfcbcea3c4066737bf5"},"cell_type":"code","source":"verify_images('../working/wolf', delete=True, max_size=500)\nverify_images('../working/dog', delete=True, max_size=500)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"84647c34560d4c5a3b63fe92778e3becd21b61fa"},"cell_type":"markdown","source":"## View data"},{"metadata":{"trusted":true,"_uuid":"a81299b5c3618159dc5364794a58ee6433401da2"},"cell_type":"code","source":"classes = ['wolf','dog']\nnp.random.seed(42)\ndata = ImageDataBunch.from_folder('../working', train=\".\", valid_pct=0.2,\n        ds_tfms=get_transforms(), size=224, num_workers=4).normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"76a4370e4cb94da3239f87a945e04c291025e76b"},"cell_type":"markdown","source":"Good! Let's take a look at some of our pictures then."},{"metadata":{"trusted":true,"_uuid":"996ea4d4ab6cf8dd5c9bd56b73a273f94d13f354"},"cell_type":"code","source":"data.classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"813ab1720c531c05c779ec1d41876dcb9c6fbadf"},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"432366a66c72cfb7835fadbb8b000a1c58499327"},"cell_type":"code","source":"data.classes, data.c, len(data.train_ds), len(data.valid_ds)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"979c61028ab06267ed73eaba6e849cc2e18874e8"},"cell_type":"markdown","source":"## Train model"},{"metadata":{"trusted":true,"_uuid":"f96fd9736355772df6cc3c9caa7607a0ae505305"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet34, metrics=error_rate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5923aab56358d6970e4e8f6ead88651a38bccffd"},"cell_type":"code","source":"learn.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58dffb736ee0ad92a99cc425309d9c021b705950"},"cell_type":"code","source":"learn.save('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b00345c50e2141b1557934293826ca141bbd41fa"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90716508a3ebbb9cbf817b94d8ddef118e7efa1f"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"105ca57d68b2ecc1255d681e783ede942b1ae132"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"675fe8f056a28749f0fcc01f1747aea51218571c"},"cell_type":"code","source":"learn.fit_one_cycle(2, max_lr=slice(3e-5,3e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72594dea20160f3b57a0f3a279a16b940953305b"},"cell_type":"code","source":"learn.save('stage-2')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"43b88c286174e681074ae45d401c21ef7c177557"},"cell_type":"markdown","source":"## Interpretation"},{"metadata":{"trusted":true,"_uuid":"568e29b766a61ae38e54430adff849c426df50d7"},"cell_type":"code","source":"learn.load('stage-2');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9e58d49df3deabcf6d69fba9abc0f93301f2c9c"},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0ac8e04c295c0331361fcbf7cf45f368383cb3d"},"cell_type":"code","source":"interp.plot_confusion_matrix()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08fdfe394aff56d7b5ba2bb6577eb122b3d2c407"},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\nlosses,idxs = interp.top_losses()\nlen(data.valid_ds)==len(losses)==len(idxs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6bf9b7f3f4a0ede0e8a282656d3967d677d8ec6b"},"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e1b88df377f207a936652eb48250293805d83fee"},"cell_type":"markdown","source":"# Look at the images...\n# Is there something wrong ?"},{"metadata":{"trusted":true,"_uuid":"61b6cafa2a5b32321933434591865852a7cf43da"},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f3292ba7664678b63954696b8fdd22a5016219f"},"cell_type":"markdown","source":"# More info are published in §6.4 of this publication: https://www.kdd.org/kdd2016/papers/files/rfp0573-ribeiroA.pdf\n"},{"metadata":{"trusted":true,"_uuid":"d6ea056cb07ee43deeaf1c559c1027b44a7dd6df"},"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}