Skip to content

Eliminate waymo_open_dataset dependency #1905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 17, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ Copy and pasting the git commit messages is __NOT__ enough.

## [Unreleased]
### Added
- Added `waymo_open_dataset` as a module at the SMARTS repo level, to be able to load waymo scenarios without any external packages
### Changed
### Deprecated
### Fixed
### Removed
- Removed `waymo-open-dataset-tf-2-4-0` package as a dependency
### Security

## [1.0.8] # 2023-03-10
Expand Down
19 changes: 1 addition & 18 deletions cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from cli.envision import envision_cli
from cli.run import run_experiment
from cli.studio import scenario_cli
from cli.waymo import waymo_cli
from cli.zoo import zoo_cli


Expand All @@ -44,24 +45,6 @@ def scl():
scl.add_command(scenario_cli)
scl.add_command(zoo_cli)
scl.add_command(run_experiment)

try:
from cli.waymo import waymo_cli
except (ModuleNotFoundError, ImportError):

@click.group(
name="waymo",
invoke_without_command=True,
help="The `scl waymo` command requires `[waymo]`.",
)
@click.pass_context
def waymo_cli(ctx):
click.echo(
"The `scl waymo` command is unavailable. To enable, pip install the missing dependencies.\n"
"pip install pathos==0.2.8 tabulate>=0.8.10 waymo-open-dataset-tf-2-4-0"
)


scl.add_command(waymo_cli)
scl.add_command(diagnostic_cli)

Expand Down
5 changes: 1 addition & 4 deletions scenarios/waymo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ The SMARTS repository contains tools to assist with using the Waymo Motion Datas
- `gen_sumo_map.py`: a command-line program that converts the map from a Waymo scenario to a SUMO map

