import json import random from pathlib import Path def generate_nested_prompts(bboxes, k_values=[1, 2, 4]): """ 生成嵌套的 prompt 框 先抽 k=4 的框,然后从中抽 k=2,再从中抽 k=1 如果 bboxes 数量 < 4,则全部使用并重复填充到 4 个 """ total_boxes = len(bboxes) # 如果框数不足,则重复采样直到达到 4 个 if total_boxes < 4: # 重复采样达到 4 个 k4_boxes = [] while len(k4_boxes) < 4: k4_boxes.extend(bboxes) k4_boxes = k4_boxes[:4] # k=2 和 k=1 也从这 4 个中抽 k2_boxes = random.sample(k4_boxes, min(2, len(k4_boxes))) k1_boxes = random.sample(k2_boxes, min(1, len(k2_boxes))) if k2_boxes else [] else: # 框数足够:先抽 4 个 k4_boxes = random.sample(bboxes, 4) # 从 4 个中抽 2 个 k2_boxes = random.sample(k4_boxes, 2) # 从 2 个中抽 1 个 k1_boxes = random.sample(k2_boxes, 1) return { 1: k1_boxes, 2: k2_boxes, 4: k4_boxes } def regenerate_fewshot_data(input_jsonl_path, output_dir): """ 重新生成 nested 格式的 few-shot 数据 """ output_dir = Path(output_dir) output_dir.mkdir(parents=True, exist_ok=True) output_files = { 1: open(output_dir / 'fewshot_k1_nested.jsonl', 'w', encoding='utf-8'), 2: open(output_dir / 'fewshot_k2_nested.jsonl', 'w', encoding='utf-8'), 4: open(output_dir / 'fewshot_k4_nested.jsonl', 'w', encoding='utf-8') } # 只保留 GT >= 4 的图片 total_images = 0 insufficient_count = 0 with open(input_jsonl_path, 'r', encoding='utf-8') as f: for line in f: data = json.loads(line.strip()) bboxes = data['bboxes'] # 只保留 GT >= 4 的图片 if len(bboxes) < 4: insufficient_count += 1 continue total_images += 1 # 生成嵌套的 prompt 框 nested_prompts = generate_nested_prompts(bboxes, [1, 2, 4]) # 为每个 k 值生成数据 for k in [1, 2, 4]: fewshot_data = { "image": data['image'], "image_path": data.get('image_path', data['image']), "width": data['width'], "height": data['height'], "k": k, "total_gt_boxes": len(bboxes), "prompt_boxes": nested_prompts[k], "all_boxes": bboxes, "prompt_boxes_count": len(nested_prompts[k]), "remaining_boxes_count": len(bboxes) - len(nested_prompts[k]) } output_files[k].write(json.dumps(fewshot_data, ensure_ascii=False) + '\n') # 关闭文件 for f in output_files.values(): f.close() print(f"总图片数(GT≥4): {total_images}") print(f"跳过的图片数(GT<4): {insufficient_count}") # 验证嵌套关系 print("\n验证嵌套关系(抽样检查)...") verify_nested_relationship(output_dir) def verify_nested_relationship(output_dir): """ 验证 k=1 的 prompt ⊆ k=2 的 prompt ⊆ k=4 的 prompt """ with open(output_dir / 'fewshot_k1_nested.jsonl', 'r') as f1, \ open(output_dir / 'fewshot_k2_nested.jsonl', 'r') as f2, \ open(output_dir / 'fewshot_k4_nested.jsonl', 'r') as f4: k1_data = [json.loads(line) for line in f1] k2_data = [json.loads(line) for line in f2] k4_data = [json.loads(line) for line in f4] # 检查前 10 张图片 all_nested = True for i in range(min(10, len(k1_data))): k1_boxes = [box['bbox'] for box in k1_data[i]['prompt_boxes']] k2_boxes = [box['bbox'] for box in k2_data[i]['prompt_boxes']] k4_boxes = [box['bbox'] for box in k4_data[i]['prompt_boxes']] # 检查子集关系 is_subset_1_in_2 = all(box in k2_boxes for box in k1_boxes) is_subset_2_in_4 = all(box in k4_boxes for box in k2_boxes) if not (is_subset_1_in_2 and is_subset_2_in_4): all_nested = False print(f" 图片 {i+1}: 嵌套关系失败!") else: print(f" 图片 {i+1}: ✓ 嵌套关系正确 (k1⊆k2⊆k4)") if all_nested: print("\n✅ 所有检查的图片嵌套关系都正确!") else: print("\n❌ 存在嵌套关系错误") def main(): input_jsonl = "/home/disk2/hjl/ICL_QWEN/ICL_benchmark/dataset.jsonl" output_dir = "/home/disk2/hjl/ICL_QWEN/ICL_benchmark/fewshot_data/nested" print("重新生成嵌套的 few-shot 数据...") regenerate_fewshot_data(input_jsonl, output_dir) print("\n" + "="*60) print("生成完成!输出文件:") print(f" - {output_dir}/fewshot_k1_nested.jsonl") print(f" - {output_dir}/fewshot_k2_nested.jsonl") print(f" - {output_dir}/fewshot_k4_nested.jsonl") print("\n现在三个文件的 prompt 框是嵌套的,可以公平对比!") if __name__ == "__main__": random.seed(42) main()