{"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 dd\nimport math\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import StratifiedKFold, cross_val_score, train_test_split\nfrom joblib import dump, load\nfrom sklearn.metrics import matthews_corrcoef\nfrom sklearn.utils import shuffle\nimport cv2\nimport torch\nimport torch.nn as nn\nfrom torch import optim\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\nimport gc\nfrom sys import getsizeof\nfrom skimage import io\nfrom torchvision import transforms as T\nfrom torchvision import datasets, models\nimport shutil\nimport os\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-02T11:57:03.138845Z","iopub.execute_input":"2023-03-02T11:57:03.139496Z","iopub.status.idle":"2023-03-02T11:57:07.891274Z","shell.execute_reply.started":"2023-03-02T11:57:03.139374Z","shell.execute_reply":"2023-03-02T11:57:07.889860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:07.899558Z","iopub.execute_input":"2023-03-02T11:57:07.903442Z","iopub.status.idle":"2023-03-02T11:57:07.934874Z","shell.execute_reply.started":"2023-03-02T11:57:07.903366Z","shell.execute_reply":"2023-03-02T11:57:07.933431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/nfl-player-contact-detection/'","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:07.938683Z","iopub.execute_input":"2023-03-02T11:57:07.941904Z","iopub.status.idle":"2023-03-02T11:57:07.954255Z","shell.execute_reply.started":"2023-03-02T11:57:07.941785Z","shell.execute_reply":"2023-03-02T11:57:07.951900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nsubmission = dd.read_csv(path+'sample_submission.csv')\ntrack = dd.read_csv(path+'test_player_tracking.csv')\nhelmet = dd.read_csv(path+'test_baseline_helmets.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:07.959389Z","iopub.execute_input":"2023-03-02T11:57:07.960984Z","iopub.status.idle":"2023-03-02T11:57:08.281687Z","shell.execute_reply.started":"2023-03-02T11:57:07.960804Z","shell.execute_reply":"2023-03-02T11:57:08.280402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"helmet = helmet[helmet['view'] == 'Endzone']\nhelmet.drop(['game_key', 'play_id', 'view', 'video', 'player_label'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.287746Z","iopub.execute_input":"2023-03-02T11:57:08.289737Z","iopub.status.idle":"2023-03-02T11:57:08.327911Z","shell.execute_reply.started":"2023-03-02T11:57:08.289672Z","shell.execute_reply":"2023-03-02T11:57:08.326619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df = dd.read_csv(path+'test_video_metadata.csv')\nmeta_end = meta_df[meta_df.view == 'Endzone']\nmeta = meta_end[['game_play', 'start_time']]","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.331721Z","iopub.execute_input":"2023-03-02T11:57:08.332914Z","iopub.status.idle":"2023-03-02T11:57:08.349778Z","shell.execute_reply.started":"2023-03-02T11:57:08.332860Z","shell.execute_reply":"2023-03-02T11:57:08.348429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def c2g(ci):\n    tmp = ci.split('_')\n    gp = tmp[0]+'_'+tmp[1]\n    step = int(tmp[2])\n    p1 = int(tmp[3])\n    p2 = tmp[4]\n    frame = round((step/10)*59.94+(5*59.94))\n    \n    return gp, p1, p2, frame","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.356206Z","iopub.execute_input":"2023-03-02T11:57:08.359604Z","iopub.status.idle":"2023-03-02T11:57:08.369175Z","shell.execute_reply.started":"2023-03-02T11:57:08.359542Z","shell.execute_reply":"2023-03-02T11:57:08.367592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def time2msec(time_):\n    #'2020-09-11T03:01:48.200Z'\n    t = time_.split('T')[1][:-1]\n    t1, t2 = t.split('.')\n    t2 = int(t2)\n    t11, t12, t13 = map(int, t1.split(':'))\n    t11 = t11*60*60*1000\n    t12 = t12*60*1000\n    t13 = t13*1000\n    total_msec = t2+t11+t12+t13\n    return total_msec\n\ndef msec2frame(a, b):\n    return round(((a-b)/1000)*59.94)\n\ndef ts2fr(row):\n    dt = time2msec(row['datetime'])\n    st = time2msec(row['start_time'])\n    f = msec2frame(dt, st)\n    return f","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.371299Z","iopub.execute_input":"2023-03-02T11:57:08.372424Z","iopub.status.idle":"2023-03-02T11:57:08.387322Z","shell.execute_reply.started":"2023-03-02T11:57:08.372371Z","shell.execute_reply":"2023-03-02T11:57:08.385823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['game_play'], submission['nfl_player_id_1'], submission['nfl_player_id_2'], submission['frame'] = zip(*submission['contact_id'].map(c2g))","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.389818Z","iopub.execute_input":"2023-03-02T11:57:08.390837Z","iopub.status.idle":"2023-03-02T11:57:08.592958Z","shell.execute_reply.started":"2023-03-02T11:57:08.390786Z","shell.execute_reply":"2023-03-02T11:57:08.591575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shp = submission.shape[0]\nsubmission['Nums'] = [f'{i:07}' for i in range(1, shp+1)]","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.597625Z","iopub.execute_input":"2023-03-02T11:57:08.598084Z","iopub.status.idle":"2023-03-02T11:57:08.628220Z","shell.execute_reply.started":"2023-03-02T11:57:08.598043Z","shell.execute_reply":"2023-03-02T11:57:08.626866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = submission[submission['nfl_player_id_2']!='G'].copy()\nlabel.sort_values(by=['Nums'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.632779Z","iopub.execute_input":"2023-03-02T11:57:08.633567Z","iopub.status.idle":"2023-03-02T11:57:08.693033Z","shell.execute_reply.started":"2023-03-02T11:57:08.633502Z","shell.execute_reply":"2023-03-02T11:57:08.691700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tr_pp(meta, trc_df):\n    \n    #Feature Selection\n    trc_df = trc_df[trc_df['step']>-1]\n    trc_df = trc_df.merge(meta, on='game_play', how='left')\n    trc_df['frame'] = trc_df.apply(ts2fr, axis=1)\n    \n    pos = trc_df.position.unique().tolist()\n    num = [i for i in range(1, len(pos)+1)]\n    trc_df['position'].replace(pos, num, inplace=True)\n\n    team = trc_df.team.unique().tolist()\n    num1 = [i for i in range(len(team))]\n    trc_df['team'].replace(team, num1, inplace=True)\n         \n    return trc_df","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.695670Z","iopub.execute_input":"2023-03-02T11:57:08.696052Z","iopub.status.idle":"2023-03-02T11:57:08.706894Z","shell.execute_reply.started":"2023-03-02T11:57:08.696020Z","shell.execute_reply":"2023-03-02T11:57:08.705592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntr = tr_pp(meta, track)\na = 'game_play nfl_player_id position team x_position y_position speed distance direction orientation acceleration sa frame'.split(' ')\ntr = tr.loc[:, a]","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.708736Z","iopub.execute_input":"2023-03-02T11:57:08.709730Z","iopub.status.idle":"2023-03-02T11:57:08.855761Z","shell.execute_reply.started":"2023-03-02T11:57:08.709679Z","shell.execute_reply":"2023-03-02T11:57:08.854433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = {'nfl_player_id_1':'nfl_player_id'}\nb = {'nfl_player_id_2':'nfl_player_id'}\n\nlabel1_p1 = label.loc[:, ['game_play', 'nfl_player_id_1', 'frame', 'Nums']]\nlabel1_p1.rename(columns = a, inplace=True)\nlabel1_p2 = label.loc[:, ['game_play', 'nfl_player_id_2', 'frame', 'Nums']]\nlabel1_p2.rename(columns = b, inplace=True)\nlabel1_p2['nfl_player_id'] = label1_p2['nfl_player_id'].astype('int')","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.857471Z","iopub.execute_input":"2023-03-02T11:57:08.857876Z","iopub.status.idle":"2023-03-02T11:57:08.886347Z","shell.execute_reply.started":"2023-03-02T11:57:08.857831Z","shell.execute_reply":"2023-03-02T11:57:08.884900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HlTr = helmet.merge(tr, on=['game_play', 'nfl_player_id', 'frame'], how = 'outer')","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.887868Z","iopub.execute_input":"2023-03-02T11:57:08.889052Z","iopub.status.idle":"2023-03-02T11:57:08.918868Z","shell.execute_reply.started":"2023-03-02T11:57:08.888995Z","shell.execute_reply":"2023-03-02T11:57:08.916959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"htl1 = label1_p1.merge(HlTr,  on = ['game_play', 'nfl_player_id', 'frame'], how='outer')\nhtl2 = label1_p2.merge(HlTr,  on = ['game_play', 'nfl_player_id', 'frame'], how='outer')\ndel HlTr\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:08.921324Z","iopub.execute_input":"2023-03-02T11:57:08.921745Z","iopub.status.idle":"2023-03-02T11:57:09.170857Z","shell.execute_reply.started":"2023-03-02T11:57:08.921707Z","shell.execute_reply":"2023-03-02T11:57:09.169718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fill_helmet_fast(m1, a3):\n    m1 = m1.sort_values(by=['game_play', 'nfl_player_id', 'frame'])\n    tmp1 = m1.copy()\n    tmp2 = m1.copy()\n    tmp1.loc[:, a3] = tmp1.groupby(['game_play', 'nfl_player_id'])[a3].transform(lambda group: group.ffill().bfill())\n    tmp2.loc[:, a3] = tmp2.groupby(['game_play', 'nfl_player_id'])[a3].transform(lambda group: group.ffill().bfill())\n    m1.loc[:, a3] = (tmp1.loc[:, a3]+tmp2.loc[:, a3])/2\n    del tmp1, tmp2\n    gc.collect()\n    return m1","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:09.172597Z","iopub.execute_input":"2023-03-02T11:57:09.172954Z","iopub.status.idle":"2023-03-02T11:57:09.181641Z","shell.execute_reply.started":"2023-03-02T11:57:09.172923Z","shell.execute_reply":"2023-03-02T11:57:09.180094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a1 = ['x_position', 'y_position', 'speed', 'distance', 'direction', 'orientation', 'acceleration', 'team', 'position', 'sa']\na2 = ['left', 'width', 'top', 'height']\na3 = a1+a2\n\nhtl1 = fill_helmet_fast(htl1, a3)\nhtl2 = fill_helmet_fast(htl2, a3)\nhtl1 = htl1[htl1.Nums.notna()]\nhtl2 = htl2[htl2.Nums.notna()]\n\ndata = htl1.merge(htl2, on='Nums', how='inner')\n\ndel htl1, htl2\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:09.183472Z","iopub.execute_input":"2023-03-02T11:57:09.185077Z","iopub.status.idle":"2023-03-02T11:57:11.139149Z","shell.execute_reply.started":"2023-03-02T11:57:09.185023Z","shell.execute_reply":"2023-03-02T11:57:11.137648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.sort_values(by=['Nums'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:11.142396Z","iopub.execute_input":"2023-03-02T11:57:11.142906Z","iopub.status.idle":"2023-03-02T11:57:11.197972Z","shell.execute_reply.started":"2023-03-02T11:57:11.142866Z","shell.execute_reply":"2023-03-02T11:57:11.196496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fdata = data.drop(['game_play_x', 'game_play_y', 'nfl_player_id_x', 'nfl_player_id_y', 'Nums', 'frame_x', 'frame_y'], axis=1)\ndel data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:11.199839Z","iopub.execute_input":"2023-03-02T11:57:11.200340Z","iopub.status.idle":"2023-03-02T11:57:11.368019Z","shell.execute_reply.started":"2023-03-02T11:57:11.200291Z","shell.execute_reply":"2023-03-02T11:57:11.366791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def IOU(row):\n    x1, w1, y1, h1 = row['left_x'], row['width_x'], row['top_x'], row['height_x']\n    x2, w2, y2, h2 = row['left_y'], row['width_y'], row['top_y'], row['height_y']\n    \n    ep = 0.0000001\n    \n    inter_x1 = max(x1, x2)\n    inter_y1 = max(y1, y2)\n    inter_x2 = min(x1+w1, x2+w2)\n    inter_y2 = min(y1+h1, y2+h2)\n    \n    w = max(0, inter_x2-inter_x1)\n    h = max(0, inter_y2-inter_y1)\n    \n    inter_area = w*h\n    \n    uni_area = (w1*h1)+(w2*h2)-inter_area+ep\n    \n    iou = inter_area/uni_area\n    \n    return iou\n\ndef dist(a, b):\n    return math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)\n\ndef distance1(row):\n    x1, w1, y1, h1 = row['left_x'], row['width_x'], row['top_x'], row['height_x']\n    x2, w2, y2, h2 = row['left_y'], row['width_y'], row['top_y'], row['height_y']\n    \n    x1 = x1+(w1/2)\n    y1 = y1+(h1/2)\n    x2 = x2+(w2/2)\n    y2 = y2+(h2/2)\n    \n    return dist([x1, y1], [x2, y2])\n\ndef distance2(row):\n    x1 = row['x_position_x']\n    y1 = row['y_position_x']\n    x2 = row['x_position_y']\n    y2 = row['y_position_y']\n    \n    return dist([x1, y1], [x2, y2])\n\ndef distance3(row):\n    x1, w1, y1, h1 = row['left_x'], row['width_x'], row['top_x'], row['height_x']\n    x2, w2, y2, h2 = row['left_y'], row['width_y'], row['top_y'], row['height_y']\n    \n    d1 = dist([x1, y1], [x1+w1, y1+h1])\n    d2 = dist([x2, y2], [x2+w2, y2+h2])\n    d = max(d1, d2)\n    \n    return row['distance1']/d\n\ndef dir2cos(row):\n    r = abs(row['direction_x'] - row['direction_y'])\n    radians = math.radians(r)\n    cosine = math.cos(radians)\n    return cosine\n\ndef ori2cos(row):\n    r = abs(row['orientation_x'] - row['orientation_y'])\n    radians = math.radians(r)\n    cosine = math.cos(radians)\n    return cosine","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:11.369654Z","iopub.execute_input":"2023-03-02T11:57:11.370991Z","iopub.status.idle":"2023-03-02T11:57:11.390000Z","shell.execute_reply.started":"2023-03-02T11:57:11.370936Z","shell.execute_reply":"2023-03-02T11:57:11.388818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fdata['iou'] = fdata.apply(IOU, axis=1)\nfdata['distance1'] = fdata.apply(distance1, axis=1)\nfdata['distance2'] = fdata.apply(distance2, axis=1)\nfdata['distance3'] = fdata.apply(distance3, axis=1)\nfdata['dir2cos'] = fdata.apply(dir2cos, axis=1)\nfdata['ori2cos'] = fdata.apply(ori2cos, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:11.391765Z","iopub.execute_input":"2023-03-02T11:57:11.392308Z","iopub.status.idle":"2023-03-02T11:57:20.324941Z","shell.execute_reply.started":"2023-03-02T11:57:11.392233Z","shell.execute_reply":"2023-03-02T11:57:20.323684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fdata.fillna(0, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:20.326677Z","iopub.execute_input":"2023-03-02T11:57:20.327086Z","iopub.status.idle":"2023-03-02T11:57:20.345720Z","shell.execute_reply.started":"2023-03-02T11:57:20.327050Z","shell.execute_reply":"2023-03-02T11:57:20.343628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fdata['position_x'] = fdata['position_x'].astype('int')\nfdata['position_y'] = fdata['position_y'].astype('int')\nfdata['team_x'] = fdata['team_x'].astype('int')\nfdata['team_y'] = fdata['team_y'].astype('int')","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:20.348074Z","iopub.execute_input":"2023-03-02T11:57:20.348659Z","iopub.status.idle":"2023-03-02T11:57:20.382352Z","shell.execute_reply.started":"2023-03-02T11:57:20.348604Z","shell.execute_reply":"2023-03-02T11:57:20.380822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = load('/kaggle/input/nfl-training/rf.joblib')\npred1 = clf.predict(fdata).tolist()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:20.384500Z","iopub.execute_input":"2023-03-02T11:57:20.384888Z","iopub.status.idle":"2023-03-02T11:57:20.608752Z","shell.execute_reply.started":"2023-03-02T11:57:20.384855Z","shell.execute_reply":"2023-03-02T11:57:20.607635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub1 = dd.DataFrame(columns=['contact_id', 'contact'])\nsub1['contact_id'] = label['contact_id']\nsub1['contact'] = pred1","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:20.610468Z","iopub.execute_input":"2023-03-02T11:57:20.611160Z","iopub.status.idle":"2023-03-02T11:57:20.641932Z","shell.execute_reply.started":"2023-03-02T11:57:20.611108Z","shell.execute_reply":"2023-03-02T11:57:20.640763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grlb = submission[submission.nfl_player_id_2=='G'].copy()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:20.643560Z","iopub.execute_input":"2023-03-02T11:57:20.644280Z","iopub.status.idle":"2023-03-02T11:57:20.656762Z","shell.execute_reply.started":"2023-03-02T11:57:20.644218Z","shell.execute_reply":"2023-03-02T11:57:20.655096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grlb.drop('nfl_player_id_2', axis=1, inplace=True)\ngrlb.rename(columns={'nfl_player_id_1':'nfl_player_id'}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:20.664112Z","iopub.execute_input":"2023-03-02T11:57:20.665463Z","iopub.status.idle":"2023-03-02T11:57:20.677026Z","shell.execute_reply.started":"2023-03-02T11:57:20.665400Z","shell.execute_reply":"2023-03-02T11:57:20.675655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HlGr = grlb.merge(helmet, on=['game_play', 'nfl_player_id', 'frame'], how='outer')\na3 = ['left', 'width', 'top', 'height']\nHlGr = fill_helmet_fast(HlGr, a3)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:20.678776Z","iopub.execute_input":"2023-03-02T11:57:20.679348Z","iopub.status.idle":"2023-03-02T11:57:21.171023Z","shell.execute_reply.started":"2023-03-02T11:57:20.679294Z","shell.execute_reply":"2023-03-02T11:57:21.169749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HlGr = HlGr[HlGr.Nums.notna()]","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.175302Z","iopub.execute_input":"2023-03-02T11:57:21.175872Z","iopub.status.idle":"2023-03-02T11:57:21.189906Z","shell.execute_reply.started":"2023-03-02T11:57:21.175824Z","shell.execute_reply":"2023-03-02T11:57:21.188174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub2 = dd.DataFrame(columns=['contact_id', 'contact'])\nsub2['contact_id'] = HlGr[HlGr.isna().any(axis=1)]['contact_id'].copy()\nsub2['contact'] = [0]*sub2.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.191843Z","iopub.execute_input":"2023-03-02T11:57:21.192394Z","iopub.status.idle":"2023-03-02T11:57:21.206129Z","shell.execute_reply.started":"2023-03-02T11:57:21.192355Z","shell.execute_reply":"2023-03-02T11:57:21.204941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"itd = HlGr[~HlGr.isna().any(axis=1)].copy()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.207434Z","iopub.execute_input":"2023-03-02T11:57:21.207790Z","iopub.status.idle":"2023-03-02T11:57:21.219812Z","shell.execute_reply.started":"2023-03-02T11:57:21.207759Z","shell.execute_reply":"2023-03-02T11:57:21.218338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Find_2d_CNN_Out_Shape(h_in, w_in, conv, pool=1):\n    k_size = conv.kernel_size\n    st = conv.stride\n    pd = conv.padding\n    di = conv.dilation\n    \n    h = np.floor((h_in+2*pd[0]-di[0]*(k_size[0]-1)-1)/st[0]+1)\n    w = np.floor((w_in+2*pd[1]-di[1]*(k_size[1]-1)-1)/st[1]+1) \n    \n    h /=pool\n    w /=pool\n    \n    return int(h), int(w)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.224107Z","iopub.execute_input":"2023-03-02T11:57:21.224534Z","iopub.status.idle":"2023-03-02T11:57:21.234814Z","shell.execute_reply.started":"2023-03-02T11:57:21.224499Z","shell.execute_reply":"2023-03-02T11:57:21.233426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class simpleNet(nn.Module):\n    \n    def __init__(self, parameters):\n        super().__init__()\n        c_in, h_in, w_in = parameters['input_shape']\n        f = parameters['filter']\n        fc = parameters['fc']\n        self.dr = parameters['drop']\n        \n        self.conv1 = nn.Conv2d(c_in, 2*f, kernel_size=3)\n        self.bn1 = nn.BatchNorm2d(2*f)\n        self.pool1 = nn.MaxPool2d(3, 3)\n        \n        h, w = Find_2d_CNN_Out_Shape(h_in, w_in, self.conv1, 3)\n        \n        self.conv2 = nn.Conv2d(2*f, 4*f, kernel_size=3)\n        self.bn2 = nn.BatchNorm2d(4*f)\n        self.pool2 = nn.MaxPool2d(3, 3)\n        \n        h, w = Find_2d_CNN_Out_Shape(h, w, self.conv2, 3)\n\n        self.conv3 = nn.Conv2d(4*f, 8*f, kernel_size=3)\n        self.bn3 = nn.BatchNorm2d(8*f)\n        self.pool3 = nn.AvgPool2d(3, 3)\n                \n        h, w = Find_2d_CNN_Out_Shape(h, w, self.conv3, 3)\n        \n        self.num_flatten=h*w*8*f\n        \n        self.fc1 = nn.Linear(self.num_flatten, fc[0])\n        self.fc2 = nn.Linear(fc[0], fc[1])\n        self.fc3 = nn.Linear(fc[1], fc[2])\n        \n        self.relu = nn.ReLU()\n        self.drop = nn.Dropout(self.dr)\n        \n    def forward(self, x):\n        x = self.pool1(self.bn1(self.relu(self.conv1(x))))\n        x = self.pool2(self.bn2(self.relu(self.conv2(x))))\n        x = self.pool3(self.bn3(self.relu(self.conv3(x))))\n        x = x.view(-1, self.num_flatten)\n        x = self.relu(self.fc1(x))\n        x = self.drop(x)\n        x = self.relu(self.fc2(x))\n        x = self.fc3(x)\n        \n        return torch.sigmoid(x)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.236801Z","iopub.execute_input":"2023-03-02T11:57:21.237224Z","iopub.status.idle":"2023-03-02T11:57:21.253487Z","shell.execute_reply.started":"2023-03-02T11:57:21.237181Z","shell.execute_reply":"2023-03-02T11:57:21.252158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameters = {'input_shape':( 3, 150, 150),\n              'filter':8,\n              'fc':(256, 128, 1),\n              'drop':0.}","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.255146Z","iopub.execute_input":"2023-03-02T11:57:21.255681Z","iopub.status.idle":"2023-03-02T11:57:21.270884Z","shell.execute_reply.started":"2023-03-02T11:57:21.255630Z","shell.execute_reply":"2023-03-02T11:57:21.269556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = simpleNet(parameters)\nnet.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.272221Z","iopub.execute_input":"2023-03-02T11:57:21.273139Z","iopub.status.idle":"2023-03-02T11:57:21.351980Z","shell.execute_reply.started":"2023-03-02T11:57:21.273081Z","shell.execute_reply":"2023-03-02T11:57:21.350610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_func = nn.BCELoss(reduction = 'mean')\nopt = optim.Adam(net.parameters(), lr=0.0001)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.353459Z","iopub.execute_input":"2023-03-02T11:57:21.353860Z","iopub.status.idle":"2023-03-02T11:57:21.359834Z","shell.execute_reply.started":"2023-03-02T11:57:21.353825Z","shell.execute_reply":"2023-03-02T11:57:21.358794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net.load_state_dict(torch.load('/kaggle/input/nfl-training/net_g.pth', map_location=torch.device('cpu')))","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.361092Z","iopub.execute_input":"2023-03-02T11:57:21.362349Z","iopub.status.idle":"2023-03-02T11:57:21.406314Z","shell.execute_reply.started":"2023-03-02T11:57:21.362307Z","shell.execute_reply":"2023-03-02T11:57:21.405001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TrainData(Dataset):\n    def __init__(self, img_dir, img_list, transform=None):\n        self.dir = img_dir\n        self.img_list = img_list\n        self.transform = transform\n    def __getitem__(self, index):\n        path = self.dir+'/'+self.img_list[index]+'.jpg'\n        img = io.imread(path)\n        if self.transform:\n            img = self.transform(img)\n        return img\n    def __len__(self):\n        return len(self.img_list)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.407610Z","iopub.execute_input":"2023-03-02T11:57:21.408044Z","iopub.status.idle":"2023-03-02T11:57:21.414833Z","shell.execute_reply.started":"2023-03-02T11:57:21.408011Z","shell.execute_reply":"2023-03-02T11:57:21.413713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = T.Compose([T.ToTensor(),\n                  T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.416173Z","iopub.execute_input":"2023-03-02T11:57:21.416757Z","iopub.status.idle":"2023-03-02T11:57:21.433154Z","shell.execute_reply.started":"2023-03-02T11:57:21.416722Z","shell.execute_reply":"2023-03-02T11:57:21.431926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nnet.eval()\ngp_ids = itd['game_play'].unique().tolist()\n# gp_ids = ['58198_002514']\ncolor = (255, 0, 0)\nthickness = 2\n\nimg_id = []\npred_g = []\nrtf = []\nfor gp in gp_ids:\n    img1 = []\n    v1 = path+'test/'+gp+'_'+'Endzone.mp4'\n    d1 = itd[itd['game_play'] == gp]\n    \n    if not os.path.exists('/kaggle/working/Images'):\n        os.mkdir('Images')\n    \n    video1 = cv2.VideoCapture(v1)\n    l1 = int(video1.get(cv2.CAP_PROP_FRAME_COUNT))\n    fps1 = video1.get(cv2.CAP_PROP_FPS)\n\n    for index, row in d1.iterrows():\n        position = row['frame']-1\n        if position>=l1:\n            rtf.append(row['contact_id'])\n            continue\n        x, w, y, h = int(row['left']), int(row['width']), int(row['top']), int(row['height'])\n        video1.set(cv2.CAP_PROP_POS_FRAMES, position)\n        _, frame = video1.read()\n        cx, cw, cy, ch = int(x+(w/2))-int(4*w), int(8*w), int(y+(h/2))-int(4*h), int(8*h)\n        r, c, _ = frame.shape\n        x1, y1, x2, y2 = max(0, cx), max(0, cy), min(cx+cw, c), min(cy+ch, r)\n        cv2.rectangle(frame, (x, y), (x+w, y+h), color, thickness)\n        crop = frame[y1:y2, x1:x2]\n        image = cv2.resize(crop, (150, 150), interpolation = cv2.INTER_LINEAR)\n        img1.append(row['contact_id'])\n        filename = row['contact_id']\n        cv2.imwrite('/kaggle/working/Images/'+filename+'.jpg', image)\n        \n    img_id+=img1\n    dataset = TrainData('/kaggle/working/Images', img1, trans)\n    dataloader = DataLoader(dataset, batch_size=1024)\n    \n    for data in dataloader:\n        with torch.no_grad():\n            output = net(data.to(device))\n            y_pred = torch.flatten(torch.round(output)).tolist()\n            pred_g+=y_pred\n            \n    if os.path.exists('/kaggle/working/Images'):\n        shutil.rmtree('/kaggle/working/Images')\n            \n    del dataset, dataloader, img1\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T11:57:21.434795Z","iopub.execute_input":"2023-03-02T11:57:21.435457Z","iopub.status.idle":"2023-03-02T12:07:57.907770Z","shell.execute_reply.started":"2023-03-02T11:57:21.435421Z","shell.execute_reply":"2023-03-02T12:07:57.906364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub4 = dd.DataFrame(columns = ['contact_id', 'contact'])\nsub4['contact_id'] = img_id\nsub4['contact'] = pred_g","metadata":{"execution":{"iopub.status.busy":"2023-03-02T12:07:57.910421Z","iopub.execute_input":"2023-03-02T12:07:57.910949Z","iopub.status.idle":"2023-03-02T12:07:57.923278Z","shell.execute_reply.started":"2023-03-02T12:07:57.910891Z","shell.execute_reply":"2023-03-02T12:07:57.921560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub3 = dd.DataFrame(columns = ['contact_id', 'contact'])\nsub3['contact_id'] = rtf\nsub3['contact'] = [0]*len(rtf)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T12:07:57.925485Z","iopub.execute_input":"2023-03-02T12:07:57.925885Z","iopub.status.idle":"2023-03-02T12:07:57.946092Z","shell.execute_reply.started":"2023-03-02T12:07:57.925850Z","shell.execute_reply":"2023-03-02T12:07:57.944765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = dd.concat([sub1, sub2, sub3, sub4], axis=0, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T12:07:57.947793Z","iopub.execute_input":"2023-03-02T12:07:57.948162Z","iopub.status.idle":"2023-03-02T12:07:57.967864Z","shell.execute_reply.started":"2023-03-02T12:07:57.948128Z","shell.execute_reply":"2023-03-02T12:07:57.966825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_id = submission.loc[:, ['contact_id']]","metadata":{"execution":{"iopub.status.busy":"2023-03-02T12:07:57.969344Z","iopub.execute_input":"2023-03-02T12:07:57.969728Z","iopub.status.idle":"2023-03-02T12:07:57.984704Z","shell.execute_reply.started":"2023-03-02T12:07:57.969693Z","shell.execute_reply":"2023-03-02T12:07:57.983711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = sub_id.merge(sub, on = 'contact_id', how='left')","metadata":{"execution":{"iopub.status.busy":"2023-03-02T12:07:57.986296Z","iopub.execute_input":"2023-03-02T12:07:57.986654Z","iopub.status.idle":"2023-03-02T12:07:58.041044Z","shell.execute_reply.started":"2023-03-02T12:07:57.986622Z","shell.execute_reply":"2023-03-02T12:07:58.039972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.fillna(0, inplace=True)\nsubmission['contact'] = submission['contact'].astype('int')\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T12:07:58.042127Z","iopub.execute_input":"2023-03-02T12:07:58.042521Z","iopub.status.idle":"2023-03-02T12:07:58.128033Z","shell.execute_reply.started":"2023-03-02T12:07:58.042485Z","shell.execute_reply":"2023-03-02T12:07:58.126865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.contact.sum()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T12:07:58.131209Z","iopub.execute_input":"2023-03-02T12:07:58.131779Z","iopub.status.idle":"2023-03-02T12:07:58.143495Z","shell.execute_reply.started":"2023-03-02T12:07:58.131737Z","shell.execute_reply":"2023-03-02T12:07:58.141558Z"},"trusted":true},"execution_count":null,"outputs":[]}]}