Upload VideoX-Fun/get_prompt_qa.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172import pandas as pdimport jsonimport pdbimport csvimport torchimport osimport itertoolsdf = pd.read_csv('/nfs/ywang29/video_reward/VideoAlign/datasets/train/ours_reasoning_dataset.csv')# df = pd.read_csv('/nfs/ywang29/Reward_finetuning/VideoX-Fun/ours_data_binary.csv', sep='\t')df = df.drop_duplicates(subset=['prompt'])
rg_dict = {}with open('/nfs/ywang29/vr_datasets.json', 'r') as f: data = json.load(f)for sample in data: # pdb.set_trace() rg_dict = [sample['video_id']] = sample['real_video']
prompts = []prompt_quesiton = {}
new_data = []top_row = ['video_id', 'real_video_path', 'ref_video', 'prompt', 'questions', 'gt_answers']new_data.append(top_row)
gen_list = ["WanI2V-480p-", "HunyuanI2V-360p-", "LTX-Video-720p-", "LTX-Video-t2v-720p-","Opensora2-256p-"]
# for idx, row in df.sample(n=2000, random_state=42).iterrows():for idx, row in df.iterrows():
# pdb.set_trace() prompts.append(row.get('prompt')) for idx in gen_list: if idx in row.get('path_A'): video_id = row.get('path_A').split(idx)[-1] break
# video_id = row['real_video_path'].split('/')[-1] uniq_id = video_id.split('.mp4')[0] if os.path.exists(f'/nfs/ywang29/Reward_finetuning/VideoX-Fun/data/preprocess_large/{uniq_id}.pt'): gt_answers = torch.load(f'/nfs/ywang29/Reward_finetuning/VideoX-Fun/data/preprocess_large/{uniq_id}.pt') # gt_answers = gt_answers.replace('\n', '') else: continue
# pdb.set_trace() ref_video = rg_dict[video_id] # ref_video = f'/s3-code/ywang29/video_reward/WanI2V/WanI2V-480p-{uniq_id}.mp4' for key in eval(row.get('extracted_answer_TA_A')): questions = [] # pdb.set_trace() description = eval(row.get('questions'))[key][0] # question = f'''Does the '{key}' of the video satisfies the following requirement: {description}?\n* Good: Most requirements are satisfied.\n* Bad: Part or most requirements are not satisfied.\nProvide your analysis and explanation in JSON with keys: score (e.g., Bad), explanation.''' question = f'''Given this AI-generated video, does it successfully fulfill the '{key}' condition: {description}? Respond with 'Yes' or 'No', Answer 'Yes' if the video largely matches the description. Answer 'No' if the video clearly contradicts the description. The presence of additional elements in the video is acceptable as long as they do not conflict with the core description. Please provide a brief explanation for your answer. \nProvide your analysis and explanation in JSON with keys: answer (e.g., Yes or No), explanation.''' questions.append(question) for item in gen_list: if item in row['path_A']: video_id = row['path_A'].split(item)[-1] pdb.set_trace() uniq_id = video_id.split('.mp4')[0] # if gt_answer = torch.load(f'/nfs/ywang29/Reward_finetuning/VideoX-Fun/data/preprocess/{uniq_id}.pt')['response']
new_line = [video_id, rg_dict[video_id], row.get('prompt'), questions]
new_data.append(new_line)
for q in eval(row.get('questions')): try: gt = gt_answers[q]['response'] gt = gt.replace('\n', '') except: continue # pdb.set_trace() q = q.replace('\n', '') new_line = [video_id, rg_dict[video_id], ref_video, row.get('prompt'), [q], [gt]] # pdb.set_trace() # new_data.append(new_line) vq_question = '''Compare the test video (the first 24 frames) to the reference frame (the last frame) and decide if the video shows obvious visual-quality (VQ) defects relative to the reference.Consider only: blur (defocus/motion), compression artifacts (blocking/ringing/mosquito), noise/grain, banding, flicker, rolling-shutter, aliasing/moire, over-smoothing.Return ONLY this JSON (no timestamps):{ "has_obvious_defect": Yes | No, "dominant_issue": "none|defocus_blur|motion_blur|blocking|ringing|mosquito_noise|grain_noise|banding|flicker|rolling_shutter|aliasing|moire|over_smoothing", "evidence": ["short visual cues vs reference, e.g., 'softer edges than reference', 'block edges visible around text'"],}Rules:- Compare against the reference frame’s look (sharpness, texture, edges, tones).- Be conservative; if unsure, choose false and note 'uncertain' in evidence.''' vq_gt = '''{\n \"has_obvious_defect\": No,\n \"dominant_issue\": \"none\",\n \"evidence\": []\n}''' phy_question = f'''You are a careful video forensics assistant. You are given two videos: a test video (the first 24 frames) which is ai-generated and a real video (the last 12 frames). The test video is generated by the caption of the real video. The caption is "{row.get('prompt')}"Your task is evaluating whether the test video shows physics-related defects. You should compared both videos and use the provided caption as high-level intent. Focus on physical plausibility, not style or aesthetics.You need to analyze it in these aspects:- Liquid flow irregularity – e.g., non-inertial or discontinuous flow, volume popping, gravity-inconsistent motion, impossible splashes.- Abnormal object deformation – e.g., rigid objects bending/stretching without cause, topology changes (parts merging/splitting).- Abnormal texture/material change – e.g., surface turns matte→glossy without cause, texture swimming/flicker detached from geometry.- Abnormal motion – e.g., inertia/acceleration violations, teleporting, time reversals, jitter not explained by camera motion.- Unnatural interpenetration – e.g., objects passing through each other or ground, missing collisions/contacts.If the prompt doesn't have related physical aspects, you should answer 'No'.Output strictly as JSON with this schema (no extra text):{{ "liquid_flow_irregularity": "Yes/No", "abnormal_deformation": "Yes/No", "abnormal_texture_change": "Yes/No", "abnormal_motion": "Yes/No", "unnatural_interpenetration": "Yes/No",}}''' phy_gt = '''```json\n{\n "liquid_flow_irregularity": "No",\n "abnormal_deformation": "No",\n "abnormal_texture_change": "No",\n "abnormal_motion": "No",\n "unnatural_interpenetration": "No"\n}\n```''' new_line = [video_id, rg_dict[video_id], ref_video, row.get('prompt'), [phy_question], [phy_gt]] new_data.append(new_line)
# gt_answers_per_sample = [] # question_list = [] # for q in eval(row.get('questions')): # try: # gt = gt_answers[q]['response'] # gt = gt.replace('\n', '') # gt_answers_per_sample.append(gt) # except: # continue # # pdb.set_trace() # q = q.replace('\n', '') # question_list.append(q) # num_answers = len(gt_answers_per_sample)
# # 条件1:如果答案数量大于等于3,则进行两两组合 # if num_answers >= 3: # # itertools.combinations 会生成所有不重复的索引对,例如 (0, 1), (0, 2), (1, 2), ... # index_combinations = itertools.combinations(range(num_answers), 2) # # 遍历每一个索引对 # for i, j in index_combinations: # # 根据索引对,创建问题和答案的配对列表 # question_pair = [question_list[i], question_list[j]] # answer_pair = [gt_answers_per_sample[i], gt_answers_per_sample[j]] # # 为这一对组合创建一个新的数据行 # new_line = [video_id, rg_dict[video_id], row.get('prompt'), question_pair, answer_pair] # # 将新行添加到 new_data 中 # new_data.append(new_line)
# # 条件2:如果答案数量为1或2,则保持原有逻辑 # elif num_answers > 0: # new_line = [video_id, rg_dict[video_id], row.get('prompt'), question_list, gt_answers_per_sample] # new_data.append(new_line)
# if len(gt_answers_per_sample) > 0: # new_line = [video_id, rg_dict[video_id], row.get('prompt'), question_list, gt_answers_per_sample] # new_data.append(new_line)
# new_line = [video_id, rg_dict[video_id], row.get('prompt'), questions]
# new_data.append(new_line)
with open('ours_data_binary_single_question_with_phy_only.csv', 'w', newline='', encoding='utf-8-sig') as csvfile: # 创建一个 csv writer 对象 writer = csv.writer(csvfile, delimiter='\t') # 写入所有数据行 writer.writerows(new_data)
# * Fair: Some requirements are satisfied but some not.VideoX-Fun/get_prompt_qa.pyAdded+172