{"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":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:05:27.804028Z","iopub.execute_input":"2021-06-05T23:05:27.804423Z","iopub.status.idle":"2021-06-05T23:05:27.835996Z","shell.execute_reply.started":"2021-06-05T23:05:27.804337Z","shell.execute_reply":"2021-06-05T23:05:27.835334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import os\nimport torch\nimport pandas as pd\n\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-05T23:05:30.53746Z","iopub.execute_input":"2021-06-05T23:05:30.537781Z","iopub.status.idle":"2021-06-05T23:05:33.340775Z","shell.execute_reply.started":"2021-06-05T23:05:30.53775Z","shell.execute_reply":"2021-06-05T23:05:33.339908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.get_device_name(0)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:05:34.410805Z","iopub.execute_input":"2021-06-05T23:05:34.411119Z","iopub.status.idle":"2021-06-05T23:05:34.448455Z","shell.execute_reply.started":"2021-06-05T23:05:34.411088Z","shell.execute_reply":"2021-06-05T23:05:34.447796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Folder Paths","metadata":{}},{"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-05T23:05:44.815076Z","iopub.execute_input":"2021-06-05T23:05:44.815437Z","iopub.status.idle":"2021-06-05T23:05:44.847882Z","shell.execute_reply.started":"2021-06-05T23:05:44.815404Z","shell.execute_reply":"2021-06-05T23:05:44.846974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reading csv submission","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(labels)\ntrain_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:05:51.645833Z","iopub.execute_input":"2021-06-05T23:05:51.64615Z","iopub.status.idle":"2021-06-05T23:05:51.853935Z","shell.execute_reply.started":"2021-06-05T23:05:51.646118Z","shell.execute_reply":"2021-06-05T23:05:51.853232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Creating Labels and changing images to include the new paths","metadata":{}},{"cell_type":"code","source":"train_dir= './kaggle/working/train/'\ntrain_data['label'] =  train_data['chain'].astype(str) + '/' + train_data['image'].astype(str)\ntrain_data['image_path'] = train_dir + train_data['chain'].astype(str) + '/' + train_data['image'].astype(str)\n\ntrain_data[:3]","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:05:58.351343Z","iopub.execute_input":"2021-06-05T23:05:58.351734Z","iopub.status.idle":"2021-06-05T23:05:58.710572Z","shell.execute_reply.started":"2021-06-05T23:05:58.351703Z","shell.execute_reply":"2021-06-05T23:05:58.709645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Unzip resized Images","metadata":{}},{"cell_type":"code","source":"!tar xzvf /kaggle/input/hotelid-224/kaggle_hotelid_train_imgs_224x224.tgz","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:06:09.975829Z","iopub.execute_input":"2021-06-05T23:06:09.976145Z","iopub.status.idle":"2021-06-05T23:06:24.113918Z","shell.execute_reply.started":"2021-06-05T23:06:09.976116Z","shell.execute_reply":"2021-06-05T23:06:24.113141Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating a model with the chain Id","metadata":{}},{"cell_type":"code","source":"batch_size = 64\ndef create_dataLoader():\n    dataset = ImageDataLoaders.from_df(df = train_data[['image_path', 'chain']],\n                                   path = '.',\n                                   folder = '.',\n                                   valid_pct=0.2,\n                                   item_tfms=Resize(128),\n                                   batch_tfms=aug_transforms(),\n                                   bs=batch_size)\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:07:41.813166Z","iopub.execute_input":"2021-06-05T23:07:41.813538Z","iopub.status.idle":"2021-06-05T23:07:41.847064Z","shell.execute_reply.started":"2021-06-05T23:07:41.813507Z","shell.execute_reply":"2021-06-05T23:07:41.846046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = create_dataLoader()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:07:44.524935Z","iopub.execute_input":"2021-06-05T23:07:44.525373Z","iopub.status.idle":"2021-06-05T23:07:59.266922Z","shell.execute_reply.started":"2021-06-05T23:07:44.525328Z","shell.execute_reply":"2021-06-05T23:07:59.266167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:07:59.268353Z","iopub.execute_input":"2021-06-05T23:07:59.268663Z","iopub.status.idle":"2021-06-05T23:08:00.140783Z","shell.execute_reply.started":"2021-06-05T23:07:59.268629Z","shell.execute_reply":"2021-06-05T23:08:00.139941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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(12, 0.005, freeze_epochs=3)\n    return learn","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:18:41.989468Z","iopub.execute_input":"2021-06-05T23:18:41.989801Z","iopub.status.idle":"2021-06-05T23:18:42.022587Z","shell.execute_reply.started":"2021-06-05T23:18:41.989769Z","shell.execute_reply":"2021-06-05T23:18:42.021809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%time\nmodel_name ='model_allData_rn101'\nlearn = training_models(dataset, model_name)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T23:18:46.157785Z","iopub.execute_input":"2021-06-05T23:18:46.158104Z","iopub.status.idle":"2021-06-06T02:12:29.311171Z","shell.execute_reply.started":"2021-06-05T23:18:46.158074Z","shell.execute_reply":"2021-06-06T02:12:29.310418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export(f'{model_name}.pkl', pickle_module=dill)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T02:27:20.228493Z","iopub.execute_input":"2021-06-06T02:27:20.228872Z","iopub.status.idle":"2021-06-06T02:27:23.079849Z","shell.execute_reply.started":"2021-06-06T02:27:20.22884Z","shell.execute_reply":"2021-06-06T02:27:23.078885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -fr './kaggle/working/train'","metadata":{},"execution_count":null,"outputs":[]}]}