Skip to content
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

Driving in traffic - dependency version and type annotation #1250

Merged
merged 57 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
05e26f9
training working
Dec 9, 2021
c902cc8
evaluate model
Dec 9, 2021
8beb14e
break into functions
Dec 10, 2021
a547ec5
add arguments and config file
Dec 10, 2021
0cd725e
finish adding arguments
Dec 10, 2021
d99ec37
run black formatting
Dec 13, 2021
36cc0d7
create readme
Dec 13, 2021
91041c3
change log path
Dec 13, 2021
cfe2240
update readme
Dec 13, 2021
c003612
create dockerfile
Dec 14, 2021
1fa1bfb
change requirements path
Dec 14, 2021
0552b70
create docker image
Dec 14, 2021
994001d
temporary fix for importing argument parser
Dec 17, 2021
4080abc
change logging save path and structure
Dec 20, 2021
d5f8705
update dockerfile to install sb3 extras
Dec 20, 2021
e388285
add colab notebook
Dec 20, 2021
ff2e326
run black
Dec 20, 2021
e2ea4a1
create mode for colab notebooks
Dec 20, 2021
1a40835
check for colab notebook
Dec 21, 2021
6b648b8
create init for sb3 example
Dec 21, 2021
ddfb421
use reward wrapper
Dec 22, 2021
ad82af9
update notebook
liu-allan Dec 22, 2021
ac66a77
run black formatting
liu-allan Dec 22, 2021
e78a00e
notebook working, without envision
liu-allan Dec 22, 2021
4397d31
remove cell outputs
liu-allan Dec 22, 2021
7af7f3e
change docker file image base, update readme, wrap monitor
liu-allan Dec 22, 2021
20c1f51
change sumo version to 1.10.0 on dockerfile
liu-allan Dec 22, 2021
e94c906
use sumo 1.10.0
liu-allan Dec 23, 2021
562033e
penalize stopping
liu-allan Dec 23, 2021
be0ef5d
change to ego_vehicle_state
liu-allan Dec 23, 2021
0d3c654
add vim and git
liu-allan Dec 24, 2021
ff18ed0
convert save path to string
liu-allan Jan 6, 2022
29234f1
remove duplicate code
liu-allan Jan 6, 2022
8d9d856
remove default argument parser
liu-allan Jan 6, 2022
d44d8bb
move create_env to separate file
liu-allan Jan 6, 2022
e895c4c
remove vim and git from dockerfile
liu-allan Jan 6, 2022
592c1a5
remove colab notebook
liu-allan Jan 6, 2022
7d7a2a6
create setup.py
liu-allan Jan 6, 2022
79a4e69
automatically build scenario
liu-allan Jan 7, 2022
09887ad
update setup.py
liu-allan Jan 7, 2022
29feb27
automatically create log folder in run
liu-allan Jan 7, 2022
ad71f9e
run black formatting
liu-allan Jan 7, 2022
b523649
include obs and action space in README
liu-allan Jan 10, 2022
5667bb7
Merge branch 'develop' into stable-baselines-3-2
Adaickalavan Jan 20, 2022
21b5bc7
Reorganize example.
Adaickalavan Jan 20, 2022
013ba08
Simplify code.
Adaickalavan Jan 20, 2022
226211e
Reorganize train test sequence.
Adaickalavan Jan 21, 2022
72b0eb1
Update requirements.txt
Adaickalavan Jan 21, 2022
cc0b5df
Segregate train test into separate function.
Adaickalavan Jan 21, 2022
d872878
Compress repeated train test steps.
Adaickalavan Jan 21, 2022
4c128fb
Simplify config.
Adaickalavan Jan 21, 2022
01cc195
Type hints.
Adaickalavan Jan 21, 2022
9ade8de
Cleaned.
Adaickalavan Jan 21, 2022
cff325b
Add Changelog.
Adaickalavan Jan 21, 2022
eae0126
Merge branch 'develop' into driving-in-traffic
Adaickalavan Jan 21, 2022
a3a32dd
Remove SB3 example.
Adaickalavan Jan 21, 2022
6c471a3
Merge branch 'develop' into driving-in-traffic
Adaickalavan Jan 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import os
import subprocess
import sys
from typing import Sequence
from pathlib import Path
from threading import Thread
from typing import Sequence

import click

