{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-13T22:52:35.332221Z","iopub.execute_input":"2021-06-13T22:52:35.332881Z","iopub.status.idle":"2021-06-13T22:52:35.371383Z","shell.execute_reply.started":"2021-06-13T22:52:35.332804Z","shell.execute_reply":"2021-06-13T22:52:35.370637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -fr './kaggle/working/train'\n# !rm -fr './model_chain_rn101_10.pkl'","metadata":{"execution":{"iopub.status.busy":"2021-06-13T22:52:35.376023Z","iopub.execute_input":"2021-06-13T22:52:35.376382Z","iopub.status.idle":"2021-06-13T22:52:35.401282Z","shell.execute_reply.started":"2021-06-13T22:52:35.376339Z","shell.execute_reply":"2021-06-13T22:52:35.400433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import fastai\nimport pandas as pd\nimport torch\nimport os\nimport dill\nfrom fastai.imports import *\nfrom fastai.vision import *\nfrom fastai.vision.all import *\nfrom torchvision import datasets, transforms\nfrom fastai.metrics import error_rate","metadata":{"execution":{"iopub.status.busy":"2021-06-13T22:52:35.402779Z","iopub.execute_input":"2021-06-13T22:52:35.403126Z","iopub.status.idle":"2021-06-13T22:52:36.259675Z","shell.execute_reply.started":"2021-06-13T22:52:35.403092Z","shell.execute_reply":"2021-06-13T22:52:36.258762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# torch.cuda.get_device_name(0)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T22:52:36.260920Z","iopub.execute_input":"2021-06-13T22:52:36.261249Z","iopub.status.idle":"2021-06-13T22:52:36.296118Z","shell.execute_reply.started":"2021-06-13T22:52:36.261216Z","shell.execute_reply":"2021-06-13T22:52:36.295240Z"},"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\nsample_sub = path/'sample_submission.csv'\nlabels = path/'train.csv'","metadata":{"execution":{"iopub.status.busy":"2021-06-13T22:52:36.297400Z","iopub.execute_input":"2021-06-13T22:52:36.297900Z","iopub.status.idle":"2021-06-13T22:52:36.332579Z","shell.execute_reply.started":"2021-06-13T22:52:36.297862Z","shell.execute_reply":"2021-06-13T22:52:36.331707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reading csv submission file","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(labels).sample(frac=1, random_state = 2021).reset_index(drop = True)\ntrain_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T22:52:36.335005Z","iopub.execute_input":"2021-06-13T22:52:36.335377Z","iopub.status.idle":"2021-06-13T22:52:36.480016Z","shell.execute_reply.started":"2021-06-13T22:52:36.335333Z","shell.execute_reply":"2021-06-13T22:52:36.479209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dir = './kaggle/working/train/'\ntrain_dir = '../input/fgvc8hoteltrain512/'\ntrain_data['image_path'] = train_dir + train_data.chain.astype('str') + '/' + train_data.image\ntrain_data[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-13T22:52:36.481287Z","iopub.execute_input":"2021-06-13T22:52:36.481640Z","iopub.status.idle":"2021-06-13T22:52:36.671975Z","shell.execute_reply.started":"2021-06-13T22:52:36.481601Z","shell.execute_reply":"2021-06-13T22:52:36.670976Z"},"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-13T22:52:36.674522Z","iopub.execute_input":"2021-06-13T22:52:36.674873Z","iopub.status.idle":"2021-06-13T22:52:36.709361Z","shell.execute_reply.started":"2021-06-13T22:52:36.674837Z","shell.execute_reply":"2021-06-13T22:52:36.708508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating dataset per chain","metadata":{}},{"cell_type":"code","source":"batch_size = 32\ndef create_dataLoader(train_subset):\n    dataset = ImageDataLoaders.from_df(df = train_subset[['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-13T22:52:36.710912Z","iopub.execute_input":"2021-06-13T22:52:36.711220Z","iopub.status.idle":"2021-06-13T22:52:36.747481Z","shell.execute_reply.started":"2021-06-13T22:52:36.711187Z","shell.execute_reply":"2021-06-13T22:52:36.746580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category_dir,folder_label =[], []\nfolder_list = os.listdir(train_dir)\nlen(folder_list)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T22:52:36.750002Z","iopub.execute_input":"2021-06-13T22:52:36.750249Z","iopub.status.idle":"2021-06-13T22:52:36.786588Z","shell.execute_reply.started":"2021-06-13T22:52:36.750224Z","shell.execute_reply":"2021-06-13T22:52:36.785582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i in range(len(folder_list)):\n#     img_dir = str(train_dir) + str(folder_list[i] + '/')\n#     if  os.listdir(img_dir) :\n#         # skip directories\n#         continue\n#     else:\n#         print(img_dir)\n#         os.rmdir(img_dir)\n        ","metadata":{"execution":{"iopub.status.busy":"2021-06-13T22:52:36.788063Z","iopub.execute_input":"2021-06-13T22:52:36.788444Z","iopub.status.idle":"2021-06-13T22:52:36.821282Z","shell.execute_reply.started":"2021-06-13T22:52:36.788410Z","shell.execute_reply":"2021-06-13T22:52:36.820503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(folder_list)):\n    img_dir = str(train_dir) + str(folder_list[i] + '/')\n    if  os.listdir(img_dir) :\n        print(folder_list[i])\n        df_subset = train_data.copy()\n        is_chain =  df_subset.chain==i\n        train_subset = df_subset[is_chain]\n        dataset_chain = create_dataLoader(train_subset)\n        learn = cnn_learner(dataset_chain, resnet101, metrics=[accuracy, error_rate],   opt_func=QHAdam).to_fp16()\n        %time\n        learn.fine_tune(12, 0.005, freeze_epochs=3)\n        model_name ='model_chain_rn101'\n        learn.export(f'{model_name}_{folder_list[i]}.pkl', pickle_module=dill)\n        ","metadata":{"execution":{"iopub.status.busy":"2021-06-13T22:52:36.822131Z","iopub.execute_input":"2021-06-13T22:52:36.822423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export(f'{model_name}_{i}.pkl', pickle_module=dill)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}