{"cells":[{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:20.291544Z","iopub.status.busy":"2020-11-07T01:42:20.290602Z","iopub.status.idle":"2020-11-07T01:42:43.567258Z","shell.execute_reply":"2020-11-07T01:42:43.566161Z"},"papermill":{"duration":23.330244,"end_time":"2020-11-07T01:42:43.567383","exception":false,"start_time":"2020-11-07T01:42:20.237139","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"try:\n    import zarr\nexcept ModuleNotFoundError:\n    !pip install --use-feature=2020-resolver zarr > /dev/null\ntry:\n    import pytorch_lightning\nexcept ModuleNotFoundError:\n    !pip install  --use-feature=2020-resolver pytorch-lightning==0.8.4  > /dev/null","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:43.74765Z","iopub.status.busy":"2020-11-07T01:42:43.746902Z","iopub.status.idle":"2020-11-07T01:42:43.750988Z","shell.execute_reply":"2020-11-07T01:42:43.750426Z"},"papermill":{"duration":0.057373,"end_time":"2020-11-07T01:42:43.751075","exception":false,"start_time":"2020-11-07T01:42:43.693702","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import os,sys\nsys.path.insert(0, \"../input/best-submission/src\")","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:43.933327Z","iopub.status.busy":"2020-11-07T01:42:43.932479Z","iopub.status.idle":"2020-11-07T01:42:43.985589Z","shell.execute_reply":"2020-11-07T01:42:43.984512Z"},"papermill":{"duration":0.102094,"end_time":"2020-11-07T01:42:43.985689","exception":false,"start_time":"2020-11-07T01:42:43.883595","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import models.pointnet as pointnet\n# from data.dataset import CustomLyftDataset_V5 as CustomLyftDataset\nfrom training.trainer import get_last_checkpoint, BaseLightningModule\nfrom training.configs.base import GenericConfig\nfrom data.dataset import collate_V5 as collate, CustomLyftDataset_V7 as CustomLyftDataset","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:44.076067Z","iopub.status.busy":"2020-11-07T01:42:44.075055Z","iopub.status.idle":"2020-11-07T01:42:44.108865Z","shell.execute_reply":"2020-11-07T01:42:44.108393Z"},"papermill":{"duration":0.081912,"end_time":"2020-11-07T01:42:44.108959","exception":false,"start_time":"2020-11-07T01:42:44.027047","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"from pathlib import Path\nimport zarr\nfrom tqdm.notebook import tqdm\nimport numpy as np, pandas as pd\nimport torch, torch.nn as nn\nfrom torch.utils.data import DataLoader","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:44.199723Z","iopub.status.busy":"2020-11-07T01:42:44.198826Z","iopub.status.idle":"2020-11-07T01:42:44.201995Z","shell.execute_reply":"2020-11-07T01:42:44.2015Z"},"papermill":{"duration":0.05011,"end_time":"2020-11-07T01:42:44.202093","exception":false,"start_time":"2020-11-07T01:42:44.151983","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import bisect, yaml\nimport itertools as it","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:44.293691Z","iopub.status.busy":"2020-11-07T01:42:44.292946Z","iopub.status.idle":"2020-11-07T01:42:44.349257Z","shell.execute_reply":"2020-11-07T01:42:44.348688Z"},"papermill":{"duration":0.10384,"end_time":"2020-11-07T01:42:44.349364","exception":false,"start_time":"2020-11-07T01:42:44.245524","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"from joblib import Parallel, delayed\npool = Parallel(3)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:44.443719Z","iopub.status.busy":"2020-11-07T01:42:44.442877Z","iopub.status.idle":"2020-11-07T01:42:44.445963Z","shell.execute_reply":"2020-11-07T01:42:44.446669Z"},"papermill":{"duration":0.053769,"end_time":"2020-11-07T01:42:44.44679","exception":false,"start_time":"2020-11-07T01:42:44.393021","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"DATA_ROOT = Path(r\"../input/lyft-motion-prediction-autonomous-vehicles\")\nZARR_PATH = Path(\"scenes/test.zarr\")\n# MODEL_PATH = r\"../input/kkiller-lyft-checkpoints/model_epoch_17.ckpt\"\nHPARAM_PATH = r\"../input/best-submission/hparams.yaml\"\nprint(\"DATA_ROOT: {}\\nZARR_PATH: {}\".format(DATA_ROOT, ZARR_PATH))","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.046445,"end_time":"2020-11-07T01:42:44.538694","exception":false,"start_time":"2020-11-07T01:42:44.492249","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:44.631131Z","iopub.status.busy":"2020-11-07T01:42:44.630354Z","iopub.status.idle":"2020-11-07T01:42:44.981795Z","shell.execute_reply":"2020-11-07T01:42:44.982444Z"},"papermill":{"duration":0.398611,"end_time":"2020-11-07T01:42:44.982594","exception":false,"start_time":"2020-11-07T01:42:44.583983","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"%%time\n\nmask = np.load(DATA_ROOT/\"scenes/mask.npz\")['arr_0']\nmask","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:45.075407Z","iopub.status.busy":"2020-11-07T01:42:45.07436Z","iopub.status.idle":"2020-11-07T01:42:45.114241Z","shell.execute_reply":"2020-11-07T01:42:45.113671Z"},"papermill":{"duration":0.088048,"end_time":"2020-11-07T01:42:45.114347","exception":false,"start_time":"2020-11-07T01:42:45.026299","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"agent_ids = np.where(mask)[0]\nagent_ids","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:45.211462Z","iopub.status.busy":"2020-11-07T01:42:45.2107Z","iopub.status.idle":"2020-11-07T01:42:45.246916Z","shell.execute_reply":"2020-11-07T01:42:45.246422Z"},"papermill":{"duration":0.086553,"end_time":"2020-11-07T01:42:45.247023","exception":false,"start_time":"2020-11-07T01:42:45.16047","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"with open(HPARAM_PATH) as f:\n    hparams = yaml.load(f, Loader=yaml.SafeLoader)\nhparams","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:45.343068Z","iopub.status.busy":"2020-11-07T01:42:45.342416Z","iopub.status.idle":"2020-11-07T01:42:45.34904Z","shell.execute_reply":"2020-11-07T01:42:45.348506Z"},"papermill":{"duration":0.05664,"end_time":"2020-11-07T01:42:45.349143","exception":false,"start_time":"2020-11-07T01:42:45.292503","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"hparams[\"data_options\"][\"train\"][\"kwargs\"].keys()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:45.449675Z","iopub.status.busy":"2020-11-07T01:42:45.448775Z","iopub.status.idle":"2020-11-07T01:42:45.453696Z","shell.execute_reply":"2020-11-07T01:42:45.454234Z"},"papermill":{"duration":0.058798,"end_time":"2020-11-07T01:42:45.454382","exception":false,"start_time":"2020-11-07T01:42:45.395584","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"HBACKWARD = hparams[\"data_options\"][\"train\"][\"kwargs\"][\"hbackward\"]\nFRAME_STRIDE = hparams[\"data_options\"][\"train\"][\"kwargs\"][\"frame_stride\"]\nAGENT_FEATURE_DIM = hparams[\"data_options\"][\"train\"][\"kwargs\"][\"agent_feature_dim\"]\nMAX_AGENTS = hparams[\"data_options\"][\"train\"][\"kwargs\"][\"max_agents\"]\nHBACKWARD,FRAME_STRIDE, AGENT_FEATURE_DIM, MAX_AGENTS","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:45.558919Z","iopub.status.busy":"2020-11-07T01:42:45.558082Z","iopub.status.idle":"2020-11-07T01:42:45.564436Z","shell.execute_reply":"2020-11-07T01:42:45.56496Z"},"papermill":{"duration":0.061079,"end_time":"2020-11-07T01:42:45.565086","exception":false,"start_time":"2020-11-07T01:42:45.504007","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"hparams[\"model_options\"][\"kwargs\"]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:45.666151Z","iopub.status.busy":"2020-11-07T01:42:45.665488Z","iopub.status.idle":"2020-11-07T01:42:45.670135Z","shell.execute_reply":"2020-11-07T01:42:45.669586Z"},"papermill":{"duration":0.056409,"end_time":"2020-11-07T01:42:45.670252","exception":false,"start_time":"2020-11-07T01:42:45.613843","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"HFORWARD = 0\nNFRAMES = 1","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:45.767873Z","iopub.status.busy":"2020-11-07T01:42:45.766949Z","iopub.status.idle":"2020-11-07T01:42:45.774264Z","shell.execute_reply":"2020-11-07T01:42:45.7737Z"},"papermill":{"duration":0.057483,"end_time":"2020-11-07T01:42:45.774361","exception":false,"start_time":"2020-11-07T01:42:45.716878","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"z = zarr.open(DATA_ROOT.joinpath(ZARR_PATH).as_posix())","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:45.873125Z","iopub.status.busy":"2020-11-07T01:42:45.872391Z","iopub.status.idle":"2020-11-07T01:42:45.885915Z","shell.execute_reply":"2020-11-07T01:42:45.885308Z"},"papermill":{"duration":0.064419,"end_time":"2020-11-07T01:42:45.886008","exception":false,"start_time":"2020-11-07T01:42:45.821589","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"frames_ij = z.scenes[\"frame_index_interval\"]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:45.987666Z","iopub.status.busy":"2020-11-07T01:42:45.986956Z","iopub.status.idle":"2020-11-07T01:42:46.646079Z","shell.execute_reply":"2020-11-07T01:42:46.64496Z"},"papermill":{"duration":0.712394,"end_time":"2020-11-07T01:42:46.646242","exception":false,"start_time":"2020-11-07T01:42:45.933848","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"agents_ij = z.frames[\"agent_index_interval\"]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:46.753953Z","iopub.status.busy":"2020-11-07T01:42:46.752095Z","iopub.status.idle":"2020-11-07T01:42:46.754788Z","shell.execute_reply":"2020-11-07T01:42:46.755296Z"},"papermill":{"duration":0.059392,"end_time":"2020-11-07T01:42:46.755426","exception":false,"start_time":"2020-11-07T01:42:46.696034","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def get_scene(agent_id):\n    frame_id = bisect.bisect_right(agents_ij[:, 0], agent_id)-1\n    scene_id = bisect.bisect_right(frames_ij[:, 0], frame_id)-1\n    \n#     print(scene_id,frame_id,agent_id)\n    \n    scene = z.scenes[scene_id]\n    frame = z.frames[frame_id]\n    agent = z.agents[agent_id]\n    return scene,(frame,frame_id+1),agent","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:46.854845Z","iopub.status.busy":"2020-11-07T01:42:46.854021Z","iopub.status.idle":"2020-11-07T01:42:46.85764Z","shell.execute_reply":"2020-11-07T01:42:46.858146Z"},"papermill":{"duration":0.055417,"end_time":"2020-11-07T01:42:46.858296","exception":false,"start_time":"2020-11-07T01:42:46.802879","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# from multiprocessing import Pool\n# pool = Pool(2)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:46.960657Z","iopub.status.busy":"2020-11-07T01:42:46.958592Z","iopub.status.idle":"2020-11-07T01:42:46.961389Z","shell.execute_reply":"2020-11-07T01:42:46.961884Z"},"papermill":{"duration":0.05555,"end_time":"2020-11-07T01:42:46.962016","exception":false,"start_time":"2020-11-07T01:42:46.906466","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# %%time\n\n# scene_data = pool.map(get_scene, agent_ids)\n# len(scene_data)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.046916,"end_time":"2020-11-07T01:42:47.056776","exception":false,"start_time":"2020-11-07T01:42:47.00986","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:47.157967Z","iopub.status.busy":"2020-11-07T01:42:47.156862Z","iopub.status.idle":"2020-11-07T01:42:47.173082Z","shell.execute_reply":"2020-11-07T01:42:47.172448Z"},"papermill":{"duration":0.068982,"end_time":"2020-11-07T01:42:47.173231","exception":false,"start_time":"2020-11-07T01:42:47.104249","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"agent_id = agent_ids[np.random.choice(len(agent_ids))]\nscene,(frame,frame_id), agent = get_scene(agent_id)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:47.276757Z","iopub.status.busy":"2020-11-07T01:42:47.276061Z","iopub.status.idle":"2020-11-07T01:42:47.281545Z","shell.execute_reply":"2020-11-07T01:42:47.282044Z"},"papermill":{"duration":0.058363,"end_time":"2020-11-07T01:42:47.282174","exception":false,"start_time":"2020-11-07T01:42:47.223811","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"scene","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:47.430262Z","iopub.status.busy":"2020-11-07T01:42:47.428994Z","iopub.status.idle":"2020-11-07T01:42:47.434501Z","shell.execute_reply":"2020-11-07T01:42:47.435029Z"},"papermill":{"duration":0.092234,"end_time":"2020-11-07T01:42:47.435165","exception":false,"start_time":"2020-11-07T01:42:47.342931","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"agent[\"track_id\"]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:47.594409Z","iopub.status.busy":"2020-11-07T01:42:47.592922Z","iopub.status.idle":"2020-11-07T01:42:47.597031Z","shell.execute_reply":"2020-11-07T01:42:47.597756Z"},"papermill":{"duration":0.082131,"end_time":"2020-11-07T01:42:47.597947","exception":false,"start_time":"2020-11-07T01:42:47.515816","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"scene.dtype","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:47.749389Z","iopub.status.busy":"2020-11-07T01:42:47.748413Z","iopub.status.idle":"2020-11-07T01:42:47.754089Z","shell.execute_reply":"2020-11-07T01:42:47.75337Z"},"papermill":{"duration":0.085188,"end_time":"2020-11-07T01:42:47.754244","exception":false,"start_time":"2020-11-07T01:42:47.669056","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"frame.dtype","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:47.905093Z","iopub.status.busy":"2020-11-07T01:42:47.904135Z","iopub.status.idle":"2020-11-07T01:42:47.910587Z","shell.execute_reply":"2020-11-07T01:42:47.911251Z"},"papermill":{"duration":0.08578,"end_time":"2020-11-07T01:42:47.911414","exception":false,"start_time":"2020-11-07T01:42:47.825634","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"agent.dtype","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.111166,"end_time":"2020-11-07T01:42:48.10776","exception":false,"start_time":"2020-11-07T01:42:47.996594","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:48.266257Z","iopub.status.busy":"2020-11-07T01:42:48.265422Z","iopub.status.idle":"2020-11-07T01:42:48.27071Z","shell.execute_reply":"2020-11-07T01:42:48.271215Z"},"papermill":{"duration":0.091462,"end_time":"2020-11-07T01:42:48.271347","exception":false,"start_time":"2020-11-07T01:42:48.179885","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"dt = CustomLyftDataset(\n                    z,\n#                     scenes = [s[0] for s in scene_data],\n                    nframes=NFRAMES,\n                    frame_stride=FRAME_STRIDE,\n                    hbackward=HBACKWARD,\n                    hforward=HFORWARD,\n                    max_agents=MAX_AGENTS,\n                    agent_feature_dim=AGENT_FEATURE_DIM,\n                )\ndt.nread_frames","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:48.975097Z","iopub.status.busy":"2020-11-07T01:42:48.974461Z","iopub.status.idle":"2020-11-07T01:42:48.979301Z","shell.execute_reply":"2020-11-07T01:42:48.978746Z"},"papermill":{"duration":0.503401,"end_time":"2020-11-07T01:42:48.979413","exception":false,"start_time":"2020-11-07T01:42:48.476012","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"DEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:49.220681Z","iopub.status.busy":"2020-11-07T01:42:49.219777Z","iopub.status.idle":"2020-11-07T01:42:49.223627Z","shell.execute_reply":"2020-11-07T01:42:49.22409Z"},"papermill":{"duration":0.067196,"end_time":"2020-11-07T01:42:49.224235","exception":false,"start_time":"2020-11-07T01:42:49.157039","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"model_name = \"ListStackedPointNetWithProjection_V3\"\nmodel_options = GenericConfig(\n    \"model\",\n    instance_name=model_name,\n    kwargs=hparams[\"model_options\"][\"kwargs\"],\n    context={\"model\": getattr(pointnet, model_name)},\n)\nmodel_options","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:49.470379Z","iopub.status.busy":"2020-11-07T01:42:49.469719Z","iopub.status.idle":"2020-11-07T01:42:49.473862Z","shell.execute_reply":"2020-11-07T01:42:49.473329Z"},"papermill":{"duration":0.066881,"end_time":"2020-11-07T01:42:49.473969","exception":false,"start_time":"2020-11-07T01:42:49.407088","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def load_model(path):\n    model = BaseLightningModule(model_options).to(DEVICE)\n    print(model.load_state_dict(torch.load(path, map_location=DEVICE)[\"state_dict\"]))\n    model.eval()\n    return model","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:49.598104Z","iopub.status.busy":"2020-11-07T01:42:49.597463Z","iopub.status.idle":"2020-11-07T01:42:57.137237Z","shell.execute_reply":"2020-11-07T01:42:57.138016Z"},"papermill":{"duration":7.60309,"end_time":"2020-11-07T01:42:57.138217","exception":false,"start_time":"2020-11-07T01:42:49.535127","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"paths = [\n    \"../input/best-submission/model_epoch15.ckpt\",\n    \"../input/best-submission/model_epoch45.ckpt\",\n    \"../input/best-submission/model_epoch46.ckpt\",\n    \"../input/best-submission/model_epoch47.ckpt\",\n    \"../input/best-submission/model_epoch50.ckpt\",\n    \"../input/best-submission/model_epoch51.ckpt\",\n    \"../input/best-submission/model_epoch52.ckpt\",\n    \"../input/best-submission/model_epoch53.ckpt\",\n    \"../input/best-submission/model_epoch54.ckpt\",\n    \"../input/best-submission/model_epoch55 (1).ckpt\",\n    \"../input/best-submission/model_epoch56.ckpt\",\n    \"../input/best-submission/model_epoch57.ckpt\",\n    \"../input/best-submission/model_epoch58.ckpt\",\n    \"../input/best-submission/model_epoch63.ckpt\",\n    \"../input/best-submission/model_epoch73.ckpt\",\n    \"../input/best-submission/model_epoch89.ckpt\",\n    \"../input/best-submission/model_epoch95.ckpt\",\n    \"../input/best-submission/model_epoch99.ckpt\"\n    \n    \n]\nweights = np.array([1,1,1,2,2,3,3,3,4,4,4,4,9,9,9,27,27,27], dtype=np.float32)\nweights /= weights.sum()\nprint(weights)\nnets = [ load_model(path) for path in paths]\nassert len(paths) == len(weights) == len(nets)\nnets[0]","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.055974,"end_time":"2020-11-07T01:42:57.252924","exception":false,"start_time":"2020-11-07T01:42:57.19695","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:57.377254Z","iopub.status.busy":"2020-11-07T01:42:57.375206Z","iopub.status.idle":"2020-11-07T01:42:57.378005Z","shell.execute_reply":"2020-11-07T01:42:57.378714Z"},"papermill":{"duration":0.067688,"end_time":"2020-11-07T01:42:57.378844","exception":false,"start_time":"2020-11-07T01:42:57.311156","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def make_colnames():\n    cols= list(\n        it.chain(*[\n    [f\"coord_x0{i}\", f\"coord_y0{i}\", f\"coord_x1{i}\", f\"coord_y1{i}\", f\"coord_x2{i}\", f\"coord_y2{i}\"]\n    for i in range(50)]))\n    return [\"timestamp\", \"track_id\"] + [\"conf_0\", \"conf_1\", \"conf_2\"] + cols","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:57.96837Z","iopub.status.busy":"2020-11-07T01:42:57.966416Z","iopub.status.idle":"2020-11-07T01:42:57.969127Z","shell.execute_reply":"2020-11-07T01:42:57.969685Z"},"papermill":{"duration":0.070765,"end_time":"2020-11-07T01:42:57.969802","exception":false,"start_time":"2020-11-07T01:42:57.899037","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def get_agent_data(agent_id):\n    scene, (frame,frame_id), agent = get_scene(agent_id)\n    X, X_light, _, _, le = dt.read_frames(\n            scene=scene,\n            start=frame_id-HBACKWARD,\n            white_tracks=[agent[\"track_id\"]],\n            encoder=True\n    )\n    \n    X_track_id = np.array([agent[\"track_id\"]], dtype=np.int64)\n    X_track = np.array([le.labels[agent[\"track_id\"]]], dtype=np.int64)\n    X_timestamp = np.array([frame[\"timestamp\"]], dtype=np.int64)\n    return X, X_light, X_track, X_track_id, X_timestamp\n    \n    \ndef collate(inp):\n    inp = zip(*inp)\n    inp = map(np.concatenate, inp)\n    inp = map(torch.from_numpy, inp)\n    inp = tuple(inp)\n    return inp\n\n\ndef get_agents_data(agent_ids):\n    mapper = delayed(get_agent_data)\n#     jobs = map(mapper, agent_ids)\n    jobs = [mapper(agent_id) for agent_id in agent_ids]\n    return collate(pool(tqdm(jobs, leave=False)))","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:58.086095Z","iopub.status.busy":"2020-11-07T01:42:58.0842Z","iopub.status.idle":"2020-11-07T01:42:58.086847Z","shell.execute_reply":"2020-11-07T01:42:58.087491Z"},"papermill":{"duration":0.062675,"end_time":"2020-11-07T01:42:58.087635","exception":false,"start_time":"2020-11-07T01:42:58.02496","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# get_agent_data(agent_ids[0])","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.057566,"end_time":"2020-11-07T01:42:58.238786","exception":false,"start_time":"2020-11-07T01:42:58.18122","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:42:58.422911Z","iopub.status.busy":"2020-11-07T01:42:58.411325Z","iopub.status.idle":"2020-11-07T01:43:01.337463Z","shell.execute_reply":"2020-11-07T01:43:01.336785Z"},"papermill":{"duration":3.043392,"end_time":"2020-11-07T01:43:01.3376","exception":false,"start_time":"2020-11-07T01:42:58.294208","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"x, x_light, x_track,x_track_id, x_timestamp = get_agents_data(agent_ids[:10])\nx.shape, x_light.shape, x_track.shape, x_track_id.shape","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:43:01.492238Z","iopub.status.busy":"2020-11-07T01:43:01.490155Z","iopub.status.idle":"2020-11-07T01:43:01.493092Z","shell.execute_reply":"2020-11-07T01:43:01.493668Z"},"papermill":{"duration":0.086699,"end_time":"2020-11-07T01:43:01.493804","exception":false,"start_time":"2020-11-07T01:43:01.407105","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"@torch.no_grad()\ndef predict_one_shot(x, x_light, x_track):\n    assert len(x) == len(x_light) == len(x_track)\n    \n    x, x_light, x_track = x.to(DEVICE), x_light.to(DEVICE), x_track.to(DEVICE)\n    \n    try:\n        c,y =  [],[]\n        for net in nets:\n            _c, _y = net((x, x_light))\n            \n            _c = torch.softmax(_c[torch.arange(len(_c)), x_track], dim=1).cpu().numpy()\n            _y = _y[torch.arange(len(_c)), x_track].cpu().numpy()\n            \n            c.append(_c)\n            y.append(_y)\n        \n        c = np.stack(c)\n        y = np.stack(y)\n        \n        c = np.sum(c*weights[:, None, None], axis=0)\n        y = np.sum(y*weights[:, None, None], axis=0)\n        \n    except Exception as e:\n        c = np.ones((len(x), 3))/3.\n        y = np.zeros((len(x), 300))\n#         print(f'Exception : {e}')\n        raise ValueError() from e\n    return c,y\n\n\n\n@torch.no_grad()\ndef predict(agent_ids, batch_size=200):\n    c, y = [], []\n    x_track_id, x_timestamp = [], []\n    for i in tqdm(list(range(0, len(agent_ids), batch_size))):\n        x, x_light, x_track, _x_track_id, _x_timestamp = get_agents_data(agent_ids[i:i+batch_size])\n        \n        _c, _y = predict_one_shot(x, x_light, x_track)\n        c.append(_c)\n        y.append(_y)\n        x_track_id.append(_x_track_id)\n        x_timestamp.append(_x_timestamp)\n        \n    c = np.vstack(c)\n    y = np.vstack(y)\n    \n    x_track_id = np.concatenate(x_track_id)\n    x_timestamp = np.concatenate(x_timestamp)\n    \n    return x_timestamp, x_track_id, c, y","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:43:01.638966Z","iopub.status.busy":"2020-11-07T01:43:01.637885Z","iopub.status.idle":"2020-11-07T01:58:22.543774Z","shell.execute_reply":"2020-11-07T01:58:22.536808Z"},"papermill":{"duration":920.990855,"end_time":"2020-11-07T01:58:22.543932","exception":false,"start_time":"2020-11-07T01:43:01.553077","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"timestamp, track_id, c, y = predict(agent_ids, batch_size=2000)\ntimestamp.shape, track_id.shape, c.shape, y.shape","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.129703,"end_time":"2020-11-07T01:58:22.819227","exception":false,"start_time":"2020-11-07T01:58:22.689524","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:58:23.082601Z","iopub.status.busy":"2020-11-07T01:58:23.081542Z","iopub.status.idle":"2020-11-07T01:58:23.087957Z","shell.execute_reply":"2020-11-07T01:58:23.088982Z"},"papermill":{"duration":0.148457,"end_time":"2020-11-07T01:58:23.089207","exception":false,"start_time":"2020-11-07T01:58:22.94075","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"pd.options.display.max_columns=305","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:58:23.265946Z","iopub.status.busy":"2020-11-07T01:58:23.265024Z","iopub.status.idle":"2020-11-07T01:58:23.269223Z","shell.execute_reply":"2020-11-07T01:58:23.268645Z"},"papermill":{"duration":0.087272,"end_time":"2020-11-07T01:58:23.269337","exception":false,"start_time":"2020-11-07T01:58:23.182065","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"y.shape","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:58:23.441791Z","iopub.status.busy":"2020-11-07T01:58:23.440894Z","iopub.status.idle":"2020-11-07T01:58:23.444555Z","shell.execute_reply":"2020-11-07T01:58:23.445077Z"},"papermill":{"duration":0.088345,"end_time":"2020-11-07T01:58:23.445214","exception":false,"start_time":"2020-11-07T01:58:23.356869","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"c.shape","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:58:23.642619Z","iopub.status.busy":"2020-11-07T01:58:23.633062Z","iopub.status.idle":"2020-11-07T01:59:27.604132Z","shell.execute_reply":"2020-11-07T01:59:27.603529Z"},"papermill":{"duration":64.077448,"end_time":"2020-11-07T01:59:27.604291","exception":false,"start_time":"2020-11-07T01:58:23.526843","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"%%time\n\ncols = make_colnames()\ndf = pd.DataFrame(columns=cols)\ndf[cols[0]] = timestamp\ndf[cols[1]] = track_id\ndf[cols[2:5]] = c\ndf[cols[5:]] = y\nsel = ~(df[cols[5:]]==0).all(1)\ndf = df[sel]\ndf.shape","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:59:27.805466Z","iopub.status.busy":"2020-11-07T01:59:27.800128Z","iopub.status.idle":"2020-11-07T01:59:28.228267Z","shell.execute_reply":"2020-11-07T01:59:28.22881Z"},"papermill":{"duration":0.538383,"end_time":"2020-11-07T01:59:28.228945","exception":false,"start_time":"2020-11-07T01:59:27.690562","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"df.head(20)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:59:28.477979Z","iopub.status.busy":"2020-11-07T01:59:28.475937Z","iopub.status.idle":"2020-11-07T01:59:28.881236Z","shell.execute_reply":"2020-11-07T01:59:28.882789Z"},"papermill":{"duration":0.557395,"end_time":"2020-11-07T01:59:28.883003","exception":false,"start_time":"2020-11-07T01:59:28.325608","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"xcols = list(it.chain(*[[f\"coord_x0{i}\", f\"coord_x1{i}\", f\"coord_x2{i}\"] for i in range(50)]))\ndf[[\"timestamp\", \"track_id\", 'conf_0', 'conf_1', 'conf_2',]+xcols].sample(20)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T01:59:29.19729Z","iopub.status.busy":"2020-11-07T01:59:29.196257Z","iopub.status.idle":"2020-11-07T02:00:24.236844Z","shell.execute_reply":"2020-11-07T02:00:24.235798Z"},"papermill":{"duration":55.19547,"end_time":"2020-11-07T02:00:24.236975","exception":false,"start_time":"2020-11-07T01:59:29.041505","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T02:00:24.446758Z","iopub.status.busy":"2020-11-07T02:00:24.445378Z","iopub.status.idle":"2020-11-07T02:00:24.489757Z","shell.execute_reply":"2020-11-07T02:00:24.490422Z"},"papermill":{"duration":0.152742,"end_time":"2020-11-07T02:00:24.490584","exception":false,"start_time":"2020-11-07T02:00:24.337842","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"xpred = y[:, ::2].reshape(y.shape[0], -1, 3)\nxpred[sel][:3, :10].round(3)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-11-07T02:00:24.727602Z","iopub.status.busy":"2020-11-07T02:00:24.726811Z","iopub.status.idle":"2020-11-07T02:00:24.734643Z","shell.execute_reply":"2020-11-07T02:00:24.734109Z"},"papermill":{"duration":0.114989,"end_time":"2020-11-07T02:00:24.734756","exception":false,"start_time":"2020-11-07T02:00:24.619767","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import shutil\nshutil.copy(HPARAM_PATH, \"hparams.yaml\")","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.104147,"end_time":"2020-11-07T02:00:24.936743","exception":false,"start_time":"2020-11-07T02:00:24.832596","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}