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.
Files changed (159) hide show
  1. diffusion_policy/__init__.py +0 -0
  2. diffusion_policy/codecs/__init__.py +0 -0
  3. diffusion_policy/codecs/imagecodecs_numcodecs.py +1408 -0
  4. diffusion_policy/common/__init__.py +0 -0
  5. diffusion_policy/common/checkpoint_util.py +64 -0
  6. diffusion_policy/common/json_logger.py +117 -0
  7. diffusion_policy/common/normalize_util.py +251 -0
  8. diffusion_policy/common/pose_repr_util.py +112 -0
  9. diffusion_policy/common/pytorch_util.py +82 -0
  10. diffusion_policy/common/q_critic_util.py +135 -0
  11. diffusion_policy/common/replay_buffer.py +672 -0
  12. diffusion_policy/common/sampler.py +204 -0
  13. diffusion_policy/config/legacy/task/blockpush_lowdim_seed.yaml +34 -0
  14. diffusion_policy/config/legacy/task/blockpush_lowdim_seed_abs.yaml +34 -0
  15. diffusion_policy/config/legacy/task/can_image.yaml +64 -0
  16. diffusion_policy/config/legacy/task/can_image_abs.yaml +64 -0
  17. diffusion_policy/config/legacy/task/can_lowdim.yaml +45 -0
  18. diffusion_policy/config/legacy/task/can_lowdim_abs.yaml +46 -0
  19. diffusion_policy/config/legacy/task/kitchen_lowdim.yaml +33 -0
  20. diffusion_policy/config/legacy/task/kitchen_lowdim_abs.yaml +38 -0
  21. diffusion_policy/config/legacy/task/lift_abs.yaml +72 -0
  22. diffusion_policy/config/legacy/task/lift_image.yaml +64 -0
  23. diffusion_policy/config/legacy/task/lift_image_abs.yaml +63 -0
  24. diffusion_policy/config/legacy/task/lift_lowdim.yaml +46 -0
  25. diffusion_policy/config/legacy/task/lift_lowdim_abs.yaml +47 -0
  26. diffusion_policy/config/legacy/task/pusht_image.yaml +40 -0
  27. diffusion_policy/config/legacy/task/pusht_lowdim.yaml +34 -0
  28. diffusion_policy/config/legacy/task/real_pusht_image.yaml +47 -0
  29. diffusion_policy/config/legacy/task/square_abs.yaml +72 -0
  30. diffusion_policy/config/legacy/task/square_image.yaml +64 -0
  31. diffusion_policy/config/legacy/task/square_image_abs.yaml +64 -0
  32. diffusion_policy/config/legacy/task/square_lowdim.yaml +46 -0
  33. diffusion_policy/config/legacy/task/square_lowdim_abs.yaml +47 -0
  34. diffusion_policy/config/legacy/task/tool_hang_abs.yaml +71 -0
  35. diffusion_policy/config/legacy/task/tool_hang_image.yaml +63 -0
  36. diffusion_policy/config/legacy/task/tool_hang_image_abs.yaml +63 -0
  37. diffusion_policy/config/legacy/task/tool_hang_lowdim.yaml +45 -0
  38. diffusion_policy/config/legacy/task/tool_hang_lowdim_abs.yaml +46 -0
  39. diffusion_policy/config/legacy/task/transport_image.yaml +75 -0
  40. diffusion_policy/config/legacy/task/transport_image_abs.yaml +75 -0
  41. diffusion_policy/config/legacy/task/transport_lowdim.yaml +49 -0
  42. diffusion_policy/config/legacy/task/transport_lowdim_abs.yaml +49 -0
  43. diffusion_policy/config/legacy/task/umi.yaml +45 -0
  44. diffusion_policy/config/legacy/task/umi_image.yaml +36 -0
  45. diffusion_policy/config/legacy/task/umi_teleop_image.yaml +41 -0
  46. diffusion_policy/config/legacy/train_bet_lowdim_workspace.yaml +132 -0
  47. diffusion_policy/config/legacy/train_diffusion_transformer_hybrid_workspace.yaml +141 -0
  48. diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_kitchen_workspace.yaml +146 -0
  49. diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_pusht_workspace.yaml +146 -0
  50. diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_workspace.yaml +146 -0
  51. diffusion_policy/config/legacy/train_diffusion_transformer_real_hybrid_workspace.yaml +144 -0
  52. diffusion_policy/config/legacy/train_diffusion_transformer_umi_teleop_hybrid_workspace.yaml +156 -0
  53. diffusion_policy/config/legacy/train_diffusion_unet_ddim_hybrid_workspace.yaml +140 -0
  54. diffusion_policy/config/legacy/train_diffusion_unet_ddim_lowdim_workspace.yaml +146 -0
  55. diffusion_policy/config/legacy/train_diffusion_unet_hybrid_workspace.yaml +137 -0
  56. diffusion_policy/config/legacy/train_diffusion_unet_image_pretrained_workspace.yaml +149 -0
  57. diffusion_policy/config/legacy/train_diffusion_unet_image_workspace.yaml +150 -0
  58. diffusion_policy/config/legacy/train_diffusion_unet_lowdim_workspace.yaml +143 -0
  59. diffusion_policy/config/legacy/train_diffusion_unet_real_hybrid_workspace.yaml +141 -0
  60. diffusion_policy/config/legacy/train_diffusion_unet_real_image_workspace.yaml +152 -0
  61. diffusion_policy/config/legacy/train_diffusion_unet_real_pretrained_workspace.yaml +152 -0
  62. diffusion_policy/config/legacy/train_diffusion_unet_timm_workspace.yaml +145 -0
  63. diffusion_policy/config/legacy/train_diffusion_unet_umi_hybrid_workspace.yaml +141 -0
  64. diffusion_policy/config/legacy/train_diffusion_unet_umi_teleop_hybrid_workspace.yaml +153 -0
  65. diffusion_policy/config/legacy/train_diffusion_unet_umi_timm_workspace.yaml +145 -0
  66. diffusion_policy/config/legacy/train_diffusion_unet_umi_workspace.yaml +150 -0
  67. diffusion_policy/config/legacy/train_diffusion_unet_video_workspace.yaml +128 -0
  68. diffusion_policy/config/legacy/train_ibc_dfo_hybrid_workspace.yaml +111 -0
  69. diffusion_policy/config/legacy/train_ibc_dfo_lowdim_workspace.yaml +109 -0
  70. diffusion_policy/config/legacy/train_ibc_dfo_real_hybrid_workspace.yaml +111 -0
  71. diffusion_policy/config/legacy/train_robomimic_image_workspace.yaml +92 -0
  72. diffusion_policy/config/legacy/train_robomimic_lowdim_workspace.yaml +92 -0
  73. diffusion_policy/config/legacy/train_robomimic_real_image_workspace.yaml +92 -0
  74. diffusion_policy/config/rl_annotate.yaml +265 -0
  75. diffusion_policy/config/rl_annotate_per_frame.yaml +162 -0
  76. diffusion_policy/config/rl_annotate_per_frame_v4.yaml +162 -0
  77. diffusion_policy/config/rl_annotate_v2.yaml +141 -0
  78. diffusion_policy/config/rl_annotate_v3.yaml +165 -0
  79. diffusion_policy/config/task/lift.yaml +89 -0
  80. diffusion_policy/config/task/square.yaml +92 -0
  81. diffusion_policy/config/task/tool_hang.yaml +93 -0
  82. diffusion_policy/config/task/tool_hang_abs.yaml +71 -0
  83. diffusion_policy/config/task/umi.yaml +105 -0
  84. diffusion_policy/config/task/umi_bimanual.yaml +135 -0
  85. diffusion_policy/config/task/umi_cup_wild.yaml +106 -0
  86. diffusion_policy/config/task/umi_image.yaml +36 -0
  87. diffusion_policy/config/task/umi_pick_lego.yaml +106 -0
  88. diffusion_policy/config/task/umi_pick_single_lego_175.yaml +106 -0
  89. diffusion_policy/config/task/umi_pick_single_lego_bbox.yaml +135 -0
  90. diffusion_policy/config/task/umi_teleop_image.yaml +41 -0
  91. diffusion_policy/config/train_diffusion_transformer_umi_bimanual_workspace.yaml +185 -0
  92. diffusion_policy/config/train_diffusion_transformer_umi_workspace.yaml +185 -0
  93. diffusion_policy/config/train_diffusion_unet_image_workspace.yaml +152 -0
  94. diffusion_policy/config/train_diffusion_unet_timm_robomimic_workspace.yaml +152 -0
  95. diffusion_policy/config/train_diffusion_unet_timm_umi_cup_wild.yaml +260 -0
  96. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox.yaml +265 -0
  97. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_awr.yaml +324 -0
  98. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_parl.yaml +399 -0
  99. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego.yaml +258 -0
  100. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_125.yaml +260 -0
  101. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_175.yaml +260 -0
  102. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_bbox.yaml +265 -0
  103. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_two_legos_bbox.yaml +265 -0
  104. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace.yaml +224 -0
  105. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego.yaml +213 -0
  106. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego_resume.yaml +213 -0
  107. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_pick_lego.yaml +254 -0
  108. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_red_cup.yaml +219 -0
  109. diffusion_policy/config/train_diffusion_unet_umi_bimanual_workspace.yaml +203 -0
  110. diffusion_policy/config/train_iql_umi.yaml +186 -0
  111. diffusion_policy/config/train_iql_umi_v2.yaml +212 -0
  112. diffusion_policy/config/train_iql_umi_v3.yaml +212 -0
  113. diffusion_policy/dataset/__init__.py +0 -0
  114. diffusion_policy/dataset/base_dataset.py +76 -0
  115. diffusion_policy/dataset/umi_dataset.py +652 -0
  116. diffusion_policy/env_runner/__init__.py +0 -0
  117. diffusion_policy/env_runner/base_image_runner.py +9 -0
  118. diffusion_policy/env_runner/real_pusht_image_runner.py +10 -0
  119. diffusion_policy/model/__init__.py +0 -0
  120. diffusion_policy/model/common/__init__.py +0 -0
  121. diffusion_policy/model/common/dict_of_tensor_mixin.py +38 -0
  122. diffusion_policy/model/common/lr_scheduler.py +46 -0
  123. diffusion_policy/model/common/module_attr_mixin.py +14 -0
  124. diffusion_policy/model/common/normalizer.py +353 -0
  125. diffusion_policy/model/common/tensor_util.py +960 -0
  126. diffusion_policy/model/diffusion/__init__.py +0 -0
  127. diffusion_policy/model/diffusion/conditional_unet1d.py +238 -0
  128. diffusion_policy/model/diffusion/conv1d_components.py +46 -0
  129. diffusion_policy/model/diffusion/ema_model.py +88 -0
  130. diffusion_policy/model/diffusion/mask_generator.py +221 -0
  131. diffusion_policy/model/diffusion/positional_embedding.py +17 -0
  132. diffusion_policy/model/vision/__init__.py +0 -0
  133. diffusion_policy/model/vision/crop_randomizer.py +288 -0
  134. diffusion_policy/model/vision/multi_image_obs_encoder.py +195 -0
  135. diffusion_policy/model/vision/timm_obs_encoder.py +525 -0
  136. diffusion_policy/policy/__init__.py +0 -0
  137. diffusion_policy/policy/base_image_policy.py +27 -0
  138. diffusion_policy/policy/diffusion_unet_image_policy.py +264 -0
  139. diffusion_policy/policy/diffusion_unet_timm_policy.py +607 -0
  140. diffusion_policy/policy/parl_action_optimizer.py +675 -0
  141. diffusion_policy/workspace/__init__.py +0 -0
  142. diffusion_policy/workspace/base_workspace.py +146 -0
  143. diffusion_policy/workspace/train_diffusion_unet_image_workspace.py +1585 -0
  144. solarian_diffusion/__init__.py +32 -0
  145. solarian_diffusion/config.py +31 -0
  146. solarian_diffusion/policy_server.py +316 -0
  147. solarian_diffusion/rtc.py +272 -0
  148. solarian_diffusion/service.py +16 -0
  149. solarian_diffusion/trainer.py +213 -0
  150. solarian_diffusion-0.1.0.dist-info/METADATA +126 -0
  151. solarian_diffusion-0.1.0.dist-info/RECORD +159 -0
  152. solarian_diffusion-0.1.0.dist-info/WHEEL +5 -0
  153. solarian_diffusion-0.1.0.dist-info/entry_points.txt +8 -0
  154. solarian_diffusion-0.1.0.dist-info/licenses/LICENSE +201 -0
  155. solarian_diffusion-0.1.0.dist-info/licenses/NOTICE +45 -0
  156. solarian_diffusion-0.1.0.dist-info/top_level.txt +3 -0
  157. umi/__init__.py +0 -0
  158. umi/common/__init__.py +0 -0
  159. umi/common/pose_util.py +115 -0
