File size: 6,630 Bytes
ce209f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from __future__ import annotations

import json
import random
import re
from pathlib import Path
from typing import Iterable

import numpy as np
import torch
from PIL import Image, ImageDraw

from datasets.cd_dataset import CDDataset


ROOT = Path(__file__).resolve().parents[1]


def safe_sample_id(sample_id: str) -> str:
    return re.sub(r"[^A-Za-z0-9_.-]+", "_", str(sample_id))


def _boundary_density(mask: np.ndarray) -> float:
    mask = mask.astype(bool)
    if not mask.any():
        return 0.0
    horiz = np.zeros_like(mask, dtype=bool)
    vert = np.zeros_like(mask, dtype=bool)
    horiz[:, 1:] = mask[:, 1:] != mask[:, :-1]
    vert[1:, :] = mask[1:, :] != mask[:-1, :]
    return float((horiz | vert).sum()) / float(mask.size)


def qualitative_manifest_path(dataset_name: str) -> Path:
    return ROOT / "results" / "qualitative_samples" / dataset_name / "sample_manifest.json"


def select_or_load_manifest(
    dataset_cfg: dict,
    count: int = 20,
    seed: int = 3407,
    force: bool = False,
) -> dict:
    dataset_name = dataset_cfg["name"]
    path = qualitative_manifest_path(dataset_name)
    if path.exists() and not force:
        with path.open("r", encoding="utf-8") as f:
            return json.load(f)

    ds = CDDataset(dataset_cfg["data_root"], "test", cfg=dataset_cfg, normalize=False, return_format="tuple")
    rng = random.Random(seed)
    rows = []
    for index, (a_path, b_path, mask_path, sample_id) in enumerate(ds.samples):
        if not (a_path.is_file() and b_path.is_file() and mask_path.is_file()):
            continue
        mask = np.asarray(Image.open(mask_path).convert("L").resize((ds.image_size, ds.image_size), Image.NEAREST))
        binary = mask > (0 if mask.max() <= 1 else ds.threshold)
        ratio = float(binary.mean())
        boundary = _boundary_density(binary)
        if ratio <= 0.0 or ratio >= 0.85 or boundary <= 0.0:
            continue
        rows.append({
            "index": index,
            "sample_id": sample_id,
            "a_path": str(a_path),
            "b_path": str(b_path),
            "mask_path": str(mask_path),
            "changed_ratio": ratio,
            "boundary_density": boundary,
            "tie": rng.random(),
        })

    if len(ds) >= count and len(rows) < count:
        raise RuntimeError(
            f"Only {len(rows)} visually useful test samples found for {dataset_name}, "
            f"but {count} are required from {len(ds)} total test samples."
        )

    rows = sorted(rows, key=lambda r: (-r["boundary_density"], -r["changed_ratio"], r["sample_id"], r["tie"]))
    selected = rows[: min(count, len(rows))]
    for rank, row in enumerate(selected, start=1):
        row["rank"] = rank
        row.pop("tie", None)
    path.parent.mkdir(parents=True, exist_ok=True)
    manifest = {
        "dataset": dataset_name,
        "split": "test",
        "seed": seed,
        "requested_count": count,
        "selected_count": len(selected),
        "total_test_samples": len(ds),
        "selection_rule": "non-empty mask, changed_ratio < 0.85, positive boundary density, sorted deterministically",
        "samples": selected,
    }
    with path.open("w", encoding="utf-8") as f:
        json.dump(manifest, f, indent=2, sort_keys=True)
    return manifest


def manifest_ids(manifest: dict) -> set[str]:
    return {str(row["sample_id"]) for row in manifest.get("samples", [])}


def denormalize(tensor: torch.Tensor, mean: list[float], std: list[float]) -> torch.Tensor:
    if tensor.ndim == 3:
        mean_t = torch.tensor(mean, dtype=tensor.dtype, device=tensor.device).view(3, 1, 1)
        std_t = torch.tensor(std, dtype=tensor.dtype, device=tensor.device).view(3, 1, 1)
    else:
        mean_t = torch.tensor(mean, dtype=tensor.dtype, device=tensor.device).view(1, 3, 1, 1)
        std_t = torch.tensor(std, dtype=tensor.dtype, device=tensor.device).view(1, 3, 1, 1)
    return tensor * std_t + mean_t


def tensor_to_rgb_image(tensor: torch.Tensor) -> Image.Image:
    if tensor.ndim == 4:
        tensor = tensor[0]
    arr = tensor.detach().cpu().float().clamp(0, 1).numpy()
    if arr.shape[0] == 1:
        arr = np.repeat(arr, 3, axis=0)
    arr = np.transpose(arr[:3], (1, 2, 0))
    return Image.fromarray((arr * 255).astype(np.uint8), mode="RGB")


def mask_to_image(mask: torch.Tensor | np.ndarray) -> Image.Image:
    if torch.is_tensor(mask):
        arr = mask.detach().cpu().numpy()
    else:
        arr = mask
    arr = np.squeeze(arr)
    arr = (arr > 0).astype(np.uint8) * 255
    return Image.fromarray(arr, mode="L").convert("RGB")


def overlay_image(gt: torch.Tensor, pred: torch.Tensor) -> Image.Image:
    gt_arr = np.squeeze(gt.detach().cpu().numpy()).astype(bool)
    pred_arr = np.squeeze(pred.detach().cpu().numpy()).astype(bool)
    rgb = np.zeros((*gt_arr.shape, 3), dtype=np.uint8)
    rgb[gt_arr & pred_arr] = (255, 255, 255)
    rgb[pred_arr & ~gt_arr] = (255, 64, 64)
    rgb[gt_arr & ~pred_arr] = (64, 160, 255)
    return Image.fromarray(rgb, mode="RGB")


def save_binary_prediction(pred: torch.Tensor, path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    mask_to_image(pred).convert("L").save(path)


def save_probability_map(prob: torch.Tensor, path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    arr = np.squeeze(prob.detach().cpu().float().clamp(0, 1).numpy())
    Image.fromarray((arr * 255).astype(np.uint8), mode="L").save(path)


def save_visual_panel(
    a: torch.Tensor,
    b: torch.Tensor,
    gt: torch.Tensor,
    pred: torch.Tensor,
    out_path: Path,
    prob: torch.Tensor | None = None,
) -> None:
    tiles = [
        ("A", tensor_to_rgb_image(a)),
        ("B", tensor_to_rgb_image(b)),
        ("GT", mask_to_image(gt)),
        ("Pred", mask_to_image(pred)),
        ("Overlay", overlay_image(gt, pred)),
    ]
    if prob is not None:
        tiles.insert(4, ("Score", mask_to_image((prob >= 0.5).float())))
    tile_w, tile_h = tiles[0][1].size
    label_h = 24
    panel = Image.new("RGB", (tile_w * len(tiles), tile_h + label_h), "white")
    draw = ImageDraw.Draw(panel)
    for i, (label, img) in enumerate(tiles):
        x = i * tile_w
        panel.paste(img.resize((tile_w, tile_h), Image.NEAREST), (x, label_h))
        draw.text((x + 6, 5), label, fill=(0, 0, 0))
    out_path.parent.mkdir(parents=True, exist_ok=True)
    panel.save(out_path)


def rank_for_sample(manifest: dict, sample_id: str) -> int | None:
    for row in manifest.get("samples", []):
        if str(row["sample_id"]) == str(sample_id):
            return int(row["rank"])
    return None