{"cells":[{"metadata":{"trusted":true,"_uuid":"78f3feec13da35b152d4c512cf7516e9f5e97bcc"},"cell_type":"code","source":"__author__ = \"imflash217\"\n__copyright__ = \"FlashAI Labs, 2019\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1b8632dc52628bc2baba8153317a0cd1da1bad3b"},"cell_type":"code","source":"# magic commands\n%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2a470913b7f38748b3cb9a804a371075ae86c741"},"cell_type":"code","source":"# libraries\nimport os\nimport numpy as np\nimport pandas as pd\nimport matplotlib \nimport scipy\nimport torch\nfrom fastai.vision import *\nfrom fastai.metrics import error_rate\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6ff408491f3717d2030ecdc9281a694a68f373db"},"cell_type":"code","source":"# setting paths\nx_path = untar_data(URLs.PETS)\nprint(x_path)\nprint(x_path.ls())                               #support function by fastai library to list items in a path\n\nx_path_annotations = x_path/'annotations'\nx_path_images = x_path/'images'","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"collapsed":true,"_uuid":"757ae54ef3cea4883ce57279b1523f169a2aadee"},"cell_type":"code","source":"# exploring the data\nx_fnames = get_image_files(x_path_images)\nprint(len(x_fnames))\nprint(type(x_fnames))\nx_fnames[0:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ebbfd770dab91661d8d31d4af2e2ddd3f3abe4b0"},"cell_type":"code","source":"# creating the databunch for training\nnp.random.seed(217)                      # setting random seed for numpy\nx_pattern = r'/([^/]+)_\\d+.jpg$'         # regex pattern\nx_batch_size = 6                           # batch size for loading data for training\n\n# creating databunch\nx_data = ImageDataBunch.from_name_re(x_path_images, x_fnames, x_pattern, ds_tfms=get_transforms(), size=224, bs=x_batch_size)\\\n         .normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"x_data.show_batch(rows=2, figsize=(7,6))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"03e774369e96df523c6374021e2c9d7c39466141"},"cell_type":"code","source":"print(x_data.classes)\nprint(type(x_data.classes))\nprint(len(x_data.classes), x_data.c)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1c0fe0388ab263a6f08850962c915b4e6c8cdd88"},"cell_type":"code","source":"# Transfer Learning from pretrained architecture\nx_learn = create_cnn(data=x_data, arch=models.resnet34, metrics=error_rate)\nprint(x_learn.model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8fa445c277ea32b7b47f3fcf2d72a57eb1f25463"},"cell_type":"code","source":"# Training the network\nx_learn.fit_one_cycle(cyc_len=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0da5709b6f0f21961da96eb266dce53853983ee2"},"cell_type":"code","source":"# saving the trained model\nx_learn.save('stage1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f721bda6f654fe81b7d978e1c73fccf21473ee55"},"cell_type":"code","source":"x_interp = ClassificationInterpretation.from_learner(x_learn)\nx_losses, x_idxs = x_interp.top_losses()\nprint(len(x_data.valid_ds)==len(x_losses)==len(x_idxs))\nx_interp.plot_top_losses(k=9, figsize=(15,15))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"e1088b98beb764ef7a493706b9ed267a4901e1b3"},"cell_type":"code","source":"x_interp.plot_confusion_matrix(figsize=(10,10), dpi=90)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"670de6363a010c9e78973599d70186876d647d6c"},"cell_type":"code","source":"x_interp.most_confused(min_val=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b432cd4dc9fd9f6a8038bef907b9f3466a70d5e4"},"cell_type":"code","source":"x_learn.unfreeze()                    # unfreezing our model to train it a bit more\nx_learn.fit_one_cycle(cyc_len=2)      # training the unfrozen model for another 2 epochs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85710ade65e90d1330490b10fc5ec04acb2fc729"},"cell_type":"code","source":"x_learn.load('stage1');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"970cb32049f2e52369c6ccb42fcc10340bb2a833"},"cell_type":"code","source":"x_learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a53801e9ec6503d8bcad15d81ce25b5196eaed7e"},"cell_type":"code","source":"x_learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d819ca849f7d24e6c8463e7d8285601e3551ee3"},"cell_type":"code","source":"x_learn.unfreeze()\nx_learn.fit_one_cycle(cyc_len=2, max_lr=slice(1e-6,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9fd4231e250574e1e361ef108759dbfabd3bcc9"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a388ddb2c622fc5ee9f2bafc1301e2778219602"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1aad45ffed8090e6ce448062fae7113e1d6bf55d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4242d301c67078b935f96094d8e6074d639fadc7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f0187bdc4080c8c1684f91c62cf8c9fbc03bec25"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"049579dbc4304e199e0dddbfba67813f118ce14e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bf61248ea432db7f571133e942bed7fc03aed15b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"212d2eedca74bff789c6ef58fd8518a7c0b41437"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"f87c07cf814f4682e7a03088cce1bce40ced6529"},"cell_type":"code","source":"help(x_data.c)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c40d774ce08f66f6af23659eb172c4646d853c16"},"cell_type":"code","source":"ImageDataBunch??","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"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}