Expand Down
4 changes: 2 additions & 2 deletions examples/driving_in_traffic/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ WORKDIR /src
RUN pip install --no-cache-dir -e ./examples/driving_in_traffic

# Build scenarios and maps.
RUN scl scenario build-all --clean ./examples/driving_in_traffic/scenarios/loop
RUN scl scenario build-all --clean ./examples/driving_in_traffic/scenarios

# For Envision.
EXPOSE 8081
Expand All @@ -50,6 +50,6 @@ EXPOSE 8081
ENV TF_XLA_FLAGS="--tf_xla_enable_xla_devices --tf_xla_auto_jit=2"

# Suppress message of missing /dev/input folder.
RUN echo "mkdir -p /dev/input\n" >> ~/.bashrc
RUN echo "mkdir -p /dev/input" >> ~/.bashrc

SHELL ["/bin/bash", "-c", "-l"]
2 changes: 1 addition & 1 deletion examples/driving_in_traffic/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Driving in Traffic
This example illustrates the training of an ego agent to drive, as fast and as far as possible, in traffic using the DreamerV2 (https://github.com/danijar/dreamerv2) reinforcement-learning algorithm.
This example illustrates the training of an ego agent to drive, as fast and as far as possible, in traffic using the [DreamerV2](https://github.com/danijar/dreamerv2) reinforcement-learning algorithm.

The ego agent earns reward for the distance travelled per-step and is penalised for colliding with other vehicles and for going off-road.

Expand Down
1 change: 1 addition & 0 deletions examples/driving_in_traffic/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ smarts:
- loop

dreamerv2:
steps: 1e8
log_every: 1e4
eval_every: 1e5
prefill: 1e4
Expand Down
6 changes: 4 additions & 2 deletions examples/driving_in_traffic/driving_in_traffic/env/action.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Callable, Tuple

import gym
import numpy as np


class Action(gym.ActionWrapper):
def __init__(self, env):
def __init__(self, env: gym.Env):
super().__init__(env)
self._wrapper, self.action_space = continuous()

Expand All @@ -14,7 +16,7 @@ def action(self, act):
return wrapped_act


def continuous():
def continuous() -> Tuple[Callable[[np.array], np.array], gym.Space]:
space = gym.spaces.Box(low=-1.0, high=1.0, shape=(3,), dtype=np.float32)

def wrapper(model_action):
Expand Down
6 changes: 4 additions & 2 deletions examples/driving_in_traffic/driving_in_traffic/env/reward.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import gym
import numpy as np

from smarts.core.sensors import Observation


class Reward(gym.Wrapper):
def __init__(self, env):
def __init__(self, env: gym.Env):
super().__init__(env)

def reset(self, **kwargs):
Expand All @@ -19,7 +21,7 @@ def step(self, action):

return obs, wrapped_reward, done, info

def _reward(self, obs, env_reward):
def _reward(self, obs: Observation, env_reward: np.float64) -> np.float32:
reward = 0

# Penalty for driving off road
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import partial
from typing import Dict
from typing import Any, Callable, Dict, Generator

import gym
from driving_in_traffic.env import action, reward

from smarts.core import agent as smarts_agent
Expand All @@ -11,14 +12,16 @@
from smarts.env.wrappers import single_agent as smarts_single_agent


def gen_env(config: Dict, seed: int):
def gen_env(
config: Dict[str, Any], seed: int
) -> Generator[Callable[[str], gym.Env], None, None]:
base_seed = seed
while True:
yield partial(make_env, config=config, seed=base_seed)
base_seed += 1


def make_env(config: Dict, seed: int, env_name: str = None):
def make_env(config: Dict[str, Any], seed: int, env_name: str = None) -> gym.Env:

vehicle_interface = smarts_agent_interface.AgentInterface(
max_episode_steps=config["max_episode_steps"],
Expand Down
76 changes: 38 additions & 38 deletions examples/driving_in_traffic/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import re
import warnings
from datetime import datetime
from typing import Callable, Generator

import gym
import numpy as np
import rich.traceback
import tensorflow as tf
Expand All @@ -33,7 +35,7 @@
seed(42)


def main(args):
def main(args: argparse.Namespace):
# Load config file.
config_file = yaml.load(
(pathlib.Path(__file__).absolute().parent / "config.yaml").read_text()
Expand All @@ -42,7 +44,6 @@ def main(args):
# Load SMARTS env config.
config_env = config_file["smarts"]
config_env["mode"] = args.mode
config_env["logdir"] = args.logdir
config_env["headless"] = not args.head
config_env["scenarios_dir"] = (
pathlib.Path(__file__).absolute().parents[0] / "scenarios"
Expand All @@ -57,46 +58,24 @@ def main(args):
tf.config.run_functions_eagerly(not config_dv2.jit)

# Setup GPU.
gpus = tf.config.list_physical_devices("GPU")
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.list_logical_devices("GPU")
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
print(e)
else:
warnings.warn(
f"Not configured to use GPU or GPU not available.",
ResourceWarning,
)
_setup_gpu()

# Train or evaluate.
if config_env["mode"] == "train":
if not config_env["logdir"]:
# Begin training from scratch.
time = datetime.now().strftime("%Y_%m_%d_%H_%M")
logdir = pathlib.Path(__file__).absolute().parents[0] / "logs" / time
else:
# Begin training from a pretrained model.
logdir = config_env["logdir"]
config_dv2 = config_dv2.update(
{
"logdir": logdir,
}
)
if config_env["mode"] == "train" and not args.logdir:
# Train from scratch.
time = datetime.now().strftime("%Y_%m_%d_%H_%M")
logdir = pathlib.Path(__file__).absolute().parents[0] / "logs" / time
elif config_env["mode"] == "train" and args.logdir:
# Begin training from a pretrained model.
logdir = args.logdir
elif config_env["mode"] == "evaluate":
config_dv2 = config_dv2.update(
{
"logdir": config_env["logdir"],
"eval_eps": 1e8,
}
)
logdir = args.logdir
config_dv2 = config_dv2.update({"eval_eps": 1e8})
else:
raise KeyError(
f'Expected \'train\' or \'evaluate\', but got {config_env["mode"]}.'
)
config_dv2 = config_dv2.update({"logdir": logdir})

# Create SMARTS env.
gen_env = single_agent.gen_env(config_env, config_env["seed"])
Expand All @@ -111,7 +90,24 @@ def _build_scenario():
os.system(build_scenario)


def _wrap_env(env, config):
def _setup_gpu():
gpus = tf.config.list_physical_devices("GPU")
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.list_logical_devices("GPU")
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
print(e)
else:
warnings.warn(
f"Not configured to use GPU or GPU not available.",
ResourceWarning,
)


def _wrap_env(env: gym.Env, config: common.config.Config):
env = dv2.common.GymWrapper(env)
env = dv2.common.ResizeImage(env)
if hasattr(env.act_space["action"], "n"):
Expand All @@ -122,12 +118,16 @@ def _wrap_env(env, config):
return env


def run(config, gen_env, mode: str):
def run(
config: common.config.Config,
gen_env: Generator[Callable[[str], gym.Env], None, None],
mode: str,
):
logdir = pathlib.Path(config.logdir).expanduser()
logdir.mkdir(parents=True, exist_ok=True)
config.save(logdir / "config.yaml")
print(config, "\n")
print("Logdir", logdir)
print("Logdir:", logdir)

train_replay = dv2.common.Replay(logdir / "train_episodes", **config.replay)
eval_replay = dv2.common.Replay(
Expand Down
2 changes: 1 addition & 1 deletion examples/driving_in_traffic/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
python_requires=">=3.7",
install_requires=[
"setuptools>=41.0.0,!=50.0",
"smarts[camera-obs] @ git+https://[email protected]/huawei-noah/SMARTS@develop",
"smarts[camera-obs]==0.5.0",
"dreamerv2==2.1.1",
"tensorflow==2.4.0",
"tensorflow-probability==0.12.2",
Expand Down
2 changes: 1 addition & 1 deletion scenarios/od_merge/scenario.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path

from smarts.sstudio.genscenario import gen_scenario
from smarts.sstudio import types as t
from smarts.sstudio.genscenario import gen_scenario

ego_missions = [t.Mission(t.Route(begin=("1_0_R", 1, 5), end=("1_2_R", 1, "max")))]

Expand Down
4 changes: 2 additions & 2 deletions smarts/core/default_map_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import os
from dataclasses import replace
from typing import NamedTuple, Optional, Tuple
from pathlib import Path
from typing import NamedTuple, Optional, Tuple

from smarts.core.road_map import RoadMap
from smarts.core.utils.file import file_md5_hash, path2hash


_existing_map = None


Expand Down
3 changes: 2 additions & 1 deletion smarts/core/lanepoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from dataclasses import dataclass
from functools import lru_cache
from typing import List, NamedTuple, Sequence, Tuple

import numpy as np

with warnings.catch_warnings():
Expand All @@ -39,7 +40,7 @@
# aggressive
from sklearn.neighbors import KDTree

from smarts.core.coordinates import Heading, Pose, Point
from smarts.core.coordinates import Heading, Point, Pose
from smarts.core.road_map import RoadMap
from smarts.core.utils.math import (
fast_quaternion_from_angle,
Expand Down
29 changes: 15 additions & 14 deletions smarts/core/opendrive_road_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import heapq
import os
import logging
import math
import os
import random
import time
from bisect import bisect
from dataclasses import dataclass
from functools import lru_cache
from typing import Dict, List, Sequence, Set, Tuple, Optional
from typing import Dict, List, Optional, Sequence, Set, Tuple

import numpy as np
import rtree
import trimesh
import trimesh.scene
from trimesh.exchange import gltf
import numpy as np
from cached_property import cached_property
from lxml import etree
from opendrive2lanelet.opendriveparser.elements.geometry import Line as LineGeometry
from opendrive2lanelet.opendriveparser.elements.opendrive import (
OpenDrive as OpenDriveElement,
Expand All @@ -50,29 +53,27 @@
PlanView as PlanViewElement,
)
from opendrive2lanelet.opendriveparser.parser import parse_opendrive
from lxml import etree
from shapely.geometry import Polygon
import rtree
from bisect import bisect
from trimesh.exchange import gltf

from smarts.core.road_map import RoadMap, Waypoint
from smarts.sstudio.types import MapSpec
from smarts.core.utils.geometry import generate_mesh_from_polygons
from smarts.core.utils.key_wrapper import KeyWrapper
from smarts.core.utils.math import (
CubicPolynomial,
constrain_angle,
distance_point_to_polygon,
get_linear_segments_for_range,
inplace_unwrap,
offset_along_shape,
position_at_shape_offset,
vec_2d,
inplace_unwrap,
radians_to_vec,
vec_2d,
)
from smarts.sstudio.types import MapSpec

from .lanepoints import LinkedLanePoint, LanePoints
from .coordinates import BoundingBox, Point, Pose, RefLinePoint, Heading
from smarts.core.utils.geometry import generate_mesh_from_polygons
from smarts.core.utils.key_wrapper import KeyWrapper
from .coordinates import BoundingBox, Heading, Point, Pose, RefLinePoint
from .lanepoints import LanePoints, LinkedLanePoint


def _convert_camera(camera):
Expand Down
9 changes: 4 additions & 5 deletions smarts/core/sumo_road_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@
import os
import random
from functools import lru_cache
from subprocess import check_output
from typing import List, Optional, Sequence, Set, Tuple

import numpy as np
import trimesh
import trimesh.scene
from cached_property import cached_property
from shapely.geometry import Point as shPoint
from shapely.geometry import Polygon
from shapely.ops import snap, nearest_points
from shapely.ops import nearest_points, snap
from trimesh.exchange import gltf
from shapely.geometry import Point as shPoint
from subprocess import check_output


from smarts.sstudio.types import MapSpec

from .coordinates import BoundingBox, Heading, Point, Pose, RefLinePoint
from .lanepoints import LanePoints, LinkedLanePoint
from .road_map import RoadMap, Waypoint
from .lanepoints import LinkedLanePoint, LanePoints
from .utils.geometry import buffered_shape, generate_mesh_from_polygons
from .utils.math import inplace_unwrap, radians_to_vec, vec_2d

Expand Down
1 change: 1 addition & 0 deletions smarts/core/sumo_traffic_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from smarts.core.utils import networking
from smarts.core.utils.logging import suppress_output
from smarts.core.vehicle import VEHICLE_CONFIGS, VehicleState

from smarts.core.utils.sumo import SUMO_PATH, traci # isort:skip
from traci.exceptions import FatalTraCIError, TraCIException # isort:skip
import traci.constants as tc # isort:skip
Expand Down
Loading