{"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":"markdown","source":"# Intro\nInference notebook for [Hotel-ID starter - classification - traning](https://www.kaggle.com/code/michaln/hotel-id-starter-classification-traning)\n\n","metadata":{"id":"DAY5rHgTm7e8","papermill":{"duration":0.024896,"end_time":"2022-03-24T14:00:54.588459","exception":false,"start_time":"2022-03-24T14:00:54.563563","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# Setup","metadata":{}},{"cell_type":"code","source":"import sys\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\nimport timm","metadata":{"executionInfo":{"elapsed":16271,"status":"ok","timestamp":1619310548121,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"alleged-legislation","outputId":"c6541e5f-ffb4-4609-d6c6-39784e6a07b1","papermill":{"duration":0.036572,"end_time":"2022-03-24T14:00:54.649254","exception":false,"start_time":"2022-03-24T14:00:54.612682","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-25T11:06:33.869744Z","iopub.execute_input":"2022-05-25T11:06:33.87012Z","iopub.status.idle":"2022-05-25T11:06:45.242927Z","shell.execute_reply.started":"2022-05-25T11:06:33.869988Z","shell.execute_reply":"2022-05-25T11:06:45.241292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{"id":"cZoSOL9Qm-Yr","papermill":{"duration":0.023644,"end_time":"2022-03-24T14:00:54.696898","exception":false,"start_time":"2022-03-24T14:00:54.673254","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport random\nimport os\nimport math","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","executionInfo":{"elapsed":14459,"status":"ok","timestamp":1619310548121,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"expired-matter","papermill":{"duration":0.030271,"end_time":"2022-03-24T14:00:54.751131","exception":false,"start_time":"2022-03-24T14:00:54.72086","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-25T11:01:31.737794Z","iopub.execute_input":"2022-05-25T11:01:31.738303Z","iopub.status.idle":"2022-05-25T11:01:31.74297Z","shell.execute_reply.started":"2022-05-25T11:01:31.738263Z","shell.execute_reply":"2022-05-25T11:01:31.742149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image as pil_image\nfrom tqdm import tqdm","metadata":{"executionInfo":{"elapsed":16003,"status":"ok","timestamp":1619310550014,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"extreme-problem","papermill":{"duration":3.220402,"end_time":"2022-03-24T14:00:57.995239","exception":false,"start_time":"2022-03-24T14:00:54.774837","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-25T11:01:36.364795Z","iopub.execute_input":"2022-05-25T11:01:36.3651Z","iopub.status.idle":"2022-05-25T11:01:36.369016Z","shell.execute_reply.started":"2022-05-25T11:01:36.365067Z","shell.execute_reply":"2022-05-25T11:01:36.368366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader","metadata":{"executionInfo":{"elapsed":19672,"status":"ok","timestamp":1619310554099,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"angry-domain","papermill":{"duration":2.727834,"end_time":"2022-03-24T14:01:00.766951","exception":false,"start_time":"2022-03-24T14:00:58.039117","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-25T11:01:37.882989Z","iopub.execute_input":"2022-05-25T11:01:37.883298Z","iopub.status.idle":"2022-05-25T11:01:39.689687Z","shell.execute_reply.started":"2022-05-25T11:01:37.883261Z","shell.execute_reply":"2022-05-25T11:01:39.688809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Global","metadata":{"id":"0B00pe7mnBTj","papermill":{"duration":0.023573,"end_time":"2022-03-24T14:01:00.814976","exception":false,"start_time":"2022-03-24T14:01:00.791403","status":"completed"},"tags":[]}},{"cell_type":"code","source":"SEED = 42\nIMG_SIZE = 256\n\nPROJECT_FOLDER = \"../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/\"\nTEST_DATA_FOLDER = PROJECT_FOLDER + \"test_images/\"","metadata":{"executionInfo":{"elapsed":589,"status":"ok","timestamp":1619310979015,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"contained-brief","papermill":{"duration":0.03175,"end_time":"2022-03-24T14:01:00.871686","exception":false,"start_time":"2022-03-24T14:01:00.839936","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.270003Z","iopub.execute_input":"2022-05-18T12:08:11.271831Z","iopub.status.idle":"2022-05-18T12:08:11.279524Z","shell.execute_reply.started":"2022-05-18T12:08:11.271789Z","shell.execute_reply":"2022-05-18T12:08:11.278645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(PROJECT_FOLDER))","metadata":{"executionInfo":{"elapsed":879,"status":"ok","timestamp":1619310979515,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"PZvmFng7ctO3","outputId":"dce0cc91-8e70-4acc-a0b8-6763ffffd5ca","papermill":{"duration":0.031651,"end_time":"2022-03-24T14:01:00.927239","exception":false,"start_time":"2022-03-24T14:01:00.895588","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.284102Z","iopub.execute_input":"2022-05-18T12:08:11.284583Z","iopub.status.idle":"2022-05-18T12:08:11.293768Z","shell.execute_reply.started":"2022-05-18T12:08:11.284543Z","shell.execute_reply":"2022-05-18T12:08:11.292881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True","metadata":{"executionInfo":{"elapsed":600,"status":"ok","timestamp":1619310981653,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"eastern-content","papermill":{"duration":0.032105,"end_time":"2022-03-24T14:01:01.031949","exception":false,"start_time":"2022-03-24T14:01:00.999844","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.299786Z","iopub.execute_input":"2022-05-18T12:08:11.300568Z","iopub.status.idle":"2022-05-18T12:08:11.308079Z","shell.execute_reply.started":"2022-05-18T12:08:11.30053Z","shell.execute_reply":"2022-05-18T12:08:11.307097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset and transformations","metadata":{"id":"xaJKvvuKnW4k","papermill":{"duration":0.023988,"end_time":"2022-03-24T14:01:01.080488","exception":false,"start_time":"2022-03-24T14:01:01.0565","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import albumentations as A\nimport albumentations.pytorch as APT\nimport cv2 \n\n# used for training dataset - augmentations and occlusions\ntrain_transform = A.Compose([\n    A.HorizontalFlip(p=0.75),\n    A.VerticalFlip(p=0.25),\n    A.ShiftScaleRotate(p=0.5, border_mode=cv2.BORDER_CONSTANT),\n    A.OpticalDistortion(p=0.25),\n    A.Perspective(p=0.25),\n    A.CoarseDropout(p=0.5, min_holes=1, max_holes=6, \n                    min_height=IMG_SIZE//16, max_height=IMG_SIZE//4,\n                    min_width=IMG_SIZE//16,  max_width=IMG_SIZE//4), # normal coarse dropout\n    \n    A.CoarseDropout(p=0.75, max_holes=1, \n                    min_height=IMG_SIZE//4, max_height=IMG_SIZE//2,\n                    min_width=IMG_SIZE//4,  max_width=IMG_SIZE//2, \n                    fill_value=(255,0,0)),# simulating occlusions in test data\n\n    A.RandomBrightnessContrast(p=0.75),\n    A.ToFloat(),\n    APT.transforms.ToTensorV2(),\n])\n\n# used for validation dataset - only occlusions\nval_transform = A.Compose([\n    A.CoarseDropout(p=0.75, max_holes=1, \n                    min_height=IMG_SIZE//4, max_height=IMG_SIZE//2,\n                    min_width=IMG_SIZE//4,  max_width=IMG_SIZE//2, \n                    fill_value=(255,0,0)),# simulating occlusions\n    A.ToFloat(),\n    APT.transforms.ToTensorV2(),\n])","metadata":{"executionInfo":{"elapsed":1519,"status":"ok","timestamp":1619310984075,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"revolutionary-membership","papermill":{"duration":0.747421,"end_time":"2022-03-24T14:01:01.852072","exception":false,"start_time":"2022-03-24T14:01:01.104651","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.309854Z","iopub.execute_input":"2022-05-18T12:08:11.310671Z","iopub.status.idle":"2022-05-18T12:08:11.335453Z","shell.execute_reply.started":"2022-05-18T12:08:11.310623Z","shell.execute_reply":"2022-05-18T12:08:11.33066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pad_image(img):\n    w, h, c = np.shape(img)\n    if w > h:\n        pad = int((w - h) / 2)\n        img = cv2.copyMakeBorder(img, 0, 0, pad, pad, cv2.BORDER_CONSTANT, value=0)\n    else:\n        pad = int((h - w) / 2)\n        img = cv2.copyMakeBorder(img, pad, pad, 0, 0, cv2.BORDER_CONSTANT, value=0)\n        \n    return img\n\n\ndef open_and_preprocess_image(image_path):\n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = pad_image(img)\n    return cv2.resize(img, (IMG_SIZE, IMG_SIZE))","metadata":{"execution":{"iopub.status.busy":"2022-05-18T12:08:11.401281Z","iopub.execute_input":"2022-05-18T12:08:11.402086Z","iopub.status.idle":"2022-05-18T12:08:11.411027Z","shell.execute_reply.started":"2022-05-18T12:08:11.40204Z","shell.execute_reply":"2022-05-18T12:08:11.409836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class HotelImageDataset:\n    def __init__(self, data, transform=None, data_folder=\"train_images/\"):\n        self.data = data\n        self.data_folder = data_folder\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.data)\n    \n    def __getitem__(self, idx):\n        record = self.data.iloc[idx]\n        image_path = self.data_folder + record[\"image_id\"]\n        \n        image = np.array(open_and_preprocess_image(image_path)).astype(np.uint8)\n\n        if self.transform:\n            transformed = self.transform(image=image)\n            image = transformed[\"image\"]\n        \n        return {\n            \"image\" : image,\n        }","metadata":{"executionInfo":{"elapsed":1058,"status":"ok","timestamp":1619310984077,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"found-mouth","papermill":{"duration":0.037507,"end_time":"2022-03-24T14:01:01.914352","exception":false,"start_time":"2022-03-24T14:01:01.876845","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.43667Z","iopub.execute_input":"2022-05-18T12:08:11.436865Z","iopub.status.idle":"2022-05-18T12:08:11.44321Z","shell.execute_reply.started":"2022-05-18T12:08:11.436836Z","shell.execute_reply":"2022-05-18T12:08:11.442494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"id":"NMDM4PwPnced","papermill":{"duration":0.023902,"end_time":"2022-03-24T14:01:01.962307","exception":false,"start_time":"2022-03-24T14:01:01.938405","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class HotelIdModel(nn.Module):\n    def __init__(self, n_classes=100, backbone_name=\"resnet34\"):\n        super(HotelIdModel, self).__init__()\n        \n        self.backbone = timm.create_model(backbone_name, num_classes=n_classes, pretrained=False)\n\n    def forward(self, x):\n        return self.backbone(x)","metadata":{"papermill":{"duration":0.032166,"end_time":"2022-03-24T14:01:02.018479","exception":false,"start_time":"2022-03-24T14:01:01.986313","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.454621Z","iopub.execute_input":"2022-05-18T12:08:11.454816Z","iopub.status.idle":"2022-05-18T12:08:11.460708Z","shell.execute_reply.started":"2022-05-18T12:08:11.454793Z","shell.execute_reply":"2022-05-18T12:08:11.459996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model helper functions","metadata":{"id":"YMZYKhUSneMY","papermill":{"duration":0.024153,"end_time":"2022-03-24T14:01:02.067537","exception":false,"start_time":"2022-03-24T14:01:02.043384","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def predict(loader, model, n_matches=5):\n    preds = []\n    with torch.no_grad():\n        t = tqdm(loader)\n        for i, sample in enumerate(t):\n            input = sample['image'].to(args.device)\n            outputs = model(input)\n            outputs = torch.sigmoid(outputs).detach().cpu().numpy()\n            preds.extend(outputs)\n    \n    # get 5 top predictions\n    preds = np.argsort(-np.array(preds), axis=1)[:, :5]\n    return preds","metadata":{"papermill":{"duration":0.034692,"end_time":"2022-03-24T14:01:02.127372","exception":false,"start_time":"2022-03-24T14:01:02.09268","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.462374Z","iopub.execute_input":"2022-05-18T12:08:11.462768Z","iopub.status.idle":"2022-05-18T12:08:11.470625Z","shell.execute_reply.started":"2022-05-18T12:08:11.462734Z","shell.execute_reply":"2022-05-18T12:08:11.469814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as A\nimport albumentations.pytorch as APT\nimport cv2 \n\n# used for training dataset - augmentations and occlusions\ntrain_transform = A.Compose([\n    A.RandomCrop(width=64, height=64),\n    A.HorizontalFlip(p=0.75),\n    #A.VerticalFlip(p=0.0),\n    A.ShiftScaleRotate(p=0.5, shift_limit=0.0625, scale_limit=0.1, rotate_limit=10, interpolation=cv2.INTER_NEAREST, border_mode=cv2.BORDER_CONSTANT),\n    A.OpticalDistortion(p=0.25, distort_limit=0.05, shift_limit=0.01),\n    A.Perspective(p=0.25, scale=(0.05, 0.1)),\n    A.ColorJitter(p=0.75, brightness=0.2, contrast=0.2, saturation=0.1, hue=0.05),\n    A.CoarseDropout(p=0.5, min_holes=1, max_holes=5, \n                    min_height=IMG_SIZE//16, max_height=IMG_SIZE//8,\n                    min_width=IMG_SIZE//16,  max_width=IMG_SIZE//8), # normal coarse dropout\n    \n    A.CoarseDropout(p=0.75, max_holes=1, \n                    min_height=IMG_SIZE//4, max_height=IMG_SIZE//2,\n                    min_width=IMG_SIZE//4,  max_width=IMG_SIZE//2, \n                    fill_value=(255,0,0)),# simulating occlusions in test data\n    #A.RandomBrightnessContrast(p=0.75),\n    A.ToFloat(),\n    APT.transforms.ToTensorV2(),\n])\n\n# used for validation dataset - only occlusions\nval_transform = A.Compose([\n    A.CoarseDropout(p=0.75, max_holes=1, \n                    min_height=IMG_SIZE//4, max_height=IMG_SIZE//2,\n                    min_width=IMG_SIZE//4,  max_width=IMG_SIZE//2, \n                    fill_value=(255,0,0)),# simulating occlusions\n    A.ToFloat(),\n    APT.transforms.ToTensorV2(),\n])\n\n# no augmentations\nbase_transform = A.Compose([\n    A.ToFloat(),\n    APT.transforms.ToTensorV2(),\n])","metadata":{"execution":{"iopub.status.busy":"2022-05-18T12:08:11.500367Z","iopub.execute_input":"2022-05-18T12:08:11.500589Z","iopub.status.idle":"2022-05-18T12:08:11.521283Z","shell.execute_reply.started":"2022-05-18T12:08:11.500558Z","shell.execute_reply":"2022-05-18T12:08:11.520494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare data","metadata":{"id":"AwShW1wXniD6","papermill":{"duration":0.023807,"end_time":"2022-03-24T14:01:02.175822","exception":false,"start_time":"2022-03-24T14:01:02.152015","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_df = pd.DataFrame(data={\"image_id\": os.listdir(TEST_DATA_FOLDER), \"hotel_id\": \"\"}).sort_values(by=\"image_id\")","metadata":{"executionInfo":{"elapsed":3742,"status":"ok","timestamp":1619311036476,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"discrete-right","outputId":"c21ed589-3139-4919-b5d5-07bcf6f1df15","papermill":{"duration":0.103381,"end_time":"2022-03-24T14:01:02.453866","exception":false,"start_time":"2022-03-24T14:01:02.350485","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.526347Z","iopub.execute_input":"2022-05-18T12:08:11.526946Z","iopub.status.idle":"2022-05-18T12:08:11.538753Z","shell.execute_reply.started":"2022-05-18T12:08:11.526911Z","shell.execute_reply":"2022-05-18T12:08:11.538021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# code hotel_id mapping created in training notebook by encoding hotel_ids\nhotel_id_code_df = pd.read_csv('../input/resnet-training/hotel_id_code_mapping.csv')\nhotel_id_code_map = hotel_id_code_df.set_index('hotel_id_code').to_dict()[\"hotel_id\"]","metadata":{"execution":{"iopub.status.busy":"2022-05-18T12:08:11.542923Z","iopub.execute_input":"2022-05-18T12:08:11.544906Z","iopub.status.idle":"2022-05-18T12:08:11.561416Z","shell.execute_reply.started":"2022-05-18T12:08:11.544867Z","shell.execute_reply":"2022-05-18T12:08:11.560649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare model","metadata":{"id":"5JPdD2bpnniP","papermill":{"duration":0.023835,"end_time":"2022-03-24T14:01:02.502786","exception":false,"start_time":"2022-03-24T14:01:02.478951","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def get_model(model_type, backbone_name, checkpoint_path, args):\n    model = HotelIdModel(args.n_classes, backbone_name)\n        \n    checkpoint = torch.load(checkpoint_path)\n    model.load_state_dict(checkpoint[\"model\"])\n    model = model.to(args.device)\n    \n    return model","metadata":{"papermill":{"duration":0.031082,"end_time":"2022-03-24T14:01:02.557921","exception":false,"start_time":"2022-03-24T14:01:02.526839","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.563205Z","iopub.execute_input":"2022-05-18T12:08:11.563444Z","iopub.status.idle":"2022-05-18T12:08:11.568638Z","shell.execute_reply.started":"2022-05-18T12:08:11.563411Z","shell.execute_reply":"2022-05-18T12:08:11.567964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class args:\n    batch_size = 64\n    num_workers = 2\n    n_classes = hotel_id_code_df[\"hotel_id\"].nunique()\n    device = ('cuda' if torch.cuda.is_available() else 'cpu')\n    \n    \nseed_everything(seed=SEED)\n\ntest_dataset = HotelImageDataset(test_df, base_transform, data_folder=TEST_DATA_FOLDER)\ntest_loader = DataLoader(test_dataset, num_workers=args.num_workers, batch_size=args.batch_size, shuffle=False)","metadata":{"executionInfo":{"elapsed":450,"status":"ok","timestamp":1619311064188,"user":{"displayName":"Jeom Jin-Ho","photoUrl":"","userId":"00155613517919499503"},"user_tz":-120},"id":"appointed-machinery","papermill":{"duration":0.069839,"end_time":"2022-03-24T14:01:02.65177","exception":false,"start_time":"2022-03-24T14:01:02.581931","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.570145Z","iopub.execute_input":"2022-05-18T12:08:11.570674Z","iopub.status.idle":"2022-05-18T12:08:11.579716Z","shell.execute_reply.started":"2022-05-18T12:08:11.570638Z","shell.execute_reply":"2022-05-18T12:08:11.579071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model(\"classification\", \"resnet34\",\n                  \"../input/resnet-training/checkpoint-classification-model-resnet34-256x256.pt\", \n                  args)","metadata":{"papermill":{"duration":5.553999,"end_time":"2022-03-24T14:01:08.229948","exception":false,"start_time":"2022-03-24T14:01:02.675949","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:11.581867Z","iopub.execute_input":"2022-05-18T12:08:11.582543Z","iopub.status.idle":"2022-05-18T12:08:16.525279Z","shell.execute_reply.started":"2022-05-18T12:08:11.582505Z","shell.execute_reply":"2022-05-18T12:08:16.524513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"papermill":{"duration":0.024227,"end_time":"2022-03-24T14:01:08.278753","exception":false,"start_time":"2022-03-24T14:01:08.254526","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%%time\n\npreds = predict(test_loader, model)\n# replace classes with hotel_id using mapping created in trainig notebook\npreds = [[hotel_id_code_map[b] for b in a] for a in preds]\n# transform array of hotel_ids into string\ntest_df[\"hotel_id\"] = [str(list(l)).strip(\"[]\").replace(\",\", \"\") for l in preds]\n\ntest_df.to_csv(\"submission.csv\", index=False)\ntest_df.head()","metadata":{"papermill":{"duration":0.047156,"end_time":"2022-03-24T14:01:08.414557","exception":false,"start_time":"2022-03-24T14:01:08.367401","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-18T12:08:16.526763Z","iopub.execute_input":"2022-05-18T12:08:16.527259Z","iopub.status.idle":"2022-05-18T12:08:22.754106Z","shell.execute_reply.started":"2022-05-18T12:08:16.527222Z","shell.execute_reply":"2022-05-18T12:08:22.753375Z"},"trusted":true},"execution_count":null,"outputs":[]}]}