{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":20479,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Dog Breed classification with fast ai\n\nThe fastai library simplifies training fast and accurate neural nets using modern best practices. This library is built on top of Pytorch. You can more about fastai by visiting this [link](https://docs.fast.ai/index.html).  \n","metadata":{"_uuid":"67b1eb93d654f93e44d8f3db5482e0df0bff824f"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom fastai.vision import *\nfrom fastai.metrics import error_rate, accuracy\n\nprint(os.listdir(\"../input\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-14T10:35:35.691642Z","iopub.execute_input":"2024-03-14T10:35:35.692007Z","iopub.status.idle":"2024-03-14T10:35:36.876422Z","shell.execute_reply.started":"2024-03-14T10:35:35.691946Z","shell.execute_reply":"2024-03-14T10:35:36.875474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2024-03-14T10:35:36.877778Z","iopub.execute_input":"2024-03-14T10:35:36.878257Z","iopub.status.idle":"2024-03-14T10:35:36.928698Z","shell.execute_reply.started":"2024-03-14T10:35:36.878205Z","shell.execute_reply":"2024-03-14T10:35:36.928097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = \"../input/\"\nMODEL_PATH = \"/tmp/model/\"","metadata":{"_uuid":"627f59e6afdcd7a46421abe1bb62d886580715be","execution":{"iopub.status.busy":"2024-03-14T10:35:36.929948Z","iopub.execute_input":"2024-03-14T10:35:36.930486Z","iopub.status.idle":"2024-03-14T10:35:36.969174Z","shell.execute_reply.started":"2024-03-14T10:35:36.930427Z","shell.execute_reply":"2024-03-14T10:35:36.967817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/labels.csv')\ndf.head(10)","metadata":{"_uuid":"3a7c648270c6671d81ae18e7b0ad1e4eb1f77b57","execution":{"iopub.status.busy":"2024-03-14T10:35:36.974773Z","iopub.execute_input":"2024-03-14T10:35:36.977202Z","iopub.status.idle":"2024-03-14T10:35:37.210679Z","shell.execute_reply.started":"2024-03-14T10:35:36.975170Z","shell.execute_reply":"2024-03-14T10:35:37.209654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df['breed'].value_counts().sort_values(ascending=False))","metadata":{"_uuid":"3924248a22ae70d4c56b164cfff5d3dfacf5bfb2","execution":{"iopub.status.busy":"2024-03-14T10:35:37.212256Z","iopub.execute_input":"2024-03-14T10:35:37.212571Z","iopub.status.idle":"2024-03-14T10:35:37.259257Z","shell.execute_reply.started":"2024-03-14T10:35:37.212512Z","shell.execute_reply":"2024-03-14T10:35:37.258305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = ImageDataBunch.from_csv(PATH, folder='train', test='test', suffix='.jpg', ds_tfms=get_transforms(max_rotate=25),\n                               csv_labels='labels.csv', fn_col=0, label_col=1, \n                               size=128, bs=64).normalize(imagenet_stats)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:35:37.260622Z","iopub.execute_input":"2024-03-14T10:35:37.261100Z","iopub.status.idle":"2024-03-14T10:35:41.197314Z","shell.execute_reply.started":"2024-03-14T10:35:37.261049Z","shell.execute_reply":"2024-03-14T10:35:41.196522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.show_batch(rows=4, figsize=(12,8))","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:35:41.198338Z","iopub.execute_input":"2024-03-14T10:35:41.198734Z","iopub.status.idle":"2024-03-14T10:35:47.682175Z","shell.execute_reply.started":"2024-03-14T10:35:41.198675Z","shell.execute_reply":"2024-03-14T10:35:47.681333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.classes)\nlen(data.classes), data.c","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:35:47.683621Z","iopub.execute_input":"2024-03-14T10:35:47.683907Z","iopub.status.idle":"2024-03-14T10:35:47.725105Z","shell.execute_reply.started":"2024-03-14T10:35:47.683842Z","shell.execute_reply":"2024-03-14T10:35:47.724305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"LR of learner","metadata":{}},{"cell_type":"markdown","source":"based on the graph plottings we have the lr parameters for fine ","metadata":{}},{"cell_type":"markdown","source":"**Resnet50 model**","metadata":{}},{"cell_type":"code","source":"data = ImageDataBunch.from_csv(PATH, folder='train', test='test', suffix='.jpg', ds_tfms=get_transforms(max_rotate=25),\n                               csv_labels='labels.csv', fn_col=0, label_col=1, \n                               size=224, bs=16).normalize(imagenet_stats)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:35:47.726181Z","iopub.execute_input":"2024-03-14T10:35:47.726471Z","iopub.status.idle":"2024-03-14T10:35:48.293596Z","shell.execute_reply.started":"2024-03-14T10:35:47.726416Z","shell.execute_reply":"2024-03-14T10:35:48.292904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.show_batch(rows= 4, figsize=(12,8))","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:35:48.294850Z","iopub.execute_input":"2024-03-14T10:35:48.295325Z","iopub.status.idle":"2024-03-14T10:35:50.948599Z","shell.execute_reply.started":"2024-03-14T10:35:48.295273Z","shell.execute_reply":"2024-03-14T10:35:50.947952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = create_cnn(data,models.resnet50, metrics = accuracy, model_dir= \"/tmp/model/\")","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:35:50.949780Z","iopub.execute_input":"2024-03-14T10:35:50.950011Z","iopub.status.idle":"2024-03-14T10:35:51.926520Z","shell.execute_reply.started":"2024-03-14T10:35:50.949971Z","shell.execute_reply":"2024-03-14T10:35:51.925830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:35:51.928110Z","iopub.execute_input":"2024-03-14T10:35:51.928415Z","iopub.status.idle":"2024-03-14T10:36:07.564730Z","shell.execute_reply.started":"2024-03-14T10:35:51.928356Z","shell.execute_reply":"2024-03-14T10:36:07.563833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"learning rate starts at (0.03)","metadata":{}},{"cell_type":"markdown","source":"learning rate starts at 1e-3 (0.03) and decreases at 1e-2(0.01)","metadata":{}},{"cell_type":"code","source":"learn.fit_one_cycle(3,max_lr=slice(1e-3,1e-2))","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:36:07.568417Z","iopub.execute_input":"2024-03-14T10:36:07.568792Z","iopub.status.idle":"2024-03-14T10:40:12.133234Z","shell.execute_reply.started":"2024-03-14T10:36:07.568726Z","shell.execute_reply":"2024-03-14T10:40:12.132337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/tmp/model/stage-1-50')","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:40:12.134639Z","iopub.execute_input":"2024-03-14T10:40:12.134893Z","iopub.status.idle":"2024-03-14T10:40:12.546993Z","shell.execute_reply.started":"2024-03-14T10:40:12.134837Z","shell.execute_reply":"2024-03-14T10:40:12.545965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:40:12.548238Z","iopub.execute_input":"2024-03-14T10:40:12.548495Z","iopub.status.idle":"2024-03-14T10:40:12.589263Z","shell.execute_reply.started":"2024-03-14T10:40:12.548443Z","shell.execute_reply":"2024-03-14T10:40:12.588395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:40:12.590459Z","iopub.execute_input":"2024-03-14T10:40:12.590743Z","iopub.status.idle":"2024-03-14T10:40:26.031686Z","shell.execute_reply.started":"2024-03-14T10:40:12.590682Z","shell.execute_reply":"2024-03-14T10:40:26.030978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3,max_lr=slice(1e-5,1e-4))","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:40:26.033448Z","iopub.execute_input":"2024-03-14T10:40:26.033770Z","iopub.status.idle":"2024-03-14T10:44:42.999858Z","shell.execute_reply.started":"2024-03-14T10:40:26.033713Z","shell.execute_reply":"2024-03-14T10:44:42.998598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('/tmp/model/stage-2-50')","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:44:43.001587Z","iopub.execute_input":"2024-03-14T10:44:43.001874Z","iopub.status.idle":"2024-03-14T10:44:43.919403Z","shell.execute_reply.started":"2024-03-14T10:44:43.001829Z","shell.execute_reply":"2024-03-14T10:44:43.918500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictionb= learn.get_preds(ds_type=DatasetType.Test)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:44:43.920864Z","iopub.execute_input":"2024-03-14T10:44:43.921187Z","iopub.status.idle":"2024-03-14T10:45:40.320856Z","shell.execute_reply.started":"2024-03-14T10:44:43.921134Z","shell.execute_reply":"2024-03-14T10:45:40.320175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictionb[0][0]","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:45:40.322418Z","iopub.execute_input":"2024-03-14T10:45:40.322698Z","iopub.status.idle":"2024-03-14T10:45:40.366829Z","shell.execute_reply.started":"2024-03-14T10:45:40.322644Z","shell.execute_reply":"2024-03-14T10:45:40.366091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/sample_submission.csv')\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:45:40.368569Z","iopub.execute_input":"2024-03-14T10:45:40.369099Z","iopub.status.idle":"2024-03-14T10:45:40.980489Z","shell.execute_reply.started":"2024-03-14T10:45:40.368852Z","shell.execute_reply":"2024-03-14T10:45:40.979575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = sample_submission.copy()\nfor i in range(len(submission)):\n    submission.iloc[i,1:] = predictionb[0][i].tolist()\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:45:40.982193Z","iopub.execute_input":"2024-03-14T10:45:40.982733Z","iopub.status.idle":"2024-03-14T10:51:19.211408Z","shell.execute_reply.started":"2024-03-14T10:45:40.982618Z","shell.execute_reply":"2024-03-14T10:51:19.210596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T10:51:19.212686Z","iopub.execute_input":"2024-03-14T10:51:19.212961Z","iopub.status.idle":"2024-03-14T10:51:22.721608Z","shell.execute_reply.started":"2024-03-14T10:51:19.212905Z","shell.execute_reply":"2024-03-14T10:51:22.720712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}