{"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-06-19T13:55:16.462743Z","iopub.execute_input":"2021-06-19T13:55:16.463098Z","iopub.status.idle":"2021-06-19T13:55:16.476390Z","shell.execute_reply.started":"2021-06-19T13:55:16.463023Z","shell.execute_reply":"2021-06-19T13:55:16.475422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import fastai\nfastai.__version__","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:17.761416Z","iopub.execute_input":"2021-06-19T13:55:17.761762Z","iopub.status.idle":"2021-06-19T13:55:17.777858Z","shell.execute_reply.started":"2021-06-19T13:55:17.761732Z","shell.execute_reply":"2021-06-19T13:55:17.777083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extracting Data","metadata":{}},{"cell_type":"code","source":"import zipfile","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:18.268499Z","iopub.execute_input":"2021-06-19T13:55:18.268833Z","iopub.status.idle":"2021-06-19T13:55:18.274116Z","shell.execute_reply.started":"2021-06-19T13:55:18.268803Z","shell.execute_reply":"2021-06-19T13:55:18.273120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with 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-06-19T13:55:18.460953Z","iopub.execute_input":"2021-06-19T13:55:18.461204Z","iopub.status.idle":"2021-06-19T13:55:35.745952Z","shell.execute_reply.started":"2021-06-19T13:55:18.461180Z","shell.execute_reply":"2021-06-19T13:55:35.745046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:35.747461Z","iopub.execute_input":"2021-06-19T13:55:35.747793Z","iopub.status.idle":"2021-06-19T13:55:38.565330Z","shell.execute_reply.started":"2021-06-19T13:55:35.747759Z","shell.execute_reply":"2021-06-19T13:55:38.564558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Validation","metadata":{}},{"cell_type":"code","source":"path = Path('./')\npath","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:38.567026Z","iopub.execute_input":"2021-06-19T13:55:38.567341Z","iopub.status.idle":"2021-06-19T13:55:38.576081Z","shell.execute_reply.started":"2021-06-19T13:55:38.567307Z","shell.execute_reply":"2021-06-19T13:55:38.575228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path.ls()","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:38.577812Z","iopub.execute_input":"2021-06-19T13:55:38.578233Z","iopub.status.idle":"2021-06-19T13:55:38.584808Z","shell.execute_reply.started":"2021-06-19T13:55:38.578194Z","shell.execute_reply":"2021-06-19T13:55:38.583686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fname = (path/\"train\").ls()[0]\nfname.name","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:38.586495Z","iopub.execute_input":"2021-06-19T13:55:38.586951Z","iopub.status.idle":"2021-06-19T13:55:38.750634Z","shell.execute_reply.started":"2021-06-19T13:55:38.586913Z","shell.execute_reply":"2021-06-19T13:55:38.749683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pattern = r'(.+)\\.\\d+.jpg$'","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:38.752003Z","iopub.execute_input":"2021-06-19T13:55:38.752345Z","iopub.status.idle":"2021-06-19T13:55:38.756081Z","shell.execute_reply.started":"2021-06-19T13:55:38.752309Z","shell.execute_reply":"2021-06-19T13:55:38.755069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\nre.findall(pattern, fname.name)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:38.757459Z","iopub.execute_input":"2021-06-19T13:55:38.757842Z","iopub.status.idle":"2021-06-19T13:55:38.768044Z","shell.execute_reply.started":"2021-06-19T13:55:38.757774Z","shell.execute_reply":"2021-06-19T13:55:38.767123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimage = Image.open(fname)\nimage.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:38.770226Z","iopub.execute_input":"2021-06-19T13:55:38.770611Z","iopub.status.idle":"2021-06-19T13:55:38.801473Z","shell.execute_reply.started":"2021-06-19T13:55:38.770559Z","shell.execute_reply":"2021-06-19T13:55:38.792627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building Data Loaders","metadata":{}},{"cell_type":"code","source":"dblock = DataBlock(\n    blocks = (ImageBlock, CategoryBlock),\n    get_items = get_image_files,\n    splitter= RandomSplitter(seed=42),\n    get_y = using_attr(RegexLabeller(pattern), 'name'),\n    item_tfms=Resize(248),\n    batch_tfms=aug_transforms(size=224, min_scale=0.75)                   \n)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:38.802767Z","iopub.execute_input":"2021-06-19T13:55:38.803091Z","iopub.status.idle":"2021-06-19T13:55:38.815163Z","shell.execute_reply.started":"2021-06-19T13:55:38.803059Z","shell.execute_reply":"2021-06-19T13:55:38.814235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataloaders = dblock.dataloaders(path/\"train\")","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:50.543122Z","iopub.execute_input":"2021-06-19T13:55:50.543489Z","iopub.status.idle":"2021-06-19T13:55:57.817983Z","shell.execute_reply.started":"2021-06-19T13:55:50.543459Z","shell.execute_reply":"2021-06-19T13:55:57.817165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataloaders.show_batch(nrows=3, ncols=5)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:55:59.150698Z","iopub.execute_input":"2021-06-19T13:55:59.151008Z","iopub.status.idle":"2021-06-19T13:56:00.274043Z","shell.execute_reply.started":"2021-06-19T13:55:59.150979Z","shell.execute_reply":"2021-06-19T13:56:00.273225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building & Training","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dataloaders, resnet50, metrics=error_rate)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:56:00.938944Z","iopub.execute_input":"2021-06-19T13:56:00.939289Z","iopub.status.idle":"2021-06-19T13:56:06.946664Z","shell.execute_reply.started":"2021-06-19T13:56:00.939259Z","shell.execute_reply":"2021-06-19T13:56:06.945823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(1)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:56:15.177429Z","iopub.execute_input":"2021-06-19T13:56:15.177791Z","iopub.status.idle":"2021-06-19T13:56:21.281184Z","shell.execute_reply.started":"2021-06-19T13:56:15.177758Z","shell.execute_reply":"2021-06-19T13:56:21.276591Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Results Validation","metadata":{}},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix(figsize=(12,12), dpi=60)","metadata":{"execution":{"iopub.status.busy":"2021-06-18T08:22:13.140979Z","iopub.execute_input":"2021-06-18T08:22:13.141362Z","iopub.status.idle":"2021-06-18T08:22:43.36963Z","shell.execute_reply.started":"2021-06-18T08:22:13.141327Z","shell.execute_reply":"2021-06-18T08:22:43.368364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-06-18T08:22:47.724433Z","iopub.execute_input":"2021-06-18T08:22:47.724783Z","iopub.status.idle":"2021-06-18T08:23:15.815093Z","shell.execute_reply.started":"2021-06-18T08:22:47.724749Z","shell.execute_reply":"2021-06-18T08:23:15.814076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = (path/\"test1\")","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:56:30.685280Z","iopub.execute_input":"2021-06-19T13:56:30.685639Z","iopub.status.idle":"2021-06-19T13:56:30.689582Z","shell.execute_reply.started":"2021-06-19T13:56:30.685607Z","shell.execute_reply":"2021-06-19T13:56:30.688633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = get_image_files(test_images)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:56:46.140925Z","iopub.execute_input":"2021-06-19T13:56:46.141226Z","iopub.status.idle":"2021-06-19T13:56:46.388275Z","shell.execute_reply.started":"2021-06-19T13:56:46.141200Z","shell.execute_reply":"2021-06-19T13:56:46.387447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images[0]","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:56:56.021979Z","iopub.execute_input":"2021-06-19T13:56:56.022307Z","iopub.status.idle":"2021-06-19T13:56:56.028619Z","shell.execute_reply.started":"2021-06-19T13:56:56.022278Z","shell.execute_reply":"2021-06-19T13:56:56.027644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst_dl = dataloaders.test_dl(images)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:57:47.993194Z","iopub.execute_input":"2021-06-19T13:57:47.993533Z","iopub.status.idle":"2021-06-19T13:57:48.002559Z","shell.execute_reply.started":"2021-06-19T13:57:47.993487Z","shell.execute_reply":"2021-06-19T13:57:48.001516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst_dl.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:57:55.356793Z","iopub.execute_input":"2021-06-19T13:57:55.357105Z","iopub.status.idle":"2021-06-19T13:57:56.170040Z","shell.execute_reply.started":"2021-06-19T13:57:55.357076Z","shell.execute_reply":"2021-06-19T13:57:56.169129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p = learn.get_preds(dl=tst_dl)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:58:17.077495Z","iopub.execute_input":"2021-06-19T13:58:17.077836Z","iopub.status.idle":"2021-06-19T13:59:11.288318Z","shell.execute_reply.started":"2021-06-19T13:58:17.077807Z","shell.execute_reply":"2021-06-19T13:59:11.287499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(p)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:59:11.290045Z","iopub.execute_input":"2021-06-19T13:59:11.290399Z","iopub.status.idle":"2021-06-19T13:59:11.297869Z","shell.execute_reply.started":"2021-06-19T13:59:11.290361Z","shell.execute_reply":"2021-06-19T13:59:11.296866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p[0]","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:59:36.403376Z","iopub.execute_input":"2021-06-19T13:59:36.403732Z","iopub.status.idle":"2021-06-19T13:59:36.421515Z","shell.execute_reply.started":"2021-06-19T13:59:36.403702Z","shell.execute_reply":"2021-06-19T13:59:36.420539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p[1]","metadata":{"execution":{"iopub.status.busy":"2021-06-19T13:59:40.720049Z","iopub.execute_input":"2021-06-19T13:59:40.720354Z","iopub.status.idle":"2021-06-19T13:59:40.724074Z","shell.execute_reply.started":"2021-06-19T13:59:40.720324Z","shell.execute_reply":"2021-06-19T13:59:40.723123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_paths = list(image_paths)","metadata":{"execution":{"iopub.status.busy":"2021-06-18T08:55:39.75953Z","iopub.execute_input":"2021-06-18T08:55:39.759878Z","iopub.status.idle":"2021-06-18T08:55:39.854598Z","shell.execute_reply.started":"2021-06-18T08:55:39.759845Z","shell.execute_reply":"2021-06-18T08:55:39.853643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-06-18T08:50:30.683098Z","iopub.execute_input":"2021-06-18T08:50:30.683426Z","iopub.status.idle":"2021-06-18T08:50:30.692391Z","shell.execute_reply.started":"2021-06-18T08:50:30.683395Z","shell.execute_reply":"2021-06-18T08:50:30.69114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"??learn.get_preds","metadata":{"execution":{"iopub.status.busy":"2021-06-18T08:46:32.794876Z","iopub.execute_input":"2021-06-18T08:46:32.795249Z","iopub.status.idle":"2021-06-18T08:46:32.819711Z","shell.execute_reply.started":"2021-06-18T08:46:32.795218Z","shell.execute_reply":"2021-06-18T08:46:32.81819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"raw","source":"results = list()","metadata":{"execution":{"iopub.status.busy":"2021-06-18T08:54:53.065486Z","iopub.execute_input":"2021-06-18T08:54:53.06583Z","iopub.status.idle":"2021-06-18T08:54:53.073589Z","shell.execute_reply.started":"2021-06-18T08:54:53.065797Z","shell.execute_reply":"2021-06-18T08:54:53.07254Z"}}},{"cell_type":"code","source":"for index in range(len(image_paths)):\n    result = dict()\n    im_path = image_paths[index]\n    preds = learn.predict(im_path)[0]\n    result['id'] = im_path\n    result['target'] = preds  \n    results.append(result)","metadata":{"execution":{"iopub.status.busy":"2021-06-18T08:58:56.33517Z","iopub.execute_input":"2021-06-18T08:58:56.335504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}