找出你应该用多少/什么相机训练#

目前在Isaac Lab,有几种相机类型; USD相机(标准)、分块相机和光线投射相机。这些相机类型在功能和性能上有所不同。 benchmark_cameras.py 脚本可用于了解相机类型的差异,以及表征它们在不同参数(如相机数量、图像尺寸和数据类型)下的相对性能。

这个实用程序的目的是让用户能够轻松找到在满足用户场景要求的情况下性能最优的相机类型/参数。该实用程序还可以帮助估计用户可以实际运行的相机最大数量,假设用户想要最大化环境数量同时最小化步骤时间。

这个实用程序可以将相机注入到来自健身房注册表的现有任务中,这对于在特定场景中对相机进行基准测试可能很有用。此外,如果您安装了 pynvml ,则可以让此实用程序自动查找可以在您的任务环境中运行的相机的最大数量,直到达到特定指定的系统资源利用阈值为止(不进行训练,在每个时间步骤上不采取任何行动)。

这个指南配套使用 scripts/benchmarks 目录中的 benchmark_cameras.py 脚本。

代码 for benchmark_cameras.py
  1# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
  2# All rights reserved.
  3#
  4# SPDX-License-Identifier: BSD-3-Clause
  5
  6"""
  7This script might help you determine how many cameras your system can realistically run
  8at different desired settings.
  9
 10You can supply different task environments to inject cameras into, or just test a sample scene.
 11Additionally, you can automatically find the maximum amount of cameras you can run a task with
 12through the auto-tune functionality.
 13
 14.. code-block:: bash
 15
 16    # Usage with GUI
 17    ./isaaclab.sh -p scripts/benchmarks/benchmark_cameras.py -h
 18
 19    # Usage with headless
 20    ./isaaclab.sh -p scripts/benchmarks/benchmark_cameras.py -h --headless
 21
 22"""
 23
 24"""Launch Isaac Sim Simulator first."""
 25
 26import argparse
 27from collections.abc import Callable
 28
 29from isaaclab.app import AppLauncher
 30
 31# parse the arguments
 32args_cli = argparse.Namespace()
 33
 34parser = argparse.ArgumentParser(description="This script can help you benchmark how many cameras you could run.")
 35
 36"""
 37The following arguments only need to be supplied for when one wishes
 38to try injecting cameras into their environment, and automatically determining
 39the maximum camera count.
 40"""
 41parser.add_argument(
 42    "--task",
 43    type=str,
 44    default=None,
 45    required=False,
 46    help="Supply this argument to spawn cameras within an known manager-based task environment.",
 47)
 48
 49parser.add_argument(
 50    "--autotune",
 51    default=False,
 52    action="store_true",
 53    help=(
 54        "Autotuning is only supported for provided task environments."
 55        " Supply this argument to increase the number of environments until a desired threshold is reached."
 56        "Install pynvml in your environment; ./isaaclab.sh -m pip install pynvml"
 57    ),
 58)
 59
 60parser.add_argument(
 61    "--task_num_cameras_per_env",
 62    type=int,
 63    default=1,
 64    help="The number of cameras per environment to use when using a known task.",
 65)
 66
 67parser.add_argument(
 68    "--use_fabric", action="store_true", default=False, help="Enable fabric and use USD I/O operations."
 69)
 70
 71parser.add_argument(
 72    "--autotune_max_percentage_util",
 73    nargs="+",
 74    type=float,
 75    default=[100.0, 80.0, 80.0, 80.0],
 76    required=False,
 77    help=(
 78        "The system utilization percentage thresholds to reach before an autotune is finished. "
 79        "If any one of these limits are hit, the autotune stops."
 80        "Thresholds are, in order, maximum CPU percentage utilization,"
 81        "maximum RAM percentage utilization, maximum GPU compute percent utilization, "
 82        "amd maximum GPU memory utilization."
 83    ),
 84)
 85
 86parser.add_argument(
 87    "--autotune_max_camera_count", type=int, default=4096, help="The maximum amount of cameras allowed in an autotune."
 88)
 89
 90parser.add_argument(
 91    "--autotune_camera_count_interval",
 92    type=int,
 93    default=25,
 94    help=(
 95        "The number of cameras to try to add to the environment if the current camera count"
 96        " falls within permitted system resource utilization limits."
 97    ),
 98)
 99
