File size: 2,822 Bytes
7c61ee6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Paper qualitative figure: test objects at fixed percentiles of our CD (no hand-picking).
Columns: stimulus | GT points | MinD-3D | ours | GT category mean | GT image (upper bound)."""
import glob
import json
import os

import numpy as np
import nvdiffrast.torch as dr
from PIL import Image, ImageDraw

from render_compare import (CAT_NAMES, GT_ROOT, O, STIM_DIR, cell_with_label, font, hstack, load_mesh,
                            render_mesh, render_points)

OURS = f"{O}/ft/frozen_0001/sub0001/test_coef_rescaled_cfg50"
COLS = [("MinD-3D", f"{O}/mind3d_sub01", "mind3d_official", "baseline"),
        ("Ours", OURS, "ft_frozen_sub01_k16_cfg50", "ours"),
        ("Ref: GT category mean", f"{O}/diag/category_mean", "diag_category_mean", "reference"),
        ("Ref: GT image", f"{O}/upper_bound_512_f24", "oracle_gt_image", "reference")]
PCTS = [5, 20, 35, 50, 65, 80, 95]
OUT = f"{O}/renders/paper"


def main():
    os.makedirs(OUT, exist_ok=True)
    gt_paths = {os.path.basename(p)[:-4]: p for p in glob.glob(f"{GT_ROOT}/*/_/*/*.npy")}
    rows = {k: {r["id"]: r for r in json.load(open(f"{d}/metrics.json"))["rows"]} for _, d, k, _ in COLS}
    ours = sorted(rows["ft_frozen_sub01_k16_cfg50"].values(), key=lambda r: r["cd"])
    picks = [ours[min(len(ours) - 1, int(round(p / 100 * (len(ours) - 1))))]["id"] for p in PCTS]
    ctx = dr.RasterizeCudaContext()
    lines = []
    for p, obj in zip(PCTS, picks):
        cat, uid = obj.split("/")
        name = f"{cat}_{uid}"
        cells = [cell_with_label(Image.open(f"{STIM_DIR}/{name}.png").convert("RGB"),
                                 f"{CAT_NAMES[cat]} ({p}th pct.)", "input"),
                 cell_with_label(render_points(np.load(gt_paths[uid])), "GT point cloud", "input")]
        for label, d, key, group in COLS:
            r = rows[key][obj]
            cache = f"{O}/renders/cells/{key}/{name}.png"
            if os.path.exists(cache):
                img = Image.open(cache)
            else:
                path = [q for q in glob.glob(f"{d}/meshes/{name}.*") if q.endswith((".ply", ".obj"))][0]
                img = render_mesh(ctx, *load_mesh(path, r["up"], r["azimuth"]))
                os.makedirs(os.path.dirname(cache), exist_ok=True)
                img.save(cache)
            cells.append(cell_with_label(img, label, group, f"CD {r['cd'] * 1e3:.1f}"))
        lines.append(hstack(cells))
        print(p, obj, flush=True)
    gap = 8
    sheet = Image.new("RGB", (lines[0].width, sum(r.height + gap for r in lines) - gap), "white")
    y = 0
    for r in lines:
        sheet.paste(r, (0, y))
        y += r.height + gap
    sheet.save(f"{OUT}/qualitative_percentiles.png")
    json.dump({"percentiles": PCTS, "ids": picks}, open(f"{OUT}/qualitative_percentiles.json", "w"), indent=1)


if __name__ == "__main__":
    main()