{"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":"import numpy as np  \nimport pandas as pd  \nimport os\nimport sys\nsys.path.append(\"/kaggle/input/nfl3-inference-script\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-05T01:51:25.279312Z","iopub.execute_input":"2023-03-05T01:51:25.279682Z","iopub.status.idle":"2023-03-05T01:51:25.284817Z","shell.execute_reply.started":"2023-03-05T01:51:25.279649Z","shell.execute_reply":"2023-03-05T01:51:25.283778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**XGB preprocessing step**","metadata":{}},{"cell_type":"code","source":"! python /kaggle/input/nfl3-inference-script/xgb_preprocess.py","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-05T01:53:00.836633Z","iopub.execute_input":"2023-03-05T01:53:00.83702Z","iopub.status.idle":"2023-03-05T01:53:14.666936Z","shell.execute_reply.started":"2023-03-05T01:53:00.836986Z","shell.execute_reply":"2023-03-05T01:53:14.665636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python /kaggle/input/nfl3-inference-script/xgb_pair_v1.py","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-05T01:53:21.251844Z","iopub.execute_input":"2023-03-05T01:53:21.25225Z","iopub.status.idle":"2023-03-05T01:53:30.472801Z","shell.execute_reply.started":"2023-03-05T01:53:21.252214Z","shell.execute_reply":"2023-03-05T01:53:30.471455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Install libraries for mmaction2 model**","metadata":{}},{"cell_type":"code","source":"!pip install /kaggle/input/mmdetection-offline-lib/mmcv_full-1.3.14-cp37-cp37m-linux_x86_64.whl --no-deps\n!pip install /kaggle/input/mmdetection-offline-lib/pycocotools-2.0.2-cp37-cp37m-linux_x86_64.whl --no-deps\n!pip install /kaggle/input/mmdetection-offline-lib/terminaltables-3.1.0-py3-none-any.whl --no-deps\n!pip install /kaggle/input/mmdetection-offline-lib/pytest_runner-5.3.1-py3-none-any.whl --no-deps\n!pip install /kaggle/input/mmdetection-offline-lib/mmpycocotools-12.0.3-cp37-cp37m-linux_x86_64.whl --no-deps\n!pip install /kaggle/input/mmdetection-offline-lib/terminal-0.4.0-py3-none-any.whl --no-deps\n!pip install /kaggle/input/mmdetection-offline-lib/mmdet-2.17.0-py3-none-any.whl --no-deps\n!pip install /kaggle/input/mmdetection-offline-lib/addict-2.4.0-py3-none-any.whl --no-deps\n!pip install /kaggle/input/mmdetection-offline-lib/yapf-0.31.0-py2.py3-none-any.whl --no-deps","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-05T01:29:55.871716Z","iopub.execute_input":"2023-03-05T01:29:55.872161Z","iopub.status.idle":"2023-03-05T01:33:18.354785Z","shell.execute_reply.started":"2023-03-05T01:29:55.872105Z","shell.execute_reply":"2023-03-05T01:33:18.35346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom model_csn1 import NModel\nimport cv2\nimport albumentations as A\nfrom tqdm.notebook import tqdm\nimport time\nimport gc","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:18.358168Z","iopub.execute_input":"2023-03-05T01:33:18.358582Z","iopub.status.idle":"2023-03-05T01:33:28.974837Z","shell.execute_reply.started":"2023-03-05T01:33:18.358533Z","shell.execute_reply":"2023-03-05T01:33:28.973739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Crop endzone, sideline for player-player function**","metadata":{}},{"cell_type":"code","source":"def get_sample(vid, fr_id, idx1, idx2, window_size = 20, out_size = 128):\n    ws = []\n    hs = []\n    for fr in range(fr_id + window_size[0], fr_id + window_size[-1] + 1):\n        if fr in det_dict[vid]:\n            if idx1 in det_dict[vid][fr] and idx2 in det_dict[vid][fr]: \n                x, y, w, h = det_dict[vid][fr][idx1]['box']\n                ws.append(w)\n                hs.append(h)\n                x, y, w, h = det_dict[vid][fr][idx2]['box']\n                ws.append(w)\n                hs.append(h)\n\n    if len(ws)>0:\n        crop_size = int(5*max(np.mean(ws), np.mean(hs)))\n    else:\n        crop_size = out_size\n\n    bboxes = []\n    for fr in range(fr_id + window_size[0], fr_id + window_size[-1] + 1):\n        if fr in det_dict[vid]:\n            if idx1 in det_dict[vid][fr] and idx2 in det_dict[vid][fr]: \n                x, y, w, h = det_dict[vid][fr][idx1]['box']\n                x1 = x + w/2\n                y1 = y + h/2\n\n                x, y, w, h = det_dict[vid][fr][idx2]['box']\n                x2 = x + w/2\n                y2 = y + h/2\n\n                xc = 0.5*x1 + 0.5*x2\n                yc = 0.5*y1 + 0.5*y2\n\n                bboxes.append([xc-crop_size, yc-crop_size, xc+crop_size, yc+crop_size])\n            else:\n                bboxes.append([np.nan, np.nan, np.nan, np.nan])\n        else:\n            bboxes.append([np.nan, np.nan, np.nan, np.nan])\n\n    bboxes = pd.DataFrame(bboxes).interpolate(limit_direction='both').values\n    images = []\n    masks1 = []\n    masks2 = []\n    empty_count = 0\n    for i, ii in enumerate(window_size):\n        if bboxes.sum() > 0:\n            fr = ii + fr_id\n            path = f'{vid}_{fr}'\n            \n            if path in image_dict:\n                image = image_dict[path]\n            else:\n                image = np.zeros((720, 1280,3), dtype = np.uint8)\n                empty_count +=1\n\n            mask1 = np.zeros((720, 1280), dtype = np.uint8)\n            mask2 = np.zeros((720, 1280), dtype = np.uint8)\n\n            x1, y1, x2, y2 = list(map(int, bboxes[ii-window_size[0]]))\n\n            y1 = y1 + int(0.2*crop_size)\n            x2 = x1 + crop_size*2\n            y2 = y1 + crop_size*2\n\n            if fr in det_dict[vid]:\n                if idx1 in det_dict[vid][fr]: \n                    x, y, w, h = det_dict[vid][fr][idx1]['box']\n                    # mask1[y:y+h, x:x+w] = 255\n                    cv2.circle(mask1, (x+w//2, y+h//2), int(0.25*h+0.25*w), 255, thickness=-1)\n\n                if idx2 in det_dict[vid][fr]:\n                    x, y, w, h = det_dict[vid][fr][idx2]['box']\n                    # mask2[y:y+h, x:x+w] = 255\n                    cv2.circle(mask2, (x+w//2, y+h//2), int(0.25*h+0.25*w), 255, thickness=-1)\n\n            crop = image[y1:y2, x1:x2]\n            crop_mask1 = mask1[y1:y2, x1:x2]\n            crop_mask2 = mask2[y1:y2, x1:x2]\n\n            cr_y, cr_x = crop.shape[:2]\n            if cr_x == crop_size*2 and cr_y == crop_size*2:\n                crop = cv2.resize(crop, (out_size*2,out_size*2))\n                crop_mask1 = cv2.resize(crop_mask1, (out_size*2,out_size*2))\n                crop_mask2 = cv2.resize(crop_mask2, (out_size*2,out_size*2))\n                images.append(crop)\n                masks1.append(crop_mask1)\n                masks2.append(crop_mask2)\n            else:\n                tmp_crop =  np.zeros((crop_size*2, crop_size*2,3), dtype = np.uint8)\n                tmp_mask1 =  np.zeros((crop_size*2, crop_size*2), dtype = np.uint8)\n                tmp_mask2 =  np.zeros((crop_size*2, crop_size*2), dtype = np.uint8)\n                if x1 < 0:\n                    if y2>=720:\n                        tmp_crop[crop_size*2-cr_y:,:cr_x] = crop\n                        tmp_mask1[crop_size*2-cr_y:,:cr_x] = crop_mask1\n                        tmp_mask2[crop_size*2-cr_y:,:cr_x] = crop_mask2\n                    else:\n                        tmp_crop[:cr_y,:cr_x] = crop\n                        tmp_mask1[:cr_y,:cr_x] = crop_mask1\n                        tmp_mask2[:cr_y,:cr_x] = crop_mask2\n\n                elif x2> 1280:\n                    if y2>=720:\n                        tmp_crop[crop_size*2-cr_y:,crop_size*2-cr_x:] = crop\n                        tmp_mask1[crop_size*2-cr_y:,crop_size*2-cr_x:] = crop_mask1\n                        tmp_mask2[crop_size*2-cr_y:,crop_size*2-cr_x:] = crop_mask2\n                    else:\n                        tmp_crop[:cr_y,crop_size*2-cr_x:] = crop\n                        tmp_mask1[:cr_y,crop_size*2-cr_x:] = crop_mask1\n                        tmp_mask2[:cr_y,crop_size*2-cr_x:] = crop_mask2\n\n                tmp_crop = cv2.resize(tmp_crop, (out_size*2,out_size*2))\n                tmp_mask1 = cv2.resize(tmp_mask1, (out_size*2,out_size*2))\n                tmp_mask2 = cv2.resize(tmp_mask2, (out_size*2,out_size*2))\n                images.append(tmp_crop)\n                masks1.append(tmp_mask1)\n                masks2.append(tmp_mask2)\n        else:\n            empty_count +=1\n            crop =  np.zeros((out_size*2, out_size*2,3), dtype = np.uint8)\n            crop_mask1 =  np.zeros((out_size*2, out_size*2), dtype = np.uint8)\n            crop_mask2 =  np.zeros((out_size*2, out_size*2), dtype = np.uint8)\n            images.append(crop)\n            masks1.append(crop_mask1)\n            masks2.append(crop_mask2)\n    return images, masks1, masks2","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:28.976631Z","iopub.execute_input":"2023-03-05T01:33:28.976983Z","iopub.status.idle":"2023-03-05T01:33:29.007379Z","shell.execute_reply.started":"2023-03-05T01:33:28.976947Z","shell.execute_reply":"2023-03-05T01:33:29.005683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Crop endzone, sideline for player-ground function**\n","metadata":{}},{"cell_type":"code","source":"def get_sample_G(vid, fr_id, idx1, idx2, window_size = 20, out_size = 128, scale=4):\n    ws = []\n    hs = []\n    for fr in range(fr_id + window_size[0], fr_id + window_size[-1] + 1):\n        if fr in det_dict[vid]:\n            if idx1 in det_dict[vid][fr]: \n                x, y, w, h = det_dict[vid][fr][idx1]['box']\n                ws.append(w)\n                hs.append(h)\n    \n    if len(ws)>0:\n        crop_size = int(scale*max(np.mean(ws), np.mean(hs)))\n    else:\n        crop_size = out_size\n\n    bboxes = []\n    for fr in range(fr_id + window_size[0], fr_id + window_size[-1] + 1):\n        if fr in det_dict[vid]:\n            if idx1 in det_dict[vid][fr]: \n                x, y, w, h = det_dict[vid][fr][idx1]['box']\n                xc = x + w/2\n                yc = y + h/2\n\n                bboxes.append([xc-crop_size, yc-crop_size, xc+crop_size, yc+crop_size])\n            else:\n                bboxes.append([np.nan, np.nan, np.nan, np.nan])\n        else:\n            bboxes.append([np.nan, np.nan, np.nan, np.nan])\n\n    bboxes = pd.DataFrame(bboxes).interpolate(limit_direction='both').values\n    images = []\n    masks1 = []\n    empty_count = 0\n    for i, ii in enumerate(window_size):\n        if bboxes.sum() > 0:\n            fr = ii + fr_id\n            path = f'{vid}_{fr}'\n\n            if path in image_dict:\n                if scale != 4:\n                    image = image_dict1[path]\n                    p_path = f'{vid}_{fr-2}'\n                    if p_path in image_dict1:\n                        p_img = image_dict1[p_path]\n                    else:\n                        p_img = image\n\n                    n_path = f'{vid}_{fr+2}'\n                    if n_path in image_dict1:\n                        n_img = image_dict1[n_path]\n                    else:\n                        n_img = image\n\n                    image = np.stack([p_img, image, n_img]).transpose(1,2,0)\n                else:\n                    image = image_dict[path]\n            else:\n                image = np.zeros((720, 1280,3), dtype = np.uint8)\n                empty_count +=1\n\n            mask1 = np.zeros((720, 1280), dtype = np.uint8)\n\n            x1, y1, x2, y2 = list(map(int, bboxes[ii-window_size[0]]))\n\n            y1 = y1 + int(0.2*crop_size)\n            x2 = x1 + crop_size*2\n            y2 = y1 + crop_size*2\n\n            if fr in det_dict[vid]:\n                if idx1 in det_dict[vid][fr]: \n                    x, y, w, h = det_dict[vid][fr][idx1]['box']\n                    # cv2.rectangle(image, (x, y), (x+w, y+h), (0,0,255), 2)\n                    # mask1[y:y+h, x:x+w] = 255\n                    cv2.circle(mask1, (x+w//2, y+h//2), int(0.3*h+0.3*w), 255, thickness=-1)\n\n\n            crop = image[y1:y2, x1:x2]\n            crop_mask1 = mask1[y1:y2, x1:x2]\n\n            cr_y, cr_x = crop.shape[:2]\n            if cr_x == crop_size*2 and cr_y == crop_size*2:\n                crop = cv2.resize(crop, (out_size*2,out_size*2))\n                crop_mask1 = cv2.resize(crop_mask1, (out_size*2,out_size*2))\n                images.append(crop)\n                masks1.append(crop_mask1)\n            else:\n                tmp_crop =  np.zeros((crop_size*2, crop_size*2,3), dtype = np.uint8)\n                tmp_mask1 =  np.zeros((crop_size*2, crop_size*2), dtype = np.uint8)\n                if x1 < 0:\n                    if y2>=720:\n                        tmp_crop[crop_size*2-cr_y:,:cr_x] = crop\n                        tmp_mask1[crop_size*2-cr_y:,:cr_x] = crop_mask1\n                    else:\n                        tmp_crop[:cr_y,:cr_x] = crop\n                        tmp_mask1[:cr_y,:cr_x] = crop_mask1\n\n                elif x2> 1280:\n                    if y2>=720:\n                        tmp_crop[crop_size*2-cr_y:,crop_size*2-cr_x:] = crop\n                        tmp_mask1[crop_size*2-cr_y:,crop_size*2-cr_x:] = crop_mask1\n                    else:\n                        tmp_crop[:cr_y,crop_size*2-cr_x:] = crop\n                        tmp_mask1[:cr_y,crop_size*2-cr_x:] = crop_mask1\n\n                tmp_crop = cv2.resize(tmp_crop, (out_size*2,out_size*2))\n                tmp_mask1 = cv2.resize(tmp_mask1, (out_size*2,out_size*2))\n                images.append(tmp_crop)\n                masks1.append(tmp_mask1)\n        else:\n            empty_count +=1\n            # crop =  np.zeros((crop_size*2, crop_size*2,3))\n            crop =  np.zeros((out_size*2, out_size*2,3), dtype = np.uint8)\n            crop_mask1 =  np.zeros((out_size*2, out_size*2), dtype = np.uint8)\n            images.append(crop)\n            masks1.append(crop_mask1)\n    return images, masks1, empty_count","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:29.01043Z","iopub.execute_input":"2023-03-05T01:33:29.010769Z","iopub.status.idle":"2023-03-05T01:33:29.035718Z","shell.execute_reply.started":"2023-03-05T01:33:29.010741Z","shell.execute_reply":"2023-03-05T01:33:29.034799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Test dataset**","metadata":{}},{"cell_type":"code","source":"class TestDataset(torch.utils.data.Dataset):\n    def __init__(self, df, image_dict, tfms=None, cfg=None):\n        self.df = df.reset_index(drop=True)\n        self.image_dict = image_dict\n        self.transform = A.ReplayCompose([\n        A.Resize(cfg.img_size, cfg.img_size, interpolation=1, p=1),\n    ])\n        self.cfg = cfg\n        \n        self.trk_step = 0\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, index):\n        row = self.df.loc[index]\n        idx1 = int(row['nfl_player_id_1'])\n        if self.cfg.is_G:\n            idx2 = row['nfl_player_id_2']\n        else:\n            idx2 = int(row['nfl_player_id_2'])\n        fr_id = int(row['frame'])\n        step = int(row['step'])\n        \n        if self.cfg.is_G:\n            e_images, e_masks1, _ = get_sample_G(e_vid, fr_id, idx1, idx2, window_size = cfg.window_size, scale=cfg.scale)\n            s_images, s_masks1, _ = get_sample_G(s_vid, fr_id, idx1, idx2, window_size = cfg.window_size, out_size = 128, scale=cfg.scale)\n        else:\n            e_images, e_masks1, e_masks2 = get_sample(e_vid, fr_id, idx1, idx2, window_size = cfg.window_size)\n            s_images, s_masks1, s_masks2 = get_sample(s_vid, fr_id, idx1, idx2, window_size = cfg.window_size, out_size = 128)\n        \n        images_e = []\n        images_s = []\n        for ii in range(len(e_images)):\n            e_img = e_images[ii]\n            e_img[e_masks1[ii]>100] = 255\n            if not  self.cfg.is_G:\n                e_img[e_masks2[ii]>100] = 0\n\n            s_img = s_images[ii]\n            s_img[s_masks1[ii]>100] = 255\n            if not self.cfg.is_G:\n                s_img[s_masks2[ii]>100] = 0\n\n            images_e.append(e_img)\n            images_s.append(s_img)\n\n\n        e_images = np.array(images_e)\n        s_images = np.array(images_s)\n        \n        \n        num_empty = 0\n        for img in e_images:\n            h, w, c = img.shape \n            if np.sum(img<2)/(h*w*c) > 0.9:\n                num_empty += 1\n        if len(e_images) - num_empty < 2:\n            # print('e empty')\n            e_images = s_images.copy()\n\n        num_empty = 0\n        for img in s_images:\n            h, w, c = img.shape \n            if np.sum(img<2)/(h*w*c) > 0.9:\n                num_empty += 1\n        if len(s_images) - num_empty < 2:\n            # print('s empty')\n            s_images = e_images.copy()\n            \n        \n        if self.trk_step == 0:\n            self.trk_step = len(e_images)//2\n\n        if not self.cfg.is_G:\n            trk_images = self.render_trk(vid, step, idx1, idx2)\n            \n        images = []\n        for i in range(len(s_images)):\n            if not self.cfg.is_G:\n                trk_img = trk_images[i]\n                img = np.hstack([e_images[i], trk_img, s_images[i]])\n            else:\n                img = np.hstack([e_images[i], s_images[i]])\n            images.append(img)\n        img = np.array(images)\n        img = img.transpose(3,0,1,2) #C T H W\n\n        img = img/255\n\n        return torch.tensor(img, dtype=torch.float)\n    \n    def render_trk(self, vid, step, idx1, idx2):\n        # simulate tracking data as image 128x256x3\n        shift_x = 0\n        shift_y = 0\n\n        d_x = 5\n        scale = 60/d_x\n\n        idx = f'{vid}_{step}'\n        images = []\n        x1 = trk_dict[idx][idx1]['x']\n        y1 = trk_dict[idx][idx1]['y']\n\n        x2 = trk_dict[idx][idx2]['x']\n        y2 = trk_dict[idx][idx2]['y']\n\n        xc = 0.5*x1 + 0.5*x2\n        yc = 0.5*y1 + 0.5*y2\n\n        for st in range(step-self.trk_step, step + self.trk_step + 1):\n            #print('=======',step, self.trk_step)\n            this_idx = f'{vid}_{st}'\n            img = np.zeros((3, self.cfg.img_size, 128), dtype=np.uint8)\n            if this_idx in trk_dict:\n                for p_id, meta in trk_dict[this_idx].items():\n                    x = meta['x']\n                    y = meta['y']\n                    t = meta['t']\n\n                    x = x - xc + d_x\n                    y = y - yc + (2*d_x)\n\n                    x = round(x*scale) + shift_x\n                    y = round(y*scale) + shift_y\n\n                    if x>0 and y>0 and x<128 and y<self.cfg.img_size:\n                        radius = 3\n                        val = 125\n                        if p_id in [idx1, idx2]:\n                            radius = 5\n                            val = 255\n\n                        cv2.circle(img[0], (x, y), radius, val, thickness=-1)\n                        if t == 'home':\n                            cv2.circle(img[1], (x, y), radius, val, thickness=-1)\n                        else:\n                            cv2.circle(img[2], (x, y), radius, val, thickness=-1)\n\n\n            img = img.transpose(1,2,0)\n            images.append(img)\n\n        return images","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:29.038539Z","iopub.execute_input":"2023-03-05T01:33:29.038797Z","iopub.status.idle":"2023-03-05T01:33:29.066402Z","shell.execute_reply.started":"2023-03-05T01:33:29.038772Z","shell.execute_reply":"2023-03-05T01:33:29.065321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Function to run inference for player-player contact**","metadata":{}},{"cell_type":"code","source":"def predict_pair(window_size, df_p, models):\n    if df_p.shape[0] == 0:\n        return df_p, 0\n    cfg.is_G = 0\n    cfg.window_size = window_size\n    \n    test_dataset = TestDataset(df_p, image_dict, cfg=cfg)\n    test_loader = torch.utils.data.DataLoader(test_dataset,\n            batch_size=8,\n            num_workers=4,\n            shuffle=False)\n    \n    y_preds = []\n    with torch.no_grad():\n        bar = tqdm(test_loader)\n        for batch_idx, images in enumerate(bar):\n            images = images.float().to(device)\n            for ii, model in enumerate(models):\n                with torch.cuda.amp.autocast():\n                    if ii%2==0:\n                        pred = model(images)\n                    else:\n                        pred = model(images.flip(-1))\n                    logit = pred['out1'].sigmoid().detach().cpu().numpy()\n                if ii == 0:\n                    out = logit\n                else:\n                    out += logit\n            out = out/len(models)\n            y_preds.append(out)\n    y_preds = np.concatenate(y_preds)\n    print(y_preds.shape, df_p.shape)\n    df_p['pred'] = y_preds\n    return df_p, 1","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:29.068047Z","iopub.execute_input":"2023-03-05T01:33:29.068865Z","iopub.status.idle":"2023-03-05T01:33:29.081421Z","shell.execute_reply.started":"2023-03-05T01:33:29.068826Z","shell.execute_reply":"2023-03-05T01:33:29.080393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Function to run inference for player-ground contact**","metadata":{}},{"cell_type":"code","source":"def predict_g(window_size, df_g, models, scale):\n    if df_g.shape[0] == 0:\n        return df_g, 0\n    cfg.is_G = 1\n    cfg.window_size = window_size\n    cfg.scale = scale \n    \n    test_dataset = TestDataset(df_g, image_dict, cfg=cfg)\n    test_loader = torch.utils.data.DataLoader(test_dataset,\n            batch_size=8,\n            num_workers=4,\n            shuffle=False)\n    y_preds = []\n    with torch.no_grad():\n        bar = tqdm(test_loader)\n        for batch_idx, images in enumerate(bar):\n            images = images.float().to(device)\n            for ii, model in enumerate(models):\n                with torch.cuda.amp.autocast():\n                    if ii%2==0:\n                        pred = model(images)\n                    else:\n                        pred = model(images.flip(-1))\n                    logit = pred['out1'].sigmoid().detach().cpu().numpy()\n                if ii == 0:\n                    out = logit\n                else:\n                    out += logit\n            out = out/len(models)\n            y_preds.append(out)\n    y_preds = np.concatenate(y_preds)\n    print(y_preds.shape, df_g.shape)\n    df_g['pred'] = y_preds\n    return df_g, 1","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:29.082797Z","iopub.execute_input":"2023-03-05T01:33:29.083244Z","iopub.status.idle":"2023-03-05T01:33:29.094178Z","shell.execute_reply.started":"2023-03-05T01:33:29.083206Z","shell.execute_reply":"2023-03-05T01:33:29.093215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Load trained models**","metadata":{}},{"cell_type":"code","source":"class config:\n    model_name = 'r50ir' \n    pool_type = 'avg'\n    is_G = 0\n    img_size = 256","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:29.098513Z","iopub.execute_input":"2023-03-05T01:33:29.099508Z","iopub.status.idle":"2023-03-05T01:33:29.108672Z","shell.execute_reply.started":"2023-03-05T01:33:29.09948Z","shell.execute_reply":"2023-03-05T01:33:29.107706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = config","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:29.110295Z","iopub.execute_input":"2023-03-05T01:33:29.111209Z","iopub.status.idle":"2023-03-05T01:33:29.119402Z","shell.execute_reply.started":"2023-03-05T01:33:29.111173Z","shell.execute_reply":"2023-03-05T01:33:29.118433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = \"cuda\"","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:29.121061Z","iopub.execute_input":"2023-03-05T01:33:29.121427Z","iopub.status.idle":"2023-03-05T01:33:29.130959Z","shell.execute_reply.started":"2023-03-05T01:33:29.121391Z","shell.execute_reply":"2023-03-05T01:33:29.130014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**player-player models, single model train 4 seeds with all data**","metadata":{}},{"cell_type":"code","source":"models_pair = []\nfor fold in [5,6,7,8]:\n    ckpt_path = f'/kaggle/input/nfl3-ckpts/r50ir_csn_c15_m1_d2_all_last_f{fold}.pth'\n    model = NModel(cfg)\n    checkpoint = torch.load(ckpt_path, map_location=\"cpu\")\n    model.load_state_dict(checkpoint)\n    model.to(device)\n    model.eval()\n    models_pair.append(model)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:29.132609Z","iopub.execute_input":"2023-03-05T01:33:29.13305Z","iopub.status.idle":"2023-03-05T01:33:37.55776Z","shell.execute_reply.started":"2023-03-05T01:33:29.133014Z","shell.execute_reply":"2023-03-05T01:33:37.556777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**player-ground, single model train 3 seeds with all data (config 11)**","metadata":{}},{"cell_type":"code","source":"models_g_base = []\nfor fold in [6,7,8]:\n    ckpt_path = f'/kaggle/input/nfl3-ckpts/r50ir_csn_c11_m1_d2_G_all_last_f{fold}.pth'\n    model = NModel(cfg)\n    checkpoint = torch.load(ckpt_path, map_location=\"cpu\")\n    model.load_state_dict(checkpoint)\n    model.to(device)\n    model.eval()\n    models_g_base.append(model)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:40:50.31364Z","iopub.execute_input":"2023-03-05T01:40:50.314019Z","iopub.status.idle":"2023-03-05T01:40:53.97472Z","shell.execute_reply.started":"2023-03-05T01:40:50.313988Z","shell.execute_reply":"2023-03-05T01:40:53.973769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**player-ground, single model train 3 seeds with all data (config 15)**","metadata":{}},{"cell_type":"code","source":"models_g = []\nfor fold in [6,7,8]:\n    ckpt_path = f'/kaggle/input/nfl3-ckpts/r50ir_csn_c15_m1_d2_G_all_last_f{fold}.pth'\n    model_g = NModel(cfg)\n    checkpoint = torch.load(ckpt_path, map_location=\"cpu\")\n    model_g.load_state_dict(checkpoint)\n    model_g.to(device)\n    model_g.eval()\n    models_g.append(model_g)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:43:06.399417Z","iopub.execute_input":"2023-03-05T01:43:06.399781Z","iopub.status.idle":"2023-03-05T01:43:10.219914Z","shell.execute_reply.started":"2023-03-05T01:43:06.399751Z","shell.execute_reply":"2023-03-05T01:43:10.218964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Load results of xgb preprocessing step and filter easy negative samples**","metadata":{}},{"cell_type":"code","source":"#filter easy negative player-player \ndf = pd.read_csv('test_pair_xgb_v1.csv')\np_df = df[df.pred>0.002][['contact_id', 'frame']]","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:53:43.914972Z","iopub.execute_input":"2023-03-05T01:53:43.915656Z","iopub.status.idle":"2023-03-05T01:53:43.935483Z","shell.execute_reply.started":"2023-03-05T01:53:43.91562Z","shell.execute_reply":"2023-03-05T01:53:43.934512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#filter easy negative player-ground \ng_df = pd.read_csv('test_G.csv')\ng_df = g_df[g_df.pred>0.0005][['contact_id', 'frame']]","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:53:46.774794Z","iopub.execute_input":"2023-03-05T01:53:46.775166Z","iopub.status.idle":"2023-03-05T01:53:46.790215Z","shell.execute_reply.started":"2023-03-05T01:53:46.775118Z","shell.execute_reply":"2023-03-05T01:53:46.789338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([p_df, g_df])","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:53:49.086137Z","iopub.execute_input":"2023-03-05T01:53:49.087114Z","iopub.status.idle":"2023-03-05T01:53:49.093677Z","shell.execute_reply.started":"2023-03-05T01:53:49.087078Z","shell.execute_reply":"2023-03-05T01:53:49.092722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['vid'] = df['contact_id'].apply(lambda x: '_'.join(x.split('_')[:2]))\ndf['step'] = df['contact_id'].apply(lambda x: int(x.split('_')[2]))\ndf['nfl_player_id_1'] = df['contact_id'].apply(lambda x: int(x.split('_')[3]))\ndf['nfl_player_id_2'] = df['contact_id'].apply(lambda x: x.split('_')[4])\nvids = df.vid.unique()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:53:51.142694Z","iopub.execute_input":"2023-03-05T01:53:51.143189Z","iopub.status.idle":"2023-03-05T01:53:51.175421Z","shell.execute_reply.started":"2023-03-05T01:53:51.143123Z","shell.execute_reply":"2023-03-05T01:53:51.174393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:53:53.382233Z","iopub.execute_input":"2023-03-05T01:53:53.382602Z","iopub.status.idle":"2023-03-05T01:53:53.404777Z","shell.execute_reply.started":"2023-03-05T01:53:53.382571Z","shell.execute_reply":"2023-03-05T01:53:53.403912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#baseline helmet bboxes and tracking position is stored in dictionary for converience.\ndet_dict = np.load('det_dict.npy', allow_pickle=True).item()\ntrk_dict = np.load('trk_pos.npy', allow_pickle=True).item()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:53:55.73663Z","iopub.execute_input":"2023-03-05T01:53:55.737052Z","iopub.status.idle":"2023-03-05T01:53:56.125023Z","shell.execute_reply.started":"2023-03-05T01:53:55.737007Z","shell.execute_reply":"2023-03-05T01:53:56.124015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#define list of frame in each config\nwindow_size_g = [-54, -48, -42, -36, -30, -24, -18, -13, -8, -4, -2, 0, 2, 4, 8, 13, 18, 24, 30, 36, 42, 48, 54]\n\nwindow_size_g_c15 = [-72, -66, -60, -54, -48, -42, -36, -30, -24, -18, -13, -8, -4, -2, 0, 2, 4, 8, 13, 18, 24, 30, 36]\nwindow_size_g_c15 = [x+6 for x in window_size_g_c15]\n\nframe_shift = 6\nwindow_size_p = [-44, -37, -30, -24, -18, -13, -8, -4, -2, 0, 2, 4, 8, 13, 18, 24, 30, 37]\nwindow_size_p = [x+frame_shift for x in window_size_p]","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:53:58.993588Z","iopub.execute_input":"2023-03-05T01:53:58.993954Z","iopub.status.idle":"2023-03-05T01:53:59.001885Z","shell.execute_reply.started":"2023-03-05T01:53:58.993923Z","shell.execute_reply":"2023-03-05T01:53:59.000927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Main inference code**","metadata":{}},{"cell_type":"code","source":"vid_dir = '/kaggle/input/nfl-player-contact-detection/test/'\nresults_g_base = []\nresults_g_pred = []\nresults_p_base = []\nresults_p_pred = []\nfor vid in tqdm(vids):\n    print(f'process vid {vid}')\n    #vid = vids[0]\n    e_vid = vid + f'_Endzone'\n    s_vid = vid + f'_Sideline'\n    e_vid_path = f'{vid_dir}/{e_vid}.mp4'\n    s_vid_path = f'{vid_dir}/{s_vid}.mp4'\n    \n    image_dict = {}\n    image_dict1 = {}\n    \n    cap = cv2.VideoCapture(e_vid_path)\n    cap.set(cv2.CAP_PROP_POS_FRAMES,230)\n    frame_count = 230\n    while 1:\n        ret, frame = cap.read()\n        if not ret:\n            break\n        kk = f'{e_vid}_{frame_count}'\n        image_dict[kk] = frame\n        image_dict1[kk] = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n        frame_count +=1\n\n    cap = cv2.VideoCapture(s_vid_path)\n    cap.set(cv2.CAP_PROP_POS_FRAMES, 230)\n    frame_count = 230\n    while 1:\n        ret, frame = cap.read()\n        if not ret:\n            break\n        kk = f'{s_vid}_{frame_count}'\n        image_dict[kk] = frame\n        image_dict1[kk] = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n        frame_count +=1\n        \n    df_g = df[(df.vid==vid)&(df.nfl_player_id_2=='G')]\n    df_p = df[(df.vid==vid)&(df.nfl_player_id_2!='G')]\n    \n    #df_p_base, nona_p_base = predict_pair(window_size_p, df_p, models_pair_base)\n    #if nona_p_base:\n    #    df_p = df_p_base[df_p_base.pred>0.01]\n    df_p_pred, nona_p_pred = predict_pair(window_size_p, df_p, models_pair)\n    \n    df_g_base, nona_g_base = predict_g(window_size_g, df_g, models_g_base, scale=4)\n    if nona_g_base:\n        df_g = df_g_base[df_g_base.pred>0.01]\n        \n    df_g_pred, nona_g_pred = predict_g(window_size_g_c15, df_g, models_g, scale=4.5)\n    \n    #if nona_p_base:\n    #    results_p_base.append(df_p_base)\n    if nona_p_pred:\n        results_p_pred.append(df_p_pred)\n        \n    if nona_g_base:\n        results_g_base.append(df_g_base)\n    if nona_g_pred:\n        results_g_pred.append(df_g_pred)\n        \n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:54:01.488017Z","iopub.execute_input":"2023-03-05T01:54:01.488963Z","iopub.status.idle":"2023-03-05T02:09:36.984515Z","shell.execute_reply.started":"2023-03-05T01:54:01.488925Z","shell.execute_reply":"2023-03-05T02:09:36.983425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_g_base = pd.concat(results_g_base)\ndf_g_pred = pd.concat(results_g_pred)\n#df_p_base = pd.concat(results_p_base)\ndf_p_pred = pd.concat(results_p_pred)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:12:40.429125Z","iopub.execute_input":"2023-03-05T02:12:40.429534Z","iopub.status.idle":"2023-03-05T02:12:40.442768Z","shell.execute_reply.started":"2023-03-05T02:12:40.429501Z","shell.execute_reply":"2023-03-05T02:12:40.441821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**XGB post processing player-ground**","metadata":{}},{"cell_type":"code","source":"import xgboost as xgb","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:12:42.983667Z","iopub.execute_input":"2023-03-05T02:12:42.98404Z","iopub.status.idle":"2023-03-05T02:12:43.021681Z","shell.execute_reply.started":"2023-03-05T02:12:42.984007Z","shell.execute_reply":"2023-03-05T02:12:43.020823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_oof(df_pred):\n    pred_step_dict = {}\n    for i, row in df_pred.iterrows():\n        c_id = row['contact_id']\n        pred = row['pred']\n        gk, gp, st, idx1, idx2 = c_id.split('_')\n        idx = f'{gk}_{gp}_{idx1}_{idx2}_{st}'\n        pred_step_dict[idx] = row['pred']\n\n    return pred_step_dict\n\ndef feature_engineering_G():\n    train_df = pd.read_csv('test_G.csv')\n    train_df['step'] = train_df['contact_id'].apply(lambda x: int(x.split('_')[2]))\n    train_df['vid'] = train_df['contact_id'].apply(lambda x: '_'.join(x.split('_')[:2]))\n    train_df['nfl_player_id_1'] = train_df['contact_id'].apply(lambda x: int(x.split('_')[3]))\n    train_df['nfl_player_id_2'] = 'G'\n\n    pred_step_dict1 = get_oof(train_df)\n\n    pred_step_dict = get_oof(df_g_base)\n    pred_step_dict2 = get_oof(df_g_pred)\n    \n    results = []\n    for _, row in tqdm(train_df.iterrows()):\n        idx1 = row['nfl_player_id_1']\n        idx2 = row['nfl_player_id_2']\n        vid = row['vid']\n        step = row['step']\n\n        item = {'contact_id':row['contact_id'], 'step':row['step'], 'frame': row['frame']}\n        item['nfl_player_id_1'] = row['nfl_player_id_1']\n        item['nfl_player_id_2'] = row['nfl_player_id_2']\n        item['vid'] = row['vid']\n\n        for i in range(-15,15):\n            this_idx = f'{vid}_{idx1}_{idx2}_{step+i}'\n            prob = 0\n            weight = 0\n            if this_idx in pred_step_dict:\n                prob += pred_step_dict[this_idx]\n                weight += 1\n\n            if this_idx in pred_step_dict2:\n                prob += pred_step_dict2[this_idx]\n                weight += 1\n\n            if weight > 0:\n                item[f'prob_{i}'] = prob/weight\n            else:\n                item[f'prob_{i}'] = np.nan\n\n            if this_idx in pred_step_dict1:\n                item[f'prob1_{i}'] = pred_step_dict1[this_idx]\n            else:\n                item[f'prob1_{i}'] = np.nan\n\n        results.append(item) \n\n    train_df = pd.DataFrame(results)\n\n    results = []\n    for i, row in tqdm(train_df.iterrows()):\n        item = {}\n        for i in range(-15,15):\n            if i>-10 and i < 10:\n                item[f'prob_{i}'] = row[f'prob_{i}']\n                item[f'prob1_{i}'] = row[f'prob1_{i}']\n            item[f'prob3_{i}'] = 0.85*row[f'prob_{i}']  + 0.15*row[f'prob1_{i}']\n\n        feature_cols = list(item.keys())\n\n        item['contact_id'] = row['contact_id']\n\n        results.append(item)\n\n\n    train_df = pd.DataFrame(results)\n\n    return train_df, feature_cols\n\ntrain_df_g, feature_cols = feature_engineering_G()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:00.521356Z","iopub.execute_input":"2023-03-05T02:40:00.521728Z","iopub.status.idle":"2023-03-05T02:40:04.263182Z","shell.execute_reply.started":"2023-03-05T02:40:00.5217Z","shell.execute_reply":"2023-03-05T02:40:04.262191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid = train_df_g[feature_cols]\ndvalid = xgb.DMatrix(x_valid)\nfor fold in [0,1,2,3,4]:\n    model_path = f'/kaggle/input/nfl3-ckpts/g_pp/xgb_fold{fold}_xgb_1st.model'\n    model = xgb.Booster()\n    model.load_model(model_path)\n    \n    if fold==0:\n        pred_i = model.predict(dvalid) \n    else:\n        pred_i += model.predict(dvalid)\n    print(pred_i.shape)\npred_i = pred_i/5\n","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:06.053633Z","iopub.execute_input":"2023-03-05T02:40:06.053981Z","iopub.status.idle":"2023-03-05T02:40:06.561192Z","shell.execute_reply.started":"2023-03-05T02:40:06.053952Z","shell.execute_reply":"2023-03-05T02:40:06.560126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_g['pred'] = pred_i\ntrain_df_g = train_df_g[['contact_id', 'pred']]","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:10.931597Z","iopub.execute_input":"2023-03-05T02:40:10.932058Z","iopub.status.idle":"2023-03-05T02:40:10.939546Z","shell.execute_reply.started":"2023-03-05T02:40:10.932008Z","shell.execute_reply":"2023-03-05T02:40:10.938404Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_g[train_df_g.pred>0.5].shape","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:13.23627Z","iopub.execute_input":"2023-03-05T02:40:13.236838Z","iopub.status.idle":"2023-03-05T02:40:13.246589Z","shell.execute_reply.started":"2023-03-05T02:40:13.236793Z","shell.execute_reply":"2023-03-05T02:40:13.245244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**XGB post processing player-player**","metadata":{}},{"cell_type":"code","source":"def feature_engineering_p():\n    train_df = pd.read_csv('test_pair_xgb_v1.csv')\n    train_df['step'] = train_df['contact_id'].apply(lambda x: int(x.split('_')[2]))\n    train_df['vid'] = train_df['contact_id'].apply(lambda x: '_'.join(x.split('_')[:2]))\n    train_df['nfl_player_id_1'] = train_df['contact_id'].apply(lambda x: int(x.split('_')[3]))\n    train_df['nfl_player_id_2'] = train_df['contact_id'].apply(lambda x: int(x.split('_')[4]))\n    \n    print(train_df.shape)\n\n    #pred_step_dict = get_oof(df_p_base)\n    pred_step_dict2 = get_oof(df_p_pred)\n    \n    xgb_df = pd.read_csv('test_pair_xgb_v1.csv')\n    xgb_dict = get_oof(xgb_df)\n\n    results = []\n    for _, row in tqdm(train_df.iterrows()):\n        idx1 = row['nfl_player_id_1']\n        idx2 = row['nfl_player_id_2']\n        step = row['step']\n        vid = row['vid']\n\n        item = {'contact_id':row['contact_id']}\n        item['nfl_player_id_1'] = row['nfl_player_id_1']\n        item['nfl_player_id_2'] = row['nfl_player_id_2']\n\n        for i in range(-10,10):\n            this_idx = f'{vid}_{idx1}_{idx2}_{step+i}'\n            prob = 0\n            weight = 0\n            if this_idx in pred_step_dict2:\n                prob += pred_step_dict2[this_idx]\n                weight += 1\n\n            if weight > 0:\n                item[f'prob_{i}'] = prob/weight\n            else:\n                item[f'prob_{i}'] = np.nan\n\n            if this_idx in xgb_dict:\n                item[f'prob1_{i}'] = xgb_dict[this_idx]\n            else:\n                item[f'prob1_{i}'] = np.nan\n\n        results.append(item) \n\n    train_df = pd.DataFrame(results)\n\n    results = []\n    for i, row in tqdm(train_df.iterrows()):\n        item = {}\n        for i in range(-10,10):\n            item[f'prob4_{i}'] = 0.8*row[f'prob_{i}'] + 0.2*row[f'prob1_{i}']\n\n        feature_cols = list(item.keys())\n\n        item['contact_id'] = row['contact_id']\n\n        results.append(item)\n\n    train_df = pd.DataFrame(results)\n\n    return train_df, feature_cols\n\ntrain_df_p, feature_cols = feature_engineering_p()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:18.801086Z","iopub.execute_input":"2023-03-05T02:40:18.801672Z","iopub.status.idle":"2023-03-05T02:40:21.078543Z","shell.execute_reply.started":"2023-03-05T02:40:18.801638Z","shell.execute_reply":"2023-03-05T02:40:21.077651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid = train_df_p[feature_cols]\ndvalid = xgb.DMatrix(x_valid)\nfor fold in [0,1,2,3,4]:\n    model_path = f'/kaggle/input/nfl3-ckpts/pair_pp/xgb_fold{fold}_xgb_1st.model'\n    model = xgb.Booster()\n    model.load_model(model_path)\n    \n    if fold==0:\n        pred_i = model.predict(dvalid) \n    else:\n        pred_i += model.predict(dvalid)\n    print(pred_i.shape)\npred_i = pred_i/5\n","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:24.947143Z","iopub.execute_input":"2023-03-05T02:40:24.947759Z","iopub.status.idle":"2023-03-05T02:40:26.382473Z","shell.execute_reply.started":"2023-03-05T02:40:24.947706Z","shell.execute_reply":"2023-03-05T02:40:26.381518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_p['pred'] = pred_i*0.95\ntrain_df_p = train_df_p[['contact_id', 'pred']]","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:28.720515Z","iopub.execute_input":"2023-03-05T02:40:28.720869Z","iopub.status.idle":"2023-03-05T02:40:28.728048Z","shell.execute_reply.started":"2023-03-05T02:40:28.720838Z","shell.execute_reply":"2023-03-05T02:40:28.727111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_p[train_df_p.pred>0.5].shape","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:30.875054Z","iopub.execute_input":"2023-03-05T02:40:30.875557Z","iopub.status.idle":"2023-03-05T02:40:30.887712Z","shell.execute_reply.started":"2023-03-05T02:40:30.875517Z","shell.execute_reply":"2023-03-05T02:40:30.886452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Merge results and save to submission.csv**","metadata":{}},{"cell_type":"code","source":"df = pd.concat([train_df_p, train_df_g])","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:39.63233Z","iopub.execute_input":"2023-03-05T02:40:39.632689Z","iopub.status.idle":"2023-03-05T02:40:39.639035Z","shell.execute_reply.started":"2023-03-05T02:40:39.632659Z","shell.execute_reply":"2023-03-05T02:40:39.638051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:41.298579Z","iopub.execute_input":"2023-03-05T02:40:41.299034Z","iopub.status.idle":"2023-03-05T02:40:41.307271Z","shell.execute_reply.started":"2023-03-05T02:40:41.298991Z","shell.execute_reply":"2023-03-05T02:40:41.306177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['contact'] = 1*(df['pred']>0.38)\ndf = df[['contact_id', 'contact']].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:43.182283Z","iopub.execute_input":"2023-03-05T02:40:43.182719Z","iopub.status.idle":"2023-03-05T02:40:43.193115Z","shell.execute_reply.started":"2023-03-05T02:40:43.182679Z","shell.execute_reply":"2023-03-05T02:40:43.192104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/nfl-player-contact-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:46.691305Z","iopub.execute_input":"2023-03-05T02:40:46.691663Z","iopub.status.idle":"2023-03-05T02:40:46.722816Z","shell.execute_reply.started":"2023-03-05T02:40:46.691633Z","shell.execute_reply":"2023-03-05T02:40:46.721899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:48.46112Z","iopub.execute_input":"2023-03-05T02:40:48.461643Z","iopub.status.idle":"2023-03-05T02:40:48.473115Z","shell.execute_reply.started":"2023-03-05T02:40:48.461598Z","shell.execute_reply":"2023-03-05T02:40:48.472008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.concat([df, sub])","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:50.5112Z","iopub.execute_input":"2023-03-05T02:40:50.512351Z","iopub.status.idle":"2023-03-05T02:40:50.52052Z","shell.execute_reply.started":"2023-03-05T02:40:50.512306Z","shell.execute_reply":"2023-03-05T02:40:50.519666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.contact.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:52.617873Z","iopub.execute_input":"2023-03-05T02:40:52.618257Z","iopub.status.idle":"2023-03-05T02:40:52.628202Z","shell.execute_reply.started":"2023-03-05T02:40:52.618224Z","shell.execute_reply":"2023-03-05T02:40:52.627222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = df1.drop_duplicates(subset=['contact_id'])","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:54.96623Z","iopub.execute_input":"2023-03-05T02:40:54.966652Z","iopub.status.idle":"2023-03-05T02:40:54.990386Z","shell.execute_reply.started":"2023-03-05T02:40:54.966613Z","shell.execute_reply":"2023-03-05T02:40:54.989422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.contact.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:56.929874Z","iopub.execute_input":"2023-03-05T02:40:56.930317Z","iopub.status.idle":"2023-03-05T02:40:56.951352Z","shell.execute_reply.started":"2023-03-05T02:40:56.930279Z","shell.execute_reply":"2023-03-05T02:40:56.950516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:40:59.085121Z","iopub.execute_input":"2023-03-05T02:40:59.08564Z","iopub.status.idle":"2023-03-05T02:40:59.09705Z","shell.execute_reply.started":"2023-03-05T02:40:59.08559Z","shell.execute_reply":"2023-03-05T02:40:59.096045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T02:41:01.072853Z","iopub.execute_input":"2023-03-05T02:41:01.07324Z","iopub.status.idle":"2023-03-05T02:41:01.136105Z","shell.execute_reply.started":"2023-03-05T02:41:01.073207Z","shell.execute_reply":"2023-03-05T02:41:01.135176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#EOF","metadata":{"execution":{"iopub.status.busy":"2023-03-05T01:33:38.366628Z","iopub.status.idle":"2023-03-05T01:33:38.367923Z","shell.execute_reply.started":"2023-03-05T01:33:38.36767Z","shell.execute_reply":"2023-03-05T01:33:38.367694Z"},"trusted":true},"execution_count":null,"outputs":[]}]}