tbp.monty.frameworks.environments#

tbp.monty.frameworks.environments.environment#

class Environment(*args, **kwargs)[source]#

Bases: Protocol

Base protocol for all environments that support steppable actions.

__init__(*args, **kwargs)#
close() → None[source]#

Close the environment and release all resources.

Any call to any other environment method may raise an exception.

Return type:

None

step(actions: Sequence[Action]) → tuple[Observations, ProprioceptiveState][source]#

Apply the given actions to the environment.

Parameters:

actions (Sequence[Action]) – The actions to apply to the environment.

Return type:

tuple[Observations, ProprioceptiveState]

Returns:

The current observations and proprioceptive state.

Note

If the actions are an empty sequence, the current observations are returned.

class ObjectEnvironment(*args, **kwargs)[source]#

Bases: Protocol

Protocol for environments that support adding and removing objects.

__init__(*args, **kwargs)#
add_object(name: str, position: tuple[float, float, float] = (0.0, 0.0, 0.0), rotation: tuple[float, float, float, float] = (1.0, 0.0, 0.0, 0.0), scale: tuple[float, float, float] = (1.0, 1.0, 1.0), semantic_id: SemanticID | None = None, primary_target_object: ObjectID | None = None) → ObjectInfo[source]#

Add an object to the environment.

Parameters:
  • name (str) – The name of the object to add.

  • position (tuple[float, float, float]) – The initial absolute position of the object.

  • rotation (tuple[float, float, float, float]) – The initial rotation WXYZ quaternion of the object. Defaults to (1,0,0,0).

  • scale (tuple[float, float, float]) – The scale of the object to add. Defaults to (1,1,1).

  • semantic_id (SemanticID (int) | None) – Optional override for the object semantic ID. Defaults to None.

  • primary_target_object (ObjectID (int) | None) – The ID of the primary target object. If not None, the added object will be positioned so that it does not obscure the initial view of the primary target object (which avoiding collision alone cannot guarantee). Used when adding multiple objects. Defaults to None.

Return type:

ObjectInfo

Returns:

ObjectInfo structure for the added object.

remove_all_objects() → None[source]#

Remove all objects from the environment.

Return type:

None

TODO: This remove_all_objects interface is elevated from

HabitatSim.remove_all_objects and is quite specific to HabitatSim implementation. We should consider refactoring this to be more generic.

class ObjectID#

Unique identifier for an object in the environment.

alias of int

class ObjectInfo(object_id: ObjectID, semantic_id: SemanticID | None) → None[source]#

Bases: object

Contains the identifying information of an object created in the environment.

__init__(object_id: ObjectID, semantic_id: SemanticID | None) → None#
object_id: ObjectID#
semantic_id: SemanticID | None#
class ResettableEnvironment(*args, **kwargs)[source]#

Bases: Protocol

Protocol for environments that can be reset to their initial state.

__init__(*args, **kwargs)#
reset() → tuple[Observations, ProprioceptiveState][source]#

Reset the environment to its initial state.

Return type:

tuple[Observations, ProprioceptiveState]

Returns:

The environment’s initial observations and proprioceptive state.

class SemanticID#

Unique identifier for an object’s semantic class.

alias of int

class SimulatedEnvironment(*args, **kwargs)[source]#

Bases: Environment, ResettableEnvironment, Protocol

class SimulatedObjectEnvironment(*args, **kwargs)[source]#

Bases: Environment, ObjectEnvironment, ResettableEnvironment, Protocol

tbp.monty.frameworks.environments.object_init_samplers#

class Default[source]#

Bases: object

class MultiObjectNames[source]#

Bases: TypedDict

num_distractors: int#
source_object_list: Sequence[str]#
targets_list: Sequence[str]#
class ObjectInitParams[source]#

Bases: TypedDict

euler_rotation: npt.NDArray[np.float64] | EulerAnglesXYZ#
position: VectorXYZ#
quat_rotation: NotRequired[npt.NDArray[np.float64]]#
rotation: QuaternionWXYZ#
scale: VectorXYZ#
class Predefined(positions: Sequence[tuple[float, float, float]] | None = None, rotations: Sequence[tuple[float, float, float]] | None = None, scales: Sequence[tuple[float, float, float]] | None = None, change_every_episode: bool | None = None)[source]#

Bases: Default

__init__(positions: Sequence[tuple[float, float, float]] | None = None, rotations: Sequence[tuple[float, float, float]] | None = None, scales: Sequence[tuple[float, float, float]] | None = None, change_every_episode: bool | None = None)[source]#
all_combinations_of_params()[source]#
class RandomRotation(position: tuple[float, float, float] | None = None, scale: tuple[float, float, float] | None = None)[source]#

Bases: Default

__init__(position: tuple[float, float, float] | None = None, scale: tuple[float, float, float] | None = None)[source]#

