Delete metrics.py
Browse files- metrics.py +0 -160
metrics.py
DELETED
|
@@ -1,160 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
-
import torchvision
|
| 6 |
-
from tqdm import tqdm
|
| 7 |
-
from PIL import Image
|
| 8 |
-
|
| 9 |
-
from gaussian_renderer import render
|
| 10 |
-
from gaussian_renderer import GaussianModel
|
| 11 |
-
from scene.cameras import Camera
|
| 12 |
-
from utils.graphics_utils import getWorld2View2, getProjectionMatrix
|
| 13 |
-
|
| 14 |
-
import lpips
|
| 15 |
-
import piq
|
| 16 |
-
from pytorch_fid import fid_score
|
| 17 |
-
from skimage.metrics import structural_similarity as ssim_fn
|
| 18 |
-
|
| 19 |
-
# ----------------------------
|
| 20 |
-
# Utils
|
| 21 |
-
# ----------------------------
|
| 22 |
-
def load_cameras(camera_json):
|
| 23 |
-
with open(camera_json, 'r') as f:
|
| 24 |
-
cams = json.load(f)
|
| 25 |
-
|
| 26 |
-
cameras = []
|
| 27 |
-
for cam in cams:
|
| 28 |
-
W2C = np.array(cam["world_view_transform"])
|
| 29 |
-
P = np.array(cam["projection_matrix"])
|
| 30 |
-
|
| 31 |
-
camera = Camera(
|
| 32 |
-
colmap_id=cam["id"],
|
| 33 |
-
R=W2C[:3, :3].T,
|
| 34 |
-
T=W2C[:3, 3],
|
| 35 |
-
FoVx=cam["FoVx"],
|
| 36 |
-
FoVy=cam["FoVy"],
|
| 37 |
-
image=torch.zeros(3, cam["height"], cam["width"]),
|
| 38 |
-
image_name=cam["image_name"],
|
| 39 |
-
uid=cam["id"]
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
camera.world_view_transform = torch.tensor(W2C).cuda()
|
| 43 |
-
camera.projection_matrix = torch.tensor(P).cuda()
|
| 44 |
-
camera.full_proj_transform = camera.world_view_transform @ camera.projection_matrix
|
| 45 |
-
camera.camera_center = camera.world_view_transform.inverse()[3, :3]
|
| 46 |
-
|
| 47 |
-
cameras.append(camera)
|
| 48 |
-
|
| 49 |
-
return cameras
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def load_gt(gt_dir, name):
|
| 53 |
-
img = Image.open(os.path.join(gt_dir, name + ".png")).convert("RGB")
|
| 54 |
-
img = torchvision.transforms.ToTensor()(img)
|
| 55 |
-
return img.cuda()
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# ----------------------------
|
| 59 |
-
# Render function
|
| 60 |
-
# ----------------------------
|
| 61 |
-
@torch.no_grad()
|
| 62 |
-
def render_ply(ply_path, cameras, out_dir, sh_degree=3):
|
| 63 |
-
os.makedirs(out_dir, exist_ok=True)
|
| 64 |
-
|
| 65 |
-
gaussians = GaussianModel(sh_degree)
|
| 66 |
-
gaussians.load_ply(ply_path)
|
| 67 |
-
gaussians = gaussians.cuda()
|
| 68 |
-
|
| 69 |
-
bg = torch.tensor([0, 0, 0], device="cuda", dtype=torch.float32)
|
| 70 |
-
|
| 71 |
-
rendered = {}
|
| 72 |
-
|
| 73 |
-
for cam in tqdm(cameras, desc=f"Rendering {os.path.basename(ply_path)}"):
|
| 74 |
-
img = render(
|
| 75 |
-
cam,
|
| 76 |
-
gaussians,
|
| 77 |
-
pipeline=None,
|
| 78 |
-
background=bg
|
| 79 |
-
)["render"].clamp(0, 1)
|
| 80 |
-
|
| 81 |
-
torchvision.utils.save_image(
|
| 82 |
-
img,
|
| 83 |
-
os.path.join(out_dir, cam.image_name + ".png")
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
rendered[cam.image_name] = img
|
| 87 |
-
|
| 88 |
-
return rendered
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
# ----------------------------
|
| 92 |
-
# Metrics
|
| 93 |
-
# ----------------------------
|
| 94 |
-
def compute_metrics(preds, gt_dir):
|
| 95 |
-
psnr, ssim, lp, niqe = [], [], [], []
|
| 96 |
-
|
| 97 |
-
lpips_fn = lpips.LPIPS(net='alex').cuda()
|
| 98 |
-
|
| 99 |
-
for name, pred in preds.items():
|
| 100 |
-
gt = load_gt(gt_dir, name)
|
| 101 |
-
|
| 102 |
-
psnr.append(piq.psnr(pred, gt).item())
|
| 103 |
-
ssim.append(
|
| 104 |
-
ssim_fn(
|
| 105 |
-
gt.permute(1,2,0).cpu().numpy(),
|
| 106 |
-
pred.permute(1,2,0).cpu().numpy(),
|
| 107 |
-
channel_axis=2,
|
| 108 |
-
data_range=1.0
|
| 109 |
-
)
|
| 110 |
-
)
|
| 111 |
-
lp.append(lpips_fn(pred.unsqueeze(0), gt.unsqueeze(0)).item())
|
| 112 |
-
niqe.append(piq.niqe(pred.unsqueeze(0)).item())
|
| 113 |
-
|
| 114 |
-
return {
|
| 115 |
-
"PSNR": np.mean(psnr),
|
| 116 |
-
"SSIM": np.mean(ssim),
|
| 117 |
-
"LPIPS": np.mean(lp),
|
| 118 |
-
"NIQE": np.mean(niqe)
|
| 119 |
-
}
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
def compute_fid(pred_dir, gt_dir):
|
| 123 |
-
return fid_score.calculate_fid_given_paths(
|
| 124 |
-
[pred_dir, gt_dir],
|
| 125 |
-
batch_size=8,
|
| 126 |
-
device="cuda",
|
| 127 |
-
dims=2048
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# ----------------------------
|
| 132 |
-
# Main
|
| 133 |
-
# ----------------------------
|
| 134 |
-
if __name__ == "__main__":
|
| 135 |
-
|
| 136 |
-
ply_a = "ply_a.ply"
|
| 137 |
-
ply_b = "ply_b.ply"
|
| 138 |
-
camera_json = "cameras.json"
|
| 139 |
-
gt_dir = "gt"
|
| 140 |
-
|
| 141 |
-
cameras = load_cameras(camera_json)
|
| 142 |
-
|
| 143 |
-
preds_a = render_ply(ply_a, cameras, "renders_a")
|
| 144 |
-
preds_b = render_ply(ply_b, cameras, "renders_b")
|
| 145 |
-
|
| 146 |
-
metrics_a = compute_metrics(preds_a, gt_dir)
|
| 147 |
-
metrics_b = compute_metrics(preds_b, gt_dir)
|
| 148 |
-
|
| 149 |
-
fid_a = compute_fid("renders_a", gt_dir)
|
| 150 |
-
fid_b = compute_fid("renders_b", gt_dir)
|
| 151 |
-
|
| 152 |
-
print("\n====== Model A ======")
|
| 153 |
-
for k, v in metrics_a.items():
|
| 154 |
-
print(f"{k}: {v:.4f}")
|
| 155 |
-
print(f"FID: {fid_a:.4f}")
|
| 156 |
-
|
| 157 |
-
print("\n====== Model B ======")
|
| 158 |
-
for k, v in metrics_b.items():
|
| 159 |
-
print(f"{k}: {v:.4f}")
|
| 160 |
-
print(f"FID: {fid_b:.4f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|