100"""
101The following arguments are shared for when injecting cameras into a task environment,
102as well as when creating cameras independent of a task environment.
103"""
104
105parser.add_argument(
106    "--num_tiled_cameras",
107    type=int,
108    default=0,
109    required=False,
110    help="Number of tiled cameras to create. For autotuning, this is how many cameras to start with.",
111)
112
113parser.add_argument(
114    "--num_standard_cameras",
115    type=int,
116    default=0,
117    required=False,
118    help="Number of standard cameras to create. For autotuning, this is how many cameras to start with.",
119)
120
121parser.add_argument(
122    "--num_ray_caster_cameras",
123    type=int,
124    default=0,
125    required=False,
126    help="Number of ray caster cameras to create. For autotuning, this is how many cameras to start with.",
127)
128
129parser.add_argument(
130    "--tiled_camera_data_types",
131    nargs="+",
132    type=str,
133    default=["rgb", "depth"],
134    help="The data types rendered by the tiled camera",
135)
136
137parser.add_argument(
138    "--standard_camera_data_types",
139    nargs="+",
140    type=str,
141    default=["rgb", "distance_to_image_plane", "distance_to_camera"],
142    help="The data types rendered by the standard camera",
143)
144
145parser.add_argument(
146    "--ray_caster_camera_data_types",
147    nargs="+",
148    type=str,
149    default=["distance_to_image_plane"],
150    help="The data types rendered by the ray caster camera.",
151)
152
153parser.add_argument(
154    "--ray_caster_visible_mesh_prim_paths",
155    nargs="+",
156    type=str,
157    default=["/World/ground"],
158    help="WARNING: Ray Caster can currently only cast against a single, static, object",
159)
160
161parser.add_argument(
162    "--convert_depth_to_camera_to_image_plane",
163    action="store_true",
164    default=True,
165    help=(
166        "Enable undistorting from perspective view (distance to camera data_type)"
167        "to orthogonal view (distance to plane data_type) for depth."
168        "This is currently needed to create undisorted depth images/point cloud."
169    ),
170)
171
172parser.add_argument(
173    "--keep_raw_depth",
174    dest="convert_depth_to_camera_to_image_plane",
175    action="store_false",
176    help=(
177        "Disable undistorting from perspective view (distance to camera)"
178        "to orthogonal view (distance to plane data_type) for depth."
179    ),
180)
181
182parser.add_argument(
183    "--height",
184    type=int,
185    default=120,
186    required=False,
187    help="Height in pixels of cameras",
188)
189
190parser.add_argument(
191    "--width",
192    type=int,
193    default=140,
194    required=False,
195    help="Width in pixels of cameras",
196)
197
198parser.add_argument(
199    "--warm_start_length",
200    type=int,
201    default=3,
202    required=False,
203    help=(
204        "Number of steps to run the sim before starting benchmark."
205        "Needed to avoid blank images at the start of the simulation."
206    ),
207)
208
209parser.add_argument(
210    "--experiment_length",
211    type=int,
212    default=15,
213    required=False,
214    help="Number of steps to average over",
215)
216
217# This argument is only used when a task is not provided.
218parser.add_argument(
219    "--num_objects",
220    type=int,
221    default=10,
222    required=False,
223    help="Number of objects to spawn into the scene when not using a known task.",
224)
225
226
227AppLauncher.add_app_launcher_args(parser)
228args_cli = parser.parse_args()
229args_cli.enable_cameras = True
230
231if args_cli.autotune:
232    import pynvml
233
234if len(args_cli.ray_caster_visible_mesh_prim_paths) > 1:
235    print("[WARNING]: Ray Casting is only currently supported for a single, static object")
236# launch omniverse app
237app_launcher = AppLauncher(args_cli)
238simulation_app = app_launcher.app
239
240"""Rest everything follows."""
241
242import random
243import time
244
245import gymnasium as gym
246import numpy as np
247import psutil
248import torch
249
250import isaaclab.sim as sim_utils
251from isaaclab.assets import RigidObject, RigidObjectCfg
252from isaaclab.scene.interactive_scene import InteractiveScene
253from isaaclab.sensors import (
254    Camera,
255    CameraCfg,
256    RayCasterCamera,
257    RayCasterCameraCfg,
258    TiledCamera,
259    TiledCameraCfg,
260    patterns,
261)
262from isaaclab.utils.math import orthogonalize_perspective_depth, unproject_depth
263
264from isaaclab_tasks.utils import load_cfg_from_registry
265
266"""
267Camera Creation
268"""
269
270
271def create_camera_base(
272    camera_cfg: type[CameraCfg | TiledCameraCfg],
273    num_cams: int,
274    data_types: list[str],
275    height: int,
276    width: int,
277    prim_path: str | None = None,
278    instantiate: bool = True,
279) -> Camera | TiledCamera | CameraCfg | TiledCameraCfg | None:
280    """Generalized function to create a camera or tiled camera sensor."""
281    # Determine prim prefix based on the camera class
282    name = camera_cfg.class_type.__name__
283
284    if instantiate:
285        # Create the necessary prims
286        for idx in range(num_cams):
287            sim_utils.create_prim(f"/World/{name}_{idx:02d}", "Xform")
288    if prim_path is None:
289        prim_path = f"/World/{name}_.*/{name}"
290    # If valid camera settings are provided, create the camera
291    if num_cams > 0 and len(data_types) > 0 and height > 0 and width > 0:
292        cfg = camera_cfg(
293            prim_path=prim_path,
294            update_period=0,
295            height=height,
296            width=width,
297            data_types=data_types,
298            spawn=sim_utils.PinholeCameraCfg(
299                focal_length=24, focus_distance=400.0, horizontal_aperture=20.955, clipping_range=(0.1, 1e4)
300            ),
301        )
302        if instantiate:
303            return camera_cfg.class_type(cfg=cfg)
304        else:
305            return cfg
306    else:
307        return None
308
309
310def create_tiled_cameras(
311    num_cams: int = 2, data_types: list[str] | None = None, height: int = 100, width: int = 120
312) -> TiledCamera | None:
313    if data_types is None:
314        data_types = ["rgb", "depth"]
315    """Defines the tiled camera sensor to add to the scene."""
316    return create_camera_base(
317        camera_cfg=TiledCameraCfg,
318        num_cams=num_cams,
319        data_types=data_types,
320        height=height,
321        width=width,
322    )
323
324
325def create_cameras(
326    num_cams: int = 2, data_types: list[str] | None = None, height: int = 100, width: int = 120
327) -> Camera | None:
328    """Defines the Standard cameras."""
329    if data_types is None:
330        data_types = ["rgb", "depth"]
331    return create_camera_base(
332        camera_cfg=CameraCfg, num_cams=num_cams, data_types=data_types, height=height, width=width
333    )
334
335
336def create_ray_caster_cameras(
337    num_cams: int = 2,
338    data_types: list[str] = ["distance_to_image_plane"],
339    mesh_prim_paths: list[str] = ["/World/ground"],
340    height: int = 100,
341    width: int = 120,
342    prim_path: str = "/World/RayCasterCamera_.*/RayCaster",
343    instantiate: bool = True,
344) -> RayCasterCamera | RayCasterCameraCfg | None:
345    """Create the raycaster cameras; different configuration than Standard/Tiled camera"""
346    for idx in range(num_cams):
347        sim_utils.create_prim(f"/World/RayCasterCamera_{idx:02d}/RayCaster", "Xform")
348
349    if num_cams > 0 and len(data_types) > 0 and height > 0 and width > 0:
350        cam_cfg = RayCasterCameraCfg(
351            prim_path=prim_path,
352            mesh_prim_paths=mesh_prim_paths,
353            update_period=0,
354            offset=RayCasterCameraCfg.OffsetCfg(pos=(0.0, 0.0, 0.0), rot=(1.0, 0.0, 0.0, 0.0)),
355            data_types=data_types,
356            debug_vis=False,
357            pattern_cfg=patterns.PinholeCameraPatternCfg(
358                focal_length=24.0,
359                horizontal_aperture=20.955,
360                height=480,
361                width=640,
362            ),
363        )
364        if instantiate:
365            return RayCasterCamera(cfg=cam_cfg)
366        else:
367            return cam_cfg
368
369    else:
370        return None
371
372
373def create_tiled_camera_cfg(prim_path: str) -> TiledCameraCfg:
374    """Grab a simple tiled camera config for injecting into task environments."""
375    return create_camera_base(
376        TiledCameraCfg,
377        num_cams=args_cli.num_tiled_cameras,
378        data_types=args_cli.tiled_camera_data_types,
379        width=args_cli.width,
380        height=args_cli.height,
381        prim_path="{ENV_REGEX_NS}/" + prim_path,
382        instantiate=False,
383    )
384
385
386def create_standard_camera_cfg(prim_path: str) -> CameraCfg:
387    """Grab a simple standard camera config for injecting into task environments."""
388    return create_camera_base(
389        CameraCfg,
390        num_cams=args_cli.num_standard_cameras,
391        data_types=args_cli.standard_camera_data_types,
392        width=args_cli.width,
393        height=args_cli.height,
394        prim_path="{ENV_REGEX_NS}/" + prim_path,
395        instantiate=False,
396    )
397
398
399def create_ray_caster_camera_cfg(prim_path: str) -> RayCasterCameraCfg:
400    """Grab a simple ray caster config for injecting into task environments."""
401    return create_ray_caster_cameras(
402        num_cams=args_cli.num_ray_caster_cameras,
403        data_types=args_cli.ray_caster_camera_data_types,
404        width=args_cli.width,
405        height=args_cli.height,
406        prim_path="{ENV_REGEX_NS}/" + prim_path,
407    )
408
409
410"""
411Scene Creation
412"""
413
414
415def design_scene(
416    num_tiled_cams: int = 2,
417    num_standard_cams: int = 0,
418    num_ray_caster_cams: int = 0,
419    tiled_camera_data_types: list[str] | None = None,
420    standard_camera_data_types: list[str] | None = None,
421    ray_caster_camera_data_types: list[str] | None = None,
422    height: int = 100,
423    width: int = 200,
424    num_objects: int = 20,
425    mesh_prim_paths: list[str] = ["/World/ground"],
426) -> dict:
427    """Design the scene."""
428    if tiled_camera_data_types is None:
429        tiled_camera_data_types = ["rgb"]
430    if standard_camera_data_types is None:
431        standard_camera_data_types = ["rgb"]
432    if ray_caster_camera_data_types is None:
433        ray_caster_camera_data_types = ["distance_to_image_plane"]
434
435    # Populate scene
436    # -- Ground-plane
437    cfg = sim_utils.GroundPlaneCfg()
438    cfg.func("/World/ground", cfg)
439    # -- Lights
440    cfg = sim_utils.DistantLightCfg(intensity=3000.0, color=(0.75, 0.75, 0.75))
441    cfg.func("/World/Light", cfg)
442
443    # Create a dictionary for the scene entities
444    scene_entities = {}
445
446    # Xform to hold objects
447    sim_utils.create_prim("/World/Objects", "Xform")
448    # Random objects
449    for i in range(num_objects):
450        # sample random position
451        position = np.random.rand(3) - np.asarray([0.05, 0.05, -1.0])
452        position *= np.asarray([1.5, 1.5, 0.5])
453        # sample random color
454        color = (random.random(), random.random(), random.random())
455        # choose random prim type
456        prim_type = random.choice(["Cube", "Cone", "Cylinder"])
457        common_properties = {
458            "rigid_props": sim_utils.RigidBodyPropertiesCfg(),
459            "mass_props": sim_utils.MassPropertiesCfg(mass=5.0),
460            "collision_props": sim_utils.CollisionPropertiesCfg(),
461            "visual_material": sim_utils.PreviewSurfaceCfg(diffuse_color=color, metallic=0.5),
462            "semantic_tags": [("class", prim_type)],
463        }
464        if prim_type == "Cube":
465            shape_cfg = sim_utils.CuboidCfg(size=(0.25, 0.25, 0.25), **common_properties)
466        elif prim_type == "Cone":
467            shape_cfg = sim_utils.ConeCfg(radius=0.1, height=0.25, **common_properties)
468        elif prim_type == "Cylinder":
469            shape_cfg = sim_utils.CylinderCfg(radius=0.25, height=0.25, **common_properties)
470        # Rigid Object
471        obj_cfg = RigidObjectCfg(
472            prim_path=f"/World/Objects/Obj_{i:02d}",
473            spawn=shape_cfg,
474            init_state=RigidObjectCfg.InitialStateCfg(pos=position),
475        )
476        scene_entities[f"rigid_object{i}"] = RigidObject(cfg=obj_cfg)
477
478    # Sensors
479    standard_camera = create_cameras(
480        num_cams=num_standard_cams, data_types=standard_camera_data_types, height=height, width=width
481    )
482    tiled_camera = create_tiled_cameras(
483        num_cams=num_tiled_cams, data_types=tiled_camera_data_types, height=height, width=width
484    )
485    ray_caster_camera = create_ray_caster_cameras(
486        num_cams=num_ray_caster_cams,
487        data_types=ray_caster_camera_data_types,
488        mesh_prim_paths=mesh_prim_paths,
489        height=height,
490        width=width,
491    )
492    # return the scene information
493    if tiled_camera is not None:
494        scene_entities["tiled_camera"] = tiled_camera
495    if standard_camera is not None:
496        scene_entities["standard_camera"] = standard_camera
497    if ray_caster_camera is not None:
498        scene_entities["ray_caster_camera"] = ray_caster_camera
499    return scene_entities
500
501
502def inject_cameras_into_task(
503    task: str,
504    num_cams: int,
505    camera_name_prefix: str,
506    camera_creation_callable: Callable,
507    num_cameras_per_env: int = 1,
508) -> gym.Env:
509    """Loads the task, sticks cameras into the config, and creates the environment."""
510    cfg = load_cfg_from_registry(task, "env_cfg_entry_point")
511    cfg.sim.device = args_cli.device
512    cfg.sim.use_fabric = args_cli.use_fabric
513    scene_cfg = cfg.scene
514
515    num_envs = int(num_cams / num_cameras_per_env)
516    scene_cfg.num_envs = num_envs
517
518    for idx in range(num_cameras_per_env):
519        suffix = "" if idx == 0 else str(idx)
520        name = camera_name_prefix + suffix
521        setattr(scene_cfg, name, camera_creation_callable(name))
522    cfg.scene = scene_cfg
523    env = gym.make(task, cfg=cfg)
524    return env
525
526
527"""
528System diagnosis
529"""
530
531
532def get_utilization_percentages(reset: bool = False, max_values: list[float] = [0.0, 0.0, 0.0, 0.0]) -> list[float]:
533    """Get the maximum CPU, RAM, GPU utilization (processing), and
534    GPU memory usage percentages since the last time reset was true."""
535    if reset:
536        max_values[:] = [0, 0, 0, 0]  # Reset the max values
537
538    # CPU utilization
539    cpu_usage = psutil.cpu_percent(interval=0.1)
540    max_values[0] = max(max_values[0], cpu_usage)
541
542    # RAM utilization
543    memory_info = psutil.virtual_memory()
544    ram_usage = memory_info.percent
545    max_values[1] = max(max_values[1], ram_usage)
546
547    # GPU utilization using pynvml
548    if torch.cuda.is_available():
549        if args_cli.autotune:
550            pynvml.nvmlInit()  # Initialize NVML
551            for i in range(torch.cuda.device_count()):
552                handle = pynvml.nvmlDeviceGetHandleByIndex(i)
553
554                # GPU Utilization
555                gpu_utilization = pynvml.nvmlDeviceGetUtilizationRates(handle)
556                gpu_processing_utilization_percent = gpu_utilization.gpu  # GPU core utilization
557                max_values[2] = max(max_values[2], gpu_processing_utilization_percent)
558
559                # GPU Memory Usage
560                memory_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
561                gpu_memory_total = memory_info.total
562                gpu_memory_used = memory_info.used
563                gpu_memory_utilization_percent = (gpu_memory_used / gpu_memory_total) * 100
564                max_values[3] = max(max_values[3], gpu_memory_utilization_percent)
565
566            pynvml.nvmlShutdown()  # Shutdown NVML after usage
567    else:
568        gpu_processing_utilization_percent = None
569        gpu_memory_utilization_percent = None
570    return max_values
571
572
573"""
574Experiment
575"""
576
577
578def run_simulator(
579    sim: sim_utils.SimulationContext | None,
580    scene_entities: dict | InteractiveScene,
581    warm_start_length: int = 10,
582    experiment_length: int = 100,
583    tiled_camera_data_types: list[str] | None = None,
584    standard_camera_data_types: list[str] | None = None,
585    ray_caster_camera_data_types: list[str] | None = None,
586    depth_predicate: Callable = lambda x: "to" in x or x == "depth",
587    perspective_depth_predicate: Callable = lambda x: x == "distance_to_camera",
588    convert_depth_to_camera_to_image_plane: bool = True,
589    max_cameras_per_env: int = 1,
590    env: gym.Env | None = None,
591) -> dict:
592    """Run the simulator with all cameras, and return timing analytics. Visualize if desired."""
593
594    if tiled_camera_data_types is None:
595        tiled_camera_data_types = ["rgb"]
596    if standard_camera_data_types is None:
597        standard_camera_data_types = ["rgb"]
598    if ray_caster_camera_data_types is None:
599        ray_caster_camera_data_types = ["distance_to_image_plane"]
600
601    # Initialize camera lists
602    tiled_cameras = []
603    standard_cameras = []
604    ray_caster_cameras = []
605
606    # Dynamically extract cameras from the scene entities up to max_cameras_per_env
607    for i in range(max_cameras_per_env):
608        # Extract tiled cameras
609        tiled_camera_key = f"tiled_camera{i}" if i > 0 else "tiled_camera"
610        standard_camera_key = f"standard_camera{i}" if i > 0 else "standard_camera"
611        ray_caster_camera_key = f"ray_caster_camera{i}" if i > 0 else "ray_caster_camera"
612
613        try:  # if instead you checked ... if key is in scene_entities... # errors out always even if key present
614            tiled_cameras.append(scene_entities[tiled_camera_key])
615            standard_cameras.append(scene_entities[standard_camera_key])
616            ray_caster_cameras.append(scene_entities[ray_caster_camera_key])
617        except KeyError:
618            break
619
620    # Initialize camera counts
621    camera_lists = [tiled_cameras, standard_cameras, ray_caster_cameras]
622    camera_data_types = [tiled_camera_data_types, standard_camera_data_types, ray_caster_camera_data_types]
623    labels = ["tiled", "standard", "ray_caster"]
624
625    if sim is not None:
626        # Set camera world poses
627        for camera_list in camera_lists:
628            for camera in camera_list:
629                num_cameras = camera.data.intrinsic_matrices.size(0)
630                positions = torch.tensor([[2.5, 2.5, 2.5]], device=sim.device).repeat(num_cameras, 1)
631                targets = torch.tensor([[0.0, 0.0, 0.0]], device=sim.device).repeat(num_cameras, 1)
632                camera.set_world_poses_from_view(positions, targets)
633
634    # Initialize timing variables
635    timestep = 0
636    total_time = 0.0
637    valid_timesteps = 0
638    sim_step_time = 0.0
639
640    while simulation_app.is_running() and timestep < experiment_length:
641        print(f"On timestep {timestep} of {experiment_length}, with warm start of {warm_start_length}")
642        get_utilization_percentages()
643
644        # Measure the total simulation step time
645        step_start_time = time.time()
646
647        if sim is not None:
648            sim.step()
649
650        if env is not None:
651            with torch.inference_mode():
652                # compute zero actions
653                actions = torch.zeros(env.action_space.shape, device=env.unwrapped.device)
654                # apply actions
655                env.step(actions)
656
657        # Update cameras and process vision data within the simulation step
658        clouds = {}
659        images = {}
660        depth_images = {}
661
662        # Loop through all camera lists and their data_types
663        for camera_list, data_types, label in zip(camera_lists, camera_data_types, labels):
664            for cam_idx, camera in enumerate(camera_list):
665                if env is None:  # No env, need to step cams manually
666                    # Only update the camera if it hasn't been updated as part of scene_entities.update ...
667                    camera.update(dt=sim.get_physics_dt())
668
669                for data_type in data_types:
670                    data_label = f"{label}_{cam_idx}_{data_type}"
671
672                    if depth_predicate(data_type):  # is a depth image, want to create cloud
673                        depth = camera.data.output[data_type]
674                        depth_images[data_label + "_raw"] = depth
675                        if perspective_depth_predicate(data_type) and convert_depth_to_camera_to_image_plane:
676                            depth = orthogonalize_perspective_depth(
677                                camera.data.output[data_type], camera.data.intrinsic_matrices
678                            )
679                            depth_images[data_label + "_undistorted"] = depth
680
681                        pointcloud = unproject_depth(depth=depth, intrinsics=camera.data.intrinsic_matrices)
682                        clouds[data_label] = pointcloud
683                    else:  # rgb image, just save it
684                        image = camera.data.output[data_type]
685                        images[data_label] = image
686
687        # End timing for the step
688        step_end_time = time.time()
689        sim_step_time += step_end_time - step_start_time
690
691        if timestep > warm_start_length:
692            get_utilization_percentages(reset=True)
693            total_time += step_end_time - step_start_time
694            valid_timesteps += 1
695
696        timestep += 1
697
698    # Calculate average timings
699    if valid_timesteps > 0:
700        avg_timestep_duration = total_time / valid_timesteps
701        avg_sim_step_duration = sim_step_time / experiment_length
702    else:
703        avg_timestep_duration = 0.0
704        avg_sim_step_duration = 0.0
705
706    # Package timing analytics in a dictionary
707    timing_analytics = {
708        "average_timestep_duration": avg_timestep_duration,
709        "average_sim_step_duration": avg_sim_step_duration,
710        "total_simulation_time": sim_step_time,
711        "total_experiment_duration": sim_step_time,
712    }
713
714    system_utilization_analytics = get_utilization_percentages()
715
716    print("--- Benchmark Results ---")
717    print(f"Average timestep duration: {avg_timestep_duration:.6f} seconds")
718    print(f"Average simulation step duration: {avg_sim_step_duration:.6f} seconds")
719    print(f"Total simulation time: {sim_step_time:.6f} seconds")
720    print("\nSystem Utilization Statistics:")
721    print(
722        f"| CPU:{system_utilization_analytics[0]}% | "
723        f"RAM:{system_utilization_analytics[1]}% | "
724        f"GPU Compute:{system_utilization_analytics[2]}% | "
725        f" GPU Memory: {system_utilization_analytics[3]:.2f}% |"
726    )
727
728    return {"timing_analytics": timing_analytics, "system_utilization_analytics": system_utilization_analytics}
729
730
731def main():
732    """Main function."""
733    # Load simulation context
734    if args_cli.num_tiled_cameras + args_cli.num_standard_cameras + args_cli.num_ray_caster_cameras <= 0:
735        raise ValueError("You must select at least one camera.")
736    if (
737        (args_cli.num_tiled_cameras > 0 and args_cli.num_standard_cameras > 0)
738        or (args_cli.num_ray_caster_cameras > 0 and args_cli.num_standard_cameras > 0)
739        or (args_cli.num_ray_caster_cameras > 0 and args_cli.num_tiled_cameras > 0)
740    ):
741        print("[WARNING]: You have elected to use more than one camera type.")
742        print("[WARNING]: For a benchmark to be meaningful, use ONLY ONE camera type at a time.")
743        print(
744            "[WARNING]: For example, if num_tiled_cameras=100, for a meaningful benchmark,"
745            "num_standard_cameras should be 0, and num_ray_caster_cameras should be 0"
746        )
747        raise ValueError("Benchmark one camera at a time.")
748
749    print("[INFO]: Designing the scene")
750    if args_cli.task is None:
751        print("[INFO]: No task environment provided, creating random scene.")
752        sim_cfg = sim_utils.SimulationCfg(device=args_cli.device)
753        sim = sim_utils.SimulationContext(sim_cfg)
754        # Set main camera
755        sim.set_camera_view([2.5, 2.5, 2.5], [0.0, 0.0, 0.0])
756        scene_entities = design_scene(
757            num_tiled_cams=args_cli.num_tiled_cameras,
758            num_standard_cams=args_cli.num_standard_cameras,
759            num_ray_caster_cams=args_cli.num_ray_caster_cameras,
760            tiled_camera_data_types=args_cli.tiled_camera_data_types,
761            standard_camera_data_types=args_cli.standard_camera_data_types,
762            ray_caster_camera_data_types=args_cli.ray_caster_camera_data_types,
763            height=args_cli.height,
764            width=args_cli.width,
765            num_objects=args_cli.num_objects,
766            mesh_prim_paths=args_cli.ray_caster_visible_mesh_prim_paths,
767        )
768        # Play simulator
769        sim.reset()
770        # Now we are ready!
771        print("[INFO]: Setup complete...")
772        # Run simulator
773        run_simulator(
774            sim=sim,
775            scene_entities=scene_entities,
776            warm_start_length=args_cli.warm_start_length,
777            experiment_length=args_cli.experiment_length,
778            tiled_camera_data_types=args_cli.tiled_camera_data_types,
779            standard_camera_data_types=args_cli.standard_camera_data_types,
780            ray_caster_camera_data_types=args_cli.ray_caster_camera_data_types,
781            convert_depth_to_camera_to_image_plane=args_cli.convert_depth_to_camera_to_image_plane,
782        )
783    else:
784        print("[INFO]: Using known task environment, injecting cameras.")
785        autotune_iter = 0
786        max_sys_util_thresh = [0.0, 0.0, 0.0]
787        max_num_cams = max(args_cli.num_tiled_cameras, args_cli.num_standard_cameras, args_cli.num_ray_caster_cameras)
788        cur_num_cams = max_num_cams
789        cur_sys_util = max_sys_util_thresh
790        interval = args_cli.autotune_camera_count_interval
791
792        if args_cli.autotune:
793            max_sys_util_thresh = args_cli.autotune_max_percentage_util
794            max_num_cams = args_cli.autotune_max_camera_count
795            print("[INFO]: Auto tuning until any of the following threshold are met")
796            print(f"|CPU: {max_sys_util_thresh[0]}% | RAM {max_sys_util_thresh[1]}% | GPU: {max_sys_util_thresh[2]}% |")
797            print(f"[INFO]: Maximum number of cameras allowed: {max_num_cams}")
798        # Determine which camera is being tested...
799        tiled_camera_cfg = create_tiled_camera_cfg("tiled_camera")
800        standard_camera_cfg = create_standard_camera_cfg("standard_camera")
801        ray_caster_camera_cfg = create_ray_caster_camera_cfg("ray_caster_camera")
802        camera_name_prefix = ""
803        camera_creation_callable = None
804        num_cams = 0
805        if tiled_camera_cfg is not None:
806            camera_name_prefix = "tiled_camera"
807            camera_creation_callable = create_tiled_camera_cfg
808            num_cams = args_cli.num_tiled_cameras
809        elif standard_camera_cfg is not None:
810            camera_name_prefix = "standard_camera"
811            camera_creation_callable = create_standard_camera_cfg
812            num_cams = args_cli.num_standard_cameras
813        elif ray_caster_camera_cfg is not None:
814            camera_name_prefix = "ray_caster_camera"
815            camera_creation_callable = create_ray_caster_camera_cfg
816            num_cams = args_cli.num_ray_caster_cameras
817
818        while (
819            all(cur <= max_thresh for cur, max_thresh in zip(cur_sys_util, max_sys_util_thresh))
820            and cur_num_cams <= max_num_cams
821        ):
822            cur_num_cams = num_cams + interval * autotune_iter
823            autotune_iter += 1
824
825            env = inject_cameras_into_task(
826                task=args_cli.task,
827                num_cams=cur_num_cams,
828                camera_name_prefix=camera_name_prefix,
829                camera_creation_callable=camera_creation_callable,
830                num_cameras_per_env=args_cli.task_num_cameras_per_env,
831            )
832            env.reset()
833            print(f"Testing with {cur_num_cams} {camera_name_prefix}")
834            analysis = run_simulator(
835                sim=None,
836                scene_entities=env.unwrapped.scene,
837                warm_start_length=args_cli.warm_start_length,
838                experiment_length=args_cli.experiment_length,
839                tiled_camera_data_types=args_cli.tiled_camera_data_types,
840                standard_camera_data_types=args_cli.standard_camera_data_types,
841                ray_caster_camera_data_types=args_cli.ray_caster_camera_data_types,
842                convert_depth_to_camera_to_image_plane=args_cli.convert_depth_to_camera_to_image_plane,
843                max_cameras_per_env=args_cli.task_num_cameras_per_env,
844                env=env,
845            )
846
847            cur_sys_util = analysis["system_utilization_analytics"]
848            print("Triggering reset...")
849            env.close()
850            sim_utils.create_new_stage()
851        print("[INFO]: DONE! Feel free to CTRL + C Me ")
852        print(f"[INFO]: If you've made it this far, you can likely simulate {cur_num_cams} {camera_name_prefix}")
853        print("Keep in mind, this is without any training running on the GPU.")
854        print("Set lower utilization thresholds to account for training.")
855
856        if not args_cli.autotune:
857            print("[WARNING]: GPU Util Statistics only correct while autotuning, ignore above.")
858
859
860if __name__ == "__main__":
861    # run the main function
862    main()
863    # close sim app
864    simulation_app.close()