tbp.monty.frameworks.environments.positioning_procedures#

exception ObjectNotVisibleError[source]#

Bases: ValueError

Positioning found no visible target pixels in its sensor’s view.

class GetGoodView(agent_id: AgentID, good_view_distance: float, good_view_percentage: float, multiple_objects_present: bool, sensor_id: SensorID, target_semantic_id: SemanticID, allow_translation: bool = True, max_orientation_attempts: int = 1) → None[source]#

Bases: PositioningProcedure

Positioning procedure to get a good view of the object before an episode.

Used to position the distant agent so that it finds the initial view of an object at the beginning of an episode with respect to a given sensor (the surface agent is positioned using the TouchObject positioning procedure instead). Also currently used by the distant agent after a “jump” has been initialized by a model-based policy.

First, the agent is moved towards the target object until the object fills a minimum of percentage (given by good_view_percentage) of the sensor’s field of view or the closest point of the object is less than good_view_distance from the sensor. This makes sure that big and small objects all fill similar amount of space in the sensor’s field of view. Otherwise small objects may be too small to perform saccades or the sensor ends up inside of big objects. This step is performed by default but can be skipped by setting allow_translation=False.

Second, the agent will then be oriented towards the object so that the sensor’s central pixel is on-object. In the case of multi-object experiments, (i.e., when multiple_objects_present=True), there is an additional orientation step performed prior to the translational movement step.

__init__(agent_id: AgentID, good_view_distance: float, good_view_percentage: float, multiple_objects_present: bool, sensor_id: SensorID, target_semantic_id: SemanticID, allow_translation: bool = True, max_orientation_attempts: int = 1) → None[source]#

Initialize the GetGoodView positioning procedure.

Parameters:
  • agent_id (AgentID (str)) – The ID of the agent to generate actions for.

  • good_view_distance (float) – The desired distance to the object for a good view.

  • good_view_percentage (float) – The percentage of the sensor that should be filled with the object.

  • multiple_objects_present (bool) – Whether there are multiple objects in the scene.

  • sensor_id (SensorID (str)) – The ID of the sensor to use for positioning.

  • target_semantic_id (SemanticID (int)) – The semantic ID of the target object.

  • allow_translation (bool) – Whether to allow movement toward the object via the motor systems’s move_close_enough method. If False, only orientienting movements are performed. Defaults to True.

  • max_orientation_attempts (int) – The maximum number of orientation attempts allowed before giving up and truncating the procedure indicating that the sensor is not on the target object.

compute_look_amounts(relative_location: numpy.ndarray, state: MotorSystemState) → tuple[float, float][source]#

Compute the amount to look down and left given a relative location.

This function computes the amount needed to look down and left in order for the sensor to be aimed at the target. The returned amounts are relative to the agent’s current position and rotation. Looking up and right is done by returning negative amounts.

TODO: Test whether this function works when the agent is facing in the positive z-direction. It may be fine, but there were some adjustments to accommodate the z-axis positive direction pointing opposite the body’s initial orientation (e.g., using negative z in left_amount = -np.degrees(np.arctan2(x_rot, -z_rot))).

Parameters:
  • relative_location (ndarray) – the x,y,z coordinates of the target with respect to the sensor.

  • state (MotorSystemState) – The current state of the motor system. Defaults to None.

Returns:

Amount to look down (degrees). left_amount: Amount to look left (degrees).

Return type:

tuple[float, float]

find_location_to_look_at(sem3d_obs: numpy.ndarray, image_shape: tuple[int, int], state: MotorSystemState) → numpy.ndarray[source]#

Find the location to look at in the observation.

Takes in a semantic 3D observation and returns an x,y,z location.

The location is on the object and surrounded by pixels that are also on the object. This is done by smoothing the on_object image and then taking the maximum of this smoothed image.

Parameters:
  • sem3d_obs (ndarray) – The location of each pixel and the semantic ID associated with that location.

  • image_shape (tuple[int, int]) – The shape of the camera image.

  • state (MotorSystemState) – The current state of the motor system.

Return type:

ndarray

Returns:

The x,y,z coordinates of the target with respect to the sensor.

is_on_target_object(observation: Mapping) → bool[source]#

Check if a sensor is on the target object.

Parameters:

observation (Mapping) – The observation to use for positioning.

Return type:

bool

Returns:

Whether the sensor is on the target object.

move_close_enough(observation: Mapping) → Action | None[source]#

Move closer to the object until we are close enough.

Parameters:

observation (Mapping) – The observation to use for positioning.

Return type:

Action | None

Returns:

The next action to take, or None if we are already close enough to the object.

Raises:

ObjectNotVisibleError – If the object is not visible.

orient_to_object(observation: Mapping, state: MotorSystemState) → list[Action][source]#