## Setup
Follow the SMARTS setup instructions in the main [README](https://github.com/huawei-noah/SMARTS/). Then install the `[waymo]` dependencies.
```bash
$ pip install -e .[waymo]
```
Follow the SMARTS setup instructions in the main [README](https://github.com/huawei-noah/SMARTS/).

Download the dataset files from the [Waymo Motion Dataset](https://waymo.com/open/download/). It is recommended to download the dataset files from the `uncompressed/scenario/training_20s` folder as they have the full traffic capture for each scenario. Note: Waymo provides 2 different formats for the dataset files. SMARTS expects the `Scenario protos` format (not the `tf.Example protos` format). It is also recommended to use version 1.1 of the dataset, which includes enhanced map information.

Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ train =
waymo =
pathos==0.2.8
tabulate>=0.8.10
waymo-open-dataset-tf-2-4-0
opendrive =
opendrive2lanelet>=1.2.1

Expand Down
9 changes: 1 addition & 8 deletions smarts/core/default_map_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,7 @@ def get_road_map(map_spec) -> Tuple[Optional[RoadMap], Optional[str]]:
map_class = OpenDriveRoadNetwork

elif map_type == _WAYMO_MAP:
try:
from smarts.core.waymo_map import WaymoMap # pytype: disable=import-error
except (ImportError, ModuleNotFoundError):
print(sys.exc_info())
print(
"You may not have installed the [waymo] dependencies required to build and use WaymoMap Scenario. Install them first using the command `pip install -e .[waymo]` at the source directory."
)
return None, None
from smarts.core.waymo_map import WaymoMap
map_class = WaymoMap
else:
return None, None
Expand Down
2 changes: 1 addition & 1 deletion smarts/core/waymo_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ def parse_source_to_scenario(source: str):
f"Dataset file does not contain scenario with id: {scenario_id}"
)
parsed_scenario = scenario_pb2.Scenario()
parsed_scenario.ParseFromString(bytearray(record))
parsed_scenario.ParseFromString(bytes(record))
WaymoMap._scenario_cache[parsed_scenario.scenario_id] = parsed_scenario
if parsed_scenario.scenario_id == scenario_id:
return parsed_scenario
Expand Down
15 changes: 3 additions & 12 deletions smarts/sstudio/genhistories.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,8 @@
)
from smarts.sstudio import types
from smarts.waymo.waymo_utils import WaymoDatasetError

try:
# pytype: disable=import-error
from waymo_open_dataset.protos import scenario_pb2
from waymo_open_dataset.protos.map_pb2 import TrafficSignalLaneState

# pytype: enable=import-error
except ImportError:
print(
"You may not have installed the [waymo] dependencies required to use the waymo replay simulation. Install them first using the command `pip install -e .[waymo]` at the source directory."
)
from waymo_open_dataset.protos import scenario_pb2
from waymo_open_dataset.protos.map_pb2 import TrafficSignalLaneState

METERS_PER_FOOT = 0.3048
DEFAULT_LANE_WIDTH = 3.7 # a typical US highway lane is 12ft ~= 3.7m wide
Expand Down Expand Up @@ -810,7 +801,7 @@ def _get_scenario(self):
dataset = read_tfrecord_file(self._dataset_spec["input_path"])
for record in dataset:
parsed_scenario = scenario_pb2.Scenario()
parsed_scenario.ParseFromString(bytearray(record))
parsed_scenario.ParseFromString(bytes(record))
if parsed_scenario.scenario_id == scenario_id:
return parsed_scenario
raise ValueError(
Expand Down
7 changes: 5 additions & 2 deletions smarts/sstudio/scenario_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ def build_scenario(
f"from smarts.core import seed as smarts_seed; smarts_seed({seed});\n"
)
c.write(o.read())

c.flush()

# Set the python path to the base dir so the script can find modules properly
env = os.environ.copy()
env["PYTHONPATH"] = Path(__file__).parents[2]
subprocess.check_call(
[sys.executable, Path(c.name).name], cwd=scenario_root
[sys.executable, Path(c.name).name], cwd=scenario_root, env=env
)


Expand Down
4 changes: 2 additions & 2 deletions smarts/waymo/gen_sumo_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pathlib import Path
from typing import Callable, Dict, List, Tuple

from waymo_open_dataset.protos import map_pb2
from smarts.waymo import map_pb2

from smarts.core.waymo_map import WaymoMap

Expand Down Expand Up @@ -98,7 +98,7 @@ def _create_edge(
edge_id: str,
start_id: str,
end_id: str,
shape_str: Tuple[List[float], List[float]],
shape_str: str,
width: float = 5,
):
edge = ET.SubElement(self.edges_root, "edge")
Expand Down
2 changes: 1 addition & 1 deletion smarts/waymo/waymo_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ def get_scenario_and_tag_dict(
dataset = read_tfrecord_file(tfrecord_file)
for record in dataset:
scenario = scenario_pb2.Scenario()
scenario.ParseFromString(bytearray(record))
scenario.ParseFromString(bytes(record))
scenario_id = scenario.scenario_id
scenario_dict[scenario_id] = [scenario, None, None]
tags_per_scenario[scenario_id] = []
Expand Down
2 changes: 1 addition & 1 deletion smarts/waymo/waymo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def get_tfrecord_info(tfrecord_file: str) -> Dict[str, Dict[str, Any]]:
records = read_tfrecord_file(tfrecord_file)
for record in records:
scenario = scenario_pb2.Scenario()
scenario.ParseFromString(bytearray(record))
scenario.ParseFromString(bytes(record))
scenario_id = scenario.scenario_id
num_vehicles = 0
num_pedestrians = 0
Expand Down
22 changes: 22 additions & 0 deletions waymo_open_dataset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Waymo Open Dataset

This module is intended to eliminate dependence on the [`waymo_open_dataset`](https://github.com/waymo-research/waymo-open-dataset) package in SMARTS. We only include the `scenario` and `map` protobuf definitions, as well as their compiled Python files. The current Python files were generated using version `3.19.6` of the protobuf compiler. See the below instructions if you encounter any errors related to protobuf versions that require regenerating the Python files.

# Compilation Instructions

To regenerate the `scenario_pb2.py` and `map_pb2.py` files:

1. Install the protobuf compiler: see https://grpc.io/docs/protoc-installation/
2. (Optional) Download the proto files if necessary

```sh
curl https://raw.githubusercontent.com/waymo-research/waymo-open-dataset/master/waymo_open_dataset/protos/scenario.proto -o waymo_open_dataset/protos/scenario.proto

curl https://raw.githubusercontent.com/waymo-research/waymo-open-dataset/master/waymo_open_dataset/protos/map.proto -o waymo_open_dataset/protos/map.proto
```

3. Compile the proto files

```sh
protoc -I=. --python_out=. waymo_open_dataset/protos/scenario.proto && protoc -I=. --python_out=. waymo_open_dataset/protos/map.proto
```
Empty file added waymo_open_dataset/__init__.py
Empty file.
Loading