{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Installing l5kit offline\n!pip install --no-index -f ../input/kaggle-l5kit pip==20.2.2 >/dev/nul\n!pip install --no-index -f ../input/kaggle-l5kit -U l5kit > /dev/nul","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport zarr\nfrom l5kit.data import LocalDataManager, ChunkedDataset, get_frames_slice_from_scenes\nfrom l5kit.dataset import AgentDataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class NoImageAgentDataset(AgentDataset):\n    '''Copy-pasted get_frame with small tweak to return empty matrix instead of image (to not use rasterizer)\n    '''\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n\n    def get_frame(self, scene_index: int, state_index: int, track_id = None) -> dict:\n        \"\"\"\n        A utility function to get the rasterisation and trajectory target for a given agent in a given frame\n\n        Args:\n            scene_index (int): the index of the scene in the zarr\n            state_index (int): a relative frame index in the scene\n            track_id (Optional[int]): the agent to rasterize or None for the AV\n        Returns:\n            dict: the rasterised image, the target trajectory (position and yaw) along with their availability,\n            the 2D matrix to center that agent, the agent track (-1 if ego) and the timestamp\n\n        \"\"\"\n        frames = self.dataset.frames[get_frames_slice_from_scenes(self.dataset.scenes[scene_index])]\n        data = self.sample_function(state_index, frames, self.dataset.agents, self.dataset.tl_faces, track_id)\n\n        target_positions = np.array(data[\"target_positions\"], dtype=np.float32)\n        target_yaws = np.array(data[\"target_yaws\"], dtype=np.float32)\n\n        history_positions = np.array(data[\"history_positions\"], dtype=np.float32)\n        history_yaws = np.array(data[\"history_yaws\"], dtype=np.float32)\n\n        timestamp = frames[state_index][\"timestamp\"]\n        track_id = np.int64(-1 if track_id is None else track_id)  # always a number to avoid crashing torch\n\n        return {\n            \"image\": np.zeros(2),\n            \"target_positions\": target_positions,\n            \"target_yaws\": target_yaws,\n            \"target_availabilities\": data[\"target_availabilities\"],\n            \"history_positions\": history_positions,\n            \"history_yaws\": history_yaws,\n            \"history_availabilities\": data[\"history_availabilities\"],\n            \"world_to_image\": data[\"world_to_image\"],\n            \"track_id\": track_id,\n            \"timestamp\": timestamp,\n            \"centroid\": data[\"centroid\"],\n            \"yaw\": data[\"yaw\"],\n            \"extent\": data[\"extent\"],\n        }\n    @staticmethod\n    def from_agent_ds(ds):\n        return NoImageAgentDataset(\n            ds.cfg,\n            ds.dataset,\n            ds.rasterizer,\n            ds.perturbation,\n            ds.agents_mask\n        )\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cfg = {\n     'model_params': {\n         'model_architecture': 'resnet50',\n         'history_num_frames': 0, \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    'raster_params': {\n        'raster_size': [224, 224], \n        'pixel_size': [0.5, 0.5], \n        'ego_center': [0.25, 0.5], \n        'map_type': 'py_semantic', \n        'satellite_map_key': \n        '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    'sample_data_loader': {\n        'key': 'scenes/sample.zarr', \n        'batch_size': 12, \n        'shuffle': False, \n        'num_workers': 16\n    }\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# set env variable for data\nos.environ[\"L5KIT_DATA_FOLDER\"] = \"../input/lyft-motion-prediction-autonomous-vehicles\"\ndm = LocalDataManager()\ndataset_path = dm.require(cfg['sample_data_loader']['key'])\nzarr_dataset = ChunkedDataset(dataset_path)\nzarr_dataset.open()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = NoImageAgentDataset(cfg, zarr_dataset, None)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"SCENE_IDX = 74\nagents = dataset.from_agent_ds(dataset.get_scene_dataset(SCENE_IDX))\ndf = pd.DataFrame(iter(agents))\nscene_record = zarr_dataset.scenes[SCENE_IDX]\ndisplay(scene_record['start_time'] - df['timestamp'].min(), scene_record['end_time'] - df['timestamp'].max())","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}