Caesarrr commited on
Commit
c7f75e8
·
1 Parent(s): 3eacd49
action_state/gen_task1.py DELETED
@@ -1,29 +0,0 @@
1
- """
2
- 给定一张物体在初始视角的图片,camera将围绕该静止物体进行水平移动。旋转的方向(顺时针或逆时针)是基于从物体正上方俯视(鸟瞰视角)的平面来定义的。模型需要根据给定的旋转方向和角度,推断出新视角下的物体图像,并从四个图像中选出正确的一项。
3
-
4
- 注:{angle}就是 xx degrees, {direction} 就是 clockwise / anticlockwise
5
-
6
- 1.
7
- The object in the image <image_start>[image_1]<image_end> remains **static**. Imagine a camera rotating around this object. The direction of rotation is defined from a **top-down bird's-eye view**.
8
-
9
- Please identify the view of the object after the camera rotates {angle} {direction} based on this top-down perspective, and select the correct answer.
10
-
11
- A. <image_start>[image_A]<image_end>
12
- B. <image_start>[image_B]<image_end>
13
- C. <image_start>[image_C]<image_end>
14
- D. <image_start>[image_D]<image_end>
15
-
16
-
17
- 2.
18
- Given the initial view of a static object: <image_start>[image_1]<image_end>.
19
-
20
- Imagine looking at the setup from a bird's-eye view (from directly above) to determine the direction. Now, move the camera {angle} {direction} around the object.
21
-
22
- Which of the following images shows what the object looks like from this new position?
23
-
24
- A. <image_start>[image_A]<image_end>
25
- B. <image_start>[image_B]<image_end>
26
- C. <image_start>[image_C]<image_end>
27
- D. <image_start>[image_D]<image_end>
28
-
29
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/action_state/gen_task1.py ADDED
@@ -0,0 +1,275 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 给定一张物体在初始视角的图片,camera将围绕该静止物体进行水平移动。旋转的方向(顺时针或逆时针)是基于从物体正上方俯视(鸟瞰视角)的平面来定义的。模型需要根据给定的旋转方向和角度,推断出新视角下的物体图像,并从四个图像中选出正确的一项。
3
+
4
+ 注:{angle}就是 xx degrees, {direction} 就是 clockwise / anticlockwise
5
+
6
+ 1.
7
+ The object in the image <image_start>[image_1]<image_end> remains **static**. Imagine a camera rotating around this object. The direction of rotation is defined from a **top-down bird's-eye view**.
8
+
9
+ Please identify the view of the object after the camera rotates {angle} {direction} based on this top-down perspective, and select the correct answer.
10
+
11
+ A. <image_start>[image_A]<image_end>
12
+ B. <image_start>[image_B]<image_end>
13
+ C. <image_start>[image_C]<image_end>
14
+ D. <image_start>[image_D]<image_end>
15
+
16
+
17
+ 2.
18
+ Given the initial view of a static object: <image_start>[image_1]<image_end>.
19
+
20
+ Imagine looking at the setup from a bird's-eye view (from directly above) to determine the direction. Now, move the camera {angle} {direction} around the object.
21
+
22
+ Which of the following images shows what the object looks like from this new position?
23
+
24
+ A. <image_start>[image_A]<image_end>
25
+ B. <image_start>[image_B]<image_end>
26
+ C. <image_start>[image_C]<image_end>
27
+ D. <image_start>[image_D]<image_end>
28
+
29
+ """
30
+
31
+
32
+ import json
33
+ import os
34
+ import random
35
+ import numpy as np
36
+ from scipy.spatial.transform import Rotation as SciRotation
37
+
38
+ # --- 1. 引入你提供的 Utils 函数 ---
39
+
40
+ def get_relative_horizontal_rotation(root_path, category, sequence_number, frame_idx_1, frame_idx_2, annotations_cache=None):
41
+ """
42
+ 计算 frame_2 相对于 frame_1 在水平方向上的旋转角度 (Yaw)。
43
+ 增加了 annotations_cache 以避免重复读取大文件。
44
+ """
45
+ if annotations_cache is None:
46
+ json_path = os.path.join(root_path, 'data', 'original', category, 'frame_annotations.json')
47
+ with open(json_path, 'r') as f:
48
+ annotations = json.load(f)
49
+ else:
50
+ annotations = annotations_cache
51
+
52
+ R1 = None
53
+ R2 = None
54
+
55
+ for item in annotations:
56
+ if item.get('sequence_name') != sequence_number:
57
+ continue
58
+ if item['frame_number'] == frame_idx_1:
59
+ R1 = np.array(item['viewpoint']['R'])
60
+ elif item['frame_number'] == frame_idx_2:
61
+ R2 = np.array(item['viewpoint']['R'])
62
+ if R1 is not None and R2 is not None:
63
+ break
64
+
65
+ if R1 is None or R2 is None:
66
+ return None
67
+
68
+ # 计算相对旋转 R_rel = R2 @ R1.T
69
+ R_rel = R2 @ R1.T
70
+
71
+ # 转换为欧拉角提取水平分量 (Y轴)
72
+ r = SciRotation.from_matrix(R_rel)
73
+ euler_angles = r.as_euler('xyz', degrees=True)
74
+ horizontal_rotation = euler_angles[1]
75
+
76
+ return horizontal_rotation
77
+
78
+ # --- 2. 核心生成逻辑 ---
79
+
80
+ class CO3DQuestionGenerator:
81
+ def __init__(self, root_path, category, output_file="co3d_rotation_questions.json"):
82
+ self.root_path = root_path
83
+ self.category = category
84
+ self.output_file = output_file
85
+
86
+ # 预加载 annotations 以提高速度
87
+ print(f"Loading annotations for category: {self.category}...")
88
+ json_path = os.path.join(root_path, 'data', 'original', category, 'frame_annotations.json')
89
+ with open(json_path, 'r') as f:
90
+ self.annotations = json.load(f)
91
+
92
+ # 整理数据结构:Sequence -> List of Frames
93
+ self.seq_map = {}
94
+ for item in self.annotations:
95
+ seq_name = item['sequence_name']
96
+ if seq_name not in self.seq_map:
97
+ self.seq_map[seq_name] = []
98
+ self.seq_map[seq_name].append(item['frame_number'])
99
+
100
+ # 确保帧是排序的,虽然对于随机采样不是必须的,但有助于调试
101
+ for seq in self.seq_map:
102
+ self.seq_map[seq].sort()
103
+
104
+ def format_angle_direction(self, angle):
105
+ """
106
+ 将带符号的角度转换为绝对值角度和方向字符串。
107
+ 假设:正值 = 顺时针 (Clockwise), 负值 = 逆时针 (Anticlockwise)
108
+ 注意:具体正负定义取决于坐标系,这里基于 utils 中的逻辑假设。
109
+ """
110
+ # 归一化到 -180 到 180
111
+ angle = (angle + 180) % 360 - 180
112
+
113
+ direction = "clockwise" if angle > 0 else "anticlockwise"
114
+ abs_angle = abs(angle)
115
+
116
+ return int(round(abs_angle)), direction
117
+
118
+ def get_image_path(self, sequence_name, frame_idx):
119
+ # 假设图片路径结构,根据实际情况调整
120
+ # 例如: data/original/motorcycle/sequence_name/frame00001.jpg
121
+ # 这里假设 frame_idx 是整数,文件名可能是 0 填充的
122
+ # 注意:CO3D 的文件名通常在 annotation 的 'image' 字段里,这里为了简化直接拼凑,
123
+ # 如果文件名不规则,建议建立 frame_idx -> image_path 的映射。
124
+
125
+ # 更稳健的方法是从 annotations 找 image path
126
+ for item in self.annotations:
127
+ if item['sequence_name'] == sequence_name and item['frame_number'] == frame_idx:
128
+ # item['image']['path'] 通常是相对路径
129
+ return os.path.join(self.root_path, item['image']['path'])
130
+ return None
131
+
132
+ def generate_sample(self, sequence_name):
133
+ frames = self.seq_map[sequence_name]
134
+ if len(frames) < 5: # 至少需要 1个起始 + 1个正确 + 3个干扰
135
+ return None
136
+
137
+ # 1. 随机选择起始帧 (Start Frame)
138
+ start_frame_idx = random.choice(frames)
139
+
140
+ # 2. 随机选择目标帧 (Target Frame / Correct Answer)
141
+ # 为了保证题目有意义,角度最好不要太小(比如 < 10度)
142
+ possible_targets = [f for f in frames if f != start_frame_idx]
143
+ random.shuffle(possible_targets)
144
+
145
+ target_frame_idx = None
146
+ angle_val = 0
147
+
148
+ for t_idx in possible_targets:
149
+ yaw = get_relative_horizontal_rotation(
150
+ self.root_path, self.category, sequence_name,
151
+ start_frame_idx, t_idx, self.annotations
152
+ )
153
+ if yaw is None: continue
154
+
155
+ # 过滤掉角度过小的样本 (例如小于 15 度)
156
+ if abs(yaw) > 15:
157
+ target_frame_idx = t_idx
158
+ angle_val = yaw
159
+ break
160
+
161
+ if target_frame_idx is None:
162
+ return None
163
+
164
+ # 3. 选择干扰项 (Distractors)
165
+ # 干扰项不能是起始帧,也不能是目标帧
166
+ remaining_frames = [f for f in frames if f != start_frame_idx and f != target_frame_idx]
167
+ if len(remaining_frames) < 3:
168
+ return None
169
+
170
+ distractor_indices = random.sample(remaining_frames, 3)
171
+
172
+ # 4. 准备数据
173
+ angle_deg, direction_str = self.format_angle_direction(angle_val)
174
+
175
+ # 获取图片路径
176
+ img_start = self.get_image_path(sequence_name, start_frame_idx)
177
+ img_correct = self.get_image_path(sequence_name, target_frame_idx)
178
+ img_distractors = [self.get_image_path(sequence_name, idx) for idx in distractor_indices]
179
+
180
+ if not all([img_start, img_correct] + img_distractors):
181
+ return None
182
+
183
+ # 5. 构建选项
184
+ options = [img_correct] + img_distractors
185
+ random.shuffle(options) # 打乱选项顺序
186
+
187
+ correct_option_idx = options.index(img_correct)
188
+ option_labels = ['A', 'B', 'C', 'D']
189
+ correct_answer_label = option_labels[correct_option_idx]
190
+
191
+ # 6. 构建 Prompt (随机选择模板 1 或 2)
192
+ template_id = random.choice([1, 2])
193
+
194
+ prompt = ""
195
+ if template_id == 1:
196
+ prompt = f"""The object in the image <image_start>[image_1]<image_end> remains **static**. Imagine a camera rotating around this object. The direction of rotation is defined from a **top-down bird's-eye view**.
197
+
198
+ Please identify the view of the object after the camera rotates {angle_deg} degrees {direction_str} based on this top-down perspective, and select the correct answer.
199
+
200
+ A. <image_start>[image_A]<image_end>
201
+ B. <image_start>[image_B]<image_end>
202
+ C. <image_start>[image_C]<image_end>
203
+ D. <image_start>[image_D]<image_end>"""
204
+ else:
205
+ prompt = f"""Given the initial view of a static object: <image_start>[image_1]<image_end>.
206
+
207
+ Imagine looking at the setup from a bird's-eye view (from directly above) to determine the direction. Now, move the camera {angle_deg} degrees {direction_str} around the object.
208
+
209
+ Which of the following images shows what the object looks like from this new position?
210
+
211
+ A. <image_start>[image_A]<image_end>
212
+ B. <image_start>[image_B]<image_end>
213
+ C. <image_start>[image_C]<image_end>
214
+ D. <image_start>[image_D]<image_end>"""
215
+
216
+ # 构建返回对象
217
+ sample = {
218
+ "id": f"{sequence_name}_{start_frame_idx}_{target_frame_idx}",
219
+ "sequence": sequence_name,
220
+ "category": self.category,
221
+ "prompt": prompt,
222
+ "images": {
223
+ "image_1": img_start,
224
+ "image_A": options[0],
225
+ "image_B": options[1],
226
+ "image_C": options[2],
227
+ "image_D": options[3]
228
+ },
229
+ "metadata": {
230
+ "start_frame": start_frame_idx,
231
+ "target_frame": target_frame_idx,
232
+ "angle_degrees": angle_deg,
233
+ "direction": direction_str,
234
+ "raw_yaw": angle_val
235
+ },
236
+ "answer": correct_answer_label
237
+ }
238
+ return sample
239
+
240
+ def run(self, num_samples=100):
241
+ results = []
242
+ sequences = list(self.seq_map.keys())
243
+
244
+ count = 0
245
+ attempts = 0
246
+ max_attempts = num_samples * 5 # 防止死循环
247
+
248
+ print(f"Start generating {num_samples} samples...")
249
+
250
+ while count < num_samples and attempts < max_attempts:
251
+ attempts += 1
252
+ seq = random.choice(sequences)
253
+ sample = self.generate_sample(seq)
254
+
255
+ if sample:
256
+ results.append(sample)
257
+ count += 1
258
+ if count % 10 == 0:
259
+ print(f"Generated {count}/{num_samples} samples...")
260
+
261
+ print(f"Finished. Saving to {self.output_file}")
262
+ with open(self.output_file, 'w') as f:
263
+ json.dump(results, f, indent=4)
264
+
265
+ # --- 3. 运行脚本 ---
266
+
267
+ if __name__ == "__main__":
268
+ # 配置参数
269
+ ROOT_PATH = "/run/determined/NAS1/public/lixinyuan/interleaved-co3d" # 你的数据根目录
270
+ CATEGORY = "motorcycle" # 你的类别
271
+ OUTPUT_JSON = "co3d_motorcycle_questions.json"
272
+ NUM_SAMPLES = 50 # 你想生成的题目数量
273
+
274
+ generator = CO3DQuestionGenerator(ROOT_PATH, CATEGORY, OUTPUT_JSON)
275
+ generator.run(NUM_SAMPLES)
{action_state → src/action_state}/gen_task2.py RENAMED
File without changes
{action_state → src/action_state}/gen_task3.py RENAMED
File without changes
{action_state → src/action_state}/gen_task4.py RENAMED
File without changes
{action_state → src/action_state}/utils.py RENAMED
@@ -15,7 +15,7 @@ def get_relative_horizontal_rotation(root_path, category, sequence_number, frame
15
  frame_idx_2 (int): 目标帧号 (例如 7)
16
 
17
  Returns:
18
- float: 相对旋转角度(单位:度 Degree),正值通常表示向右/逆时针旋转,取决于坐标系定义
19
  如果找不到文件或帧,返回 None。
20
  """
21
  json_path = os.path.join(root_path, 'data', 'original', category, 'frame_annotations.json')
 
15
  frame_idx_2 (int): 目标帧号 (例如 7)
16
 
17
  Returns:
18
+ float: 相对旋转角度(单位:度 Degree),正值表示时针旋转。
19
  如果找不到文件或帧,返回 None。
20
  """
21
  json_path = os.path.join(root_path, 'data', 'original', category, 'frame_annotations.json')