Rotate sensors so that they are centered on the object using the view finder.

The view finder needs to be in the same position as the sensor patch and the object needs to be somewhere in the view finders view.

Parameters:
  • observation (Mapping) – The observation to use for positioning.

  • state (MotorSystemState) – The current state of the motor system.

Return type:

list[Action]

Returns:

A list of actions of length two composed of actions needed to get us onto the target object.

sensor_rotation_relative_to_world(state: MotorSystemState) → Any[source]#

Derives the positioning sensor’s rotation relative to the world.

Parameters:

state (MotorSystemState) – The current state of the motor system.

Return type:

Any

Returns:

The positioning sensor’s rotation relative to the world.

class GetGoodViewFactory(agent_id: AgentID, sensor_id: SensorID, allow_translation: bool = True, good_view_distance: float = 0.03, good_view_percentage: float = 0.5, max_orientation_attempts: int = 1, multiple_objects_present: bool = False)[source]#

Bases: PositioningProcedureFactory

Factory for creating GetGoodView positioning procedures.

__init__(agent_id: AgentID, sensor_id: SensorID, allow_translation: bool = True, good_view_distance: float = 0.03, good_view_percentage: float = 0.5, max_orientation_attempts: int = 1, multiple_objects_present: bool = False)[source]#
create(target_semantic_id: SemanticID) → GetGoodView[source]#

Create a positioning procedure.

Parameters:

target_semantic_id (SemanticID (int)) – The semantic ID of the target object.

Return type:

GetGoodView

Returns:

A positioning procedure.

class PositioningProcedure(*args, **kwargs)[source]#

Bases: Protocol

Positioning procedure to position the agent in the scene.

The positioning procedure should be called repeatedly until the procedure result indicates that the procedure has terminated or truncated.

static depth_at_center(agent_id: AgentID, observations: Observations, sensor_id: SensorID) → float[source]#

Determine the depth of the central pixel for the sensor.

Parameters:
  • agent_id (AgentID (str)) – The ID of the agent to use.

  • observations (Observations) – The observations to use.

  • sensor_id (SensorID (str)) – The ID of the sensor to use.

Return type:

float

Returns:

The depth of the central pixel for the sensor.

__init__(*args, **kwargs)#
class PositioningProcedureFactory(*args, **kwargs)[source]#

Bases: Protocol

Factory for creating positioning procedures.

__init__(*args, **kwargs)#
create(target_semantic_id: SemanticID) → PositioningProcedure[source]#

Create a positioning procedure.

Parameters:

target_semantic_id (SemanticID (int)) – The semantic ID of the target object.

Return type:

PositioningProcedure

Returns:

A positioning procedure.

class PositioningProcedureResult(actions: list[Action] = <factory>, success: bool = False, terminated: bool = False, truncated: bool = False) → None[source]#

Bases: object

Result of a positioning procedure.

For more on the terminated/truncated terminology, see https://farama.org/Gymnasium-Terminated-Truncated-Step-API.

__init__(actions: list[Action] = <factory>, success: bool = False, terminated: bool = False, truncated: bool = False) → None#
actions: list[Action]#

Actions to take.

success: bool = False#

Whether the procedure succeeded in its positioning goal.

terminated: bool = False#

Whether the procedure reached a terminal state with success or failure.

truncated: bool = False#

Whether the procedure was truncated due to a limit on the number of attempts or other criteria.

tbp.monty.frameworks.environments.two_d_data#

class OmniglotEnvironment(patch_size: int = 10, data_path: str | Path | None = None)[source]#

Bases: SimulatedEnvironment

Environment for Omniglot dataset.

__init__(patch_size: int = 10, data_path: str | Path | None = None)[source]#

Initialize environment.

Parameters:
  • patch_size (int) – height and width of patch in pixels, defaults to 10

  • data_path (str | Path | None) – path to the omniglot dataset. If None, defaults to ~/tbp/data/omniglot/python/

close() → None[source]#

Close the environment and release all resources.

Any call to any other environment method may raise an exception.

Return type:

None

get_image_patch(img: NDArray[bool], loc: NDArray[float64], patch_size: int)[source]#
load_new_character_data()[source]#
motor_to_locations(motor: Sequence[NDArray[float64]])[source]#
reset() → tuple[Observations, ProprioceptiveState][source]#

Reset the environment to its initial state.

Return type:

tuple[Observations, ProprioceptiveState]

Returns:

The environment’s initial observations and proprioceptive state.

step(actions: Sequence[Action]) → tuple[Observations, ProprioceptiveState][source]#

Retrieve the next observation.

Since the omniglot dataset includes stroke information (the order in which the character was drawn as a list of x,y coordinates) we use that for movement. This means we start at the first x,y coordinate saved in the move path and then move in increments specified by amount through this list. Overall there are usually several hundred points (~200-400) but it varies between characters and versions. If we reach the end of a move path and the episode is not finished, we start from the beginning again. If len(move_path) % amount != 0, we will sample different points on the second pass.