可能的参数#

首先,运行

./isaaclab.sh -p scripts/benchmarks/benchmark_cameras.py -h

要查看您可以使用此实用程序变化的所有可能参数。

请参阅与 autotune 相关的命令行参数,了解有关自动确定最大相机数量的更多信息。

比较任务环境中的性能并自动确定任务最大相机数量#

目前,分块相机是能够处理多个动态对象并且具有最佳性能的相机。

例如,要查看您的系统如何在cartpole环境中处理100个分块相机,每个环境中有2个相机(总共50个环境),只在RGB模式下运行。

./isaaclab.sh -p scripts/benchmarks/benchmark_cameras.py \
--task Isaac-Cartpole-v0 --num_tiled_cameras 100 \
--task_num_cameras_per_env 2 \
--tiled_camera_data_types rgb

如果您已安装pynvml, (./isaaclab.sh -p -m pip install pynvml), 您还可以找到在指定环境中运行的相机的最大数量,直到达到某个性能阈值(由最大CPU利用率百分比、最大RAM利用率百分比、最大GPU计算百分比和最大GPU内存百分比指定)。例如,要找出您可以用cartpole运行的相机的最大数量,您可以运行:

./isaaclab.sh -p scripts/benchmarks/benchmark_cameras.py \
--task Isaac-Cartpole-v0 --num_tiled_cameras 100 \
--task_num_cameras_per_env 2 \
--tiled_camera_data_types rgb --autotune \
--autotune_max_percentage_util 100 80 50 50

