| import json |
| import random |
| from pathlib import Path |
|
|
| def scale_bbox_to_1023(bbox, img_width, img_height): |
| """ |
| 将 bbox 坐标缩放到 0-1023 范围 |
| bbox: [x1, y1, x2, y2] |
| """ |
| x1, y1, x2, y2 = bbox |
| x1_scaled = int(x1 / img_width * 1023) |
| y1_scaled = int(y1 / img_height * 1023) |
| x2_scaled = int(x2 / img_width * 1023) |
| y2_scaled = int(y2 / img_height * 1023) |
| return [x1_scaled, y1_scaled, x2_scaled, y2_scaled] |
|
|
| def bbox_to_token_string(bbox_scaled): |
| """ |
| 将缩放后的 bbox 转换为特殊 token 字符串 |
| 输入: [x1, y1, x2, y2] 每个值在 0-1023 范围 |
| 输出: "<x{x1}><y{y1}><x{x2}><y{y2}>" |
| """ |
| x1, y1, x2, y2 = bbox_scaled |
| return f"<x{x1}><y{y1}><x{x2}><y{y2}>" |
|
|
| def format_prompt_boxes_string(prompt_boxes, img_width, img_height): |
| """ |
| 格式化 prompt boxes 为字符串,用逗号分隔 |
| """ |
| box_strings = [] |
| for box in prompt_boxes: |
| bbox = box['bbox'] |
| bbox_scaled = scale_bbox_to_1023(bbox, img_width, img_height) |
| box_strings.append(bbox_to_token_string(bbox_scaled)) |
| return ", ".join(box_strings) |
|
|
| def format_all_boxes_string(all_boxes, img_width, img_height): |
| """ |
| 格式化所有 boxes 为字符串数组格式 |
| 返回: ["<x...><y...>", "<x...><y...>", ...] |
| """ |
| box_strings = [] |
| for box in all_boxes: |
| bbox = box['bbox'] |
| bbox_scaled = scale_bbox_to_1023(bbox, img_width, img_height) |
| box_strings.append(bbox_to_token_string(bbox_scaled)) |
| return box_strings |
|
|
| def convert_to_conversation_format(input_jsonl_path, output_jsonl_path): |
| """ |
| 将 few-shot JSONL 转换为对话格式 |
| """ |
| with open(input_jsonl_path, 'r', encoding='utf-8') as f_in, \ |
| open(output_jsonl_path, 'w', encoding='utf-8') as f_out: |
| |
| for line_num, line in enumerate(f_in, 1): |
| data = json.loads(line.strip()) |
| |
| img_width = data['width'] |
| img_height = data['height'] |
| prompt_boxes = data['prompt_boxes'] |
| all_boxes = data['all_boxes'] |
| |
| |
| prompt_boxes_str = format_prompt_boxes_string(prompt_boxes, img_width, img_height) |
| |
| |
| all_boxes_str_list = format_all_boxes_string(all_boxes, img_width, img_height) |
| |
| |
| assistant_response = { |
| "category": "objects", |
| "bboxes": all_boxes_str_list |
| } |
| assistant_response_str = json.dumps(assistant_response, ensure_ascii=False) |
| |
| |
| conversations = [ |
| { |
| "from": "human", |
| "value": f"<image>\nPlease detect all objects belonging to the same category as the boxes [{prompt_boxes_str}] in the image." |
| }, |
| { |
| "from": "gpt", |
| "value": assistant_response_str |
| } |
| ] |
| |
| |
| conversations_str = json.dumps(conversations, ensure_ascii=False) |
| |
| |
| output_line = { |
| "image": data['image'], |
| "conversations": conversations_str |
| } |
| |
| f_out.write(json.dumps(output_line, ensure_ascii=False) + '\n') |
| |
| if line_num % 1000 == 0: |
| print(f"已处理 {line_num} 条...") |
| |
| print(f"完成!输出文件: {output_jsonl_path}") |
|
|
| def process_all_k_values(input_dir, output_dir, k_values=[1, 2, 4]): |
| """ |
| 处理所有 k 值的文件 |
| """ |
| input_dir = Path(input_dir) |
| output_dir = Path(output_dir) |
| output_dir.mkdir(parents=True, exist_ok=True) |
| |
| for k in k_values: |
| print(f"\n处理 k={k}...") |
| input_file = input_dir / f"fewshot_k{k}_nested.jsonl" |
| output_file = output_dir / f"conversation_k{k}.jsonl" |
| |
| if not input_file.exists(): |
| print(f"警告: {input_file} 不存在,跳过") |
| continue |
| |
| convert_to_conversation_format(input_file, output_file) |
| |
| |
| print("\n" + "="*60) |
| print("转换完成!输出文件:") |
| for k in k_values: |
| output_file = output_dir / f"conversation_k{k}.jsonl" |
| if output_file.exists(): |
| size_mb = output_file.stat().st_size / 1024 / 1024 |
| print(f" - {output_file} ({size_mb:.2f} MB)") |
|
|
| def main(): |
| |
| input_dir = Path("/home/disk2/hjl/ICL_QWEN/ICL_benchmark/fewshot_data/nested") |
| |
| |
| output_dir = Path("/home/disk2/hjl/ICL_QWEN/ICL_benchmark/fewshot_data/conversation") |
| |
| process_all_k_values(input_dir, output_dir, k_values=[1, 2, 4]) |
|
|
| if __name__ == "__main__": |
| main() |