3DEval / fid2.py
slongle's picture
Upload fid2.py with huggingface_hub
05a1a46 verified
Raw History Blame Contribute Delete
2.17 kB
import os
import cv2
import json
import numpy as np
from glob import glob
from tqdm import tqdm
import torch
from torchvision import datasets, transforms, models
_ = torch.manual_seed(123)
from torchmetrics.image.fid import FrechetInceptionDistance
def get_imagenet_data(num=1000):
root_dir = "/ibex/user/dengj/3DEval/ImageNet/val"
img_paths = glob(os.path.join(root_dir, "*", "*.JPEG"))
img_paths = sorted(img_paths)
img_paths = img_paths[:num]
imgs = []
for img_path in tqdm(img_paths):
img = cv2.imread(img_path)
img = cv2.resize(img, (299, 299))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.transpose(2, 0, 1)
img = torch.tensor(img)
img = img.unsqueeze(0)
imgs.append(img)
imgs = torch.cat(imgs, dim=0)
return imgs
def get_method_data(method, prompt_idx):
image_root_dir = "/ibex/user/dengj/3DEval/GPTEval3D/data/tournament-3darena/methods"
root_dir = os.path.join(image_root_dir, method, str(prompt_idx), "0")
img_paths = glob(os.path.join(root_dir, "rgb_*.png"))
imgs = []
for img_path in tqdm(img_paths):
img = cv2.imread(img_path)
img = cv2.resize(img, (299, 299))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.transpose(2, 0, 1)
img = torch.tensor(img)
img = img.unsqueeze(0)
imgs.append(img)
imgs = torch.cat(imgs, dim=0)
return imgs
real_images = get_imagenet_data()
methods = ["Meshy", "Unique3D", "InstantMesh"]
fid_result = []
for prompt_idx in tqdm(range(51)):
_fids = []
for method_idx in range(3):
method = methods[method_idx]
gen_images = get_method_data(method=method, prompt_idx=prompt_idx)
fid = FrechetInceptionDistance(feature=64)
fid.update(real_images, real=True)
fid.update(gen_images, real=False)
fid_val = fid.compute().cpu().item()
print(fid_val)
_fids.append(fid_val)
fid_result.append(_fids)
print(fid_result)
json.dump(fid_result, open("fid_result.json", "w"), indent=4)
# Meshy: 37.8894
# Unique3D: 44.5088
# InstantMesh: 41.0059