import os import cv2 import json import numpy as np from glob import glob from tqdm import tqdm import ImageReward as RM from PIL import Image, ImageDraw, ImageFont def mkdir(path): if not os.path.exists(path): os.makedirs(path) def add_text_to_image(image, text, position): # position: X, Y coordinates image = Image.fromarray(image) draw = ImageDraw.Draw(image) font = ImageFont.load_default(size=30) draw.text(position, text, (255, 255, 255), font=font) image = np.array(image) return image def main(): gpt_result_root = "/ibex/user/dengj/3DEval/GPTEval3D/comparisons/full-2024-Aug-30-02-22-02" vote_result_dir = "/ibex/user/dengj/3DEval/3DArena/data/votelog.json" data_root = "/ibex/user/dengj/3DEval/GPTEval3D/data/tournament-3darena/methods" vote_result = json.load(open(vote_result_dir)) gpt_result = json.load(open(os.path.join(gpt_result_root, "answered.json"))) fid_result = json.load(open("/ibex/user/dengj/3DEval/fid_result.json")) model = RM.load("ImageReward-v1.0") cmp_cnt = 0 correct_cnt = 0 image_correct_cnt = 0 fid_correct_cnt = 0 mkdir(os.path.join(gpt_result_root, "gpt")) not_match_cnt = 0 method_indices = {"Meshy": 0, "Unique3D": 1, "InstantMesh": 2} correct_result = [] for _, _v in gpt_result.items(): # if _ in ["57", "101"]: # continue prompt = _v["question"]["prompt"] prompt_id = _v["question"]["prompt_id"] method_0 = _v["question"]["m1"] method_1 = _v["question"]["m2"] method_idx_0 = method_indices[method_0] method_idx_1 = method_indices[method_1] cnt_0 = 0 cnt_1 = 0 for i in range(len(_v["answer"])): if _v["answer"][i][-1] == -1: cnt_0 += 1 else: cnt_1 += 1 gpt_cmp = cnt_0 / (cnt_0 + cnt_1) # method_0 win rate _prompt = prompt.replace(" ", "_") # print(_prompt) key_0 = f"{method_0}_{method_1}" key_1 = f"{method_1}_{method_0}" cnt_0 = vote_result[_prompt].get(key_0, 0) cnt_1 = vote_result[_prompt].get(key_1, 0) if cnt_0 + cnt_1 < 5: print(_, cnt_0, cnt_1) continue vote_cmp = cnt_0 / (cnt_0 + cnt_1) # method_0 win rate tag = (gpt_cmp > 0.5) == (vote_cmp >= 0.5) fid_cmp = 0 fid_0 = fid_result[int(prompt_id)][method_idx_0] fid_1 = fid_result[int(prompt_id)][method_idx_1] fid_cmp = fid_0 < fid_1 root_0 = os.path.join(data_root, method_0, f"{prompt_id}", "0") root_1 = os.path.join(data_root, method_1, f"{prompt_id}", "0") img_0_dir = [os.path.join(root_0, f"rgb_{i}.png") for i in [0, 30, 60, 90]] img_1_dir = [os.path.join(root_1, f"rgb_{i}.png") for i in [0, 30, 60, 90]] img_dir = img_0_dir + img_1_dir # print(img_dir) rewards = model.score(_prompt, img_dir) # print(rewards) s0 = np.mean(rewards[:4]) s1 = np.mean(rewards[4:]) # print(rewards) # print(s0, s1) mx_0 = np.max(rewards[:4]) mx_1 = np.max(rewards[4:]) image_cmp = s0 > s1 if image_cmp == (vote_cmp >= 0.5): image_correct_cnt += 1 if fid_cmp == (vote_cmp >= 0.5): fid_correct_cnt += 1 # tag = tag or (image_cmp == (vote_cmp >= 0.5)) print(_, gpt_cmp, vote_cmp, image_cmp, tag) cmp_cnt += 1 if tag: correct_cnt += 1 gpt_correct = bool((gpt_cmp > 0.5) == (vote_cmp >= 0.5)) fid_correct = bool(fid_cmp == (vote_cmp >= 0.5)) imagereward_correct = bool(image_cmp == (vote_cmp >= 0.5)) correct_result.append([gpt_correct, fid_correct, imagereward_correct]) # continue # continue img = cv2.imread(os.path.join(gpt_result_root, f"{_}.png")) H, W, C = img.shape img = add_text_to_image(img, f"{method_0}: {vote_cmp:.3f}/{cnt_0 + cnt_1}", (10, 10)) img = add_text_to_image( img, f"{method_1}: {1 - vote_cmp:.3f}/{cnt_0 + cnt_1}", (10 + W // 2, 10) ) img = add_text_to_image(img, f"imagereward: {s0:.3f}", (10, 90)) img = add_text_to_image(img, f"imagereward: {s1:.3f}", (10 + W // 2, 90)) img = add_text_to_image(img, f"FID: {fid_0}", (10, 130)) img = add_text_to_image(img, f"FID: {fid_1}", (10 + W // 2, 130)) if gpt_cmp > 0.5: img = add_text_to_image(img, f"GPTEval3D: Left", (10, 50)) else: img = add_text_to_image(img, f"GPTEval3D: Right", (10, 50)) img = add_text_to_image(img, f"Text prompt: {prompt}", (10, H // 2 - 50)) print(os.path.join(gpt_result_root, "gpt", f"{_}.png")) cv2.imwrite(os.path.join(gpt_result_root, "gpt", f"{_}.png"), img) # break print(f"{correct_cnt}/{cmp_cnt}") print(f"{image_correct_cnt}/{cmp_cnt}") print(f"{fid_correct_cnt}/{cmp_cnt}") json.dump(correct_result, open("correct_result.json", "w"), indent=4) """ 54/98 58/98 51/98 """ if __name__ == "__main__": main()