自动调谐可能会导致程序崩溃,这意味着它试图一次运行太多相机。然而,最大百分比利用参数旨在阻止这种情况发生。

基准测试的输出不包括训练网络的开销,因此考虑减少最大利用率百分比以考虑这种开销。最终输出的相机数量是针对所有相机的,因此要获取总环境数量,将输出的相机数量除以每个环境的相机数量。

比较相机类型和性能(未指定任务情形下)#

这个工具还可以在没有任务环境的情况下评估性能。例如,要查看通过两个标准相机查看100个随机物体,可以运行

./isaaclab.sh -p scripts/benchmarks/benchmark_cameras.py \
--height 100 --width 100 --num_standard_cameras 2 \
--standard_camera_data_types instance_segmentation_fast normals --num_objects 100 \
--experiment_length 100

如果由于性能原因而无法处理此项,则该进程将被终止。建议在运行此脚本时监视 CPU/RAM 利用率和 GPU 利用率,以了解渲染所需的摄像机需要多少资源。在 Ubuntu 中,您可以使用 htopnvtop 等工具在运行此脚本时实时监视资源,而在 Windows 中,您可以使用任务管理器。

如果您的系统无法处理所需的相机,您可以尝试以下操作

  • 切换到无头模式(提供 --headless

  • 确保您使用的是GPU pipeline,而不是CPU!

  • 如果您没有使用分块相机,请切换到分块相机。

  • 减少相机分辨率

  • 减少每个相机的数据类型数量。

  • 减少相机数量

  • 减少场景中的物体数量

如果您的系统能够处理相机的数量,那么时间统计将被打印到终端。在仿真停止后,可以使用 CTRL+C 关闭它。