Skip to content

Commit 9273f9c

Browse files
authored
Merge pull request #199 from ARISE-Initiative/obs_register_fix
Obs register fix
2 parents 29d6ca2 + 07dc68a commit 9273f9c

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

robomimic/models/base_nets.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from torchvision import models as vision_models
1717

1818
import robomimic.utils.tensor_utils as TensorUtils
19+
import robomimic.utils.obs_utils as ObsUtils
1920

2021

2122
CONV_ACTIVATIONS = {
@@ -463,6 +464,20 @@ class ConvBase(Module):
463464
def __init__(self):
464465
super(ConvBase, self).__init__()
465466

467+
def __init_subclass__(cls, **kwargs):
468+
"""
469+
Hook method to automatically register all valid subclasses so we can keep track of valid observation encoders
470+
in a global dict.
471+
472+
This global dict stores mapping from observation encoder network name to class.
473+
We keep track of these registries to enable automated class inference at runtime, allowing
474+
users to simply extend our base encoder class and refer to that class in string form
475+
in their config, without having to manually register their class internally.
476+
This also future-proofs us for any additional encoder classes we would
477+
like to add ourselves.
478+
"""
479+
ObsUtils.register_encoder_backbone(cls)
480+
466481
# dirty hack - re-implement to pass the buck onto subclasses from ABC parent
467482
def output_shape(self, input_shape):
468483
"""

robomimic/models/obs_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def __init__(
9999

100100
# extract only relevant kwargs for this specific backbone
101101
backbone_kwargs = extract_class_init_kwargs_from_dict(
102-
cls = ObsUtils.OBS_ENCODER_CORES[backbone_class],
102+
cls = ObsUtils.OBS_ENCODER_BACKBONES[backbone_class],
103103
dic=backbone_kwargs, copy=True)
104104

105105
# visual backbone

robomimic/utils/obs_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
# in their config, without having to manually register their class internally.
4242
# This also future-proofs us for any additional encoder / randomizer classes we would
4343
# like to add ourselves.
44-
OBS_ENCODER_CORES = {"None": None} # Include default None
45-
OBS_RANDOMIZERS = {"None": None} # Include default None
44+
OBS_ENCODER_CORES = {"None": None} # Per-modality core net as defined in obs_cores.py, e.g., "VisualCore"
45+
OBS_RANDOMIZERS = {"None": None} # Obs randomizer defined in obs_cores.py, e.g., "CropRandomizer"
46+
OBS_ENCODER_BACKBONES = {"None": None} # Architecture backbones for encoding obervation, e.g., "ResNet18Conv"
4647

4748

4849
def register_obs_key(target_class):
@@ -59,6 +60,10 @@ def register_randomizer(target_class):
5960
assert target_class not in OBS_RANDOMIZERS, f"Already registered obs randomizer {target_class}!"
6061
OBS_RANDOMIZERS[target_class.__name__] = target_class
6162

63+
def register_encoder_backbone(target_class):
64+
assert target_class not in OBS_ENCODER_BACKBONES, f"Already registered obs encoder backbone {target_class}!"
65+
OBS_ENCODER_BACKBONES[target_class.__name__] = target_class
66+
6267

6368
class ObservationKeyToModalityDict(dict):
6469
"""

0 commit comments

Comments
 (0)