{"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":"# %%script false --no-raise-error\n!rm -fr './kaggle/'\n","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:19:27.574327Z","iopub.execute_input":"2021-06-14T12:19:27.574646Z","iopub.status.idle":"2021-06-14T12:19:30.419223Z","shell.execute_reply.started":"2021-06-14T12:19:27.574616Z","shell.execute_reply":"2021-06-14T12:19:30.418049Z"},"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-14T12:19:30.420993Z","iopub.execute_input":"2021-06-14T12:19:30.421394Z","iopub.status.idle":"2021-06-14T12:19:30.493362Z","shell.execute_reply.started":"2021-06-14T12:19:30.421349Z","shell.execute_reply":"2021-06-14T12:19:30.492231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport torch\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport fastai\nimport dill\nfrom fastai.imports import *\nfrom fastai.vision import *\nfrom fastai.vision.all import *\nfrom fastai.metrics import error_rate, accuracy","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:19:31.768107Z","iopub.execute_input":"2021-06-14T12:19:31.768466Z","iopub.status.idle":"2021-06-14T12:19:31.808859Z","shell.execute_reply.started":"2021-06-14T12:19:31.768434Z","shell.execute_reply":"2021-06-14T12:19:31.807727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Defining Directories\npath = Path('../input/hotel-id-2021-fgvc8')\n\n## csv\nsample_sub = path/'sample_submission.csv'\nlabels = path/'train.csv'","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:19:33.738720Z","iopub.execute_input":"2021-06-14T12:19:33.739326Z","iopub.status.idle":"2021-06-14T12:19:33.775135Z","shell.execute_reply.started":"2021-06-14T12:19:33.739278Z","shell.execute_reply":"2021-06-14T12:19:33.774241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(labels)\ntrain_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:19:35.650817Z","iopub.execute_input":"2021-06-14T12:19:35.651168Z","iopub.status.idle":"2021-06-14T12:19:35.877620Z","shell.execute_reply.started":"2021-06-14T12:19:35.651135Z","shell.execute_reply":"2021-06-14T12:19:35.876671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar xzvf /kaggle/input/hotelid-224/kaggle_hotelid_train_imgs_224x224.tgz","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:19:37.688266Z","iopub.execute_input":"2021-06-14T12:19:37.688606Z","iopub.status.idle":"2021-06-14T12:19:47.672212Z","shell.execute_reply.started":"2021-06-14T12:19:37.688574Z","shell.execute_reply":"2021-06-14T12:19:47.671223Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = './kaggle/working/train/'\n\ntrain_data['image_path'] = train_dir + train_data.chain.astype('str') + '/' + train_data.image\ntrain_data[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:19:47.675664Z","iopub.execute_input":"2021-06-14T12:19:47.675919Z","iopub.status.idle":"2021-06-14T12:19:47.873844Z","shell.execute_reply.started":"2021-06-14T12:19:47.675889Z","shell.execute_reply":"2021-06-14T12:19:47.873020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating a data Loader of the entire Dataset","metadata":{}},{"cell_type":"code","source":"batch_size = 32\nbatch_size = 32\ndef create_dataLoader():\n    dataset = ImageDataLoaders.from_df(df = train_data[['image_path', 'hotel_id']],\n                                   path = '.',\n                                   folder = '.',\n                                   valid_pct=0.2,\n                                   item_tfms=Resize(224, method='pad', pad_mode='reflection'),\n                                   batch_tfms=aug_transforms(size=224),\n                                   bs=batch_size)\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:04.019848Z","iopub.execute_input":"2021-06-14T12:20:04.020262Z","iopub.status.idle":"2021-06-14T12:20:04.057496Z","shell.execute_reply.started":"2021-06-14T12:20:04.020227Z","shell.execute_reply":"2021-06-14T12:20:04.056709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = create_dataLoader()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:06.825195Z","iopub.execute_input":"2021-06-14T12:20:06.825523Z","iopub.status.idle":"2021-06-14T12:20:22.779295Z","shell.execute_reply.started":"2021-06-14T12:20:06.825493Z","shell.execute_reply":"2021-06-14T12:20:22.778392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:22.780795Z","iopub.execute_input":"2021-06-14T12:20:22.781197Z","iopub.status.idle":"2021-06-14T12:20:23.603868Z","shell.execute_reply.started":"2021-06-14T12:20:22.781156Z","shell.execute_reply":"2021-06-14T12:20:23.603090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training dataset","metadata":{}},{"cell_type":"code","source":"%%script false --no-raise-error\ndef training_models(dataset, model_name):\n    learn = cnn_learner(dataset,  models.resnet101, \n                        metrics=[accuracy, top_k_accuracy], \n                        opt_func=QHAdam).to_fp16()\n    learn.fine_tune(5, 0.005, freeze_epochs=3)\n    learn.save(model_name)\n    return learn","metadata":{"execution":{"iopub.status.busy":"2021-06-04T19:39:15.13247Z","iopub.execute_input":"2021-06-04T19:39:15.133001Z","iopub.status.idle":"2021-06-04T19:39:15.193586Z","shell.execute_reply.started":"2021-06-04T19:39:15.132954Z","shell.execute_reply":"2021-06-04T19:39:15.192581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%script false --no-raise-error\n%time\nmodel_name ='model_allData_rn50'\nlearn = training_models(dataset, model_name)","metadata":{"execution":{"iopub.status.busy":"2021-06-04T19:39:15.194945Z","iopub.execute_input":"2021-06-04T19:39:15.195497Z","iopub.status.idle":"2021-06-04T19:51:47.024676Z","shell.execute_reply.started":"2021-06-04T19:39:15.19546Z","shell.execute_reply":"2021-06-04T19:51:47.020836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exporting the model\n","metadata":{}},{"cell_type":"code","source":"%%script false --no-raise-error\nlearn.export(f'{model_name}.pkl', pickle_module=dill)","metadata":{"execution":{"iopub.status.busy":"2021-06-04T19:51:47.026173Z","iopub.status.idle":"2021-06-04T19:51:47.026912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%script false --no-raise-error\nfromDir ='../input/hotelid-model/model_Data_category_rn101.pkl'\ntoDir ='.'\ncopy_tree(fromDir, toDir)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:17:22.721749Z","iopub.execute_input":"2021-06-14T12:17:22.722099Z","iopub.status.idle":"2021-06-14T12:17:22.737330Z","shell.execute_reply.started":"2021-06-14T12:17:22.722063Z","shell.execute_reply":"2021-06-14T12:17:22.736145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading the model","metadata":{}},{"cell_type":"code","source":"model_path ='../input/hotelid-model/model_Data_category_rn101.pkl'\nlearn = load_learner(fname = Path(model_path), cpu=False, pickle_module=dill) ","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:32.929612Z","iopub.execute_input":"2021-06-14T12:20:32.929938Z","iopub.status.idle":"2021-06-14T12:20:36.144530Z","shell.execute_reply.started":"2021-06-14T12:20:32.929905Z","shell.execute_reply":"2021-06-14T12:20:36.143595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predictions","metadata":{}},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/hotel-id-2021-fgvc8/sample_submission.csv')\nsubmission = sample_submission.copy()\nsubmission['image'] = '../input/hotel-id-2021-fgvc8/test_images/' + submission['image']\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:39.052912Z","iopub.execute_input":"2021-06-14T12:20:39.053316Z","iopub.status.idle":"2021-06-14T12:20:39.104877Z","shell.execute_reply.started":"2021-06-14T12:20:39.053283Z","shell.execute_reply":"2021-06-14T12:20:39.103876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = learn.dls.test_dl(submission)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:41.627333Z","iopub.execute_input":"2021-06-14T12:20:41.627662Z","iopub.status.idle":"2021-06-14T12:20:41.675221Z","shell.execute_reply.started":"2021-06-14T12:20:41.627632Z","shell.execute_reply":"2021-06-14T12:20:41.674305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl.show_batch() ","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:44.829430Z","iopub.execute_input":"2021-06-14T12:20:44.829756Z","iopub.status.idle":"2021-06-14T12:20:45.656853Z","shell.execute_reply.started":"2021-06-14T12:20:44.829724Z","shell.execute_reply":"2021-06-14T12:20:45.656052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probs, _ = learn.tta(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:48.645124Z","iopub.execute_input":"2021-06-14T12:20:48.645498Z","iopub.status.idle":"2021-06-14T12:20:51.770666Z","shell.execute_reply.started":"2021-06-14T12:20:48.645465Z","shell.execute_reply":"2021-06-14T12:20:51.769800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_idx = probs.topk(5)[1]","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:53.070423Z","iopub.execute_input":"2021-06-14T12:20:53.070763Z","iopub.status.idle":"2021-06-14T12:20:53.111607Z","shell.execute_reply.started":"2021-06-14T12:20:53.070726Z","shell.execute_reply":"2021-06-14T12:20:53.110602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = [' '.join(map(str, learn.dls.vocab[pred])) for pred in preds_idx]\npreds[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:55.014952Z","iopub.execute_input":"2021-06-14T12:20:55.015335Z","iopub.status.idle":"2021-06-14T12:20:55.055351Z","shell.execute_reply.started":"2021-06-14T12:20:55.015302Z","shell.execute_reply":"2021-06-14T12:20:55.054502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\".\")\nsubmission = sample_submission.copy()\nsubmission['hotel_id'] = preds\n\nsubmission.to_csv('submission.csv', index = False)\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:20:57.389720Z","iopub.execute_input":"2021-06-14T12:20:57.390064Z","iopub.status.idle":"2021-06-14T12:20:57.672565Z","shell.execute_reply.started":"2021-06-14T12:20:57.390026Z","shell.execute_reply":"2021-06-14T12:20:57.671778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -fr './kaggle/working/train'","metadata":{"execution":{"iopub.status.busy":"2021-06-14T12:21:16.225513Z","iopub.execute_input":"2021-06-14T12:21:16.225849Z","iopub.status.idle":"2021-06-14T12:21:19.110572Z","shell.execute_reply.started":"2021-06-14T12:21:16.225817Z","shell.execute_reply":"2021-06-14T12:21:19.109544Z"},"trusted":true},"execution_count":null,"outputs":[]}]}