{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-04T08:23:49.909863Z","iopub.execute_input":"2021-07-04T08:23:49.910212Z","iopub.status.idle":"2021-07-04T08:23:49.922312Z","shell.execute_reply.started":"2021-07-04T08:23:49.910136Z","shell.execute_reply":"2021-07-04T08:23:49.921403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\n\nimport fastai\nfrom fastai.vision.all import *\nfastai.__version__","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:25:19.824083Z","iopub.execute_input":"2021-07-04T08:25:19.824425Z","iopub.status.idle":"2021-07-04T08:25:19.831114Z","shell.execute_reply.started":"2021-07-04T08:25:19.824373Z","shell.execute_reply":"2021-07-04T08:25:19.830334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unzip File","metadata":{}},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"./\")\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"./\")","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:26:19.385175Z","iopub.execute_input":"2021-07-04T08:26:19.385625Z","iopub.status.idle":"2021-07-04T08:26:37.296915Z","shell.execute_reply.started":"2021-07-04T08:26:19.385584Z","shell.execute_reply":"2021-07-04T08:26:37.296006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check Data & Build Dataloaders","metadata":{}},{"cell_type":"code","source":"path = \"./train\"\nfiles = get_image_files(path)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:28:11.631668Z","iopub.execute_input":"2021-07-04T08:28:11.632024Z","iopub.status.idle":"2021-07-04T08:28:11.925394Z","shell.execute_reply.started":"2021-07-04T08:28:11.631994Z","shell.execute_reply":"2021-07-04T08:28:11.924491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(files)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:28:12.002611Z","iopub.execute_input":"2021-07-04T08:28:12.002901Z","iopub.status.idle":"2021-07-04T08:28:12.011326Z","shell.execute_reply.started":"2021-07-04T08:28:12.002873Z","shell.execute_reply":"2021-07-04T08:28:12.010445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files[:5]","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:28:12.386074Z","iopub.execute_input":"2021-07-04T08:28:12.386370Z","iopub.status.idle":"2021-07-04T08:28:12.392359Z","shell.execute_reply.started":"2021-07-04T08:28:12.386340Z","shell.execute_reply":"2021-07-04T08:28:12.391326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_name_re(\n        path=path,\n        fnames=files,\n        pat=r'(.+)\\.\\d+.jpg$',\n        valid_pct=0.2, \n        seed=13,\n        item_tfms=Resize(224))\n\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:31:59.751836Z","iopub.execute_input":"2021-07-04T08:31:59.752166Z","iopub.status.idle":"2021-07-04T08:32:02.495190Z","shell.execute_reply.started":"2021-07-04T08:31:59.752136Z","shell.execute_reply":"2021-07-04T08:32:02.494267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build CNN Model & Start Training","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, pretrained=True, metrics=[accuracy])","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:32:33.208775Z","iopub.execute_input":"2021-07-04T08:32:33.209207Z","iopub.status.idle":"2021-07-04T08:32:38.695507Z","shell.execute_reply.started":"2021-07-04T08:32:33.209165Z","shell.execute_reply":"2021-07-04T08:32:38.694673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:32:38.696929Z","iopub.execute_input":"2021-07-04T08:32:38.697285Z","iopub.status.idle":"2021-07-04T08:33:07.948328Z","shell.execute_reply.started":"2021-07-04T08:32:38.697248Z","shell.execute_reply":"2021-07-04T08:33:07.947448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(1, 1e-3)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:33:07.950311Z","iopub.execute_input":"2021-07-04T08:33:07.950824Z","iopub.status.idle":"2021-07-04T08:37:01.234668Z","shell.execute_reply.started":"2021-07-04T08:33:07.950778Z","shell.execute_reply":"2021-07-04T08:37:01.233827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:47:22.209755Z","iopub.execute_input":"2021-07-04T08:47:22.210094Z","iopub.status.idle":"2021-07-04T08:47:26.666767Z","shell.execute_reply.started":"2021-07-04T08:47:22.210056Z","shell.execute_reply":"2021-07-04T08:47:26.665914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(3, 7e-7)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:37:03.581469Z","iopub.execute_input":"2021-07-04T08:37:03.581818Z","iopub.status.idle":"2021-07-04T08:44:57.283692Z","shell.execute_reply.started":"2021-07-04T08:37:03.581777Z","shell.execute_reply":"2021-07-04T08:44:57.282804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate Model","metadata":{}},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:50:17.959528Z","iopub.execute_input":"2021-07-04T08:50:17.959860Z","iopub.status.idle":"2021-07-04T08:50:44.352405Z","shell.execute_reply.started":"2021-07-04T08:50:17.959825Z","shell.execute_reply":"2021-07-04T08:50:44.351416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:50:44.354021Z","iopub.execute_input":"2021-07-04T08:50:44.354339Z","iopub.status.idle":"2021-07-04T08:50:44.480964Z","shell.execute_reply.started":"2021-07-04T08:50:44.354306Z","shell.execute_reply":"2021-07-04T08:50:44.479956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(9,9))","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:53:25.637276Z","iopub.execute_input":"2021-07-04T08:53:25.637631Z","iopub.status.idle":"2021-07-04T08:53:26.388978Z","shell.execute_reply.started":"2021-07-04T08:53:25.637595Z","shell.execute_reply":"2021-07-04T08:53:26.388032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"learn.predict(files[1])","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:56:38.543832Z","iopub.execute_input":"2021-07-04T08:56:38.544190Z","iopub.status.idle":"2021-07-04T08:56:38.597641Z","shell.execute_reply.started":"2021-07-04T08:56:38.544156Z","shell.execute_reply":"2021-07-04T08:56:38.596562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T08:56:54.900822Z","iopub.execute_input":"2021-07-04T08:56:54.901158Z","iopub.status.idle":"2021-07-04T08:56:55.940227Z","shell.execute_reply.started":"2021-07-04T08:56:54.901128Z","shell.execute_reply":"2021-07-04T08:56:55.939281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}