| import os |
| import json |
| import shutil |
| from pathlib import Path |
|
|
| def filter_negative_samples(source_folder): |
| |
| source_path = Path(f"{source_folder}/train_sample_full") |
| target_path = Path(f"{source_folder}/negative") |
| |
| |
| target_path.mkdir(exist_ok=True) |
| |
| |
| json_files = list(source_path.glob("*.json")) |
| |
| count = 0 |
| for json_file in json_files: |
| |
| if "metadata" in json_file.name: |
| continue |
| |
| try: |
| with open(json_file, 'r', encoding='utf-8') as f: |
| data = json.load(f) |
| |
| |
| |
| ref_answers = data.get("pred_answer", []) |
| |
| is_negative = False |
| for ans_str in ref_answers: |
| |
| |
| if '"answer": "No"' in ans_str: |
| is_negative = True |
| break |
| |
| |
| if is_negative: |
| |
| file_stem = json_file.stem |
| video_file = source_path / f"{file_stem}.mp4" |
| |
| |
| shutil.copy2(json_file, target_path / json_file.name) |
| |
| |
| if video_file.exists(): |
| shutil.copy2(video_file, target_path / video_file.name) |
| shutil.copy2(video_file, target_path / video_file.name) |
|
|
| print(f"已复制: {file_stem}") |
| else: |
| print(f"警告: 找到负例但未找到对应视频 {video_file.name}") |
| |
| count += 1 |
| |
| except Exception as e: |
| print(f"处理文件 {json_file.name} 时出错: {e}") |
|
|
| print(f"\n处理完成!共提取 {count} 组负样本到 negative 文件夹。") |
|
|
| |
| |
| FOLDER_PATH = "/nfs/ywang29/Reward_finetuning/VideoX-Fun/output_sd_object2/" |
|
|
| if __name__ == "__main__": |
| filter_negative_samples(FOLDER_PATH) |