| """ |
| A simple example showing how to construct an ObservationEncoder for processing multiple input modalities. |
| This is purely for instructional purposes, in case others would like to make use of or extend the |
| functionality. |
| """ |
|
|
| from collections import OrderedDict |
|
|
| import torch |
| from robomimic.models.base_nets import MLP |
| from robomimic.models.obs_nets import ObservationEncoder, ObservationDecoder |
| from robomimic.models.obs_core import CropRandomizer |
| import robomimic.utils.tensor_utils as TensorUtils |
| import robomimic.utils.obs_utils as ObsUtils |
|
|
|
|
| def simple_obs_example(): |
| obs_encoder = ObservationEncoder(feature_activation=torch.nn.ReLU) |
|
|
| |
|
|
| |
|
|
| |
| camera1_shape = [3, 224, 224] |
|
|
| |
| net_class = "VisualCore" |
|
|
| |
| net_kwargs = { |
| "input_shape": camera1_shape, |
| "backbone_class": "ResNet18Conv", |
| "backbone_kwargs": {"pretrained": False, "input_coord_conv": False}, |
| "pool_class": "SpatialSoftmax", |
| "pool_kwargs": {"num_kp": 32} |
| } |
|
|
| |
| obs_encoder.register_obs_key( |
| name="camera1", |
| shape=camera1_shape, |
| net_class=net_class, |
| net_kwargs=net_kwargs, |
| ) |
|
|
| |
|
|
| |
| camera2_shape = [3, 160, 240] |
|
|
| |
| image_randomizer = CropRandomizer(input_shape=camera2_shape, crop_height=140, crop_width=220) |
|
|
| |
| net_kwargs["input_shape"] = image_randomizer.output_shape_in(camera2_shape) |
| net = ObsUtils.OBS_ENCODER_CORES[net_class](**net_kwargs) |
|
|
| obs_encoder.register_obs_key( |
| name="camera2", |
| shape=camera2_shape, |
| net=net, |
| randomizers=image_randomizer, |
| ) |
|
|
| |
| camera3_shape = [3, 224, 224] |
| obs_encoder.register_obs_key( |
| name="camera3", |
| shape=camera3_shape, |
| share_net_from="camera1", |
| ) |
|
|
| |
| proprio_shape = [12] |
| net = MLP(input_dim=12, output_dim=32, layer_dims=(128,), output_activation=None) |
| obs_encoder.register_obs_key( |
| name="proprio", |
| shape=proprio_shape, |
| net=net, |
| ) |
|
|
| |
| |
| obs_modality_mapping = { |
| "low_dim": ["proprio"], |
| "rgb": ["camera1", "camera2", "camera3"], |
| } |
| ObsUtils.initialize_obs_modality_mapping_from_dict(modality_mapping=obs_modality_mapping) |
|
|
| |
| obs_encoder.make() |
|
|
| |
| print(obs_encoder) |
|
|
| |
| inputs = { |
| "camera1": torch.randn(camera1_shape), |
| "camera2": torch.randn(camera2_shape), |
| "camera3": torch.randn(camera3_shape), |
| "proprio": torch.randn(proprio_shape) |
| } |
|
|
| |
| inputs = TensorUtils.to_batch(inputs) |
|
|
| |
| if torch.cuda.is_available(): |
| inputs = TensorUtils.to_device(inputs, torch.device("cuda:0")) |
| obs_encoder.cuda() |
|
|
| |
| |
| obs_feature = obs_encoder(inputs) |
|
|
| print(obs_feature.shape) |
|
|
| |
| obs_decoder = ObservationDecoder( |
| input_feat_dim=obs_encoder.output_shape()[0], |
| decode_shapes=OrderedDict({"action": (7,)}) |
| ) |
|
|
| |
| if torch.cuda.is_available(): |
| obs_decoder.cuda() |
|
|
| print(obs_decoder(obs_feature)) |
|
|
|
|
| if __name__ == "__main__": |
| simple_obs_example() |
|
|