solarian-diffusion 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffusion_policy/__init__.py +0 -0
- diffusion_policy/codecs/__init__.py +0 -0
- diffusion_policy/codecs/imagecodecs_numcodecs.py +1408 -0
- diffusion_policy/common/__init__.py +0 -0
- diffusion_policy/common/checkpoint_util.py +64 -0
- diffusion_policy/common/json_logger.py +117 -0
- diffusion_policy/common/normalize_util.py +251 -0
- diffusion_policy/common/pose_repr_util.py +112 -0
- diffusion_policy/common/pytorch_util.py +82 -0
- diffusion_policy/common/q_critic_util.py +135 -0
- diffusion_policy/common/replay_buffer.py +672 -0
- diffusion_policy/common/sampler.py +204 -0
- diffusion_policy/config/legacy/task/blockpush_lowdim_seed.yaml +34 -0
- diffusion_policy/config/legacy/task/blockpush_lowdim_seed_abs.yaml +34 -0
- diffusion_policy/config/legacy/task/can_image.yaml +64 -0
- diffusion_policy/config/legacy/task/can_image_abs.yaml +64 -0
- diffusion_policy/config/legacy/task/can_lowdim.yaml +45 -0
- diffusion_policy/config/legacy/task/can_lowdim_abs.yaml +46 -0
- diffusion_policy/config/legacy/task/kitchen_lowdim.yaml +33 -0
- diffusion_policy/config/legacy/task/kitchen_lowdim_abs.yaml +38 -0
- diffusion_policy/config/legacy/task/lift_abs.yaml +72 -0
- diffusion_policy/config/legacy/task/lift_image.yaml +64 -0
- diffusion_policy/config/legacy/task/lift_image_abs.yaml +63 -0
- diffusion_policy/config/legacy/task/lift_lowdim.yaml +46 -0
- diffusion_policy/config/legacy/task/lift_lowdim_abs.yaml +47 -0
- diffusion_policy/config/legacy/task/pusht_image.yaml +40 -0
- diffusion_policy/config/legacy/task/pusht_lowdim.yaml +34 -0
- diffusion_policy/config/legacy/task/real_pusht_image.yaml +47 -0
- diffusion_policy/config/legacy/task/square_abs.yaml +72 -0
- diffusion_policy/config/legacy/task/square_image.yaml +64 -0
- diffusion_policy/config/legacy/task/square_image_abs.yaml +64 -0
- diffusion_policy/config/legacy/task/square_lowdim.yaml +46 -0
- diffusion_policy/config/legacy/task/square_lowdim_abs.yaml +47 -0
- diffusion_policy/config/legacy/task/tool_hang_abs.yaml +71 -0
- diffusion_policy/config/legacy/task/tool_hang_image.yaml +63 -0
- diffusion_policy/config/legacy/task/tool_hang_image_abs.yaml +63 -0
- diffusion_policy/config/legacy/task/tool_hang_lowdim.yaml +45 -0
- diffusion_policy/config/legacy/task/tool_hang_lowdim_abs.yaml +46 -0
- diffusion_policy/config/legacy/task/transport_image.yaml +75 -0
- diffusion_policy/config/legacy/task/transport_image_abs.yaml +75 -0
- diffusion_policy/config/legacy/task/transport_lowdim.yaml +49 -0
- diffusion_policy/config/legacy/task/transport_lowdim_abs.yaml +49 -0
- diffusion_policy/config/legacy/task/umi.yaml +45 -0
- diffusion_policy/config/legacy/task/umi_image.yaml +36 -0
- diffusion_policy/config/legacy/task/umi_teleop_image.yaml +41 -0
- diffusion_policy/config/legacy/train_bet_lowdim_workspace.yaml +132 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_hybrid_workspace.yaml +141 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_kitchen_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_pusht_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_real_hybrid_workspace.yaml +144 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_umi_teleop_hybrid_workspace.yaml +156 -0
- diffusion_policy/config/legacy/train_diffusion_unet_ddim_hybrid_workspace.yaml +140 -0
- diffusion_policy/config/legacy/train_diffusion_unet_ddim_lowdim_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_unet_hybrid_workspace.yaml +137 -0
- diffusion_policy/config/legacy/train_diffusion_unet_image_pretrained_workspace.yaml +149 -0
- diffusion_policy/config/legacy/train_diffusion_unet_image_workspace.yaml +150 -0
- diffusion_policy/config/legacy/train_diffusion_unet_lowdim_workspace.yaml +143 -0
- diffusion_policy/config/legacy/train_diffusion_unet_real_hybrid_workspace.yaml +141 -0
- diffusion_policy/config/legacy/train_diffusion_unet_real_image_workspace.yaml +152 -0
- diffusion_policy/config/legacy/train_diffusion_unet_real_pretrained_workspace.yaml +152 -0
- diffusion_policy/config/legacy/train_diffusion_unet_timm_workspace.yaml +145 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_hybrid_workspace.yaml +141 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_teleop_hybrid_workspace.yaml +153 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_timm_workspace.yaml +145 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_workspace.yaml +150 -0
- diffusion_policy/config/legacy/train_diffusion_unet_video_workspace.yaml +128 -0
- diffusion_policy/config/legacy/train_ibc_dfo_hybrid_workspace.yaml +111 -0
- diffusion_policy/config/legacy/train_ibc_dfo_lowdim_workspace.yaml +109 -0
- diffusion_policy/config/legacy/train_ibc_dfo_real_hybrid_workspace.yaml +111 -0
- diffusion_policy/config/legacy/train_robomimic_image_workspace.yaml +92 -0
- diffusion_policy/config/legacy/train_robomimic_lowdim_workspace.yaml +92 -0
- diffusion_policy/config/legacy/train_robomimic_real_image_workspace.yaml +92 -0
- diffusion_policy/config/rl_annotate.yaml +265 -0
- diffusion_policy/config/rl_annotate_per_frame.yaml +162 -0
- diffusion_policy/config/rl_annotate_per_frame_v4.yaml +162 -0
- diffusion_policy/config/rl_annotate_v2.yaml +141 -0
- diffusion_policy/config/rl_annotate_v3.yaml +165 -0
- diffusion_policy/config/task/lift.yaml +89 -0
- diffusion_policy/config/task/square.yaml +92 -0
- diffusion_policy/config/task/tool_hang.yaml +93 -0
- diffusion_policy/config/task/tool_hang_abs.yaml +71 -0
- diffusion_policy/config/task/umi.yaml +105 -0
- diffusion_policy/config/task/umi_bimanual.yaml +135 -0
- diffusion_policy/config/task/umi_cup_wild.yaml +106 -0
- diffusion_policy/config/task/umi_image.yaml +36 -0
- diffusion_policy/config/task/umi_pick_lego.yaml +106 -0
- diffusion_policy/config/task/umi_pick_single_lego_175.yaml +106 -0
- diffusion_policy/config/task/umi_pick_single_lego_bbox.yaml +135 -0
- diffusion_policy/config/task/umi_teleop_image.yaml +41 -0
- diffusion_policy/config/train_diffusion_transformer_umi_bimanual_workspace.yaml +185 -0
- diffusion_policy/config/train_diffusion_transformer_umi_workspace.yaml +185 -0
- diffusion_policy/config/train_diffusion_unet_image_workspace.yaml +152 -0
- diffusion_policy/config/train_diffusion_unet_timm_robomimic_workspace.yaml +152 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_cup_wild.yaml +260 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox.yaml +265 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_awr.yaml +324 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_parl.yaml +399 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego.yaml +258 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_125.yaml +260 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_175.yaml +260 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_bbox.yaml +265 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_two_legos_bbox.yaml +265 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace.yaml +224 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego.yaml +213 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego_resume.yaml +213 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_pick_lego.yaml +254 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_red_cup.yaml +219 -0
- diffusion_policy/config/train_diffusion_unet_umi_bimanual_workspace.yaml +203 -0
- diffusion_policy/config/train_iql_umi.yaml +186 -0
- diffusion_policy/config/train_iql_umi_v2.yaml +212 -0
- diffusion_policy/config/train_iql_umi_v3.yaml +212 -0
- diffusion_policy/dataset/__init__.py +0 -0
- diffusion_policy/dataset/base_dataset.py +76 -0
- diffusion_policy/dataset/umi_dataset.py +652 -0
- diffusion_policy/env_runner/__init__.py +0 -0
- diffusion_policy/env_runner/base_image_runner.py +9 -0
- diffusion_policy/env_runner/real_pusht_image_runner.py +10 -0
- diffusion_policy/model/__init__.py +0 -0
- diffusion_policy/model/common/__init__.py +0 -0
- diffusion_policy/model/common/dict_of_tensor_mixin.py +38 -0
- diffusion_policy/model/common/lr_scheduler.py +46 -0
- diffusion_policy/model/common/module_attr_mixin.py +14 -0
- diffusion_policy/model/common/normalizer.py +353 -0
- diffusion_policy/model/common/tensor_util.py +960 -0
- diffusion_policy/model/diffusion/__init__.py +0 -0
- diffusion_policy/model/diffusion/conditional_unet1d.py +238 -0
- diffusion_policy/model/diffusion/conv1d_components.py +46 -0
- diffusion_policy/model/diffusion/ema_model.py +88 -0
- diffusion_policy/model/diffusion/mask_generator.py +221 -0
- diffusion_policy/model/diffusion/positional_embedding.py +17 -0
- diffusion_policy/model/vision/__init__.py +0 -0
- diffusion_policy/model/vision/crop_randomizer.py +288 -0
- diffusion_policy/model/vision/multi_image_obs_encoder.py +195 -0
- diffusion_policy/model/vision/timm_obs_encoder.py +525 -0
- diffusion_policy/policy/__init__.py +0 -0
- diffusion_policy/policy/base_image_policy.py +27 -0
- diffusion_policy/policy/diffusion_unet_image_policy.py +264 -0
- diffusion_policy/policy/diffusion_unet_timm_policy.py +607 -0
- diffusion_policy/policy/parl_action_optimizer.py +675 -0
- diffusion_policy/workspace/__init__.py +0 -0
- diffusion_policy/workspace/base_workspace.py +146 -0
- diffusion_policy/workspace/train_diffusion_unet_image_workspace.py +1585 -0
- solarian_diffusion/__init__.py +32 -0
- solarian_diffusion/config.py +31 -0
- solarian_diffusion/policy_server.py +316 -0
- solarian_diffusion/rtc.py +272 -0
- solarian_diffusion/service.py +16 -0
- solarian_diffusion/trainer.py +213 -0
- solarian_diffusion-0.1.0.dist-info/METADATA +126 -0
- solarian_diffusion-0.1.0.dist-info/RECORD +159 -0
- solarian_diffusion-0.1.0.dist-info/WHEEL +5 -0
- solarian_diffusion-0.1.0.dist-info/entry_points.txt +8 -0
- solarian_diffusion-0.1.0.dist-info/licenses/LICENSE +201 -0
- solarian_diffusion-0.1.0.dist-info/licenses/NOTICE +45 -0
- solarian_diffusion-0.1.0.dist-info/top_level.txt +3 -0
- umi/__init__.py +0 -0
- umi/common/__init__.py +0 -0
- umi/common/pose_util.py +115 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
|
|
4
|
+
class DictOfTensorMixin(nn.Module):
|
|
5
|
+
def __init__(self, params_dict=None):
|
|
6
|
+
super().__init__()
|
|
7
|
+
if params_dict is None:
|
|
8
|
+
params_dict = nn.ParameterDict()
|
|
9
|
+
self.params_dict = params_dict
|
|
10
|
+
|
|
11
|
+
@property
|
|
12
|
+
def device(self):
|
|
13
|
+
return next(iter(self.parameters())).device
|
|
14
|
+
|
|
15
|
+
def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs):
|
|
16
|
+
def dfs_add(dest, keys, value: torch.Tensor):
|
|
17
|
+
if len(keys) == 1:
|
|
18
|
+
dest[keys[0]] = value
|
|
19
|
+
return
|
|
20
|
+
|
|
21
|
+
if keys[0] not in dest:
|
|
22
|
+
dest[keys[0]] = nn.ParameterDict()
|
|
23
|
+
dfs_add(dest[keys[0]], keys[1:], value)
|
|
24
|
+
|
|
25
|
+
def load_dict(state_dict, prefix):
|
|
26
|
+
out_dict = nn.ParameterDict()
|
|
27
|
+
for key, value in state_dict.items():
|
|
28
|
+
value: torch.Tensor
|
|
29
|
+
if key.startswith(prefix):
|
|
30
|
+
param_keys = key[len(prefix):].split('.')[1:]
|
|
31
|
+
# if len(param_keys) == 0:
|
|
32
|
+
# import pdb; pdb.set_trace()
|
|
33
|
+
dfs_add(out_dict, param_keys, value.clone())
|
|
34
|
+
return out_dict
|
|
35
|
+
|
|
36
|
+
self.params_dict = load_dict(state_dict, prefix + 'params_dict')
|
|
37
|
+
self.params_dict.requires_grad_(False)
|
|
38
|
+
return
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from diffusers.optimization import (
|
|
2
|
+
Union, SchedulerType, Optional,
|
|
3
|
+
Optimizer, TYPE_TO_SCHEDULER_FUNCTION
|
|
4
|
+
)
|
|
5
|
+
|
|
6
|
+
def get_scheduler(
|
|
7
|
+
name: Union[str, SchedulerType],
|
|
8
|
+
optimizer: Optimizer,
|
|
9
|
+
num_warmup_steps: Optional[int] = None,
|
|
10
|
+
num_training_steps: Optional[int] = None,
|
|
11
|
+
**kwargs
|
|
12
|
+
):
|
|
13
|
+
"""
|
|
14
|
+
Added kwargs vs diffuser's original implementation
|
|
15
|
+
|
|
16
|
+
Unified API to get any scheduler from its name.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
name (`str` or `SchedulerType`):
|
|
20
|
+
The name of the scheduler to use.
|
|
21
|
+
optimizer (`torch.optim.Optimizer`):
|
|
22
|
+
The optimizer that will be used during training.
|
|
23
|
+
num_warmup_steps (`int`, *optional*):
|
|
24
|
+
The number of warmup steps to do. This is not required by all schedulers (hence the argument being
|
|
25
|
+
optional), the function will raise an error if it's unset and the scheduler type requires it.
|
|
26
|
+
num_training_steps (`int``, *optional*):
|
|
27
|
+
The number of training steps to do. This is not required by all schedulers (hence the argument being
|
|
28
|
+
optional), the function will raise an error if it's unset and the scheduler type requires it.
|
|
29
|
+
"""
|
|
30
|
+
name = SchedulerType(name)
|
|
31
|
+
schedule_func = TYPE_TO_SCHEDULER_FUNCTION[name]
|
|
32
|
+
if name == SchedulerType.CONSTANT:
|
|
33
|
+
return schedule_func(optimizer, **kwargs)
|
|
34
|
+
|
|
35
|
+
# All other schedulers require `num_warmup_steps`
|
|
36
|
+
if num_warmup_steps is None:
|
|
37
|
+
raise ValueError(f"{name} requires `num_warmup_steps`, please provide that argument.")
|
|
38
|
+
|
|
39
|
+
if name == SchedulerType.CONSTANT_WITH_WARMUP:
|
|
40
|
+
return schedule_func(optimizer, num_warmup_steps=num_warmup_steps, **kwargs)
|
|
41
|
+
|
|
42
|
+
# All other schedulers require `num_training_steps`
|
|
43
|
+
if num_training_steps is None:
|
|
44
|
+
raise ValueError(f"{name} requires `num_training_steps`, please provide that argument.")
|
|
45
|
+
|
|
46
|
+
return schedule_func(optimizer, num_warmup_steps=num_warmup_steps, num_training_steps=num_training_steps, **kwargs)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import torch.nn as nn
|
|
2
|
+
|
|
3
|
+
class ModuleAttrMixin(nn.Module):
|
|
4
|
+
def __init__(self):
|
|
5
|
+
super().__init__()
|
|
6
|
+
self._dummy_variable = nn.Parameter(requires_grad=False)
|
|
7
|
+
|
|
8
|
+
@property
|
|
9
|
+
def device(self):
|
|
10
|
+
return next(iter(self.parameters())).device
|
|
11
|
+
|
|
12
|
+
@property
|
|
13
|
+
def dtype(self):
|
|
14
|
+
return next(iter(self.parameters())).dtype
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
from typing import Union, Dict
|
|
2
|
+
|
|
3
|
+
import unittest
|
|
4
|
+
import zarr
|
|
5
|
+
import numpy as np
|
|
6
|
+
import torch
|
|
7
|
+
import torch.nn as nn
|
|
8
|
+
from diffusion_policy.common.pytorch_util import dict_apply
|
|
9
|
+
from diffusion_policy.model.common.dict_of_tensor_mixin import DictOfTensorMixin
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class LinearNormalizer(DictOfTensorMixin):
|
|
13
|
+
avaliable_modes = ['limits', 'gaussian']
|
|
14
|
+
|
|
15
|
+
@torch.no_grad()
|
|
16
|
+
def fit(self,
|
|
17
|
+
data: Union[Dict, torch.Tensor, np.ndarray, zarr.Array],
|
|
18
|
+
last_n_dims=1,
|
|
19
|
+
dtype=torch.float32,
|
|
20
|
+
mode='limits',
|
|
21
|
+
output_max=1.,
|
|
22
|
+
output_min=-1.,
|
|
23
|
+
range_eps=1e-4,
|
|
24
|
+
fit_offset=True):
|
|
25
|
+
if isinstance(data, dict):
|
|
26
|
+
for key, value in data.items():
|
|
27
|
+
self.params_dict[key] = _fit(value,
|
|
28
|
+
last_n_dims=last_n_dims,
|
|
29
|
+
dtype=dtype,
|
|
30
|
+
mode=mode,
|
|
31
|
+
output_max=output_max,
|
|
32
|
+
output_min=output_min,
|
|
33
|
+
range_eps=range_eps,
|
|
34
|
+
fit_offset=fit_offset)
|
|
35
|
+
else:
|
|
36
|
+
self.params_dict['_default'] = _fit(data,
|
|
37
|
+
last_n_dims=last_n_dims,
|
|
38
|
+
dtype=dtype,
|
|
39
|
+
mode=mode,
|
|
40
|
+
output_max=output_max,
|
|
41
|
+
output_min=output_min,
|
|
42
|
+
range_eps=range_eps,
|
|
43
|
+
fit_offset=fit_offset)
|
|
44
|
+
|
|
45
|
+
def __call__(self, x: Union[Dict, torch.Tensor, np.ndarray]) -> torch.Tensor:
|
|
46
|
+
return self.normalize(x)
|
|
47
|
+
|
|
48
|
+
def __getitem__(self, key: str):
|
|
49
|
+
return SingleFieldLinearNormalizer(self.params_dict[key])
|
|
50
|
+
|
|
51
|
+
def __setitem__(self, key: str , value: 'SingleFieldLinearNormalizer'):
|
|
52
|
+
self.params_dict[key] = value.params_dict
|
|
53
|
+
|
|
54
|
+
def _normalize_impl(self, x, forward=True):
|
|
55
|
+
if isinstance(x, dict):
|
|
56
|
+
result = dict()
|
|
57
|
+
for key, value in x.items():
|
|
58
|
+
params = self.params_dict[key]
|
|
59
|
+
result[key] = _normalize(value, params, forward=forward)
|
|
60
|
+
return result
|
|
61
|
+
else:
|
|
62
|
+
if '_default' not in self.params_dict:
|
|
63
|
+
raise RuntimeError("Not initialized")
|
|
64
|
+
params = self.params_dict['_default']
|
|
65
|
+
return _normalize(x, params, forward=forward)
|
|
66
|
+
|
|
67
|
+
def normalize(self, x: Union[Dict, torch.Tensor, np.ndarray]) -> torch.Tensor:
|
|
68
|
+
return self._normalize_impl(x, forward=True)
|
|
69
|
+
|
|
70
|
+
def unnormalize(self, x: Union[Dict, torch.Tensor, np.ndarray]) -> torch.Tensor:
|
|
71
|
+
return self._normalize_impl(x, forward=False)
|
|
72
|
+
|
|
73
|
+
def get_input_stats(self) -> Dict:
|
|
74
|
+
if len(self.params_dict) == 0:
|
|
75
|
+
raise RuntimeError("Not initialized")
|
|
76
|
+
if len(self.params_dict) == 1 and '_default' in self.params_dict:
|
|
77
|
+
return self.params_dict['_default']['input_stats']
|
|
78
|
+
|
|
79
|
+
result = dict()
|
|
80
|
+
for key, value in self.params_dict.items():
|
|
81
|
+
if key != '_default':
|
|
82
|
+
result[key] = value['input_stats']
|
|
83
|
+
return result
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def get_output_stats(self, key='_default'):
|
|
87
|
+
input_stats = self.get_input_stats()
|
|
88
|
+
if 'min' in input_stats:
|
|
89
|
+
# no dict
|
|
90
|
+
return dict_apply(input_stats, self.normalize)
|
|
91
|
+
|
|
92
|
+
result = dict()
|
|
93
|
+
for key, group in input_stats.items():
|
|
94
|
+
this_dict = dict()
|
|
95
|
+
for name, value in group.items():
|
|
96
|
+
this_dict[name] = self.normalize({key:value})[key]
|
|
97
|
+
result[key] = this_dict
|
|
98
|
+
return result
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class SingleFieldLinearNormalizer(DictOfTensorMixin):
|
|
102
|
+
avaliable_modes = ['limits', 'gaussian']
|
|
103
|
+
|
|
104
|
+
@torch.no_grad()
|
|
105
|
+
def fit(self,
|
|
106
|
+
data: Union[torch.Tensor, np.ndarray, zarr.Array],
|
|
107
|
+
last_n_dims=1,
|
|
108
|
+
dtype=torch.float32,
|
|
109
|
+
mode='limits',
|
|
110
|
+
output_max=1.,
|
|
111
|
+
output_min=-1.,
|
|
112
|
+
range_eps=1e-4,
|
|
113
|
+
fit_offset=True):
|
|
114
|
+
self.params_dict = _fit(data,
|
|
115
|
+
last_n_dims=last_n_dims,
|
|
116
|
+
dtype=dtype,
|
|
117
|
+
mode=mode,
|
|
118
|
+
output_max=output_max,
|
|
119
|
+
output_min=output_min,
|
|
120
|
+
range_eps=range_eps,
|
|
121
|
+
fit_offset=fit_offset)
|
|
122
|
+
|
|
123
|
+
@classmethod
|
|
124
|
+
def create_fit(cls, data: Union[torch.Tensor, np.ndarray, zarr.Array], **kwargs):
|
|
125
|
+
obj = cls()
|
|
126
|
+
obj.fit(data, **kwargs)
|
|
127
|
+
return obj
|
|
128
|
+
|
|
129
|
+
@classmethod
|
|
130
|
+
def create_manual(cls,
|
|
131
|
+
scale: Union[torch.Tensor, np.ndarray],
|
|
132
|
+
offset: Union[torch.Tensor, np.ndarray],
|
|
133
|
+
input_stats_dict: Dict[str, Union[torch.Tensor, np.ndarray]]):
|
|
134
|
+
def to_tensor(x):
|
|
135
|
+
if not isinstance(x, torch.Tensor):
|
|
136
|
+
x = torch.from_numpy(x)
|
|
137
|
+
x = x.flatten()
|
|
138
|
+
return x
|
|
139
|
+
|
|
140
|
+
# check
|
|
141
|
+
for x in [offset] + list(input_stats_dict.values()):
|
|
142
|
+
assert x.shape == scale.shape
|
|
143
|
+
assert x.dtype == scale.dtype
|
|
144
|
+
|
|
145
|
+
params_dict = nn.ParameterDict({
|
|
146
|
+
'scale': to_tensor(scale),
|
|
147
|
+
'offset': to_tensor(offset),
|
|
148
|
+
'input_stats': nn.ParameterDict(
|
|
149
|
+
dict_apply(input_stats_dict, to_tensor))
|
|
150
|
+
})
|
|
151
|
+
return cls(params_dict)
|
|
152
|
+
|
|
153
|
+
@classmethod
|
|
154
|
+
def create_identity(cls, dtype=torch.float32):
|
|
155
|
+
scale = torch.tensor([1], dtype=dtype)
|
|
156
|
+
offset = torch.tensor([0], dtype=dtype)
|
|
157
|
+
input_stats_dict = {
|
|
158
|
+
'min': torch.tensor([-1], dtype=dtype),
|
|
159
|
+
'max': torch.tensor([1], dtype=dtype),
|
|
160
|
+
'mean': torch.tensor([0], dtype=dtype),
|
|
161
|
+
'std': torch.tensor([1], dtype=dtype)
|
|
162
|
+
}
|
|
163
|
+
return cls.create_manual(scale, offset, input_stats_dict)
|
|
164
|
+
|
|
165
|
+
def normalize(self, x: Union[torch.Tensor, np.ndarray]) -> torch.Tensor:
|
|
166
|
+
return _normalize(x, self.params_dict, forward=True)
|
|
167
|
+
|
|
168
|
+
def unnormalize(self, x: Union[torch.Tensor, np.ndarray]) -> torch.Tensor:
|
|
169
|
+
return _normalize(x, self.params_dict, forward=False)
|
|
170
|
+
|
|
171
|
+
def get_input_stats(self):
|
|
172
|
+
return self.params_dict['input_stats']
|
|
173
|
+
|
|
174
|
+
def get_output_stats(self):
|
|
175
|
+
return dict_apply(self.params_dict['input_stats'], self.normalize)
|
|
176
|
+
|
|
177
|
+
def __call__(self, x: Union[torch.Tensor, np.ndarray]) -> torch.Tensor:
|
|
178
|
+
return self.normalize(x)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _fit(data: Union[torch.Tensor, np.ndarray, zarr.Array],
|
|
183
|
+
last_n_dims=1,
|
|
184
|
+
dtype=torch.float32,
|
|
185
|
+
mode='limits',
|
|
186
|
+
output_max=1.,
|
|
187
|
+
output_min=-1.,
|
|
188
|
+
range_eps=1e-4,
|
|
189
|
+
fit_offset=True):
|
|
190
|
+
assert mode in ['limits', 'gaussian']
|
|
191
|
+
assert last_n_dims >= 0
|
|
192
|
+
assert output_max > output_min
|
|
193
|
+
|
|
194
|
+
# convert data to torch and type
|
|
195
|
+
if isinstance(data, zarr.Array):
|
|
196
|
+
data = data[:]
|
|
197
|
+
if isinstance(data, np.ndarray):
|
|
198
|
+
data = torch.from_numpy(data)
|
|
199
|
+
if dtype is not None:
|
|
200
|
+
data = data.type(dtype)
|
|
201
|
+
|
|
202
|
+
# convert shape
|
|
203
|
+
dim = 1
|
|
204
|
+
if last_n_dims > 0:
|
|
205
|
+
dim = np.prod(data.shape[-last_n_dims:])
|
|
206
|
+
data = data.reshape(-1,dim)
|
|
207
|
+
|
|
208
|
+
# compute input stats min max mean std
|
|
209
|
+
input_min, _ = data.min(axis=0)
|
|
210
|
+
input_max, _ = data.max(axis=0)
|
|
211
|
+
input_mean = data.mean(axis=0)
|
|
212
|
+
input_std = data.std(axis=0)
|
|
213
|
+
|
|
214
|
+
# compute scale and offset
|
|
215
|
+
if mode == 'limits':
|
|
216
|
+
if fit_offset:
|
|
217
|
+
# unit scale
|
|
218
|
+
input_range = input_max - input_min
|
|
219
|
+
ignore_dim = input_range < range_eps
|
|
220
|
+
input_range[ignore_dim] = output_max - output_min
|
|
221
|
+
scale = (output_max - output_min) / input_range
|
|
222
|
+
offset = output_min - scale * input_min
|
|
223
|
+
offset[ignore_dim] = (output_max + output_min) / 2 - input_min[ignore_dim]
|
|
224
|
+
# ignore dims scaled to mean of output max and min
|
|
225
|
+
else:
|
|
226
|
+
# use this when data is pre-zero-centered.
|
|
227
|
+
assert output_max > 0
|
|
228
|
+
assert output_min < 0
|
|
229
|
+
# unit abs
|
|
230
|
+
output_abs = min(abs(output_min), abs(output_max))
|
|
231
|
+
input_abs = torch.maximum(torch.abs(input_min), torch.abs(input_max))
|
|
232
|
+
ignore_dim = input_abs < range_eps
|
|
233
|
+
input_abs[ignore_dim] = output_abs
|
|
234
|
+
# don't scale constant channels
|
|
235
|
+
scale = output_abs / input_abs
|
|
236
|
+
offset = torch.zeros_like(input_mean)
|
|
237
|
+
elif mode == 'gaussian':
|
|
238
|
+
ignore_dim = input_std < range_eps
|
|
239
|
+
scale = input_std.clone()
|
|
240
|
+
scale[ignore_dim] = 1
|
|
241
|
+
scale = 1 / scale
|
|
242
|
+
|
|
243
|
+
if fit_offset:
|
|
244
|
+
offset = - input_mean * scale
|
|
245
|
+
else:
|
|
246
|
+
offset = torch.zeros_like(input_mean)
|
|
247
|
+
|
|
248
|
+
# save
|
|
249
|
+
this_params = nn.ParameterDict({
|
|
250
|
+
'scale': scale,
|
|
251
|
+
'offset': offset,
|
|
252
|
+
'input_stats': nn.ParameterDict({
|
|
253
|
+
'min': input_min,
|
|
254
|
+
'max': input_max,
|
|
255
|
+
'mean': input_mean,
|
|
256
|
+
'std': input_std
|
|
257
|
+
})
|
|
258
|
+
})
|
|
259
|
+
for p in this_params.parameters():
|
|
260
|
+
p.requires_grad_(False)
|
|
261
|
+
return this_params
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _normalize(x, params, forward=True):
|
|
265
|
+
assert 'scale' in params
|
|
266
|
+
if isinstance(x, np.ndarray):
|
|
267
|
+
x = torch.from_numpy(x)
|
|
268
|
+
scale = params['scale']
|
|
269
|
+
offset = params['offset']
|
|
270
|
+
x = x.to(device=scale.device, dtype=scale.dtype)
|
|
271
|
+
src_shape = x.shape
|
|
272
|
+
x = x.reshape(-1, scale.shape[0])
|
|
273
|
+
if forward:
|
|
274
|
+
x = x * scale + offset
|
|
275
|
+
else:
|
|
276
|
+
x = (x - offset) / scale
|
|
277
|
+
x = x.reshape(src_shape)
|
|
278
|
+
return x
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def test():
|
|
282
|
+
data = torch.zeros((100,10,9,2)).uniform_()
|
|
283
|
+
data[...,0,0] = 0
|
|
284
|
+
|
|
285
|
+
normalizer = SingleFieldLinearNormalizer()
|
|
286
|
+
normalizer.fit(data, mode='limits', last_n_dims=2)
|
|
287
|
+
datan = normalizer.normalize(data)
|
|
288
|
+
assert datan.shape == data.shape
|
|
289
|
+
assert np.allclose(datan.max(), 1.)
|
|
290
|
+
assert np.allclose(datan.min(), -1.)
|
|
291
|
+
dataun = normalizer.unnormalize(datan)
|
|
292
|
+
assert torch.allclose(data, dataun, atol=1e-7)
|
|
293
|
+
|
|
294
|
+
input_stats = normalizer.get_input_stats()
|
|
295
|
+
output_stats = normalizer.get_output_stats()
|
|
296
|
+
|
|
297
|
+
normalizer = SingleFieldLinearNormalizer()
|
|
298
|
+
normalizer.fit(data, mode='limits', last_n_dims=1, fit_offset=False)
|
|
299
|
+
datan = normalizer.normalize(data)
|
|
300
|
+
assert datan.shape == data.shape
|
|
301
|
+
assert np.allclose(datan.max(), 1., atol=1e-3)
|
|
302
|
+
assert np.allclose(datan.min(), 0., atol=1e-3)
|
|
303
|
+
dataun = normalizer.unnormalize(datan)
|
|
304
|
+
assert torch.allclose(data, dataun, atol=1e-7)
|
|
305
|
+
|
|
306
|
+
data = torch.zeros((100,10,9,2)).uniform_()
|
|
307
|
+
normalizer = SingleFieldLinearNormalizer()
|
|
308
|
+
normalizer.fit(data, mode='gaussian', last_n_dims=0)
|
|
309
|
+
datan = normalizer.normalize(data)
|
|
310
|
+
assert datan.shape == data.shape
|
|
311
|
+
assert np.allclose(datan.mean(), 0., atol=1e-3)
|
|
312
|
+
assert np.allclose(datan.std(), 1., atol=1e-3)
|
|
313
|
+
dataun = normalizer.unnormalize(datan)
|
|
314
|
+
assert torch.allclose(data, dataun, atol=1e-7)
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
# dict
|
|
318
|
+
data = torch.zeros((100,10,9,2)).uniform_()
|
|
319
|
+
data[...,0,0] = 0
|
|
320
|
+
|
|
321
|
+
normalizer = LinearNormalizer()
|
|
322
|
+
normalizer.fit(data, mode='limits', last_n_dims=2)
|
|
323
|
+
datan = normalizer.normalize(data)
|
|
324
|
+
assert datan.shape == data.shape
|
|
325
|
+
assert np.allclose(datan.max(), 1.)
|
|
326
|
+
assert np.allclose(datan.min(), -1.)
|
|
327
|
+
dataun = normalizer.unnormalize(datan)
|
|
328
|
+
assert torch.allclose(data, dataun, atol=1e-7)
|
|
329
|
+
|
|
330
|
+
input_stats = normalizer.get_input_stats()
|
|
331
|
+
output_stats = normalizer.get_output_stats()
|
|
332
|
+
|
|
333
|
+
data = {
|
|
334
|
+
'obs': torch.zeros((1000,128,9,2)).uniform_() * 512,
|
|
335
|
+
'action': torch.zeros((1000,128,2)).uniform_() * 512
|
|
336
|
+
}
|
|
337
|
+
normalizer = LinearNormalizer()
|
|
338
|
+
normalizer.fit(data)
|
|
339
|
+
datan = normalizer.normalize(data)
|
|
340
|
+
dataun = normalizer.unnormalize(datan)
|
|
341
|
+
for key in data:
|
|
342
|
+
assert torch.allclose(data[key], dataun[key], atol=1e-4)
|
|
343
|
+
|
|
344
|
+
input_stats = normalizer.get_input_stats()
|
|
345
|
+
output_stats = normalizer.get_output_stats()
|
|
346
|
+
|
|
347
|
+
state_dict = normalizer.state_dict()
|
|
348
|
+
n = LinearNormalizer()
|
|
349
|
+
n.load_state_dict(state_dict)
|
|
350
|
+
datan = n.normalize(data)
|
|
351
|
+
dataun = n.unnormalize(datan)
|
|
352
|
+
for key in data:
|
|
353
|
+
assert torch.allclose(data[key], dataun[key], atol=1e-4)
|