Parameters:
  • actions (Sequence[Action]) – Not used at the moment since we just follow the draw path. However,

  • at (we do use the rotation_degrees to determine the amount of pixels to move)

  • step. (each)

Return type:

tuple[Observations, ProprioceptiveState]

Returns:

The observations and proprioceptive state.

switch_to_object(alphabet_id: int, character_id: int, version_id: int)[source]#
class SaccadeOnImageEnvironment(patch_size: int = 64, data_path: str | Path | None = None, hfov: float = 54.201, depth_clip_value: float = 1.1)[source]#

Bases: SimulatedEnvironment

Environment for moving over a 2D image with depth channel.

Images should be stored in .png format for rgb and .data format for depth.

__init__(patch_size: int = 64, data_path: str | Path | None = None, hfov: float = 54.201, depth_clip_value: float = 1.1)[source]#

Initialize environment.

Parameters:
close() → None[source]#

Close the environment and release all resources.

Any call to any other environment method may raise an exception.

Return type:

None

get_3d_coordinates_from_pixel_indices(pixel_idx: NDArray[int64])[source]#

Retrieve 3D coordinates of a pixel.

Returns:

The 3D coordinates of the pixel.

get_3d_scene_point_cloud()[source]#

Turn 2D depth image into 3D pointcloud using DepthTo3DLocations.

This point cloud is used to estimate the sensor displacement in 3D space between two subsequent steps. Without this we get displacements in pixel space which does not work with our 3D models.

Returns:

The 3D scene point cloud. current_sf_scene_point_cloud: The 3D scene point cloud in sensor frame.

Return type:

current_scene_point_cloud

get_image_patch(loc: NDArray[int64])[source]#

Extract 2D image patch from a location in pixel space.

Returns:

The depth patch. rgb_patch: The rgb patch. depth3d_patch: The depth3d patch. sensor_frame_patch: The sensor frame patch.

Return type:

depth_patch

get_move_area()[source]#

Calculate area in which patch can move on the image.

Returns:

The move area.

get_next_loc(action_name: str, amount: int)[source]#

Calculate next location in pixel space given the current action.

Returns:

The next location in pixel space.

load_depth_data(depth_path: Path, height: int, width: int)[source]#

Load depth image from .data file.

Returns:

The depth image.

load_new_scene_data()[source]#

Load depth and rgb data for next scene environment.

Returns:

The depth image. current_rgb_image: The RGB image. start_location: The start location.

Return type:

current_depth_image

load_rgb_data(rgb_path: Path)[source]#

Load RGB image and put into np array.

Returns:

The RGB image.

process_depth_data(depth: NDArray[float32])[source]#

Process depth data by reshaping, clipping and flipping.

Returns:

The processed depth image.

reset() → tuple[Observations, ProprioceptiveState][source]#

Reset environment and extract image patch.

TODO: clean up. Do we need this? No reset is required in this environment

interface, so this should be indicated more clearly.

Return type:

tuple[Observations, ProprioceptiveState]

Returns:

The observation from the image patch.

step(actions: Sequence[Action]) → tuple[Observations, ProprioceptiveState][source]#

Retrieve the next observation.

Parameters:

actions (Sequence[Action]) – moving up, down, left or right from current location.

Return type:

tuple[Observations, ProprioceptiveState]

Returns:

The observation and proprioceptive state.

switch_to_object(scene_id: int, scene_version_id: int)[source]#

Load new image to be used as environment.

class SaccadeOnImageFromStreamEnvironment(patch_size: int = 64, data_path: str | Path | None = None, hfov: float = 54.201, depth_clip_value: float = 1.1)[source]#

Bases: SaccadeOnImageEnvironment

Environment for moving over a 2D streamed image with depth channel.

__init__(patch_size: int = 64, data_path: str | Path | None = None, hfov: float = 54.201, depth_clip_value: float = 1.1)[source]#

Initialize environment.

Parameters:
  • patch_size (int) – height and width of patch in pixels, defaults to 64

  • data_path (str | Path | None) – path to the image dataset. If None, defaults to ~/tbp/data/worldimages/world_data_stream/

  • hfov (float) – horizontal field of view (degrees) of the capturing camera, used to unproject depth into 3D. Defaults to 54.201 (iPad front camera).

  • depth_clip_value (float) – depth (meters) beyond which points are treated as background when unprojecting. Defaults to 1.1.

load_new_scene_data()[source]#

Load depth and rgb data for next scene environment.

Returns:

The depth image. current_rgb_image: The RGB image. start_location: The start location.

Return type:

current_depth_image

switch_to_scene(scene_id: int)[source]#