Skip to content

Commit

Permalink
Fix example (#657)
Browse files Browse the repository at this point in the history
* fix test

* add test for examples

* CUDA

* fix one test

* fixed let's roll

* make them executable

* add usr/bin/env/python

* fix test
  • Loading branch information
QuanyiLi authored Feb 21, 2024
1 parent fc04519 commit c326ae5
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 26 deletions.
6 changes: 4 additions & 2 deletions metadrive/component/sensors/base_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def track(self, new_parent_node: NodePath, position, hpr):
self.cam.setPos(*position)
self.cam.setHpr(*hpr)

def perceive(self, clip=True, new_parent_node: Union[NodePath, None] = None, position=None, hpr=None) -> np.ndarray:
def perceive(
self, to_float=True, new_parent_node: Union[NodePath, None] = None, position=None, hpr=None
) -> np.ndarray:
"""
When clip is set to False, the image will be represented by unit8 with component value ranging from [0-255].
Otherwise, it will be float type with component value ranging from [0.-1.]. By default, the reset parameters are
Expand Down Expand Up @@ -163,7 +165,7 @@ def perceive(self, clip=True, new_parent_node: Union[NodePath, None] = None, pos
self.cam.setHpr(original_hpr)
self.cam.setPos(original_position)

if not clip:
if not to_float:
return ret.astype(np.uint8, copy=False, order="C")
else:
return ret / 255
Expand Down
6 changes: 4 additions & 2 deletions metadrive/engine/core/main_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,9 @@ def set_mouse_to_center(self):
def mouse_into_window(self):
return True if not self._last_frame_has_mouse and self.has_mouse else False

def perceive(self, clip=True, new_parent_node: Union[NodePath, None] = None, position=None, hpr=None) -> np.ndarray:
def perceive(
self, to_float=True, new_parent_node: Union[NodePath, None] = None, position=None, hpr=None
) -> np.ndarray:
"""
When clip is set to False, the image will be represented by unit8 with component value ranging from [0-255].
Otherwise, it will be float type with component value ranging from [0.-1.]. By default, the reset parameters are
Expand Down Expand Up @@ -503,7 +505,7 @@ def perceive(self, clip=True, new_parent_node: Union[NodePath, None] = None, pos
self.camera.setHpr(original_hpr)
self.camera.setPos(original_position)

if not clip:
if not to_float:
return img.astype(np.uint8, copy=False, order="C")
else:
return img / 255
Expand Down
Empty file modified metadrive/examples/__init__.py
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions metadrive/examples/custom_inramp_env.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
"""
This script defines a custom environment with single block: inramp.
"""
Expand Down
1 change: 1 addition & 0 deletions metadrive/examples/draw_maps.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
import random

import matplotlib.pyplot as plt
Expand Down
1 change: 1 addition & 0 deletions metadrive/examples/drive_in_multi_agent_env.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
"""
This script demonstrates how to setup the Multi-agent RL environments.
Expand Down
1 change: 1 addition & 0 deletions metadrive/examples/drive_in_real_env.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
"""
This script demonstrates how to use the environment where traffic and road map are loaded from Waymo dataset.
"""
Expand Down
1 change: 1 addition & 0 deletions metadrive/examples/drive_in_safe_metadrive_env.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
"""
This script demonstrates how to setup the Safe RL environments.
Expand Down
1 change: 1 addition & 0 deletions metadrive/examples/drive_in_single_agent_env.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
"""
Please feel free to run this script to enjoy a journey by keyboard!
Remember to press H to see help message!
Expand Down
10 changes: 6 additions & 4 deletions metadrive/examples/generate_video_for_bev_and_interface.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
"""
This file will run environment for one episode and generate two videos:
Expand Down Expand Up @@ -30,6 +31,7 @@

env = MetaDriveEnv(
dict(
show_terrain="METADRIVE_TEST_EXAMPLE" not in os.environ,
num_scenarios=num_scenarios,
start_seed=start_seed,
random_traffic=False,
Expand All @@ -52,18 +54,18 @@
while True:
o, r, tm, tc, info = env.step(env.action_space.sample())

img_interface = env.render(mode="rgb_array")
img_interface = env.main_camera.perceive(to_float=False)
# BGR to RGB
img_interface = img_interface[..., ::-1]
img_bev = env.render(
mode="topdown",
target_vehicle_heading_up=False,
draw_target_vehicle_trajectory=True,
film_size=(3000, 3000),
screen_size=(3000, 3000),
crash_vehicle_done=False,
screen_size=(800, 800),
)

if generate_video:
img_bev = pygame.surfarray.array3d(img_bev)
img_bev = img_bev.swapaxes(0, 1)
video_bev.append(img_bev)
video_interface.append(img_interface)
Expand Down
1 change: 1 addition & 0 deletions metadrive/examples/procedural_generation.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
from metadrive.tests.vis_block.vis_big import vis_big

from metadrive.utils import setup_logger
Expand Down
1 change: 1 addition & 0 deletions metadrive/examples/profile_metadrive.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
import argparse
import time

Expand Down
1 change: 1 addition & 0 deletions metadrive/examples/profile_metadrive_marl.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
import argparse
import logging
import time
Expand Down
Empty file modified metadrive/examples/read_and_visualize_scenario_description.py
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions metadrive/examples/top_down_metadrive.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
"""
This file illustrate how to use top-down renderer to provide observation in form of multiple channels of semantic maps.
Expand Down
Empty file modified metadrive/examples/train_generalization_experiment.py
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions metadrive/examples/verify_headless_installation.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
from panda3d.core import loadPrcFileData
from metadrive.tests.test_installation import verify_installation
import argparse
Expand Down
16 changes: 13 additions & 3 deletions metadrive/examples/verify_image_observation.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#!/usr/bin/env python
import argparse
import time

torch_available = True
import cv2
import torch
from torch.utils.dlpack import from_dlpack
try:
import torch
from torch.utils.dlpack import from_dlpack
except ImportError:
torch_available = False
print("Can not find torch")
from metadrive.component.sensors.depth_camera import DepthCamera
from metadrive.component.sensors.semantic_camera import SemanticCamera
from metadrive.component.sensors.rgb_camera import RGBCamera
from metadrive import MetaDriveEnv
import os
from metadrive.policy.idm_policy import IDMPolicy


Expand All @@ -30,6 +37,7 @@ def _test_rgb_camera_as_obs(render=False, image_on_cuda=True, debug=False, camer

env = MetaDriveEnv(
dict(
show_terrain="METADRIVE_TEST_EXAMPLE" not in os.environ,
num_scenarios=1,
start_seed=1010,
agent_policy=IDMPolicy,
Expand Down Expand Up @@ -67,7 +75,7 @@ def _test_rgb_camera_as_obs(render=False, image_on_cuda=True, debug=False, camer
start = time.time()
if image_on_cuda:
torch_tensor = from_dlpack(o["image"].toDlpack())
else:
elif torch_available:
torch_tensor = torch.Tensor(o["image"])

if debug:
Expand All @@ -92,6 +100,8 @@ def _test_rgb_camera_as_obs(render=False, image_on_cuda=True, debug=False, camer
parser.add_argument("--debug", action="store_true")
parser.add_argument("--camera", default="main", choices=["main", "rgb", "depth", "semantic"])
args = parser.parse_args()
if args.cuda:
assert torch_available, "You have to install torch to use CUDA"
_test_rgb_camera_as_obs(args.render, image_on_cuda=args.cuda, debug=args.debug, camera=args.camera)
print(
"Test Successful !! The FPS should go beyond 400 FPS, if you are using CUDA in offscreen mode "
Expand Down
44 changes: 33 additions & 11 deletions metadrive/tests/test_examples/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
#!/usr/bin/env python

import os.path
import subprocess
from metadrive import MetaDrive_PACKAGE_DIR
import time
import pytest
import os

os.environ['CUDA_VISIBLE_DEVICES'] = ""
os.environ["METADRIVE_TEST_EXAMPLE"] = "1"

examples = [
"draw_maps.py", "drive_in_multi_agent_env.py --top_down", "drive_in_real_env.py --top_down",
"drive_in_real_env.py --top_down --waymo", "drive_in_real_env.py --reactive_traffic",
"drive_in_safe_metadrive_env.py", "drive_in_single_agent_env.py", "procedural_generation.py",
"profile_metadrive.py", "profile_metadrive_marl.py", "top_down_metadrive.py",
"draw_maps.py", ["drive_in_multi_agent_env.py", "--top_down"], ["drive_in_real_env.py", "--top_down"],
["drive_in_real_env.py", "--top_down",
"--waymo"], ["drive_in_real_env.py",
"--reactive_traffic"], "drive_in_safe_metadrive_env.py", "drive_in_single_agent_env.py",
"procedural_generation.py", "profile_metadrive.py", "profile_metadrive_marl.py", "top_down_metadrive.py",
"generate_video_for_bev_and_interface.py", "verify_headless_installation.py", "verify_image_observation.py"
]
examples_dir_path = os.path.join(MetaDrive_PACKAGE_DIR, "examples")
scripts = [os.path.join(examples_dir_path, exp) for exp in examples]
scripts = []
for exp in examples:
if isinstance(exp, list):
exp[0] = os.path.join(examples_dir_path, exp[0])
scripts.append(exp)
elif isinstance(exp, str):
scripts.append(os.path.join(examples_dir_path, exp))


@pytest.mark.parametrize("script", scripts, ids=examples)
@pytest.mark.parametrize("script", scripts, ids=[i for i, _ in enumerate(examples)])
def test_script(script, timeout=60):
"""
Run script in a subprocess and check its running time.
Expand All @@ -26,17 +39,26 @@ def test_script(script, timeout=60):
Returns: None
"""
start_time = time.time()

# Run your script using subprocess
process = subprocess.Popen(['python', script])
process = subprocess.Popen(['python'] + script if isinstance(script, list) else [script])

# Wait for the script to finish or timeout after 60 seconds
killed = False
try:
process.wait(timeout=timeout)
except subprocess.TimeoutExpired:
# If the script is still running after 60 seconds, terminate it and pass the test
process.kill()
killed = True
finally:
runtime = time.time() - start_time
assert runtime >= 0, "Script terminated unexpectedly"
if killed:
return
# Check if the process was terminated by a signal
if process.returncode < 0:
raise ValueError(f"Process terminated by signal {-process.returncode}.")
elif process.returncode > 0:
raise ValueError(f"Process exited with error code {process.returncode}.")


if __name__ == '__main__':
test_script(scripts[0])
14 changes: 10 additions & 4 deletions metadrive/tests/test_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@


def capture_headless_image(cuda, image_source="main_camera"):
if image_source == "main_camera":
sensors = {"main_camera": ()}
elif image_source == "rgb_camera":
sensors = {"rgb_camera": (RGBCamera, 256, 256)}
elif image_source == "depth_camera":
sensors = {"depth_camera": (DepthCamera, 256, 256)}
else:
sensors = {}
env = MetaDriveEnv(
dict(
use_render=False,
show_terrain="METADRIVE_TEST_EXAMPLE" not in os.environ,
start_seed=666,
image_on_cuda=cuda,
traffic_density=0.1,
image_observation=True,
window_size=(600, 400),
sensors={
"rgb_camera": (RGBCamera, 256, 256),
"depth_camera": (DepthCamera, 256, 256)
},
sensors=sensors,
interface_panel=[],
vehicle_config={
"image_source": image_source,
Expand Down

0 comments on commit c326ae5

Please sign in to comment.