File without changes
@@ -0,0 +1,64 @@
1
+ from typing import Any, Optional, Dict
2
+ import os
3
+
4
+ class TopKCheckpointManager:
5
+ def __init__(self,
6
+ save_dir,
7
+ monitor_key: str,
8
+ mode='min',
9
+ k=1,
10
+ format_str='epoch_{epoch:03d}-train_loss_{train_loss:.3f}.ckpt',
11
+ static_format_kwargs: Optional[Dict[str, Any]] = None
12
+ ):
13
+ assert mode in ['max', 'min']
14
+ assert k >= 0
15
+
16
+ self.save_dir = save_dir
17
+ self.monitor_key = monitor_key
18
+ self.mode = mode
19
+ self.k = k
20
+ self.format_str = format_str
21
+ # Constant fields (e.g. task_name, model_type) that should be
22
+ # injected into format_str alongside the per-step metrics.
23
+ self.static_format_kwargs = dict(static_format_kwargs or {})
24
+ self.path_value_map = dict()
25
+
26
+ def get_ckpt_path(self, data: Dict[str, float]) -> Optional[str]:
27
+ if self.k == 0:
28
+ return None
29
+
30
+ value = data[self.monitor_key]
31
+ format_kwargs = {**self.static_format_kwargs, **data}
32
+ ckpt_path = os.path.join(
33
+ self.save_dir, self.format_str.format(**format_kwargs))
34
+
35
+ if len(self.path_value_map) < self.k:
36
+ # under-capacity
37
+ self.path_value_map[ckpt_path] = value
38
+ return ckpt_path
39
+
40
+ # at capacity
41
+ sorted_map = sorted(self.path_value_map.items(), key=lambda x: x[1])
42
+ min_path, min_value = sorted_map[0]
43
+ max_path, max_value = sorted_map[-1]
44
+
45
+ delete_path = None
46
+ if self.mode == 'max':
47
+ if value > min_value:
48
+ delete_path = min_path
49
+ else:
50
+ if value < max_value:
51
+ delete_path = max_path
52
+
53
+ if delete_path is None:
54
+ return None
55
+ else:
56
+ del self.path_value_map[delete_path]
57
+ self.path_value_map[ckpt_path] = value
58
+
59
+ if not os.path.exists(self.save_dir):
60
+ os.mkdir(self.save_dir)
61
+
62
+ if os.path.exists(delete_path):
63
+ os.remove(delete_path)
64
+ return ckpt_path
@@ -0,0 +1,117 @@
1
+ from typing import Optional, Callable, Any, Sequence
2
+ import os
3
+ import copy
4
+ import json
5
+ import numbers
6
+ import pandas as pd
7
+
8
+
9
+ def read_json_log(path: str,
10
+ required_keys: Sequence[str]=tuple(),
11
+ **kwargs) -> pd.DataFrame:
12
+ """
13
+ Read json-per-line file, with potentially incomplete lines.
14
+ kwargs passed to pd.read_json
15
+ """
16
+ lines = list()
17
+ with open(path, 'r') as f:
18
+ while True:
19
+ # one json per line
20
+ line = f.readline()
21
+ if len(line) == 0:
22
+ # EOF
23
+ break
24
+ elif not line.endswith('\n'):
25
+ # incomplete line
26
+ break
27
+ is_relevant = False
28
+ for k in required_keys:
29
+ if k in line:
30
+ is_relevant = True
31
+ break
32
+ if is_relevant:
33
+ lines.append(line)
34
+ if len(lines) < 1:
35
+ return pd.DataFrame()
36
+ json_buf = f'[{",".join([line for line in (line.strip() for line in lines) if line])}]'
37
+ df = pd.read_json(json_buf, **kwargs)
38
+ return df
39
+
40
+ class JsonLogger:
41
+ def __init__(self, path: str,
42
+ filter_fn: Optional[Callable[[str,Any],bool]]=None):
43
+ if filter_fn is None:
44
+ filter_fn = lambda k,v: isinstance(v, numbers.Number)
45
+
46
+ # default to append mode
47
+ self.path = path
48
+ self.filter_fn = filter_fn
49
+ self.file = None
50
+ self.last_log = None
51
+
52
+ def start(self):
53
+ # use line buffering
54
+ try:
55
+ self.file = file = open(self.path, 'r+', buffering=1)
56
+ except FileNotFoundError:
57
+ self.file = file = open(self.path, 'w+', buffering=1)
58
+
59
+ # Move the pointer (similar to a cursor in a text editor) to the end of the file
60
+ pos = file.seek(0, os.SEEK_END)
61
+
62
+ # Read each character in the file one at a time from the last
63
+ # character going backwards, searching for a newline character
64
+ # If we find a new line, exit the search
65
+ while pos > 0 and file.read(1) != "\n":
66
+ pos -= 1
67
+ file.seek(pos, os.SEEK_SET)
68
+ # now the file pointer is at one past the last '\n'
69
+ # and pos is at the last '\n'.
70
+ last_line_end = file.tell()
71
+
72
+ # find the start of second last line
73
+ pos = max(0, pos-1)
74
+ file.seek(pos, os.SEEK_SET)
75
+ while pos > 0 and file.read(1) != "\n":
76
+ pos -= 1
77
+ file.seek(pos, os.SEEK_SET)
78
+ # now the file pointer is at one past the second last '\n'
79
+ last_line_start = file.tell()
80
+
81
+ if last_line_start < last_line_end:
82
+ # has last line of json
83
+ last_line = file.readline()
84
+ self.last_log = json.loads(last_line)
85
+
86
+ # remove the last incomplete line
87
+ file.seek(last_line_end)
88
+ file.truncate()
89
+
90
+ def stop(self):
91
+ self.file.close()
92
+ self.file = None
93
+
94
+ def __enter__(self):
95
+ self.start()
96
+ return self
97
+
98
+ def __exit__(self, exc_type, exc_val, exc_tb):
99
+ self.stop()
100
+
101
+ def log(self, data: dict):
102
+ filtered_data = dict(
103
+ filter(lambda x: self.filter_fn(*x), data.items()))
104
+ # save current as last log
105
+ self.last_log = filtered_data
106
+ for k, v in filtered_data.items():
107
+ if isinstance(v, numbers.Integral):
108
+ filtered_data[k] = int(v)
109
+ elif isinstance(v, numbers.Number):
110
+ filtered_data[k] = float(v)
111
+ buf = json.dumps(filtered_data)
112
+ # ensure one line per json
113
+ buf = buf.replace('\n','') + '\n'
114
+ self.file.write(buf)
115
+
116
+ def get_last_log(self):
117
+ return copy.deepcopy(self.last_log)
@@ -0,0 +1,251 @@
1
+ from diffusion_policy.model.common.normalizer import SingleFieldLinearNormalizer
2
+ from diffusion_policy.common.pytorch_util import dict_apply, dict_apply_reduce, dict_apply_split
3
+ import numpy as np
4
+ import torch
5
+
6
+
7
+ def get_range_normalizer_from_stat(stat, output_max=1, output_min=-1, range_eps=1e-7):
8
+ # -1, 1 normalization
9
+ input_max = stat['max']
10
+ input_min = stat['min']
11
+ input_range = input_max - input_min
12
+ ignore_dim = input_range < range_eps
13
+ input_range[ignore_dim] = output_max - output_min
14
+ scale = (output_max - output_min) / input_range
15
+ offset = output_min - scale * input_min
16
+ offset[ignore_dim] = (output_max + output_min) / 2 - input_min[ignore_dim]
17
+
18
+ return SingleFieldLinearNormalizer.create_manual(
19
+ scale=scale,
20
+ offset=offset,
21
+ input_stats_dict=stat
22
+ )
23
+
24
+ def get_image_range_normalizer():
25
+ scale = np.array([2], dtype=np.float32)
26
+ offset = np.array([-1], dtype=np.float32)
27
+ stat = {
28
+ 'min': np.array([0], dtype=np.float32),
29
+ 'max': np.array([1], dtype=np.float32),
30
+ 'mean': np.array([0.5], dtype=np.float32),
31
+ 'std': np.array([np.sqrt(1/12)], dtype=np.float32)
32
+ }
33
+ return SingleFieldLinearNormalizer.create_manual(
34
+ scale=scale,
35
+ offset=offset,
36
+ input_stats_dict=stat
37
+ )
38
+
39
+ def get_image_identity_normalizer():
40
+ scale = np.array([1], dtype=np.float32)
41
+ offset = np.array([0], dtype=np.float32)
42
+ stat = {
43
+ 'min': np.array([0], dtype=np.float32),
44
+ 'max': np.array([1], dtype=np.float32),
45
+ 'mean': np.array([0.5], dtype=np.float32),
46
+ 'std': np.array([np.sqrt(1/12)], dtype=np.float32)
47
+ }
48
+ return SingleFieldLinearNormalizer.create_manual(
49
+ scale=scale,
50
+ offset=offset,
51
+ input_stats_dict=stat
52
+ )
53
+
54
+ def get_identity_normalizer_from_stat(stat):
55
+ scale = np.ones_like(stat['min'])
56
+ offset = np.zeros_like(stat['min'])
57
+ return SingleFieldLinearNormalizer.create_manual(
58
+ scale=scale,
59
+ offset=offset,
60
+ input_stats_dict=stat
61
+ )
62
+
63
+ def robomimic_abs_action_normalizer_from_stat(stat, rotation_transformer):
64
+ result = dict_apply_split(
65
+ stat, lambda x: {
66
+ 'pos': x[...,:3],
67
+ 'rot': x[...,3:6],
68
+ 'gripper': x[...,6:]
69
+ })
70
+
71
+ def get_pos_param_info(stat, output_max=1, output_min=-1, range_eps=1e-7):
72
+ # -1, 1 normalization
73
+ input_max = stat['max']
74
+ input_min = stat['min']
75
+ input_range = input_max - input_min
76
+ ignore_dim = input_range < range_eps
77
+ input_range[ignore_dim] = output_max - output_min
78
+ scale = (output_max - output_min) / input_range
79
+ offset = output_min - scale * input_min
80
+ offset[ignore_dim] = (output_max + output_min) / 2 - input_min[ignore_dim]
81
+
82
+ return {'scale': scale, 'offset': offset}, stat
83
+
84
+ def get_rot_param_info(stat):
85
+ example = rotation_transformer.forward(stat['mean'])
86
+ scale = np.ones_like(example)
87
+ offset = np.zeros_like(example)
88
+ info = {
89
+ 'max': np.ones_like(example),
90
+ 'min': np.full_like(example, -1),
91
+ 'mean': np.zeros_like(example),
92
+ 'std': np.ones_like(example)
93
+ }
94
+ return {'scale': scale, 'offset': offset}, info
95
+
96
+ def get_gripper_param_info(stat):
97
+ example = stat['max']
98
+ scale = np.ones_like(example)
99
+ offset = np.zeros_like(example)
100
+ info = {
101
+ 'max': np.ones_like(example),
102
+ 'min': np.full_like(example, -1),
103
+ 'mean': np.zeros_like(example),
104
+ 'std': np.ones_like(example)
105
+ }
106
+ return {'scale': scale, 'offset': offset}, info
107
+
108
+ pos_param, pos_info = get_pos_param_info(result['pos'])
109
+ rot_param, rot_info = get_rot_param_info(result['rot'])
110
+ gripper_param, gripper_info = get_gripper_param_info(result['gripper'])
111
+
112
+ param = dict_apply_reduce(
113
+ [pos_param, rot_param, gripper_param],
114
+ lambda x: np.concatenate(x,axis=-1))
115
+ info = dict_apply_reduce(
116
+ [pos_info, rot_info, gripper_info],
117
+ lambda x: np.concatenate(x,axis=-1))
118
+
119
+ return SingleFieldLinearNormalizer.create_manual(
120
+ scale=param['scale'],
121
+ offset=param['offset'],
122
+ input_stats_dict=info
123
+ )
124
+
125
+
126
+ def robomimic_abs_action_only_normalizer_from_stat(stat):
127
+ result = dict_apply_split(
128
+ stat, lambda x: {
129
+ 'pos': x[...,:3],
130
+ 'other': x[...,3:]
131
+ })
132
+
133
+ def get_pos_param_info(stat, output_max=1, output_min=-1, range_eps=1e-7):
134
+ # -1, 1 normalization
135
+ input_max = stat['max']
136
+ input_min = stat['min']
137
+ input_range = input_max - input_min
138
+ ignore_dim = input_range < range_eps
139
+ input_range[ignore_dim] = output_max - output_min
140
+ scale = (output_max - output_min) / input_range
141
+ offset = output_min - scale * input_min
142
+ offset[ignore_dim] = (output_max + output_min) / 2 - input_min[ignore_dim]
143
+
144
+ return {'scale': scale, 'offset': offset}, stat
145
+
146
+
147
+ def get_other_param_info(stat):
148
+ example = stat['max']
149
+ scale = np.ones_like(example)
150
+ offset = np.zeros_like(example)
151
+ info = {
152
+ 'max': np.ones_like(example),
153
+ 'min': np.full_like(example, -1),
154
+ 'mean': np.zeros_like(example),
155
+ 'std': np.ones_like(example)
156
+ }
157
+ return {'scale': scale, 'offset': offset}, info
158
+
159
+ pos_param, pos_info = get_pos_param_info(result['pos'])
160
+ other_param, other_info = get_other_param_info(result['other'])
161
+
162
+ param = dict_apply_reduce(
163
+ [pos_param, other_param],
164
+ lambda x: np.concatenate(x,axis=-1))
165
+ info = dict_apply_reduce(
166
+ [pos_info, other_info],
167
+ lambda x: np.concatenate(x,axis=-1))
168
+
169
+ return SingleFieldLinearNormalizer.create_manual(
170
+ scale=param['scale'],
171
+ offset=param['offset'],
172
+ input_stats_dict=info
173
+ )
174
+
175
+
176
+ def robomimic_abs_action_only_dual_arm_normalizer_from_stat(stat):
177
+ Da = stat['max'].shape[-1]
178
+ Dah = Da // 2
179
+ result = dict_apply_split(
180
+ stat, lambda x: {
181
+ 'pos0': x[...,:3],
182
+ 'other0': x[...,3:Dah],
183
+ 'pos1': x[...,Dah:Dah+3],
184
+ 'other1': x[...,Dah+3:]
185
+ })
186
+
187
+ def get_pos_param_info(stat, output_max=1, output_min=-1, range_eps=1e-7):
188
+ # -1, 1 normalization
189
+ input_max = stat['max']
190
+ input_min = stat['min']
191
+ input_range = input_max - input_min
192
+ ignore_dim = input_range < range_eps
193
+ input_range[ignore_dim] = output_max - output_min
194
+ scale = (output_max - output_min) / input_range
195
+ offset = output_min - scale * input_min
196
+ offset[ignore_dim] = (output_max + output_min) / 2 - input_min[ignore_dim]
197
+
198
+ return {'scale': scale, 'offset': offset}, stat
199
+
200
+
201
+ def get_other_param_info(stat):
202
+ example = stat['max']
203
+ scale = np.ones_like(example)
204
+ offset = np.zeros_like(example)
205
+ info = {
206
+ 'max': np.ones_like(example),
207
+ 'min': np.full_like(example, -1),
208
+ 'mean': np.zeros_like(example),
209
+ 'std': np.ones_like(example)
210
+ }
211
+ return {'scale': scale, 'offset': offset}, info
212
+
213
+ pos0_param, pos0_info = get_pos_param_info(result['pos0'])
214
+ pos1_param, pos1_info = get_pos_param_info(result['pos1'])
215
+ other0_param, other0_info = get_other_param_info(result['other0'])
216
+ other1_param, other1_info = get_other_param_info(result['other1'])
217
+
218
+ param = dict_apply_reduce(
219
+ [pos0_param, other0_param, pos1_param, other1_param],
220
+ lambda x: np.concatenate(x,axis=-1))
221
+ info = dict_apply_reduce(
222
+ [pos0_info, other0_info, pos1_info, other1_info],
223
+ lambda x: np.concatenate(x,axis=-1))
224
+
225
+ return SingleFieldLinearNormalizer.create_manual(
226
+ scale=param['scale'],
227
+ offset=param['offset'],
228
+ input_stats_dict=info
229
+ )
230
+
231
+
232
+ def array_to_stats(arr: np.ndarray):
233
+ stat = {
234
+ 'min': np.min(arr, axis=0),
235
+ 'max': np.max(arr, axis=0),
236
+ 'mean': np.mean(arr, axis=0),
237
+ 'std': np.std(arr, axis=0)
238
+ }
239
+ return stat
240
+
241
+ def concatenate_normalizer(normalizers: list):
242
+ scale = torch.concatenate([normalizer.params_dict['scale'] for normalizer in normalizers], axis=-1)
243
+ offset = torch.concatenate([normalizer.params_dict['offset'] for normalizer in normalizers], axis=-1)
244
+ input_stats_dict = dict_apply_reduce(
245
+ [normalizer.params_dict['input_stats'] for normalizer in normalizers],
246
+ lambda x: torch.concatenate(x,axis=-1))
247
+ return SingleFieldLinearNormalizer.create_manual(
248
+ scale=scale,
249
+ offset=offset,
250
+ input_stats_dict=input_stats_dict
251
+ )
@@ -0,0 +1,112 @@
1
+ import numpy as np
2
+
3
+
4
+ def compute_relative_pose(pos, rot, base_pos, base_rot_mat,
5
+ rot_transformer_to_mat,
6
+ rot_transformer_to_target,
7
+ backward=False,
8
+ delta=False):
9
+ if not backward:
10
+ # forward pass
11
+ if not delta:
12
+ output_pos = pos if base_pos is None else pos - base_pos
13
+ output_rot = rot_transformer_to_target.forward(
14
+ rot_transformer_to_mat.forward(rot) @ np.linalg.inv(base_rot_mat))
15
+ return output_pos, output_rot
16
+ else:
17
+ all_pos = np.concatenate([base_pos[None,...], pos], axis=0)
18
+ output_pos = np.diff(all_pos, axis=0)
19
+
20
+ rot_mat = rot_transformer_to_mat.forward(rot)
21
+ all_rot_mat = np.concatenate([base_rot_mat[None,...], rot_mat], axis=0)
22
+ prev_rot = np.linalg.inv(all_rot_mat[:-1])
23
+ curr_rot = all_rot_mat[1:]
24
+ rot = np.matmul(curr_rot, prev_rot)
25
+ output_rot = rot_transformer_to_target.forward(rot)
26
+ return output_pos, output_rot
27
+
28
+ else:
29
+ # backward pass
30
+ if not delta:
31
+ output_pos = pos if base_pos is None else pos + base_pos
32
+ output_rot = rot_transformer_to_mat.inverse(
33
+ rot_transformer_to_target.inverse(rot) @ base_rot_mat)
34
+ return output_pos, output_rot
35
+ else:
36
+ output_pos = np.cumsum(pos, axis=0) + base_pos
37
+
38
+ rot_mat = rot_transformer_to_target.inverse(rot)
39
+ output_rot_mat = np.zeros_like(rot_mat)
40
+ curr_rot = base_rot_mat
41
+ for i in range(len(rot_mat)):
42
+ curr_rot = rot_mat[i] @ curr_rot
43
+ output_rot_mat[i] = curr_rot
44
+ output_rot = rot_transformer_to_mat.inverse(rot)
45
+ return output_pos, output_rot
46
+
47
+
48
+ def convert_pose_mat_rep(pose_mat, base_pose_mat, pose_rep='abs', backward=False):
49
+ if not backward:
50
+ # training transform
51
+ if pose_rep == 'abs':
52
+ return pose_mat
53
+ elif pose_rep == 'rel':
54
+ # legacy buggy implementation
55
+ # for compatibility
56
+ pos = pose_mat[...,:3,3] - base_pose_mat[:3,3]
57
+ rot = pose_mat[...,:3,:3] @ np.linalg.inv(base_pose_mat[:3,:3])
58
+ out = np.copy(pose_mat)
59
+ out[...,:3,:3] = rot
60
+ out[...,:3,3] = pos
61
+ return out
62
+ elif pose_rep == 'relative':
63
+ out = np.linalg.inv(base_pose_mat) @ pose_mat
64
+ return out
65
+ elif pose_rep == 'delta':
66
+ all_pos = np.concatenate([base_pose_mat[None,:3,3], pose_mat[...,:3,3]], axis=0)
67
+ out_pos = np.diff(all_pos, axis=0)
68
+
69
+ all_rot_mat = np.concatenate([base_pose_mat[None,:3,:3], pose_mat[...,:3,:3]], axis=0)
70
+ prev_rot = np.linalg.inv(all_rot_mat[:-1])
71
+ curr_rot = all_rot_mat[1:]
72
+ out_rot = np.matmul(curr_rot, prev_rot)
73
+
74
+ out = np.copy(pose_mat)
75
+ out[...,:3,:3] = out_rot
76
+ out[...,:3,3] = out_pos
77
+ return out
78
+ else:
79
+ raise RuntimeError(f"Unsupported pose_rep: {pose_rep}")
80
+
81
+ else:
82
+ # eval transform
83
+ if pose_rep == 'abs':
84
+ return pose_mat
85
+ elif pose_rep == 'rel':
86
+ # legacy buggy implementation
87
+ # for compatibility
88
+ pos = pose_mat[...,:3,3] + base_pose_mat[:3,3]
89
+ rot = pose_mat[...,:3,:3] @ base_pose_mat[:3,:3]
90
+ out = np.copy(pose_mat)
91
+ out[...,:3,:3] = rot
92
+ out[...,:3,3] = pos
93
+ return out
94
+ elif pose_rep == 'relative':
95
+ out = base_pose_mat @ pose_mat
96
+ return out
97
+ elif pose_rep == 'delta':
98
+ output_pos = np.cumsum(pose_mat[...,:3,3], axis=0) + base_pose_mat[:3,3]
99
+
100
+ output_rot_mat = np.zeros_like(pose_mat[...,:3,:3])
101
+ curr_rot = base_pose_mat[:3,:3]
102
+ for i in range(len(pose_mat)):
103
+ curr_rot = pose_mat[i,:3,:3] @ curr_rot
104
+ output_rot_mat[i] = curr_rot
105
+
106
+ out = np.copy(pose_mat)
107
+ out[...,:3,:3] = output_rot_mat
108
+ out[...,:3,3] = output_pos
109
+ return out
110
+ else:
111
+ raise RuntimeError(f"Unsupported pose_rep: {pose_rep}")
112
+
@@ -0,0 +1,82 @@
1
+ from typing import Dict, Callable, List
2
+ import collections
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+ def dict_apply(
7
+ x: Dict[str, torch.Tensor],
8
+ func: Callable[[torch.Tensor], torch.Tensor]
9
+ ) -> Dict[str, torch.Tensor]:
10
+ result = dict()
11
+ for key, value in x.items():
12
+ if isinstance(value, dict):
13
+ result[key] = dict_apply(value, func)
14
+ else:
15
+ result[key] = func(value)
16
+ return result
17
+
18
+ def pad_remaining_dims(x, target):
19
+ assert x.shape == target.shape[:len(x.shape)]
20
+ return x.reshape(x.shape + (1,)*(len(target.shape) - len(x.shape)))
21
+
22
+ def dict_apply_split(
23
+ x: Dict[str, torch.Tensor],
24
+ split_func: Callable[[torch.Tensor], Dict[str, torch.Tensor]]
25
+ ) -> Dict[str, torch.Tensor]:
26
+ results = collections.defaultdict(dict)
27
+ for key, value in x.items():
28
+ result = split_func(value)
29
+ for k, v in result.items():
30
+ results[k][key] = v
31
+ return results
32
+
33
+ def dict_apply_reduce(
34
+ x: List[Dict[str, torch.Tensor]],
35
+ reduce_func: Callable[[List[torch.Tensor]], torch.Tensor]
36
+ ) -> Dict[str, torch.Tensor]:
37
+ result = dict()
38
+ for key in x[0].keys():
39
+ result[key] = reduce_func([x_[key] for x_ in x])
40
+ return result
41
+
42
+
43
+ def replace_submodules(
44
+ root_module: nn.Module,
45
+ predicate: Callable[[nn.Module], bool],
46
+ func: Callable[[nn.Module], nn.Module]) -> nn.Module:
47
+ """
48
+ predicate: Return true if the module is to be replaced.
49
+ func: Return new module to use.
50
+ """
51
+ if predicate(root_module):
52
+ return func(root_module)
53
+
54
+ bn_list = [k.split('.') for k, m
55
+ in root_module.named_modules(remove_duplicate=True)
56
+ if predicate(m)]
57
+ for *parent, k in bn_list:
58
+ parent_module = root_module
59
+ if len(parent) > 0:
60
+ parent_module = root_module.get_submodule('.'.join(parent))
61
+ if isinstance(parent_module, nn.Sequential):
62
+ src_module = parent_module[int(k)]
63
+ else:
64
+ src_module = getattr(parent_module, k)
65
+ tgt_module = func(src_module)
66
+ if isinstance(parent_module, nn.Sequential):
67
+ parent_module[int(k)] = tgt_module
68
+ else:
69
+ setattr(parent_module, k, tgt_module)
70
+ # verify that all BN are replaced
71
+ bn_list = [k.split('.') for k, m
72
+ in root_module.named_modules(remove_duplicate=True)
73
+ if predicate(m)]
74
+ assert len(bn_list) == 0
75
+ return root_module
76
+
77
+ def optimizer_to(optimizer, device):
78
+ for state in optimizer.state.values():
79
+ for k, v in state.items():
80
+ if isinstance(v, torch.Tensor):
81
+ state[k] = v.to(device=device)
82
+ return optimizer