0xZohar commited on
Commit
27bc948
·
verified ·
1 Parent(s): 6f73dc8

Add code/cube3d/training/process_single_ldr.py

Browse files
code/cube3d/training/process_single_ldr.py ADDED
@@ -0,0 +1,1050 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import os
3
+ import json
4
+ from scipy.spatial.transform import Rotation as R
5
+
6
+ import numpy as np
7
+ from scipy.spatial.transform import Rotation as R
8
+
9
+ # Import centralized configuration for path management
10
+ try:
11
+ from cube3d.config import get_mapping_paths
12
+ print("Successfully imported config from cube3d.config")
13
+ except ImportError as e:
14
+ print(f"Import from cube3d.config failed: {e}")
15
+ try:
16
+ from config import get_mapping_paths
17
+ print("Successfully imported config from config")
18
+ except ImportError as e:
19
+ print(f"Failed to import config: {e}")
20
+ raise ImportError("Failed to import get_mapping_paths from cube3d.config or config")
21
+
22
+ # from cube3d.training.check_rotation_onehot import rot_to_onehot24, onehot24_to_rot
23
+ # #from check_rotation_onehot import rot_to_onehot24, onehot24_to_rot
24
+ try:
25
+ from cube3d.training.check_rotation_onehot import (
26
+ rot_to_onehot24,
27
+ onehot24_to_rot,
28
+ signed_perm_mats_det_plus_1
29
+ )
30
+ print("Successfully imported from cube3d.training.check_rotation_onehot")
31
+ except ImportError as e:
32
+ print(f"Import from cube3d.training.check_rotation_onehot failed: {e}")
33
+ try:
34
+ from check_rotation_onehot import (
35
+ rot_to_onehot24,
36
+ onehot24_to_rot,
37
+ signed_perm_mats_det_plus_1
38
+ )
39
+ print("Successfully imported from check_rotation_onehot")
40
+ except ImportError as e:
41
+ print(f"Import from check_rotation_onehot failed: {e}")
42
+ raise ImportError(
43
+ "Failed to import rot_to_onehot24, onehot24_to_rot, "
44
+ "and signed_perm_mats_det_plus_1 from both "
45
+ "cube3d.training.check_rotation_onehot and check_rotation_onehot"
46
+ )
47
+
48
+ def rotation_to_onehot(rotation_matrix):
49
+ possible_angles = [0, 90, 180, 270]
50
+
51
+ # 初始化一个64维的one-hot编码
52
+ one_hot = np.zeros(64)
53
+ #import ipdb; ipdb.set_trace()
54
+ try:
55
+ #import ipdb; ipdb.set_trace()
56
+ x_angle = possible_angles.index(round(np.arctan2(np.round(rotation_matrix[2, 1], 1), np.round(rotation_matrix[2, 2],1)) * 180 / np.pi) % 360)
57
+ y_angle = possible_angles.index(round(np.arctan2(np.round(rotation_matrix[2, 0], 1), np.round(rotation_matrix[2, 2],1)) * 180 / np.pi) % 360)
58
+ z_angle = possible_angles.index(round(np.arctan2(np.round(rotation_matrix[1, 0], 1), np.round(rotation_matrix[0, 0],1)) * 180 / np.pi) % 360)
59
+
60
+ # 根据x, y, z的旋转角度组合确定one-hot的索引
61
+ index = x_angle * 16 + y_angle * 4 + z_angle
62
+
63
+ except Exception as e:
64
+
65
+
66
+ modified_matrix = rotation_matrix.copy()
67
+ max_vals = np.max(np.abs(modified_matrix), axis=1) # 每行最大绝对值
68
+ sign_matrix = np.sign(modified_matrix)
69
+
70
+ modified_matrix = sign_matrix * (np.abs(modified_matrix) == max_vals[:, None])
71
+
72
+ x_angle = possible_angles.index(round(np.arctan2(np.round(modified_matrix[2, 1], 1), np.round(modified_matrix[2, 2],1)) * 180 / np.pi) % 360)
73
+ y_angle = possible_angles.index(round(np.arctan2(np.round(modified_matrix[2, 0], 1), np.round(modified_matrix[2, 2],1)) * 180 / np.pi) % 360)
74
+ z_angle = possible_angles.index(round(np.arctan2(np.round(modified_matrix[1, 0], 1), np.round(modified_matrix[0, 0],1)) * 180 / np.pi) % 360)
75
+ index = x_angle * 16 + y_angle * 4 + z_angle
76
+ if index >= 64:
77
+ print(f"Error occurred: {e}")
78
+
79
+ with open("rotation_matrix_300_afterroundafter1_error_log.txt", "a") as file: # 使用 "a" 模式追加内容
80
+ file.write(f"Error with rotation matrix:\n{np.round(modified_matrix, 1)}\n")
81
+ file.write("-" * 50 + "\n") # 可选:添加分隔符,帮助区分不同错误
82
+
83
+
84
+ one_hot[index] = 1
85
+
86
+ return one_hot
87
+
88
+ import numpy as np
89
+
90
+ def onehot_to_rotation(one_hot):
91
+ # 定义可能的角度
92
+ possible_angles = [0, 90, 180, 270]
93
+
94
+ # 获取one-hot编码中的索引位置
95
+ index = one_hot.argmax() # 找到值为1的那个位置,即索引
96
+
97
+ # 根据索引推导出x、y、z的角度
98
+ x_angle = possible_angles[(index // 16) % 4] # x轴旋转角度
99
+ y_angle = possible_angles[(index // 4) % 4] # y轴旋转角度
100
+ z_angle = possible_angles[index % 4] # z轴旋转角度
101
+
102
+ # 根据这些角度构建旋转矩阵
103
+ # 构建绕X轴旋转矩阵
104
+ Rx = np.array([[1, 0, 0],
105
+ [0, np.cos(np.radians(x_angle)), -np.sin(np.radians(x_angle))],
106
+ [0, np.sin(np.radians(x_angle)), np.cos(np.radians(x_angle))]])
107
+
108
+ # 构建绕Y轴旋转矩阵
109
+ Ry = np.array([[np.cos(np.radians(y_angle)), 0, np.sin(np.radians(y_angle))],
110
+ [0, 1, 0],
111
+ [-np.sin(np.radians(y_angle)), 0, np.cos(np.radians(y_angle))]])
112
+
113
+ # 构建绕Z轴旋转矩阵
114
+ Rz = np.array([[np.cos(np.radians(z_angle)), -np.sin(np.radians(z_angle)), 0],
115
+ [np.sin(np.radians(z_angle)), np.cos(np.radians(z_angle)), 0],
116
+ [0, 0, 1]])
117
+
118
+ # 将这三个矩阵相乘得到总的旋转矩阵
119
+ rotation_matrix = np.dot(Rz, np.dot(Ry, Rx))
120
+
121
+ return rotation_matrix
122
+
123
+ def load_mappings(label_mapping_file, label_inverse_mapping_file):
124
+ with open(label_mapping_file, 'r') as f:
125
+ label_mapping = json.load(f)
126
+
127
+ with open(label_inverse_mapping_file, 'r') as f:
128
+ label_inverse_mapping = json.load(f)
129
+
130
+ return label_mapping, label_inverse_mapping
131
+
132
+ # 读取LDR文件,逐行读取
133
+ def read_ldr_file(file_path):
134
+ with open(file_path, 'r') as f:
135
+ return f.readlines()
136
+
137
+ # 处理LDR文件并提取数据
138
+ def process_ldr_data(lines):
139
+ data = []
140
+ filenames = []
141
+
142
+ all_coords = []
143
+ all_colors = []
144
+ all_labels = []
145
+ label_mapping = {}
146
+ label_inverse_mapping = {}
147
+
148
+ # Use config-based paths (works in both local and HF Space environments)
149
+ forward_path, inverse_path = get_mapping_paths("subset_self")
150
+ label_mapping, label_inverse_mapping = load_mappings(forward_path, inverse_path)
151
+ label_counter = 0
152
+
153
+
154
+ max_x = 212
155
+ max_y = 216
156
+ max_z = 528
157
+
158
+ for line in lines:
159
+ if line.startswith('1'): # 只处理零件数据行
160
+ parts = line.split() # 按空格分割每一列数据
161
+ # if len(parts) != 15: # 检查是否每行都有15个部分 #这里也有问题
162
+ # print(f"Skipping line due to unexpected length: {line.strip()}")
163
+ # continue # 如果数据不完整,则跳过该行
164
+
165
+ color = int(parts[1]) # 颜色
166
+ x, y, z = round(float(parts[2])), round(float(parts[3])), round(float(parts[4]))
167
+ rx = list(map(float, parts[5:8])) # 旋转矩阵第一行
168
+ ry = list(map(float, parts[8:11])) # 旋转矩阵第二行
169
+ rz = list(map(float, parts[11:14])) # 旋转矩阵第三行
170
+ filename = parts[14] # 文件名
171
+
172
+ all_coords.append([x, y, z])
173
+ all_colors.append(color)
174
+
175
+ if ".DAT" in filename:
176
+ filename = filename.replace(".DAT", ".dat")
177
+ if filename not in label_mapping:
178
+ # import ipdb; ipdb.set_trace()
179
+ label_mapping[filename] = label_counter
180
+ label_inverse_mapping[label_counter] = filename
181
+ label_counter += 1
182
+ all_labels.append(label_mapping[filename])
183
+
184
+ rotation_matrix = np.array([rx + ry + rz]).reshape(3, 3)
185
+ # r = R.from_matrix(rotation_matrix)
186
+ # quaternion = r.as_quat()
187
+
188
+ rotation_onehot, _ = rot_to_onehot24(rotation_matrix)
189
+ #data.append([color, x, y, z] + rotation_onehot.tolist())
190
+ #data.append([x, y, z] + rotation_onehot.tolist())
191
+ data.append(rotation_onehot.tolist())
192
+ #data.append([x, y, z] + quaternion.tolist())
193
+ filenames.append(filename)
194
+
195
+ all_coords = np.array(all_coords)
196
+
197
+ # min_vals = np.min(all_coords, axis=0) # 每个坐标轴的最小值
198
+ # max_vals = np.max(all_coords, axis=0) # 每个坐标轴的最大值
199
+
200
+ # print(max_vals, min_vals)
201
+ # normalized_coords = (all_coords - min_vals) / (max_vals - min_vals)
202
+
203
+ # normalized_coords = 2 * normalized_coords - 1
204
+ # for i, entry in enumerate(data):
205
+ # entry[0:3] = normalized_coords[i] # 更新 x, y, z 坐标
206
+
207
+ one_hot_x = np.eye(max_x+1)[all_coords[:, 0].astype(int)]
208
+ one_hot_y = np.eye(max_y+1)[all_coords[:, 1].astype(int)]
209
+ one_hot_z = np.eye(max_z+1)[all_coords[:, 2].astype(int)]
210
+
211
+ for i, entry in enumerate(data):
212
+ #entry.append(normalized_labels[i]) # 添加标准化标签到数据中
213
+ entry.extend(np.concatenate([one_hot_x[i], one_hot_y[i], one_hot_z[i]])) # Using numpy to concatenate
214
+ # color_min = np.min(all_colors)
215
+ # color_max = np.max(all_colors)
216
+ # normalized_colors = (np.array(all_colors) - color_min) / (color_max - color_min)
217
+
218
+ # 更新数据:将每个零件的颜色替换为标准化后的颜色
219
+ # for i, entry in enumerate(data):
220
+ # entry[0] = normalized_colors[i] # 更新颜色
221
+
222
+ if label_mapping is None:
223
+ label_mapping = {filename: idx for idx, filename in enumerate(sorted(set(all_labels)))}
224
+ else:
225
+ label_mapping = label_mapping
226
+
227
+ #all_labels = [label_mapping[label] for label in all_labels]
228
+ #label_min = np.min(all_labels) # 获取标签的最小值
229
+ #label_max = np.max(all_labels) # 获取标签的最大值
230
+ label_max = len(label_mapping) # 获取标签的最大值
231
+
232
+ # 将标签标准化到 [0, 1] 范围
233
+ #normalized_labels = (all_labels - label_min) / (label_max - label_min)
234
+ one_hot_labels = np.eye(label_max)[all_labels]
235
+
236
+
237
+ # 更新数据:将每个零件的标签替换为标准化后的标签
238
+ for i, entry in enumerate(data):
239
+ #entry.append(normalized_labels[i]) # 添加标准化标签到数据中
240
+ entry.extend(one_hot_labels[i]) # 添加one-hot编码标签到数据中
241
+
242
+ for i, entry in enumerate(data):
243
+ #entry.append(normalized_labels[i]) # 添加标准化标签到数据中
244
+ entry.extend([1,0]) #
245
+ #import ipdb; ipdb.set_trace()
246
+ return np.array(data), label_inverse_mapping # 将数据转换为NumPy数组
247
+
248
+ def process_ldr_flatten(lines):
249
+ data = []
250
+ filenames = []
251
+
252
+ all_coords = []
253
+ all_colors = []
254
+ all_labels = []
255
+ label_mapping = {}
256
+ label_inverse_mapping = {}
257
+
258
+ # Use config-based paths (works in both local and HF Space environments)
259
+ forward_path, inverse_path = get_mapping_paths("subset_1k")
260
+ label_mapping, label_inverse_mapping = load_mappings(forward_path, inverse_path)
261
+ label_counter = 0
262
+
263
+
264
+ max_x = 250
265
+ max_y = 214
266
+ max_z = 524
267
+
268
+ #print(lines)
269
+ for line in lines:
270
+ if line.startswith('1'):
271
+ parts = line.split()
272
+ #if len(parts) != 15:
273
+ # if len(parts) < 15:
274
+ # print(f"Skipping line due to unexpected length: {line.strip()}")
275
+ # continue
276
+ #print(parts)
277
+ color = int(parts[1])
278
+ x, y, z = round(float(parts[2])), round(float(parts[3])), round(float(parts[4]))
279
+ rx = list(map(float, parts[5:8]))
280
+ ry = list(map(float, parts[8:11]))
281
+ rz = list(map(float, parts[11:14]))
282
+ filename = parts[14].lower()
283
+
284
+ all_coords.append([x, y, z])
285
+ all_colors.append(color)
286
+
287
+ if ".DAT" in filename:
288
+ filename = filename.replace(".DAT", ".dat")
289
+ if filename not in label_mapping:
290
+ # import ipdb; ipdb.set_trace()
291
+ label_mapping[filename] = label_counter
292
+ label_inverse_mapping[label_counter] = filename
293
+ label_counter += 1
294
+ all_labels.append(label_mapping[filename])
295
+
296
+ rotation_matrix = np.array([rx + ry + rz]).reshape(3, 3)
297
+ # r = R.from_matrix(rotation_matrix)
298
+ # quaternion = r.as_quat()
299
+
300
+ rotation_onehot, _ = rot_to_onehot24(rotation_matrix)
301
+ rotation_id = rotation_onehot.argmax()
302
+ #data.append([color, x, y, z] + rotation_onehot.tolist())
303
+ #data.append([x, y, z] + rotation_onehot.tolist())
304
+ #data.append(rotation_onehot.tolist())
305
+ data.append([rotation_id])
306
+ #data.append([x, y, z] + quaternion.tolist())
307
+ filenames.append(filename)
308
+
309
+ all_coords = np.array(all_coords)
310
+
311
+ # min_vals = np.min(all_coords, axis=0)
312
+ # max_vals = np.max(all_coords, axis=0)
313
+
314
+ # print(max_vals, min_vals)
315
+ # normalized_coords = (all_coords - min_vals) / (max_vals - min_vals)
316
+
317
+ # normalized_coords = 2 * normalized_coords - 1
318
+ # for i, entry in enumerate(data):
319
+ # entry[0:3] = normalized_coords[i]
320
+
321
+ #print(all_coords)
322
+ one_hot_x = np.eye(max_x+1)[all_coords[:, 0].astype(int)]
323
+ one_hot_y = np.eye(max_y+1)[all_coords[:, 1].astype(int)]
324
+ one_hot_z = np.eye(max_z+1)[all_coords[:, 2].astype(int)]
325
+
326
+ # for i, entry in enumerate(data):
327
+ # #entry.append(normalized_labels[i])
328
+ # entry.extend(np.concatenate([one_hot_x[i], one_hot_y[i], one_hot_z[i]])) # Using numpy to concatenate
329
+ # color_min = np.min(all_colors)
330
+ # color_max = np.max(all_colors)
331
+ # normalized_colors = (np.array(all_colors) - color_min) / (color_max - color_min)
332
+
333
+ # for i, entry in enumerate(data):
334
+ # entry[0] = normalized_colors[i]
335
+
336
+ if label_mapping is None:
337
+ label_mapping = {filename: idx for idx, filename in enumerate(sorted(set(all_labels)))}
338
+ else:
339
+ label_mapping = label_mapping
340
+
341
+ #all_labels = [label_mapping[label] for label in all_labels]
342
+ #label_min = np.min(all_labels)
343
+ #label_max = np.max(all_labels)
344
+ label_max = len(label_mapping)
345
+
346
+ # 将标签标准化到 [0, 1] 范围
347
+ #normalized_labels = (all_labels - label_min) / (label_max - label_min)
348
+ one_hot_labels = np.eye(label_max)[all_labels]
349
+
350
+ for i, entry in enumerate(data):
351
+ #entry.append(normalized_labels[i]) #
352
+ #entry.extend(one_hot_labels[i]) #
353
+
354
+ entry.extend([all_labels[i]]) #
355
+ entry.extend(np.concatenate([
356
+ np.array([all_coords[i, 0].astype(int)]),
357
+ np.array([all_coords[i, 1].astype(int)]),
358
+ np.array([all_coords[i, 2].astype(int)])
359
+ ]))
360
+
361
+ for i, entry in enumerate(data):
362
+ #entry.append(normalized_labels[i])
363
+ entry.extend([1,0]) #
364
+ #import ipdb; ipdb.set_trace()
365
+ return np.array(data), label_inverse_mapping
366
+
367
+ def process_ldr_flatten_bottom(lines):
368
+ data = []
369
+ filenames = []
370
+
371
+ all_coords = []
372
+ all_colors = []
373
+ all_labels = []
374
+ label_mapping = {}
375
+ label_inverse_mapping = {}
376
+
377
+ label_mapping, label_inverse_mapping = load_mappings('/public/home/wangshuo/gap/assembly/data/car_1k/subset_bottom_300/label_mapping.json', '/public/home/wangshuo/gap/assembly/data/car_1k/subset_bottom_300/label_inverse_mapping.json')
378
+ label_counter = 0
379
+
380
+
381
+ max_x = 212
382
+ max_y = 72
383
+ max_z = 410
384
+
385
+ for line in lines:
386
+ if line.startswith('1'):
387
+ parts = line.split()
388
+ # if len(parts) != 15:
389
+ # print(f"Skipping line due to unexpected length: {line.strip()}")
390
+ # continue
391
+
392
+ color = int(parts[1])
393
+ x, y, z = round(float(parts[2])), round(float(parts[3])), round(float(parts[4]))
394
+ rx = list(map(float, parts[5:8]))
395
+ ry = list(map(float, parts[8:11]))
396
+ rz = list(map(float, parts[11:14]))
397
+ filename = parts[14].lower()
398
+
399
+ all_coords.append([x, y, z])
400
+ all_colors.append(color)
401
+
402
+ if ".DAT" in filename:
403
+ filename = filename.replace(".DAT", ".dat")
404
+ if filename not in label_mapping:
405
+ # import ipdb; ipdb.set_trace()
406
+ label_mapping[filename] = label_counter
407
+ label_inverse_mapping[label_counter] = filename
408
+ label_counter += 1
409
+ all_labels.append(label_mapping[filename])
410
+
411
+ rotation_matrix = np.array([rx + ry + rz]).reshape(3, 3)
412
+ # r = R.from_matrix(rotation_matrix)
413
+ # quaternion = r.as_quat()
414
+
415
+ rotation_onehot, _ = rot_to_onehot24(rotation_matrix)
416
+ rotation_id = rotation_onehot.argmax()
417
+ #data.append([color, x, y, z] + rotation_onehot.tolist())
418
+ #data.append([x, y, z] + rotation_onehot.tolist())
419
+ #data.append(rotation_onehot.tolist())
420
+ data.append([rotation_id])
421
+ #data.append([x, y, z] + quaternion.tolist())
422
+ filenames.append(filename)
423
+
424
+ all_coords = np.array(all_coords)
425
+
426
+ # min_vals = np.min(all_coords, axis=0)
427
+ # max_vals = np.max(all_coords, axis=0)
428
+
429
+ # print(max_vals, min_vals)
430
+ # normalized_coords = (all_coords - min_vals) / (max_vals - min_vals)
431
+
432
+ # normalized_coords = 2 * normalized_coords - 1
433
+ # for i, entry in enumerate(data):
434
+ # entry[0:3] = normalized_coords[i]
435
+
436
+ one_hot_x = np.eye(max_x+1)[all_coords[:, 0].astype(int)]
437
+ one_hot_y = np.eye(max_y+1)[all_coords[:, 1].astype(int)]
438
+ one_hot_z = np.eye(max_z+1)[all_coords[:, 2].astype(int)]
439
+
440
+ # for i, entry in enumerate(data):
441
+ # #entry.append(normalized_labels[i])
442
+ # entry.extend(np.concatenate([one_hot_x[i], one_hot_y[i], one_hot_z[i]])) # Using numpy to concatenate
443
+ # color_min = np.min(all_colors)
444
+ # color_max = np.max(all_colors)
445
+ # normalized_colors = (np.array(all_colors) - color_min) / (color_max - color_min)
446
+
447
+ # for i, entry in enumerate(data):
448
+ # entry[0] = normalized_colors[i]
449
+
450
+ if label_mapping is None:
451
+ label_mapping = {filename: idx for idx, filename in enumerate(sorted(set(all_labels)))}
452
+ else:
453
+ label_mapping = label_mapping
454
+
455
+ #all_labels = [label_mapping[label] for label in all_labels]
456
+ #label_min = np.min(all_labels)
457
+ #label_max = np.max(all_labels)
458
+ label_max = len(label_mapping)
459
+
460
+ # 将标签标准化到 [0, 1] 范围
461
+ #normalized_labels = (all_labels - label_min) / (label_max - label_min)
462
+ one_hot_labels = np.eye(label_max)[all_labels]
463
+
464
+ for i, entry in enumerate(data):
465
+ #entry.append(normalized_labels[i]) #
466
+ #entry.extend(one_hot_labels[i]) #
467
+
468
+ entry.extend([all_labels[i]]) #
469
+ entry.extend(np.concatenate([
470
+ np.array([all_coords[i, 0].astype(int)]),
471
+ np.array([all_coords[i, 1].astype(int)]),
472
+ np.array([all_coords[i, 2].astype(int)])
473
+ ]))
474
+
475
+ for i, entry in enumerate(data):
476
+ #entry.append(normalized_labels[i])
477
+ entry.extend([1,0]) #
478
+ #import ipdb; ipdb.set_trace()
479
+ return np.array(data), label_inverse_mapping
480
+
481
+
482
+ def save_data_as_npy(data, output_file):
483
+ np.save(output_file, data)
484
+
485
+
486
+ def logits2ldr(normalized_data, label_inverse_mapping=None, max_vals=None, min_vals=None, label_max=None, label_min=None, max_color=None, min_color=None, output_file='restored_data.ldr'):
487
+ dat_num = 604
488
+ x_num = 213
489
+ y_num = 217
490
+ z_num = 529
491
+ rot_num = 24
492
+
493
+ x = x_num
494
+ xy = x_num + y_num + rot_num
495
+ xyz = x_num + y_num + z_num + rot_num
496
+
497
+ # Use config-based paths (works in both local and HF Space environments)
498
+ forward_path, inverse_path = get_mapping_paths("subset_self")
499
+ label_mapping, label_inverse_mapping = load_mappings(forward_path, inverse_path)
500
+
501
+ if label_inverse_mapping is None:
502
+ # import ipdb; ipdb.set_trace()
503
+ # #label_inverse_mapping = {0: '98281.dat', 1: '3005.dat', 2: '3004.dat', 3: '3795.dat', 4: '3020.dat', 5: '3710.dat', 6: '3666.dat', 7: '3021.dat', 8: '2431.dat', 9: '4488.dat', 10: '3829a.dat', 11: '3829b.dat', 12: '43723.dat', 13: '3068b.dat', 14: '43722.dat', 15: '3832.dat', 16: '2432.dat', 17: '2437.dat', 18: '6231.dat', 19: '3040b.dat', 20: '3024.dat', 21: '11211.dat', 22: '2540.dat', 23: '61678.dat', 24: '3665.dat', 25: '11477.dat', 26: '93594.dat', 27: '50951.dat', 28: '4073.dat', 29: '6019.dat', 30: '6091.dat', 31: '3821.dat', 32: '3822.dat', 33: '98138.dat', 34: '3794a.dat', 35: '4081b.dat', 36: '3022.dat', 37: '30039.dat', 38: '50946.dat', 39: '4095.dat'} #blue_classic_car
504
+ label_inverse_mapping = {0: '24308b.dat', 1: '3031.dat', 2: '4079.dat', 3: '3021.dat', 4: '3024.dat', 5: '3020.dat', 6: '29120.dat', 7: '71076a.dat', 8: '3023.dat', 9: '29119.dat', 10: '2412b.dat', 11: '86876.dat', 12: '11211.dat', 13: '87087.dat', 14: '3004.dat', 15: '15068.dat', 16: '3829c01.dat', 17: '11477.dat', 18: '79393.dat', 19: '63864.dat', 20: '3710.dat', 21: 'm17f5892b_2023521_010804.dat', 22: '6141.dat', 23: '85984pc2.dat', 24: '3010.dat', 25: '30414.dat', 26: '2431pt0.dat'}
505
+
506
+
507
+ normalized_labels = normalized_data[:, xyz:xyz+dat_num].argmax(1)
508
+ #normalized_colors = normalized_data[:, 0] # 颜色列
509
+
510
+ normalized_coords_x = normalized_data[:, rot_num:rot_num+x].argmax(1)
511
+ normalized_coords_y = normalized_data[:, rot_num+x:xy].argmax(1)
512
+ normalized_coords_z = normalized_data[:, xy:xyz].argmax(1)
513
+
514
+ restored_coords = np.stack((normalized_coords_x, normalized_coords_y, normalized_coords_z), axis=-1)
515
+ #import ipdb; ipdb.set_trace()
516
+ #restored_coords = ((normalized_coords + 1) / 2) * (max_vals - min_vals) + min_vals
517
+
518
+ #restored_labels = (normalized_labels * (label_max - label_min)) + label_min
519
+
520
+ #restored_colors = normalized_colors * (max_color - min_color) + min_color
521
+ flag = normalized_data[:, xyz+dat_num:xyz+dat_num+2].argmax(1)
522
+ ldr_lines = []
523
+
524
+ for i, entry in enumerate(normalized_data):
525
+ color = 0 #int(restored_colors[i])
526
+ x, y, z = restored_coords[i]
527
+ label = label_inverse_mapping[str(np.clip(np.round(normalized_labels[i]), 0, dat_num).astype(int))]
528
+
529
+ # quaternion = entry[4:8] #
530
+ # quaternion = quaternion / np.linalg.norm(quaternion) #
531
+ # r = R.from_quat(quaternion)
532
+ rotation_matrix = onehot24_to_rot(entry[:rot_num])#r.as_matrix()
533
+ f = 1 - flag[i]
534
+ ldr_line = f"{f} {color} {x:.6f} {y:.6f} {z:.6f} " \
535
+ f"{rotation_matrix[0, 0]:.6f} {rotation_matrix[0, 1]:.6f} {rotation_matrix[0, 2]:.6f} " \
536
+ f"{rotation_matrix[1, 0]:.6f} {rotation_matrix[1, 1]:.6f} {rotation_matrix[1, 2]:.6f} " \
537
+ f"{rotation_matrix[2, 0]:.6f} {rotation_matrix[2, 1]:.6f} {rotation_matrix[2, 2]:.6f} " \
538
+ f"{label}\n"
539
+
540
+ ldr_lines.append(ldr_line)
541
+
542
+ with open(output_file, 'w') as f:
543
+ f.writelines(ldr_lines)
544
+ print(f"Restored LDR data saved to {output_file}")
545
+
546
+ return ldr_lines
547
+
548
+ def logits2ldrot(normalized_data, input_data, label_inverse_mapping='', max_vals='', min_vals='', label_max='', label_min='', max_color='', min_color='', output_file='restored_data_rot_wop.ldr'):
549
+ dat_num = 604
550
+ x_num = 213
551
+ y_num = 217
552
+ z_num = 529
553
+ rot_num = 24
554
+
555
+ x = x_num
556
+ xy = x_num + y_num + rot_num
557
+ xyz = x_num + y_num + z_num + rot_num
558
+
559
+ # Use config-based paths (works in both local and HF Space environments)
560
+ forward_path, inverse_path = get_mapping_paths("subset_self")
561
+ label_mapping, label_inverse_mapping = load_mappings(forward_path, inverse_path)
562
+
563
+ if label_inverse_mapping is None:
564
+ # import ipdb; ipdb.set_trace()
565
+ # #label_inverse_mapping = {0: '98281.dat', 1: '3005.dat', 2: '3004.dat', 3: '3795.dat', 4: '3020.dat', 5: '3710.dat', 6: '3666.dat', 7: '3021.dat', 8: '2431.dat', 9: '4488.dat', 10: '3829a.dat', 11: '3829b.dat', 12: '43723.dat', 13: '3068b.dat', 14: '43722.dat', 15: '3832.dat', 16: '2432.dat', 17: '2437.dat', 18: '6231.dat', 19: '3040b.dat', 20: '3024.dat', 21: '11211.dat', 22: '2540.dat', 23: '61678.dat', 24: '3665.dat', 25: '11477.dat', 26: '93594.dat', 27: '50951.dat', 28: '4073.dat', 29: '6019.dat', 30: '6091.dat', 31: '3821.dat', 32: '3822.dat', 33: '98138.dat', 34: '3794a.dat', 35: '4081b.dat', 36: '3022.dat', 37: '30039.dat', 38: '50946.dat', 39: '4095.dat'} #blue_classic_car
566
+ label_inverse_mapping = {0: '24308b.dat', 1: '3031.dat', 2: '4079.dat', 3: '3021.dat', 4: '3024.dat', 5: '3020.dat', 6: '29120.dat', 7: '71076a.dat', 8: '3023.dat', 9: '29119.dat', 10: '2412b.dat', 11: '86876.dat', 12: '11211.dat', 13: '87087.dat', 14: '3004.dat', 15: '15068.dat', 16: '3829c01.dat', 17: '11477.dat', 18: '79393.dat', 19: '63864.dat', 20: '3710.dat', 21: 'm17f5892b_2023521_010804.dat', 22: '6141.dat', 23: '85984pc2.dat', 24: '3010.dat', 25: '30414.dat', 26: '2431pt0.dat'}
567
+
568
+
569
+ input_labels = input_data[:, xyz:xyz+dat_num].argmax(1)
570
+
571
+ input_coords_x = input_data[:, rot_num:rot_num+x].argmax(1)
572
+ input_coords_y = input_data[:, rot_num+x:xy].argmax(1)
573
+ input_coords_z = input_data[:, xy:xyz].argmax(1)
574
+
575
+ restored_coords = np.stack((input_coords_x, input_coords_y, input_coords_z), axis=-1)
576
+ #flag = normalized_data[:, xyz+dat_num:xyz+dat_num+2].argmax(1)
577
+ flag = normalized_data[:, -2:].argmax(1)
578
+ ldr_lines = []
579
+
580
+ for i, entry in enumerate(normalized_data[:-1]):
581
+ color = 0 #int(restored_colors[i])
582
+ x, y, z = restored_coords[i]
583
+ label = label_inverse_mapping[str(np.clip(np.round(input_labels[i]), 0, dat_num).astype(int))]
584
+
585
+ # quaternion = entry[4:8] #
586
+ # quaternion = quaternion / np.linalg.norm(quaternion) #
587
+ # r = R.from_quat(quaternion)
588
+ rotation_matrix = onehot24_to_rot(entry[:rot_num])#r.as_matrix()
589
+ f = 1 - flag[i]
590
+ ldr_line = f"{f} {color} {x:.6f} {y:.6f} {z:.6f} " \
591
+ f"{rotation_matrix[0, 0]:.6f} {rotation_matrix[0, 1]:.6f} {rotation_matrix[0, 2]:.6f} " \
592
+ f"{rotation_matrix[1, 0]:.6f} {rotation_matrix[1, 1]:.6f} {rotation_matrix[1, 2]:.6f} " \
593
+ f"{rotation_matrix[2, 0]:.6f} {rotation_matrix[2, 1]:.6f} {rotation_matrix[2, 2]:.6f} " \
594
+ f"{label}\n"
595
+
596
+ ldr_lines.append(ldr_line)
597
+
598
+ with open(output_file, 'w') as f:
599
+ f.writelines(ldr_lines)
600
+ print(f"Restored LDR data saved to {output_file}")
601
+
602
+ return ldr_lines
603
+
604
+ def logits2ldrp(normalized_data, input_data, label_inverse_mapping='', max_vals='', min_vals='', label_max='', label_min='', max_color='', min_color='', output_file='restored_data_rot_wop.ldr'):
605
+ dat_num = 604
606
+ x_num = 213
607
+ y_num = 217
608
+ z_num = 529
609
+ rot_num = 24
610
+
611
+ x = x_num
612
+ xy = x_num + y_num + rot_num
613
+ xyz = x_num + y_num + z_num + rot_num
614
+
615
+ # Use config-based paths (works in both local and HF Space environments)
616
+ forward_path, inverse_path = get_mapping_paths("subset_self")
617
+ label_mapping, label_inverse_mapping = load_mappings(forward_path, inverse_path)
618
+
619
+ if label_inverse_mapping is None:
620
+ # import ipdb; ipdb.set_trace()
621
+ # #label_inverse_mapping = {0: '98281.dat', 1: '3005.dat', 2: '3004.dat', 3: '3795.dat', 4: '3020.dat', 5: '3710.dat', 6: '3666.dat', 7: '3021.dat', 8: '2431.dat', 9: '4488.dat', 10: '3829a.dat', 11: '3829b.dat', 12: '43723.dat', 13: '3068b.dat', 14: '43722.dat', 15: '3832.dat', 16: '2432.dat', 17: '2437.dat', 18: '6231.dat', 19: '3040b.dat', 20: '3024.dat', 21: '11211.dat', 22: '2540.dat', 23: '61678.dat', 24: '3665.dat', 25: '11477.dat', 26: '93594.dat', 27: '50951.dat', 28: '4073.dat', 29: '6019.dat', 30: '6091.dat', 31: '3821.dat', 32: '3822.dat', 33: '98138.dat', 34: '3794a.dat', 35: '4081b.dat', 36: '3022.dat', 37: '30039.dat', 38: '50946.dat', 39: '4095.dat'} #blue_classic_car
622
+ label_inverse_mapping = {0: '24308b.dat', 1: '3031.dat', 2: '4079.dat', 3: '3021.dat', 4: '3024.dat', 5: '3020.dat', 6: '29120.dat', 7: '71076a.dat', 8: '3023.dat', 9: '29119.dat', 10: '2412b.dat', 11: '86876.dat', 12: '11211.dat', 13: '87087.dat', 14: '3004.dat', 15: '15068.dat', 16: '3829c01.dat', 17: '11477.dat', 18: '79393.dat', 19: '63864.dat', 20: '3710.dat', 21: 'm17f5892b_2023521_010804.dat', 22: '6141.dat', 23: '85984pc2.dat', 24: '3010.dat', 25: '30414.dat', 26: '2431pt0.dat'}
623
+
624
+
625
+ input_labels = input_data[:, xyz:xyz+dat_num].argmax(1)
626
+
627
+ # input_coords_x = input_data[:, rot_num:rot_num+x].argmax(1)
628
+ # input_coords_y = input_data[:, rot_num+x:xy].argmax(1)
629
+ # input_coords_z = input_data[:, xy:xyz].argmax(1)
630
+
631
+ input_coords_x = normalized_data[:, rot_num:rot_num+x].argmax(1)
632
+ input_coords_y = normalized_data[:, rot_num+x:xy].argmax(1)
633
+ input_coords_z = normalized_data[:, xy:xyz].argmax(1)
634
+
635
+ restored_coords = np.stack((input_coords_x, input_coords_y, input_coords_z), axis=-1)
636
+ #flag = normalized_data[:, xyz+dat_num:xyz+dat_num+2].argmax(1)
637
+ flag = normalized_data[:, -2:].argmax(1)
638
+ ldr_lines = []
639
+ #for i, entry in enumerate(normalized_data[:-1]):
640
+ for i, entry in enumerate(input_data[:-1]):
641
+ color = 0 #int(restored_colors[i])
642
+ x, y, z = restored_coords[i]
643
+ label = label_inverse_mapping[str(np.clip(np.round(input_labels[i]), 0, dat_num).astype(int))]
644
+
645
+ # quaternion = entry[4:8] #
646
+ # quaternion = quaternion / np.linalg.norm(quaternion) #
647
+ # r = R.from_quat(quaternion)
648
+ rotation_matrix = onehot24_to_rot(entry[:rot_num])#r.as_matrix()
649
+ f = 1# - flag[i]
650
+ ldr_line = f"{f} {color} {x:.6f} {y:.6f} {z:.6f} " \
651
+ f"{rotation_matrix[0, 0]:.6f} {rotation_matrix[0, 1]:.6f} {rotation_matrix[0, 2]:.6f} " \
652
+ f"{rotation_matrix[1, 0]:.6f} {rotation_matrix[1, 1]:.6f} {rotation_matrix[1, 2]:.6f} " \
653
+ f"{rotation_matrix[2, 0]:.6f} {rotation_matrix[2, 1]:.6f} {rotation_matrix[2, 2]:.6f} " \
654
+ f"{label}\n"
655
+
656
+ ldr_lines.append(ldr_line)
657
+
658
+ with open(output_file, 'w') as f:
659
+ f.writelines(ldr_lines)
660
+ print(f"Restored LDR data saved to {output_file}")
661
+
662
+ return ldr_lines
663
+
664
+ def logits2flatldrp(normalized_data, input_data, label_inverse_mapping='', max_vals='', min_vals='', label_max='', label_min='', max_color='', min_color='', output_file='restored_data_rot_wop.ldr'):
665
+ dat_num = 604
666
+ x_num = 213
667
+ y_num = 217
668
+ z_num = 529
669
+ rot_num = 24
670
+
671
+ R24 = signed_perm_mats_det_plus_1()
672
+
673
+ x = x_num
674
+ xy = x_num + y_num + rot_num
675
+ xyz = x_num + y_num + z_num + rot_num
676
+
677
+ stride = 3
678
+ # Use config-based paths (works in both local and HF Space environments)
679
+ forward_path, inverse_path = get_mapping_paths("subset_self")
680
+ label_mapping, label_inverse_mapping = load_mappings(forward_path, inverse_path)
681
+
682
+ if label_inverse_mapping is None:
683
+ # import ipdb; ipdb.set_trace()
684
+ # #label_inverse_mapping = {0: '98281.dat', 1: '3005.dat', 2: '3004.dat', 3: '3795.dat', 4: '3020.dat', 5: '3710.dat', 6: '3666.dat', 7: '3021.dat', 8: '2431.dat', 9: '4488.dat', 10: '3829a.dat', 11: '3829b.dat', 12: '43723.dat', 13: '3068b.dat', 14: '43722.dat', 15: '3832.dat', 16: '2432.dat', 17: '2437.dat', 18: '6231.dat', 19: '3040b.dat', 20: '3024.dat', 21: '11211.dat', 22: '2540.dat', 23: '61678.dat', 24: '3665.dat', 25: '11477.dat', 26: '93594.dat', 27: '50951.dat', 28: '4073.dat', 29: '6019.dat', 30: '6091.dat', 31: '3821.dat', 32: '3822.dat', 33: '98138.dat', 34: '3794a.dat', 35: '4081b.dat', 36: '3022.dat', 37: '30039.dat', 38: '50946.dat', 39: '4095.dat'} #blue_classic_car
685
+ label_inverse_mapping = {0: '24308b.dat', 1: '3031.dat', 2: '4079.dat', 3: '3021.dat', 4: '3024.dat', 5: '3020.dat', 6: '29120.dat', 7: '71076a.dat', 8: '3023.dat', 9: '29119.dat', 10: '2412b.dat', 11: '86876.dat', 12: '11211.dat', 13: '87087.dat', 14: '3004.dat', 15: '15068.dat', 16: '3829c01.dat', 17: '11477.dat', 18: '79393.dat', 19: '63864.dat', 20: '3710.dat', 21: 'm17f5892b_2023521_010804.dat', 22: '6141.dat', 23: '85984pc2.dat', 24: '3010.dat', 25: '30414.dat', 26: '2431pt0.dat'}
686
+
687
+
688
+ input_labels = input_data[:, -6]
689
+
690
+ input_coords_x = input_data[:, -5] #normalized_data[1:-2:stride, :x_num+1].argmax(1)
691
+ input_coords_y = input_data[:, -4] #normalized_data[0:-3:stride, :y_num+1].argmax(1)
692
+ input_coords_z = input_data[:, -3] #normalized_data[2:-1:stride, :z_num+1].argmax(1)
693
+
694
+ restored_coords = np.stack((input_coords_x, input_coords_y, input_coords_z), axis=-1)
695
+ #flag = normalized_data[:, xyz+dat_num:xyz+dat_num+2].argmax(1)
696
+ #flag = normalized_data[:, -2:].argmax(1)
697
+ ldr_lines = []
698
+ #for i, entry in enumerate(normalized_data[:-1]):
699
+ for i, entry in enumerate(input_data[:-1]):
700
+ color = 0 #int(restored_colors[i])
701
+ x, y, z = restored_coords[i]
702
+ label = label_inverse_mapping[str(np.clip(np.round(input_labels[i]), 0, dat_num).astype(int))]
703
+
704
+ # quaternion = entry[4:8] #
705
+ # quaternion = quaternion / np.linalg.norm(quaternion) #
706
+ # r = R.from_quat(quaternion)
707
+ #import ipdb; ipdb.set_trace()
708
+ rotation_matrix = R24[entry[-7]]#r.as_matrix()
709
+ f = 1# - flag[i]
710
+ ldr_line = f"{f} {color} {x:.6f} {y:.6f} {z:.6f} " \
711
+ f"{rotation_matrix[0, 0]:.6f} {rotation_matrix[0, 1]:.6f} {rotation_matrix[0, 2]:.6f} " \
712
+ f"{rotation_matrix[1, 0]:.6f} {rotation_matrix[1, 1]:.6f} {rotation_matrix[1, 2]:.6f} " \
713
+ f"{rotation_matrix[2, 0]:.6f} {rotation_matrix[2, 1]:.6f} {rotation_matrix[2, 2]:.6f} " \
714
+ f"{label}\n"
715
+
716
+ ldr_lines.append(ldr_line)
717
+
718
+ with open(output_file, 'w') as f:
719
+ f.writelines(ldr_lines)
720
+ print(f"Restored LDR data saved to {output_file}")
721
+
722
+ return ldr_lines
723
+
724
+ def logits2flatldrpr(normalized_data, input_data, stride, given, label_inverse_mapping='', max_vals='', min_vals='', label_max='', label_min='', max_color='', min_color='', output_file='restored_data_rot_wop.ldr'):
725
+ dat_num = 604
726
+ x_num = 213
727
+ y_num = 217
728
+ z_num = 529
729
+ rot_num = 24
730
+
731
+ R24 = signed_perm_mats_det_plus_1()
732
+
733
+ x = x_num
734
+ xy = x_num + y_num + rot_num
735
+ xyz = x_num + y_num + z_num + rot_num
736
+
737
+ stride = stride
738
+ attr_shift = stride-3 #+1 for bert
739
+ bert_shift = 1
740
+ # Use config-based paths (works in both local and HF Space environments)
741
+ forward_path, inverse_path = get_mapping_paths("subset_self")
742
+ label_mapping, label_inverse_mapping = load_mappings(forward_path, inverse_path)
743
+
744
+ if label_inverse_mapping is None:
745
+ # import ipdb; ipdb.set_trace()
746
+ # #label_inverse_mapping = {0: '98281.dat', 1: '3005.dat', 2: '3004.dat', 3: '3795.dat', 4: '3020.dat', 5: '3710.dat', 6: '3666.dat', 7: '3021.dat', 8: '2431.dat', 9: '4488.dat', 10: '3829a.dat', 11: '3829b.dat', 12: '43723.dat', 13: '3068b.dat', 14: '43722.dat', 15: '3832.dat', 16: '2432.dat', 17: '2437.dat', 18: '6231.dat', 19: '3040b.dat', 20: '3024.dat', 21: '11211.dat', 22: '2540.dat', 23: '61678.dat', 24: '3665.dat', 25: '11477.dat', 26: '93594.dat', 27: '50951.dat', 28: '4073.dat', 29: '6019.dat', 30: '6091.dat', 31: '3821.dat', 32: '3822.dat', 33: '98138.dat', 34: '3794a.dat', 35: '4081b.dat', 36: '3022.dat', 37: '30039.dat', 38: '50946.dat', 39: '4095.dat'} #blue_classic_car
747
+ label_inverse_mapping = {0: '24308b.dat', 1: '3031.dat', 2: '4079.dat', 3: '3021.dat', 4: '3024.dat', 5: '3020.dat', 6: '29120.dat', 7: '71076a.dat', 8: '3023.dat', 9: '29119.dat', 10: '2412b.dat', 11: '86876.dat', 12: '11211.dat', 13: '87087.dat', 14: '3004.dat', 15: '15068.dat', 16: '3829c01.dat', 17: '11477.dat', 18: '79393.dat', 19: '63864.dat', 20: '3710.dat', 21: 'm17f5892b_2023521_010804.dat', 22: '6141.dat', 23: '85984pc2.dat', 24: '3010.dat', 25: '30414.dat', 26: '2431pt0.dat'}
748
+
749
+
750
+ #input_labels = normalized_data[0:-4:stride, :dat_num+1].argmax(1) #
751
+ input_labels = input_data[:, -6]
752
+ #input_labels[:given] = input_data[:given, -6] #normalized_data[0:-4:stride, :dat_num+1].argmax(1)
753
+
754
+ input_rot = normalized_data[1+bert_shift:-3:stride, :rot_num+1].argmax(1) # #normalized_data[1:-3:stride, :rot_num+1].argmax(1)
755
+ #input_rot = input_data[:, 0]
756
+
757
+
758
+ input_coords_x = normalized_data[1+attr_shift+bert_shift:-1:stride, rot_num+1:x+rot_num+1+1].argmax(1)
759
+ input_coords_y = normalized_data[0+attr_shift+bert_shift:-2:stride, x+rot_num+2:xy+3].argmax(1)
760
+ input_coords_z = normalized_data[2+attr_shift+bert_shift::stride, xy+3:xyz+4].argmax(1)
761
+
762
+ # input_coords_x[:given] = input_data[:given, -5]
763
+ # input_coords_y[:given] = input_data[:given, -4]
764
+ # input_coords_z[:given] = input_data[:given, -3]
765
+
766
+ restored_coords = np.stack((input_coords_x, input_coords_y, input_coords_z), axis=-1)
767
+ #flag = normalized_data[:, xyz+dat_num:xyz+dat_num+2].argmax(1)
768
+ #flag = normalized_data[:, -2:].argmax(1)
769
+
770
+ input_colors = np.zeros_like(input_data[:, 0])
771
+ #input_colors[:given] = (input_colors[:given] + 4)
772
+ ldr_lines = []
773
+ #for i, entry in enumerate(normalized_data[:-1]):
774
+ for i, entry in enumerate(input_data[:-1]):
775
+ color = int(input_colors[i])
776
+ x, y, z = restored_coords[i]
777
+ label = label_inverse_mapping[str(np.clip(np.round(input_labels[i]), 0, dat_num-1).astype(int))]
778
+
779
+ # quaternion = entry[4:8] #
780
+ # quaternion = quaternion / np.linalg.norm(quaternion) #
781
+ # r = R.from_quat(quaternion)
782
+
783
+ rotation_matrix = R24[np.clip(input_rot[i], 0, rot_num-1)] #R24[entry[-7]]#r.as_matrix()
784
+ if x>212:
785
+ f = 0# - flag[i]
786
+ else:
787
+ f = 1
788
+ ldr_line = f"{f} {color} {x:.6f} {y:.6f} {z:.6f} " \
789
+ f"{rotation_matrix[0, 0]:.6f} {rotation_matrix[0, 1]:.6f} {rotation_matrix[0, 2]:.6f} " \
790
+ f"{rotation_matrix[1, 0]:.6f} {rotation_matrix[1, 1]:.6f} {rotation_matrix[1, 2]:.6f} " \
791
+ f"{rotation_matrix[2, 0]:.6f} {rotation_matrix[2, 1]:.6f} {rotation_matrix[2, 2]:.6f} " \
792
+ f"{label}\n"
793
+
794
+ ldr_lines.append(ldr_line)
795
+
796
+ with open(output_file, 'w') as f:
797
+ f.writelines(ldr_lines)
798
+ print(f"Restored LDR data saved to {output_file}")
799
+
800
+ return ldr_lines
801
+
802
+ # def logits2botldrpr(normalized_data, input_data, stride, given, label_inverse_mapping='', max_vals='', min_vals='', label_max='', label_min='', max_color='', min_color='', output_file='restored_data_rot_wop.ldr'):
803
+ # dat_num = 286
804
+ # x_num = 213
805
+ # y_num = 73
806
+ # z_num = 411
807
+ # rot_num = 24
808
+
809
+ # R24 = signed_perm_mats_det_plus_1()
810
+
811
+ # x = x_num
812
+ # xy = x_num + y_num + rot_num
813
+ # xyz = x_num + y_num + z_num + rot_num
814
+
815
+ # stride = stride
816
+ # attr_shift = stride-3 #+1 for bert
817
+ # bert_shift = 1
818
+ # label_mapping, label_inverse_mapping = load_mappings('../data/car_1k/subset_bottom_300/label_mapping.json', '../data/car_1k/subset_bottom_300/label_inverse_mapping.json')
819
+
820
+ # if label_inverse_mapping is None:
821
+ # import ipdb; ipdb.set_trace()
822
+ # # #label_inverse_mapping = {0: '98281.dat', 1: '3005.dat', 2: '3004.dat', 3: '3795.dat', 4: '3020.dat', 5: '3710.dat', 6: '3666.dat', 7: '3021.dat', 8: '2431.dat', 9: '4488.dat', 10: '3829a.dat', 11: '3829b.dat', 12: '43723.dat', 13: '3068b.dat', 14: '43722.dat', 15: '3832.dat', 16: '2432.dat', 17: '2437.dat', 18: '6231.dat', 19: '3040b.dat', 20: '3024.dat', 21: '11211.dat', 22: '2540.dat', 23: '61678.dat', 24: '3665.dat', 25: '11477.dat', 26: '93594.dat', 27: '50951.dat', 28: '4073.dat', 29: '6019.dat', 30: '6091.dat', 31: '3821.dat', 32: '3822.dat', 33: '98138.dat', 34: '3794a.dat', 35: '4081b.dat', 36: '3022.dat', 37: '30039.dat', 38: '50946.dat', 39: '4095.dat'} #blue_classic_car
823
+ # label_inverse_mapping = {0: '24308b.dat', 1: '3031.dat', 2: '4079.dat', 3: '3021.dat', 4: '3024.dat', 5: '3020.dat', 6: '29120.dat', 7: '71076a.dat', 8: '3023.dat', 9: '29119.dat', 10: '2412b.dat', 11: '86876.dat', 12: '11211.dat', 13: '87087.dat', 14: '3004.dat', 15: '15068.dat', 16: '3829c01.dat', 17: '11477.dat', 18: '79393.dat', 19: '63864.dat', 20: '3710.dat', 21: 'm17f5892b_2023521_010804.dat', 22: '6141.dat', 23: '85984pc2.dat', 24: '3010.dat', 25: '30414.dat', 26: '2431pt0.dat'}
824
+
825
+
826
+ # #input_labels = normalized_data[0:-4:stride, :dat_num+1].argmax(1) #
827
+ # input_labels = input_data[:, -6]
828
+ # #input_labels[:given] = input_data[:given, -6] #normalized_data[0:-4:stride, :dat_num+1].argmax(1)
829
+
830
+
831
+ # input_rot = normalized_data[1+bert_shift:-3:stride, :rot_num+1].argmax(1) # #normalized_data[1:-3:stride, :rot_num+1].argmax(1)
832
+ # #input_rot = input_data[:, 0]
833
+
834
+
835
+ # input_coords_x = normalized_data[1+attr_shift+bert_shift:-1:stride, rot_num+1:x+rot_num+1+1].argmax(1)
836
+ # input_coords_y = normalized_data[0+attr_shift+bert_shift:-2:stride, x+rot_num+2:xy+3].argmax(1)
837
+ # input_coords_z = normalized_data[2+attr_shift+bert_shift::stride, xy+3:xyz+4].argmax(1)
838
+
839
+ # # input_coords_x[:given] = input_data[:given, -5]
840
+ # # input_coords_y[:given] = input_data[:given, -4]
841
+ # # input_coords_z[:given] = input_data[:given, -3]
842
+
843
+ # restored_coords = np.stack((input_coords_x, input_coords_y, input_coords_z), axis=-1)
844
+ # #flag = normalized_data[:, xyz+dat_num:xyz+dat_num+2].argmax(1)
845
+ # #flag = normalized_data[:, -2:].argmax(1)
846
+
847
+ # input_colors = np.zeros_like(input_data[:, 0])
848
+ # #input_colors[:given] = (input_colors[:given] + 4)
849
+ # ldr_lines = []
850
+ # #for i, entry in enumerate(normalized_data[:-1]):
851
+ # for i, entry in enumerate(input_data[:-1]):
852
+ # color = int(input_colors[i])
853
+ # x, y, z = restored_coords[i]
854
+ # label = label_inverse_mapping[str(np.clip(np.round(input_labels[i]), 0, dat_num-1).astype(int))]
855
+
856
+ # # quaternion = entry[4:8] #
857
+ # # quaternion = quaternion / np.linalg.norm(quaternion) #
858
+ # # r = R.from_quat(quaternion)
859
+
860
+ # rotation_matrix = R24[np.clip(input_rot[i], 0, rot_num-1)] #R24[entry[-7]]#r.as_matrix()
861
+ # if x>212:
862
+ # f = 0# - flag[i]
863
+ # else:
864
+ # f = 1
865
+ # ldr_line = f"{f} {color} {x:.6f} {y:.6f} {z:.6f} " \
866
+ # f"{rotation_matrix[0, 0]:.6f} {rotation_matrix[0, 1]:.6f} {rotation_matrix[0, 2]:.6f} " \
867
+ # f"{rotation_matrix[1, 0]:.6f} {rotation_matrix[1, 1]:.6f} {rotation_matrix[1, 2]:.6f} " \
868
+ # f"{rotation_matrix[2, 0]:.6f} {rotation_matrix[2, 1]:.6f} {rotation_matrix[2, 2]:.6f} " \
869
+ # f"{label}\n"
870
+
871
+ # ldr_lines.append(ldr_line)
872
+
873
+ # with open(output_file, 'w') as f:
874
+ # f.writelines(ldr_lines)
875
+ # print(f"Restored LDR data saved to {output_file}")
876
+
877
+ # return ldr_lines
878
+
879
+ def logits2botldrpr(normalized_data, input_data, stride, given, label_inverse_mapping='', max_vals='', min_vals='', label_max='', label_min='', max_color='', min_color='', output_file='restored_data_rot_wop.ldr'):
880
+ dat_num = 1217 #286
881
+ x_num = 251 #213
882
+ y_num = 215 #73
883
+ z_num = 525 #411
884
+ rot_num = 24
885
+
886
+ R24 = signed_perm_mats_det_plus_1()
887
+
888
+ x = x_num
889
+ xy = x_num + y_num + rot_num
890
+ xyz = x_num + y_num + z_num + rot_num
891
+
892
+ stride = stride
893
+ attr_shift = stride-3 #+1 for bert
894
+ bert_shift = 1
895
+
896
+ # Use config-based paths (works in both local and HF Space environments)
897
+ forward_path, inverse_path = get_mapping_paths("subset_1k")
898
+ label_mapping, label_inverse_mapping = load_mappings(forward_path, inverse_path)
899
+
900
+ if label_inverse_mapping is None:
901
+ # import ipdb; ipdb.set_trace()
902
+ # #label_inverse_mapping = {0: '98281.dat', 1: '3005.dat', 2: '3004.dat', 3: '3795.dat', 4: '3020.dat', 5: '3710.dat', 6: '3666.dat', 7: '3021.dat', 8: '2431.dat', 9: '4488.dat', 10: '3829a.dat', 11: '3829b.dat', 12: '43723.dat', 13: '3068b.dat', 14: '43722.dat', 15: '3832.dat', 16: '2432.dat', 17: '2437.dat', 18: '6231.dat', 19: '3040b.dat', 20: '3024.dat', 21: '11211.dat', 22: '2540.dat', 23: '61678.dat', 24: '3665.dat', 25: '11477.dat', 26: '93594.dat', 27: '50951.dat', 28: '4073.dat', 29: '6019.dat', 30: '6091.dat', 31: '3821.dat', 32: '3822.dat', 33: '98138.dat', 34: '3794a.dat', 35: '4081b.dat', 36: '3022.dat', 37: '30039.dat', 38: '50946.dat', 39: '4095.dat'} #blue_classic_car
903
+ label_inverse_mapping = {0: '24308b.dat', 1: '3031.dat', 2: '4079.dat', 3: '3021.dat', 4: '3024.dat', 5: '3020.dat', 6: '29120.dat', 7: '71076a.dat', 8: '3023.dat', 9: '29119.dat', 10: '2412b.dat', 11: '86876.dat', 12: '11211.dat', 13: '87087.dat', 14: '3004.dat', 15: '15068.dat', 16: '3829c01.dat', 17: '11477.dat', 18: '79393.dat', 19: '63864.dat', 20: '3710.dat', 21: 'm17f5892b_2023521_010804.dat', 22: '6141.dat', 23: '85984pc2.dat', 24: '3010.dat', 25: '30414.dat', 26: '2431pt0.dat'}
904
+
905
+
906
+ #input_labels = normalized_data[0:-4:stride, :dat_num+1].argmax(1) #
907
+ input_labels = input_data[:, -6]
908
+ #input_labels[:given] = input_data[:given, -6] #normalized_data[0:-4:stride, :dat_num+1].argmax(1)
909
+
910
+
911
+ input_rot = normalized_data[1+bert_shift:-3:stride, :rot_num+1].argmax(1) # #normalized_data[1:-3:stride, :rot_num+1].argmax(1)
912
+ #input_rot = input_data[:, 0]
913
+
914
+
915
+ input_coords_x = normalized_data[1+attr_shift+bert_shift:-1:stride, rot_num+1:x+rot_num+1+1].argmax(1)
916
+ input_coords_y = normalized_data[0+attr_shift+bert_shift:-2:stride, x+rot_num+2:xy+3].argmax(1)
917
+ input_coords_z = normalized_data[2+attr_shift+bert_shift::stride, xy+3:xyz+4].argmax(1)
918
+
919
+ # input_coords_x[:given] = input_data[:given, -5]
920
+ # input_coords_y[:given] = input_data[:given, -4]
921
+ # input_coords_z[:given] = input_data[:given, -3]
922
+
923
+ restored_coords = np.stack((input_coords_x, input_coords_y, input_coords_z), axis=-1)
924
+ #flag = normalized_data[:, xyz+dat_num:xyz+dat_num+2].argmax(1)
925
+ #flag = normalized_data[:, -2:].argmax(1)
926
+
927
+ input_colors = np.zeros_like(input_data[:, 0])
928
+ #input_colors[:given] = (input_colors[:given] + 4)
929
+ ldr_lines = []
930
+ #for i, entry in enumerate(normalized_data[:-1]):
931
+ for i, entry in enumerate(input_data[:-1]):
932
+ color = int(input_colors[i])
933
+ x, y, z = restored_coords[i]
934
+ label = label_inverse_mapping[str(np.clip(np.round(input_labels[i]), 0, dat_num-1).astype(int))]
935
+
936
+ # quaternion = entry[4:8] #
937
+ # quaternion = quaternion / np.linalg.norm(quaternion) #
938
+ # r = R.from_quat(quaternion)
939
+
940
+ rotation_matrix = R24[np.clip(input_rot[i], 0, rot_num-1)] #R24[entry[-7]]#r.as_matrix()
941
+ if x>(x_num-1):
942
+ f = 0# - flag[i]
943
+ else:
944
+ f = 1
945
+ ldr_line = f"{f} {color} {x:.6f} {y:.6f} {z:.6f} " \
946
+ f"{rotation_matrix[0, 0]:.6f} {rotation_matrix[0, 1]:.6f} {rotation_matrix[0, 2]:.6f} " \
947
+ f"{rotation_matrix[1, 0]:.6f} {rotation_matrix[1, 1]:.6f} {rotation_matrix[1, 2]:.6f} " \
948
+ f"{rotation_matrix[2, 0]:.6f} {rotation_matrix[2, 1]:.6f} {rotation_matrix[2, 2]:.6f} " \
949
+ f"{label}\n"
950
+
951
+ ldr_lines.append(ldr_line)
952
+
953
+ with open(output_file, 'w') as f:
954
+ f.writelines(ldr_lines)
955
+ print(f"Restored LDR data saved to {output_file}")
956
+
957
+ return ldr_lines
958
+
959
+ def ids2flatldrpr(normalized_data, input_data, stride, given, label_inverse_mapping='', max_vals='', min_vals='', label_max='', label_min='', max_color='', min_color='', output_file='restored_data_rot_wop.ldr'):
960
+ dat_num = 604
961
+ x_num = 213
962
+ y_num = 217
963
+ z_num = 529
964
+ rot_num = 24
965
+
966
+ R24 = signed_perm_mats_det_plus_1()
967
+
968
+ x = x_num
969
+ xy = x_num + y_num + rot_num
970
+ xyz = x_num + y_num + z_num + rot_num
971
+
972
+ stride = stride
973
+ attr_shift = stride-3
974
+ # Use config-based paths (works in both local and HF Space environments)
975
+ forward_path, inverse_path = get_mapping_paths("subset_self")
976
+ label_mapping, label_inverse_mapping = load_mappings(forward_path, inverse_path)
977
+
978
+ if label_inverse_mapping is None:
979
+ # import ipdb; ipdb.set_trace()
980
+ # #label_inverse_mapping = {0: '98281.dat', 1: '3005.dat', 2: '3004.dat', 3: '3795.dat', 4: '3020.dat', 5: '3710.dat', 6: '3666.dat', 7: '3021.dat', 8: '2431.dat', 9: '4488.dat', 10: '3829a.dat', 11: '3829b.dat', 12: '43723.dat', 13: '3068b.dat', 14: '43722.dat', 15: '3832.dat', 16: '2432.dat', 17: '2437.dat', 18: '6231.dat', 19: '3040b.dat', 20: '3024.dat', 21: '11211.dat', 22: '2540.dat', 23: '61678.dat', 24: '3665.dat', 25: '11477.dat', 26: '93594.dat', 27: '50951.dat', 28: '4073.dat', 29: '6019.dat', 30: '6091.dat', 31: '3821.dat', 32: '3822.dat', 33: '98138.dat', 34: '3794a.dat', 35: '4081b.dat', 36: '3022.dat', 37: '30039.dat', 38: '50946.dat', 39: '4095.dat'} #blue_classic_car
981
+ label_inverse_mapping = {0: '24308b.dat', 1: '3031.dat', 2: '4079.dat', 3: '3021.dat', 4: '3024.dat', 5: '3020.dat', 6: '29120.dat', 7: '71076a.dat', 8: '3023.dat', 9: '29119.dat', 10: '2412b.dat', 11: '86876.dat', 12: '11211.dat', 13: '87087.dat', 14: '3004.dat', 15: '15068.dat', 16: '3829c01.dat', 17: '11477.dat', 18: '79393.dat', 19: '63864.dat', 20: '3710.dat', 21: 'm17f5892b_2023521_010804.dat', 22: '6141.dat', 23: '85984pc2.dat', 24: '3010.dat', 25: '30414.dat', 26: '2431pt0.dat'}
982
+
983
+
984
+ input_labels = normalized_data[0:-4:stride, :dat_num+1] #
985
+ #input_labels[:given] = input_data[:given, -6] #normalized_data[0:-4:stride, :dat_num+1].argmax(1)
986
+
987
+ input_rot = normalized_data[1:-3:stride, :rot_num+1] #input_data[:, 0] #normalized_data[1:-3:stride, :rot_num+1].argmax(1)
988
+ #input_rot[:given] = input_data[:given, 0]
989
+
990
+ input_coords_x = normalized_data[1+attr_shift:-1:stride, :x_num+1]
991
+ input_coords_y = normalized_data[0+attr_shift:-2:stride, :y_num+1]
992
+ input_coords_z = normalized_data[2+attr_shift::stride, :z_num+1]
993
+
994
+ # input_coords_x[:given] = input_data[:given, -5]
995
+ # input_coords_y[:given] = input_data[:given, -4]
996
+ # input_coords_z[:given] = input_data[:given, -3]
997
+
998
+ restored_coords = np.stack((input_coords_x, input_coords_y, input_coords_z), axis=-1)
999
+ #flag = normalized_data[:, xyz+dat_num:xyz+dat_num+2].argmax(1)
1000
+ #flag = normalized_data[:, -2:].argmax(1)
1001
+
1002
+ input_colors = np.zeros_like(input_data[:, 0])
1003
+ #input_colors[:given] = (input_colors[:given] + 4)
1004
+ ldr_lines = []
1005
+ #for i, entry in enumerate(normalized_data[:-1]):
1006
+ for i, entry in enumerate(input_data[:-1]):
1007
+ color = int(input_colors[i])
1008
+ x, y, z = np.squeeze(restored_coords, axis=1)[i]
1009
+ label = label_inverse_mapping[str(np.clip(np.round(input_labels[i].item()), 0, dat_num-1).astype(int))]
1010
+
1011
+ # quaternion = entry[4:8] #
1012
+ # quaternion = quaternion / np.linalg.norm(quaternion) #
1013
+ # r = R.from_quat(quaternion)
1014
+
1015
+ rotation_matrix = R24[int(np.clip(input_rot[i].item(), 0, rot_num-1))] #R24[entry[-7]]#r.as_matrix()
1016
+ if x>212:
1017
+ f = 0# - flag[i]
1018
+ else:
1019
+ f = 1
1020
+ ldr_line = f"{f} {color} {x:.6f} {y:.6f} {z:.6f} " \
1021
+ f"{rotation_matrix[0, 0]:.6f} {rotation_matrix[0, 1]:.6f} {rotation_matrix[0, 2]:.6f} " \
1022
+ f"{rotation_matrix[1, 0]:.6f} {rotation_matrix[1, 1]:.6f} {rotation_matrix[1, 2]:.6f} " \
1023
+ f"{rotation_matrix[2, 0]:.6f} {rotation_matrix[2, 1]:.6f} {rotation_matrix[2, 2]:.6f} " \
1024
+ f"{label}\n"
1025
+
1026
+ ldr_lines.append(ldr_line)
1027
+
1028
+ with open(output_file, 'w') as f:
1029
+ f.writelines(ldr_lines)
1030
+ print(f"Restored LDR data saved to {output_file}")
1031
+
1032
+ return ldr_lines
1033
+
1034
+ def main(input_file):
1035
+ lines = read_ldr_file(input_file)
1036
+ processed_data, label_inverse_mapping = process_ldr_data(lines) # 处理LDR数据
1037
+
1038
+ inverted_data = logits2ldr(processed_data, label_inverse_mapping) # 将标准化数据转换回原始数据格式
1039
+
1040
+ # import ipdb; ipdb.set_trace()
1041
+ # output_file = os.path.splitext(input_file)[0] + '_wrdhot' + '.npy'
1042
+
1043
+ # save_data_as_npy(processed_data, output_file) # 保存为.npy文件
1044
+ # print(f"Processed data has been saved to {output_file}")
1045
+
1046
+ # 示例
1047
+ input_file = '/public/home/wangshuo/gap/assembly/data/blue classic car/modified_blue classic car.ldr' # 输入LDR文件路径
1048
+
1049
+
1050
+ #main(input_file)