{"cells":[{"metadata":{},"cell_type":"markdown","source":"In this kernel I wanted to set up a very simple benchmark to understand the inference pipeline and the data available to us as well as lay down a very low bar for prediction. \n\nA simple but reasonable baseline for this task is to just assume the car will continue with the same heading and speed it is currently traveling at. In this kernel I take the difference between the last two known points and then extrapolate using the last known velocity. This will by no means win the competition but can at least function as a sanity check to see how good the models actually are in comparison to this naive method.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"As shown in this discussion and the linked coursera lecture this is a fairly standard method.  https://www.kaggle.com/c/lyft-motion-prediction-autonomous-vehicles/discussion/177130\n\n![cve](https://i.imgur.com/Zvrx2bd.png)\n\n![](https://i.imgur.com/k1uAU6o.png)\n\n![](https://i.imgur.com/JDPTCuv.png)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"","execution_count":null},{"metadata":{"_uuid":"c91c3a45-0394-44f3-b518-46ddf3c05072","_cell_guid":"6dde1bc3-89fa-4129-a54e-11a6128c01fe","trusted":true},"cell_type":"code","source":"# import os\n\n# ## =====================================================================================\n# ## This is a temporarly fix for the freezing and the cuda issues. You can add this\n# ## utility script instead of kaggle_l5kit until Kaggle resolve these issues.\n# ## \n# ## You will be able to train and submit your results, but not all the functionality of\n# ## l5kit will work properly.\n\n# ## More details here:\n# ## https://www.kaggle.com/c/lyft-motion-prediction-autonomous-vehicles/discussion/177125\n\n# ## this script transports l5kit and dependencies\n# os.system('pip install --target=/kaggle/working pymap3d==2.1.0')\n# os.system('pip install --target=/kaggle/working protobuf==3.12.2')\n# os.system('pip install --target=/kaggle/working transforms3d')\n# os.system('pip install --target=/kaggle/working zarr')\n# os.system('pip install --target=/kaggle/working ptable')\n\n# os.system('pip install --no-dependencies --target=/kaggle/working l5kit')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from typing import Dict\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport torch\n# torch.multiprocessing.set_sharing_strategy('file_system')\nfrom torch import nn, optim\nfrom torch.utils.data import DataLoader\nfrom tqdm.notebook import tqdm\n\nfrom l5kit.data import LocalDataManager, ChunkedDataset\nfrom l5kit.rasterization import build_rasterizer\nfrom l5kit.evaluation import write_pred_csv, compute_metrics_csv, read_gt_csv, write_gt_csv\nfrom l5kit.evaluation.metrics import neg_multi_log_likelihood, time_displace\nfrom l5kit.geometry import transform_points\nfrom l5kit.visualization import PREDICTED_POINTS_COLOR, TARGET_POINTS_COLOR, draw_trajectory\nfrom l5kit.dataset import AgentDataset\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DIR_INPUT = \"../input/lyft-motion-prediction-autonomous-vehicles\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cfg = {\n    'format_version': 4,\n    'model_params': {\n        'history_num_frames': 2,\n        'history_step_size': 1,\n        'history_delta_time': 0.1,\n        'future_num_frames': 50,\n        'future_step_size': 1,\n        'future_delta_time': 0.1\n    },\n    \n    'raster_params': {\n        'raster_size': [300, 300],\n        'pixel_size': [0.5, 0.5],\n        'ego_center': [0.25, 0.5],\n        'map_type': 'py_semantic',\n        'satellite_map_key': 'aerial_map/aerial_map.png',\n        'semantic_map_key': 'semantic_map/semantic_map.pb',\n        'dataset_meta_key': 'meta.json',\n        'filter_agents_threshold': 0.5\n    },\n    \n    'test_data_loader': {\n        'key': 'scenes/test.zarr',\n        'batch_size': 8,\n        'shuffle': False,\n        'num_workers': 2\n    },\n    \n    'train_data_loader': {\n        'key': 'scenes/train.zarr',\n        'batch_size': 8,\n        'shuffle': True,\n        'num_workers': 2\n    },\n    \n    'val_data_loader': {\n        'key': 'scenes/validate.zarr',\n        'batch_size': 8,\n        'shuffle': False,\n        'num_workers': 2\n    }\n\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# set env variable for data\nos.environ[\"L5KIT_DATA_FOLDER\"] = DIR_INPUT\ndm = LocalDataManager(None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"rasterizer = build_rasterizer(cfg, dm)\n\n# Test dataset/dataloader\ntest_zarr = ChunkedDataset(dm.require(cfg['test_data_loader'][\"key\"])).open()\ntest_mask = np.load(f\"{DIR_INPUT}/scenes/mask.npz\")[\"arr_0\"]\ntest_dataset = AgentDataset(cfg, test_zarr, rasterizer, agents_mask=test_mask)\ntest_dataloader = DataLoader(test_dataset,\n                             shuffle=False,\n                             batch_size=8,\n                             num_workers=4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_zarr = ChunkedDataset(dm.require(cfg['val_data_loader'][\"key\"])).open()\nval_dataset = AgentDataset(cfg, val_zarr, rasterizer)\nval_dataloader = DataLoader(val_dataset,\n                             shuffle=False,\n                             batch_size=8,\n                             num_workers=4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_preds(dataloader, pred_path, truth_path, batches = 100):\n    timestamps = []\n    agent_ids = []\n    future_coords_offsets_pd = []\n    truths = []\n    target_availabilities = []\n    for batch_idx, data in enumerate(tqdm(dataloader)):\n        if batch_idx == batches:\n            break\n        timestamps.append(data[\"timestamp\"])\n        agent_ids.append(data[\"track_id\"])\n\n        u_point = data[\"history_positions\"][:, :1, :].detach().cpu().numpy()\n        pu_point = data[\"history_positions\"][:, 1, :].detach().cpu().numpy()\n        speed = (u_point[:, 0, :] - pu_point)\n        predictions = np.ones((data[\"target_positions\"].shape))*u_point\n        \n        for i in range(50):\n            predictions[:, i, :] = predictions[:, i-1, :] + speed\n        future_coords_offsets_pd.append(predictions)\n        truths.append(data[\"target_positions\"])\n        target_availabilities.append(data[\"target_availabilities\"])\n    print(np.concatenate(future_coords_offsets_pd).shape, np.concatenate(truths).shape)\n    write_pred_csv(pred_path,\n           timestamps=np.concatenate(timestamps),\n           track_ids=np.concatenate(agent_ids),\n           coords=np.concatenate(future_coords_offsets_pd),\n          )\n    write_gt_csv(truth_path,\n                 np.concatenate(timestamps),\n                 np.concatenate(agent_ids),\n                 np.concatenate(truths),\n                 np.concatenate(target_availabilities)\n          )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_labels_path = \"val_labels.csv\"\nval_predictions_path = \"val_predictions.csv\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"make_preds(val_dataloader, val_predictions_path, val_labels_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"metrics = compute_metrics_csv(val_labels_path, val_predictions_path, [neg_multi_log_likelihood, time_displace])\nfor metric_name, metric_mean in metrics.items():\n    print(metric_name, metric_mean)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"make_preds(test_dataloader, \"submission.csv\", \"test_dummy_labels.csv\", batches = -1)","execution_count":null,"outputs":[]},{"metadata":{"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}