{"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":"\n\nimport numpy as np \nimport pandas as pd \n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-24T18:35:26.456089Z","iopub.execute_input":"2021-06-24T18:35:26.45642Z","iopub.status.idle":"2021-06-24T18:35:26.549175Z","shell.execute_reply.started":"2021-06-24T18:35:26.456371Z","shell.execute_reply":"2021-06-24T18:35:26.546954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:26.551206Z","iopub.execute_input":"2021-06-24T18:35:26.551806Z","iopub.status.idle":"2021-06-24T18:35:26.660864Z","shell.execute_reply.started":"2021-06-24T18:35:26.551738Z","shell.execute_reply":"2021-06-24T18:35:26.660149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision import *\nfrom fastai.widgets import *","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2021-06-24T18:35:26.661774Z","iopub.execute_input":"2021-06-24T18:35:26.662141Z","iopub.status.idle":"2021-06-24T18:35:26.758669Z","shell.execute_reply.started":"2021-06-24T18:35:26.662104Z","shell.execute_reply":"2021-06-24T18:35:26.757389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('/kaggle/input/')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:26.760171Z","iopub.execute_input":"2021-06-24T18:35:26.760427Z","iopub.status.idle":"2021-06-24T18:35:26.85987Z","shell.execute_reply.started":"2021-06-24T18:35:26.760379Z","shell.execute_reply":"2021-06-24T18:35:26.85811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path.ls()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:26.861708Z","iopub.execute_input":"2021-06-24T18:35:26.862091Z","iopub.status.idle":"2021-06-24T18:35:26.975967Z","shell.execute_reply.started":"2021-06-24T18:35:26.86202Z","shell.execute_reply":"2021-06-24T18:35:26.974613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_train = path/'train'\npath_test = path/'test'","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:26.977952Z","iopub.execute_input":"2021-06-24T18:35:26.978403Z","iopub.status.idle":"2021-06-24T18:35:27.081016Z","shell.execute_reply.started":"2021-06-24T18:35:26.978329Z","shell.execute_reply":"2021-06-24T18:35:27.07975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fname_train = get_image_files(path_train)\nfname_test = get_image_files(path_test)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.082576Z","iopub.execute_input":"2021-06-24T18:35:27.08296Z","iopub.status.idle":"2021-06-24T18:35:27.282037Z","shell.execute_reply.started":"2021-06-24T18:35:27.08289Z","shell.execute_reply":"2021-06-24T18:35:27.184836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fname_train[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.186033Z","iopub.status.idle":"2021-06-24T18:35:27.186616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(42)\npat = r'/([^/.]+).\\d+.jpg$'","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.187276Z","iopub.status.idle":"2021-06-24T18:35:27.187768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfms = get_transforms()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.188338Z","iopub.status.idle":"2021-06-24T18:35:27.189098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = (ImageList.from_folder(path_train)\n       .random_split_by_pct()\n       .label_from_re(pat)\n        .add_test_folder('../test')\n        .transform(tfms, size=224)\n        .databunch(bs=64, num_workers=0)\n        .normalize(imagenet_stats))","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.189789Z","iopub.status.idle":"2021-06-24T18:35:27.190244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(data.train_ds))\nprint(len(data.valid_ds))\nprint(len(data.test_ds))","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.19111Z","iopub.status.idle":"2021-06-24T18:35:27.192008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.classes","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.192952Z","iopub.status.idle":"2021-06-24T18:35:27.193917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.show_batch(rows=3, figsize=(10, 10))","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.19472Z","iopub.status.idle":"2021-06-24T18:35:27.195641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resnet34","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(data, models.resnet34, metrics=accuracy, model_dir='/kaggle/working/dogsVsCats')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.19679Z","iopub.status.idle":"2021-06-24T18:35:27.197356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(4)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.198301Z","iopub.status.idle":"2021-06-24T18:35:27.199082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('stage-1-resnet34-v1')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.199972Z","iopub.status.idle":"2021-06-24T18:35:27.200591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.load('stage-1-resnet34-v1');","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.20139Z","iopub.status.idle":"2021-06-24T18:35:27.202028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.202784Z","iopub.status.idle":"2021-06-24T18:35:27.203316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.204152Z","iopub.status.idle":"2021-06-24T18:35:27.205164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.206246Z","iopub.status.idle":"2021-06-24T18:35:27.206884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(4, max_lr=slice(1e-06, 1e-05))","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.20762Z","iopub.status.idle":"2021-06-24T18:35:27.2084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('stage-2-resnet34-v1')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.20931Z","iopub.status.idle":"2021-06-24T18:35:27.210071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_losses()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.210948Z","iopub.status.idle":"2021-06-24T18:35:27.211467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.212346Z","iopub.status.idle":"2021-06-24T18:35:27.212969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"losses,idxs = interp.top_losses()\nlen(data.valid_ds)==len(losses)==len(idxs)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.213421Z","iopub.status.idle":"2021-06-24T18:35:27.2137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(15,11))\n","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.214137Z","iopub.status.idle":"2021-06-24T18:35:27.214408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.load('stage-2-resnet34-v1');","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.214826Z","iopub.status.idle":"2021-06-24T18:35:27.215124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# More Transformations with Resnet34","metadata":{}},{"cell_type":"code","source":"data = (ImageList.from_folder(path_train)\n       .split_by_rand_pct()\n       .label_from_re(pat)\n        .add_test_folder('../test')\n        .transform(tfms=get_transforms(xtra_tfms=[contrast(scale=(0.1,2.0),p=0.9)]),\n                   size=224,\n                   padding_mode='border',\n                   resize_method=ResizeMethod.PAD)\n        .databunch(bs=64, num_workers=0)\n        .normalize(imagenet_stats))","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.215525Z","iopub.status.idle":"2021-06-24T18:35:27.215828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.show_batch(row=3, figsize=(7,8))","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.216293Z","iopub.status.idle":"2021-06-24T18:35:27.216576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(data.train_ds))\nprint(len(data.valid_ds))\nprint(len(data.test_ds))","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.217002Z","iopub.status.idle":"2021-06-24T18:35:27.217287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"leran = cnn_learner(data, models.resnet34, metrics=accuracy, model_dir='/kaggle/working/DogsVsCats')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.217687Z","iopub.status.idle":"2021-06-24T18:35:27.217987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(4)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.21838Z","iopub.status.idle":"2021-06-24T18:35:27.218646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('stage-1-resnet34-v2')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.219066Z","iopub.status.idle":"2021-06-24T18:35:27.219335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.219727Z","iopub.status.idle":"2021-06-24T18:35:27.220061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.220468Z","iopub.status.idle":"2021-06-24T18:35:27.220736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(4, max_lr=slice(3e-05, 1e-04))","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.221197Z","iopub.status.idle":"2021-06-24T18:35:27.221487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_losses()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.222026Z","iopub.status.idle":"2021-06-24T18:35:27.222754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('stage-2-resnet34-v2')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.223304Z","iopub.status.idle":"2021-06-24T18:35:27.223668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict using Version 1 model","metadata":{}},{"cell_type":"code","source":"learn.load('stage-2-resnet34-v1')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.224378Z","iopub.status.idle":"2021-06-24T18:35:27.22486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds, y = learn.get_preds(ds_type=DatasetType.Test)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.225471Z","iopub.status.idle":"2021-06-24T18:35:27.225826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_preds = preds[:,1]","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.226593Z","iopub.status.idle":"2021-06-24T18:35:27.226981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id':os.listdir('../input/test'), 'label':dog_preds})","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.227595Z","iopub.status.idle":"2021-06-24T18:35:27.228096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id'] = submission['id'].map(lambda x: x.split('.')[0])","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.228733Z","iopub.status.idle":"2021-06-24T18:35:27.229233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id'] = submission['id'].astype(int)\nsubmission = submission.sort_values('id')\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T18:35:27.229919Z","iopub.status.idle":"2021-06-24T18:35:27.230283Z"},"trusted":true},"execution_count":null,"outputs":[]}]}