tbp.monty.frameworks.environments#
tbp.monty.frameworks.environments.environment#
- class Environment(*args, **kwargs)[source]#
Bases:
ProtocolBase 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:
- 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:
- 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:
ProtocolProtocol 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:
- Returns:
ObjectInfo structure for the added object.
- class ObjectInfo(object_id: ObjectID, semantic_id: SemanticID | None) None[source]#
Bases:
objectContains the identifying information of an object created in the environment.
- __init__(object_id: ObjectID, semantic_id: SemanticID | None) None#
- semantic_id: SemanticID | None#
- class ResettableEnvironment(*args, **kwargs)[source]#
Bases:
ProtocolProtocol 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:
- Returns:
The environment’s initial observations and proprioceptive state.
- 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 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
tbp.monty.frameworks.environments.positioning_procedures#
- exception ObjectNotVisibleError[source]#
Bases:
ValueErrorPositioning 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:
PositioningProcedurePositioning 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:
- 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:
- Returns:
The x,y,z coordinates of the target with respect to the sensor.
- 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:
- 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:
- 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:
- 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:
PositioningProcedureFactoryFactory 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:
- Returns:
A positioning procedure.
- class PositioningProcedure(*args, **kwargs)[source]#
Bases:
ProtocolPositioning 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.
- Return type:
- Returns:
The depth of the central pixel for the sensor.
- __init__(*args, **kwargs)#
- class PositioningProcedureFactory(*args, **kwargs)[source]#
Bases:
ProtocolFactory 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:
- Returns:
A positioning procedure.
- class PositioningProcedureResult(actions: list[Action] = <factory>, success: bool = False, terminated: bool = False, truncated: bool = False) None[source]#
Bases:
objectResult of a positioning procedure.
For more on the terminated/truncated terminology, see https://farama.org/Gymnasium-Terminated-Truncated-Step-API.
tbp.monty.frameworks.environments.two_d_data#
- class OmniglotEnvironment(patch_size: int = 10, data_path: str | Path | None = None)[source]#
Bases:
SimulatedEnvironmentEnvironment for Omniglot dataset.
- __init__(patch_size: int = 10, data_path: str | Path | None = None)[source]#
Initialize environment.
- close() None[source]#
Close the environment and release all resources.
Any call to any other environment method may raise an exception.
- Return type:
- reset() tuple[Observations, ProprioceptiveState][source]#
Reset the environment to its initial state.
- Return type:
- 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:
- Return type:
- Returns:
The observations and proprioceptive state.
- class SaccadeOnImageEnvironment(patch_size: int = 64, data_path: str | Path | None = None, hfov: float = 54.201, depth_clip_value: float = 1.1)[source]#
Bases:
SimulatedEnvironmentEnvironment 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:
patch_size (
int) – height and width of patch in pixels, defaults to 64data_path (
str|Path|None) – path to the image dataset. If None, defaults to ~/tbp/data/worldimages/labeled_scenes/hfov (
float) – horizontal field of view (degrees) of the camera that captured the images, used to unproject depth into 3D. Defaults to 54.201, the iPad front camera. Source for the default value: https://developer.apple.com/library/archive/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Cameras/Cameras.htmldepth_clip_value (
float) – depth (meters) beyond which points are treated as background when unprojecting. Defaults to 1.1, tuned for the iPad-range Monty Meets World captures.
- close() None[source]#
Close the environment and release all resources.
Any call to any other environment method may raise an exception.
- Return type:
- 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:
- Returns:
The observation from the image patch.
- step(actions: Sequence[Action]) tuple[Observations, ProprioceptiveState][source]#
Retrieve the next observation.
- class SaccadeOnImageFromStreamEnvironment(patch_size: int = 64, data_path: str | Path | None = None, hfov: float = 54.201, depth_clip_value: float = 1.1)[source]#
Bases:
SaccadeOnImageEnvironmentEnvironment 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 64data_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.