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 范围 输出: "" """ x1, y1, x2, y2 = bbox_scaled return f"" def format_prompt_boxes_string(prompt_boxes, img_width, img_height): """ 格式化 prompt boxes 为字符串,用逗号分隔 """ box_strings = [] for box in prompt_boxes: bbox = box['bbox'] # [x1, y1, x2, y2] 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 为字符串数组格式 返回: ["", "", ...] """ 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 字符串 prompt_boxes_str = format_prompt_boxes_string(prompt_boxes, img_width, img_height) # 格式化所有 boxes 为字符串数组 all_boxes_str_list = format_all_boxes_string(all_boxes, img_width, img_height) # 构建 assistant 的 response assistant_response = { "category": "objects", "bboxes": all_boxes_str_list } assistant_response_str = json.dumps(assistant_response, ensure_ascii=False) # 构建 conversations 数组 conversations = [ { "from": "human", "value": f"\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 转换为 JSON 字符串(转义后) 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(): # 输入目录(nested 数据) 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()