-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupiqal_cli.py
More file actions
1502 lines (1313 loc) · 60 KB
/
Copy pathupiqal_cli.py
File metadata and controls
1502 lines (1313 loc) · 60 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""UPIQAL CLI — Standalone command-line tool for Full-Reference Image Quality Assessment.
Runs the five-module UPIQAL pipeline on a reference/target image pair and
saves diagnostic heatmaps (PNG) and a numerical report (JSON) to a
uniquely named output directory.
Modules executed sequentially:
1. Normalizer — Minmax scaling + ImageNet normalization
2. ChromaticTransportEval — Oklab EMD color degradation map
3. DeepStatisticalExtr — VGG16 A-DISTS adaptive features
4. UncertaintyMapper — Mahalanobis distance anomaly map
5. SpatialHeuristics — JPEG blocking + Gibbs ringing masks
Usage:
python upiqal_cli.py --reference ref.png --target tgt.png
python upiqal_cli.py --reference ref.png --target tgt.png --name experiment1
python upiqal_cli.py --reference ref.png --target tgt.png --max-side 256
python upiqal_cli.py --reference ref.raw --target tgt.raw --width 640 --height 480 --pixel_format RGB888
python upiqal_cli.py --reference ref.nv21 --target tgt.nv21 --width 640 --height 480 --pixel_format NV21 --output_format npy
python upiqal_cli.py --reference ref.nv12 --target tgt.nv12 --width 640 --height 480 --pixel_format NV12
Dependencies:
torch>=2.0, torchvision>=0.15, numpy>=1.24, pillow>=10.0
"""
from __future__ import annotations
import argparse
import json
import os
import sys
import time
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, Optional
import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
# ---------------------------------------------------------------------------
# UPIQAL module imports (real implementations from the upiqal/ package)
# ---------------------------------------------------------------------------
from upiqal.normalize import Normalizer
from upiqal.color import ChromaticTransportEvaluator
from upiqal.features import DeepStatisticalExtractor
from upiqal.uncertainty import ProbabilisticUncertaintyMapper
from upiqal.heuristics import SpatialHeuristicsEngine
# Package version (imported for the report)
from upiqal import __version__ as UPIQAL_VERSION
# ======================================================================
# Raw / binary format constants
# ======================================================================
_RAW_EXTENSIONS = {".raw", ".bin", ".nv21", ".nv12", ".yuv", ".npy"}
_PIXEL_FORMATS = ("NV21", "NV12", "GRAY8", "RGB888")
# ======================================================================
# Image I/O helpers
# ======================================================================
def _yuv420sp_to_rgb(
data: bytes, width: int, height: int, chroma_order: str,
) -> np.ndarray:
"""Convert a YUV 4:2:0 semi-planar buffer to an ``(H, W, 3)`` uint8 RGB array.
Parameters
----------
data : bytes
Raw bytes of the YUV frame (Y plane followed by interleaved chroma).
width, height : int
Frame dimensions in pixels. Both must be > 0; for sub-sampled chroma
to round-trip exactly, even dimensions are recommended.
chroma_order : str
``"VU"`` for NV21 (V byte first, U byte second) or ``"UV"`` for NV12
(U byte first, V byte second).
"""
if width <= 0 or height <= 0:
raise ValueError(f"YUV420SP requires positive dimensions, got {width}x{height}")
if chroma_order not in ("VU", "UV"):
raise ValueError(f"chroma_order must be 'VU' or 'UV', got {chroma_order!r}")
y_size = width * height
# Chroma is sub-sampled by 2 in each axis; round up to keep the trailing
# row/column when the dimensions are odd.
cw = (width + 1) // 2
ch = (height + 1) // 2
uv_size = cw * ch * 2
expected = y_size + uv_size
if len(data) < expected:
fmt_name = "NV21" if chroma_order == "VU" else "NV12"
raise ValueError(
f"{fmt_name} data too short: expected {expected} bytes for "
f"{width}x{height}, got {len(data)}"
)
y_plane = (
np.frombuffer(data, dtype=np.uint8, count=y_size)
.reshape(height, width)
.astype(np.float32)
)
chroma = np.frombuffer(
data, dtype=np.uint8, offset=y_size, count=uv_size,
).reshape(ch, cw, 2)
if chroma_order == "VU":
v_plane = chroma[:, :, 0].astype(np.float32)
u_plane = chroma[:, :, 1].astype(np.float32)
else: # UV (NV12)
u_plane = chroma[:, :, 0].astype(np.float32)
v_plane = chroma[:, :, 1].astype(np.float32)
# Upsample chroma planes to full resolution by pixel-replication, then
# crop to the requested odd dimensions if necessary.
u_full = np.repeat(np.repeat(u_plane, 2, axis=0), 2, axis=1)[:height, :width]
v_full = np.repeat(np.repeat(v_plane, 2, axis=0), 2, axis=1)[:height, :width]
# YUV → RGB (BT.601 full-range coefficients used for standard NV21/NV12).
r = np.clip(y_plane + 1.370705 * (v_full - 128.0), 0, 255)
g = np.clip(y_plane - 0.337633 * (u_full - 128.0) - 0.698001 * (v_full - 128.0), 0, 255)
b = np.clip(y_plane + 1.732446 * (u_full - 128.0), 0, 255)
return np.stack([r, g, b], axis=-1).astype(np.uint8)
def _nv21_to_rgb(data: bytes, width: int, height: int) -> np.ndarray:
"""Convert NV21 (YUV420SP, VU interleaved) raw bytes to ``(H, W, 3)`` RGB."""
return _yuv420sp_to_rgb(data, width, height, chroma_order="VU")
def _nv12_to_rgb(data: bytes, width: int, height: int) -> np.ndarray:
"""Convert NV12 (YUV420SP, UV interleaved) raw bytes to ``(H, W, 3)`` RGB."""
return _yuv420sp_to_rgb(data, width, height, chroma_order="UV")
def _npy_array_to_rgb_uint8(arr: np.ndarray) -> np.ndarray:
"""Normalize an arbitrary ``np.load`` result to an ``(H, W, 3)`` uint8 RGB image.
Handles the shape and dtype variations commonly produced by CV pipelines:
* 2D ``(H, W)`` grayscale -> tiled to RGB.
* 3D ``(H, W, 1)`` -> squeezed and tiled to RGB.
* 3D ``(H, W, 3)`` / ``(H, W, 4)`` -> kept (alpha dropped).
* 3D ``(3, H, W)`` channels-first -> transposed.
* 4D leading singleton (``(1, ...)``) -> squeezed and re-evaluated.
Integer dtypes are rescaled by their full ``iinfo.max`` range; floating
dtypes have ``NaN``/``Inf`` replaced with zero, and are scaled from
``[0, 1]`` if the observed maximum is <= 1.001, otherwise clipped to
``[0, 255]``.
"""
if not isinstance(arr, np.ndarray):
raise ValueError(f".npy payload is not an ndarray: {type(arr)!r}")
if arr.size == 0:
raise ValueError(f".npy array is empty (shape={arr.shape})")
# ---- Shape normalization --------------------------------------------
# Squeeze leading singleton batch dimensions like (1, H, W, 3).
while arr.ndim == 4 and arr.shape[0] == 1:
arr = arr[0]
if arr.ndim == 2:
arr = np.stack([arr, arr, arr], axis=-1)
elif arr.ndim == 3:
h0, h1, h2 = arr.shape
if h2 == 1:
arr = np.repeat(arr, 3, axis=2)
elif h2 == 3:
pass # already (H, W, 3)
elif h2 == 4:
arr = arr[:, :, :3] # drop alpha
elif h0 == 3 and h2 != 3:
# Channels-first (3, H, W) -> (H, W, 3)
arr = np.transpose(arr, (1, 2, 0))
elif h0 == 1:
arr = np.repeat(np.transpose(arr, (1, 2, 0)), 3, axis=2)
else:
raise ValueError(
f".npy array has unsupported 3D shape {arr.shape}; "
"expected (H, W), (H, W, 1), (H, W, 3/4), or (3, H, W)"
)
else:
raise ValueError(
f".npy array has unsupported ndim={arr.ndim} (shape={arr.shape}); "
"expected 2D grayscale or 3D RGB"
)
# ---- dtype normalization --------------------------------------------
if arr.dtype == np.uint8:
return np.ascontiguousarray(arr)
if np.issubdtype(arr.dtype, np.integer):
info = np.iinfo(arr.dtype)
max_val = max(int(info.max), 1)
scaled = arr.astype(np.float32) * (255.0 / max_val)
return np.clip(scaled, 0, 255).astype(np.uint8)
if np.issubdtype(arr.dtype, np.floating):
f = np.nan_to_num(arr.astype(np.float32), nan=0.0, posinf=0.0, neginf=0.0)
peak = float(f.max()) if f.size else 0.0
if peak <= 1.001:
f = f * 255.0
return np.clip(f, 0, 255).astype(np.uint8)
raise ValueError(f".npy array has unsupported dtype {arr.dtype!r}")
def load_raw_image(
path: str, width: int, height: int, pixel_format: str,
) -> np.ndarray:
"""Load a raw/binary file and return an ``(H, W, 3)`` uint8 RGB array.
Supported pixel formats: ``NV21``, ``NV12``, ``GRAY8``, ``RGB888``.
Also handles ``.npy`` files (auto-detected from extension). The file
extensions ``.nv21`` / ``.nv12`` override the explicit *pixel_format*
argument so that callers don't need to repeat themselves.
"""
ext = Path(path).suffix.lower()
if ext == ".npy":
arr = np.load(path, allow_pickle=False)
return _npy_array_to_rgb_uint8(arr)
raw_bytes = Path(path).read_bytes()
fmt = pixel_format.upper()
# Extension-based override: .nv21/.nv12 are unambiguous.
if ext == ".nv21":
fmt = "NV21"
elif ext == ".nv12":
fmt = "NV12"
if fmt == "NV21":
return _nv21_to_rgb(raw_bytes, width, height)
if fmt == "NV12":
return _nv12_to_rgb(raw_bytes, width, height)
if fmt == "GRAY8":
expected = width * height
if expected <= 0:
raise ValueError(
f"GRAY8 requires positive width/height, got {width}x{height}"
)
if len(raw_bytes) < expected:
raise ValueError(
f"GRAY8 data too short: expected {expected} bytes, got {len(raw_bytes)}"
)
gray = np.frombuffer(raw_bytes, dtype=np.uint8, count=expected).reshape(height, width)
return np.stack([gray, gray, gray], axis=-1)
if fmt == "RGB888":
expected = width * height * 3
if expected <= 0:
raise ValueError(
f"RGB888 requires positive width/height, got {width}x{height}"
)
if len(raw_bytes) < expected:
raise ValueError(
f"RGB888 data too short: expected {expected} bytes, got {len(raw_bytes)}"
)
return np.frombuffer(raw_bytes, dtype=np.uint8, count=expected).reshape(height, width, 3)
raise ValueError(
f"Unsupported pixel_format: {pixel_format!r} (expected one of {_PIXEL_FORMATS})"
)
def _is_raw_file(path: str) -> bool:
"""Return True if the file extension indicates a raw/binary format."""
return Path(path).suffix.lower() in _RAW_EXTENSIONS
def _pyramid_target(hw: tuple[int, int], max_side: int) -> tuple[int, int]:
"""Return new (H, W) with aspect preserved and longer side at most max_side."""
h, w = hw
scale = max_side / float(max(h, w))
return (max(1, int(round(h * scale))), max(1, int(round(w * scale))))
def load_image_as_tensor(
path: str,
max_side: int = 512,
width: int = 0,
height: int = 0,
pixel_format: str = "RGB888",
) -> torch.Tensor:
"""Load an image file and return a ``(1, 3, H, W)`` float tensor in [0, 1].
For standard image files (PNG, JPEG, etc.) the *width*/*height*/*pixel_format*
parameters are ignored. For raw/binary files (``.raw``, ``.bin``, ``.nv21``,
``.npy``) the dimensions and pixel format are required.
The longer side is resized to at most *max_side* pixels (aspect ratio
preserved) using Lanczos resampling so that inference stays fast.
"""
if _is_raw_file(path):
if not width or not height:
ext = Path(path).suffix.lower()
if ext != ".npy":
raise ValueError(
f"--width and --height are required for raw file: {path}"
)
rgb = load_raw_image(path, width, height, pixel_format)
img = Image.fromarray(rgb)
else:
img = Image.open(path).convert("RGB")
w, h = img.size
if max(w, h) > max_side:
scale = max_side / max(w, h)
img = img.resize((int(w * scale), int(h * scale)), Image.LANCZOS)
arr = np.array(img, dtype=np.float32) / 255.0 # (H, W, 3)
tensor = torch.from_numpy(arr).permute(2, 0, 1).unsqueeze(0).contiguous() # (1, 3, H, W)
return tensor
def apply_jet_colormap(gray: np.ndarray) -> np.ndarray:
"""Apply a JET-like colormap to a [0, 1] grayscale array.
Returns an ``(H, W, 3)`` uint8 RGB image. Piecewise-linear
approximation matching ``web/main.py:_apply_colormap``.
Parameters
----------
gray : np.ndarray
2D array with values in [0, 1].
Returns
-------
np.ndarray
RGB image ``(H, W, 3)`` as uint8.
"""
x = np.clip(gray, 0.0, 1.0)
r = np.clip(1.5 - np.abs(4.0 * x - 3.0), 0, 1)
g = np.clip(1.5 - np.abs(4.0 * x - 2.0), 0, 1)
b = np.clip(1.5 - np.abs(4.0 * x - 1.0), 0, 1)
rgb = np.stack([r, g, b], axis=-1)
return (rgb * 255).astype(np.uint8)
# ======================================================================
# Composite diagnostic overlay ("all artefacts on one image")
# ======================================================================
# Colour per artefact class. Chosen to be perceptually separable under
# both protanopia and deuteranopia (tested against Coblis / Sim Daltonism).
# Note: "blocking" was removed from user-visible artefact outputs — the
# detector still runs internally (its severity feeds the heuristic
# penalty in the scoring formula), but it no longer appears in the
# composite overlay, the CLI PNG list, or the web heatmap gallery.
# "anomaly" is the generic catch-all for pixels where the probabilistic
# uncertainty mapper flags a deviation. It's placed FIRST in the
# palette + argmax order so that whenever anomaly severity ties or
# equals a specific class's severity, anomaly wins the pixel — the
# user's preferred behaviour is to treat anomaly as the primary /
# "main" artefact layer, with specific classes only taking over when
# they are strictly stronger than the attenuated anomaly channel.
# (R, G, B) uint8
_ARTIFACT_PALETTE: dict[str, tuple[int, int, int]] = {
"anomaly": (60, 200, 90), # bright green — generic anomaly (primary layer)
"structure": (232, 78, 132), # magenta — structural / texture divergence
"ringing": (224, 168, 0), # amber — edge proximity
"noise": (123, 75, 194), # purple — stochastic
"color_shift": (42, 176, 196), # cyan — chromatic
"blur": (88, 114, 160), # blue-gray — detail loss
}
# Display order for the composite legend (stable, independent of dict
# order). "anomaly" is FIRST so np.argmax returns it on ties — ties
# and close-calls between anomaly and a specific class both resolve to
# anomaly, matching the user preference "anomaly should be above".
_ARTIFACT_ORDER = ("anomaly", "structure", "ringing", "noise", "color_shift", "blur")
def compose_diagnostic_overlay(
target_rgb: np.ndarray,
masks: dict[str, np.ndarray],
threshold: float = 0.05,
alpha: float = 0.55,
draw_legend: bool = True,
) -> np.ndarray:
"""Blend the five artefact severity masks into a single RGB overlay.
Per-pixel argmax picks the **dominant** artefact class; the max
severity across classes picks the alpha weight. Pixels whose max
severity is below ``threshold`` are left untouched so clean regions
of the target show through.
Parameters
----------
target_rgb : np.ndarray
Target image, ``(H, W, 3)`` uint8.
masks : dict[str, np.ndarray]
Per-class severity maps in ``[0, 1]``; each entry is an
``(H, W)`` float array. Missing entries are treated as zero
(class silently skipped in the argmax). Accepted keys match
``_ARTIFACT_ORDER``; extras are ignored.
threshold : float
Pixels whose max severity is ≤ this value pass through to the
target unchanged (default 0.05).
alpha : float
Opacity scaling for the coloured overlay (default 0.55 — same as
``anomaly_overlay.png``).
draw_legend : bool
If ``True``, paint a small legend strip in the top-left corner
with one colour chip and label per class.
Returns
-------
np.ndarray
Blended ``(H, W, 3)`` uint8 RGB image.
"""
if target_rgb.ndim != 3 or target_rgb.shape[2] != 3:
raise ValueError(
f"target_rgb must be (H, W, 3); got shape {target_rgb.shape}"
)
h, w = target_rgb.shape[:2]
# Stack masks in a fixed class order. Missing classes are padded
# with zeros so the argmax still works.
stack = np.zeros((len(_ARTIFACT_ORDER), h, w), dtype=np.float32)
for i, name in enumerate(_ARTIFACT_ORDER):
m = masks.get(name)
if m is None:
continue
if m.shape != (h, w):
# Resize with bilinear interpolation via PIL so we don't add
# an OpenCV dependency.
m = np.array(
Image.fromarray((np.clip(m, 0, 1) * 255).astype(np.uint8)
).resize((w, h), Image.BILINEAR),
dtype=np.float32,
) / 255.0
stack[i] = np.clip(m, 0.0, 1.0).astype(np.float32)
# Per-pixel dominant class + its severity.
label_map = stack.argmax(axis=0) # (H, W) int in [0, 4]
sev_map = stack.max(axis=0) # (H, W) float in [0, 1]
# Build an (H, W, 3) RGB layer using the palette of the dominant class.
colour_lut = np.array(
[_ARTIFACT_PALETTE[n] for n in _ARTIFACT_ORDER], dtype=np.float32,
) # (5, 3)
colour_map = colour_lut[label_map] # (H, W, 3) float in [0, 255]
# Alpha = 0 where severity < threshold, else severity * alpha.
active = (sev_map > threshold).astype(np.float32)
alpha_mask = (sev_map * alpha * active)[..., None] # (H, W, 1)
blended = (
(1.0 - alpha_mask) * target_rgb.astype(np.float32)
+ alpha_mask * colour_map
)
blended = np.clip(blended, 0, 255).astype(np.uint8)
if draw_legend:
_draw_legend(blended)
return blended
def _draw_legend(rgb: np.ndarray) -> None:
"""Paint a small legend in the top-left corner of ``rgb`` in place."""
from PIL import ImageDraw, ImageFont
img = Image.fromarray(rgb)
draw = ImageDraw.Draw(img, "RGBA")
# Try the system's default TrueType; fall back to the bitmap font.
try:
font = ImageFont.truetype("Arial.ttf", size=12)
except Exception:
font = ImageFont.load_default()
pad = 6
chip_w, chip_h = 12, 12
line_h = chip_h + 4
# Measure the longest label to size the background box.
labels = [n.replace("_", " ") for n in _ARTIFACT_ORDER]
# Pillow 10+ removed font.getsize; use getbbox instead.
label_w = max((draw.textbbox((0, 0), lbl, font=font)[2] for lbl in labels), default=60)
box_w = pad * 2 + chip_w + 4 + label_w
box_h = pad * 2 + line_h * len(_ARTIFACT_ORDER)
draw.rectangle([(4, 4), (4 + box_w, 4 + box_h)], fill=(0, 0, 0, 170))
y = 4 + pad
for name, lbl in zip(_ARTIFACT_ORDER, labels):
colour = _ARTIFACT_PALETTE[name]
draw.rectangle(
[(4 + pad, y), (4 + pad + chip_w, y + chip_h)],
fill=colour,
)
draw.text(
(4 + pad + chip_w + 4, y - 1),
lbl,
fill=(240, 240, 240, 255),
font=font,
)
y += line_h
rgb[:] = np.array(img, dtype=np.uint8)
def save_channel(
tensor: torch.Tensor,
channel: int,
filepath: str,
use_colormap: bool = True,
output_format: str = "png",
invert: bool = False,
) -> None:
"""Extract one channel from a diagnostic tensor and save in the chosen format.
Supported *output_format* values:
- ``png`` — Colormapped RGB PNG image.
- ``npy`` — Raw float32 NumPy array (normalized [0, 1]).
- ``raw`` / ``bin`` — Flat float32 binary dump.
- ``nv21`` — NV21 (YUV420SP) binary from the grayscale channel.
*invert* flips the polarity after min-max normalisation. Use this
for channels whose semantic is reversed relative to the "hot =
damaged" convention the jet colormap implies — e.g. the structural
similarity map, where HIGH values mean MORE SIMILAR (= less damage).
Without inversion, intact-structure regions paint bright orange and
look like anomalies; with inversion, damaged regions paint hot.
"""
arr = tensor[0, channel].cpu().numpy()
# Normalize to [0, 1]
lo, hi = arr.min(), arr.max()
if hi - lo > 1e-8:
arr = (arr - lo) / (hi - lo)
else:
arr = np.zeros_like(arr)
if invert:
arr = 1.0 - arr
fmt = output_format.lower()
if fmt == "png":
if use_colormap:
rgb = apply_jet_colormap(arr)
else:
rgb = (np.stack([arr] * 3, axis=-1) * 255).astype(np.uint8)
Image.fromarray(rgb).save(filepath, format="PNG")
elif fmt == "npy":
np.save(filepath, arr.astype(np.float32))
elif fmt in ("raw", "bin"):
arr.astype(np.float32).tofile(filepath)
elif fmt == "nv21":
_save_as_nv21(arr, filepath)
else:
raise ValueError(f"Unsupported output_format: {output_format!r}")
def _save_as_nv21(gray_01: np.ndarray, filepath: str) -> None:
"""Convert a [0,1] grayscale array to NV21 bytes and write to disk.
The grayscale value is written as the Y plane; U and V are set to 128
(neutral chroma) so the result is a valid monochrome NV21 frame.
"""
h, w = gray_01.shape
# Ensure even dimensions for NV21
h2 = h if h % 2 == 0 else h - 1
w2 = w if w % 2 == 0 else w - 1
y_plane = (gray_01[:h2, :w2] * 255).clip(0, 255).astype(np.uint8)
uv_plane = np.full((h2 // 2, w2), 128, dtype=np.uint8) # VU interleaved, neutral
with open(filepath, "wb") as f:
f.write(y_plane.tobytes())
f.write(uv_plane.tobytes())
# Backwards-compatible alias
save_channel_as_png = save_channel
# ======================================================================
# Score aggregation (mirrors upiqal/model.py:_aggregate_deep_score)
# ======================================================================
def aggregate_deep_score(
l_maps: list[torch.Tensor],
s_maps: list[torch.Tensor],
p_tex: list[torch.Tensor],
target_size: tuple[int, int],
) -> torch.Tensor:
"""Aggregate per-layer structure/texture similarity into one map.
Replicates ``UPIQAL._aggregate_deep_score`` from ``upiqal/model.py``.
Parameters
----------
l_maps : list[torch.Tensor]
Luminance similarity maps per VGG stage.
s_maps : list[torch.Tensor]
Structure/texture similarity maps per VGG stage.
p_tex : list[torch.Tensor]
Texture probability maps per VGG stage.
target_size : tuple[int, int]
Output spatial resolution ``(H, W)``.
Returns
-------
torch.Tensor
Aggregated deep similarity map ``(B, 1, H, W)`` in ``[0, 1]``.
"""
combined = None
for l_map, s_map, pt in zip(l_maps, s_maps, p_tex):
# Channel-average each map
l_avg = l_map.mean(dim=1, keepdim=True)
s_avg = s_map.mean(dim=1, keepdim=True)
pt_avg = pt.mean(dim=1, keepdim=True)
# Adaptive weighting: high P_tex → rely on s(x,y); low → rely on l(x,y)
score = (1.0 - pt_avg) * l_avg + pt_avg * s_avg
# Upsample to target resolution
if score.shape[2:] != target_size:
score = F.interpolate(
score, size=target_size, mode="bilinear", align_corners=False
)
if combined is None:
combined = score
else:
combined = combined + score
# Average across layers
return combined / len(l_maps)
# ======================================================================
# Diagnostics computation — shared with web/main.py:compute_diagnostics
# ======================================================================
# Severity display multipliers. "blocking" was removed from the
# user-visible artefact set; its detector still runs and feeds the
# heuristic-penalty term in the scoring formula, but it no longer
# surfaces in severity_scores, the dominant_artifact label, or any
# rendered output. Keep this dict in sync with _ARTIFACT_LABELS.
_SEVERITY_MULTIPLIERS = {
"ringing": 5.0,
"noise": 3.0,
"color_shift": 3.0,
"blur": 2.0,
}
_ARTIFACT_LABELS = {
"ringing": "Gibbs Ringing",
"noise": "Noise / Granularity",
"color_shift": "Color Shift",
"blur": "Blur / Loss of Detail",
}
def _hf_energy(img: torch.Tensor) -> torch.Tensor:
"""Compute a scalar high-frequency energy proxy per sample.
Uses Laplacian variance on the luminance channel — a standard
sharpness / high-frequency-content measure. Larger value → more
high-frequency detail (sharpness or noise); smaller → smoother
(blurrier or flatter).
Parameters
----------
img : torch.Tensor
Image ``(B, 3, H, W)`` or ``(B, 1, H, W)`` in ``[0, 1]``.
Returns
-------
torch.Tensor
Per-sample HF energy ``(B,)``.
"""
if img.shape[1] == 3:
w = torch.tensor([0.299, 0.587, 0.114],
device=img.device, dtype=img.dtype).view(1, 3, 1, 1)
lum = (img * w).sum(dim=1, keepdim=True)
else:
lum = img
kernel = torch.tensor(
[[0.0, -1.0, 0.0], [-1.0, 4.0, -1.0], [0.0, -1.0, 0.0]],
device=img.device, dtype=img.dtype,
).view(1, 1, 3, 3)
lap = F.conv2d(lum, kernel, padding=1)
return lap.var(dim=(1, 2, 3)) # (B,)
def compute_diagnostics(
anomaly_norm: torch.Tensor,
color_norm: torch.Tensor,
deep_sim: torch.Tensor,
blocking_mask: torch.Tensor,
ringing_mask: torch.Tensor,
ref_raw: torch.Tensor = None,
tgt_raw: torch.Tensor = None,
noise_mask: torch.Tensor = None,
blur_mask: torch.Tensor = None,
) -> Dict[str, Any]:
"""Compute artifact severity scores, dominant artifact, and affected area.
Parameters
----------
anomaly_norm : torch.Tensor
Normalized anomaly map ``(B, 1, H, W)`` in [0, 1].
color_norm : torch.Tensor
Normalized color degradation ``(B, 1, H, W)`` in [0, 1].
deep_sim : torch.Tensor
Deep similarity map ``(B, 1, H, W)`` in [0, 1].
blocking_mask : torch.Tensor
Binary blocking mask ``(B, 1, H, W)``.
ringing_mask : torch.Tensor
Binary ringing mask ``(B, 1, H, W)``.
ref_raw, tgt_raw : torch.Tensor, optional
Reference / target images in ``[0, 1]`` used to compute a high-
frequency-energy discriminator. When provided (recommended), the
``dominant_artifact`` selection is gated so that ``blur`` fires only
when the target has LESS high-frequency content than the reference,
and ``noise`` only when it has MORE. Without these, the classic
max-over-severities logic is used (kept for backward compatibility).
Returns
-------
dict
Diagnostics dict with ``dominant_artifact``, ``severity_scores``,
and ``affected_area``.
"""
# Raw severity percentages (pre-multiplier, per-pixel mean)
blocking_sev = float(blocking_mask.mean().item()) * 100
ringing_sev = float(ringing_mask.mean().item()) * 100
color_sev = float(color_norm.mean().item()) * 100
# Prefer the dedicated wavelet / edge-spread detectors when available;
# fall back to the legacy proxies (anomaly_norm / deep_sim) so older
# callers still get a sane answer.
if noise_mask is not None:
noise_sev = float(noise_mask.mean().item()) * 100
else:
noise_sev = float(anomaly_norm.mean().item()) * 100
if blur_mask is not None:
blur_sev = float(blur_mask.mean().item()) * 100
else:
blur_sev = float((1.0 - deep_sim).mean().item()) * 100
# Display severities: scale by perceptual multipliers and clamp at 100.
# These numbers go into the report/UI and are NOT used for argmax below.
# "blocking" is intentionally omitted from the user-visible severity
# table — the raw blocking_sev is still computed above and still
# contributes to the heuristic-penalty term in the score formula,
# but it no longer appears in reports, UI banners, or overlays.
severity_scores = {
"ringing": round(min(ringing_sev * _SEVERITY_MULTIPLIERS["ringing"], 100.0), 1),
"noise": round(min(noise_sev * _SEVERITY_MULTIPLIERS["noise"], 100.0), 1),
"color_shift": round(min(color_sev * _SEVERITY_MULTIPLIERS["color_shift"], 100.0), 1),
"blur": round(min(blur_sev * _SEVERITY_MULTIPLIERS["blur"], 100.0), 1),
}
# ── Dominant-artifact selection ────────────────────────────────
# Strategy: rank candidates by their CONTRIBUTION to the score drop
# (raw_severity × pipeline_weight), not by the clamped display value.
# This keeps the top-1 meaningful when several display channels saturate
# at 100. Then apply a high-frequency-energy discriminator so generic
# VGG dissimilarity isn't mislabeled as "blur" when the target is
# actually SHARPER than the reference (noise / ringing / different
# content) — see bugs #2 & #5 in analysis/report.md.
#
# Weights mirror those in run_pipeline (w_anomaly=0.3, w_color=0.1,
# w_structure=0.5, w_heuristic=0.1 shared across blocking+ringing).
# Contribution weights for dominant-artifact ranking.
# Heuristic masks (blocking, ringing) are high-specificity evidence: when
# they fire at all, they almost certainly indicate the named artifact,
# so they get the largest multipliers here (even though their
# contribution to the final numerical score is small, w_heuristic=0.1).
# Color/blur/noise are generic deep-feature signals that overlap, so
# they're weighted closer to their pipeline weights.
# "blocking" deliberately excluded from the dominant-artefact race —
# it's no longer a user-visible class. The detector still runs and
# its severity still feeds the heuristic penalty in the score, but
# the label never appears in diagnostics["dominant_artifact"].
contrib = {
"ringing": ringing_sev * 1.0, # high specificity
"noise": noise_sev * 0.30,
"color_shift": color_sev * 0.30, # raised so hue shift wins over generic VGG drift
"blur": blur_sev * 0.50,
}
# HF-energy discriminator: only trust "blur" when tgt is smoother than
# ref; only trust "noise" when tgt is rougher. Prevents generic
# deep-similarity drop on unrelated images from being mislabeled as
# "blur" (bug #5).
if ref_raw is not None and tgt_raw is not None:
with torch.no_grad():
hf_ref = float(_hf_energy(ref_raw).mean().item())
hf_tgt = float(_hf_energy(tgt_raw).mean().item())
eps = 1e-8
hf_ratio = hf_tgt / (hf_ref + eps)
# 0.9 / 1.1 deadband is ~10% HF change; beyond that the direction is decisive.
if hf_ratio > 1.1:
# Target has MORE high-freq → not blur (noise / ringing territory)
contrib["blur"] *= 0.0
# Noise perturbs pixel-wise colors enough to fully saturate the
# Sinkhorn EMD channel; downweight color_shift so noise can win.
if noise_sev > 10.0:
contrib["color_shift"] *= 0.3
elif hf_ratio < 0.9:
# Target is smoother → not noise / ringing (blur territory)
contrib["noise"] *= 0.3
contrib["ringing"] *= 0.3
else:
# HF stable → spatial detail preserved. If color EMD is high,
# the delta is dominated by chromatic change, NOT blur.
if color_sev >= 25.0:
contrib["blur"] *= 0.3
# Guard: if all contributions are negligible, report "None".
# MPS / float32 drift can leave a single channel mildly non-zero on
# near-identical inputs (e.g. deep_sim ≈ 0.88 → blur_sev ≈ 12%).
# Treat that as device noise, not a real artifact.
nonzero = {k: v for k, v in severity_scores.items() if v > 0.5}
single_weak = (
len(nonzero) == 1
and next(iter(nonzero.values())) < 15.0
)
if max(contrib.values()) < 0.5 or single_weak:
dominant_artifact = "None"
else:
dominant_key = max(contrib, key=contrib.get)
dominant_artifact = _ARTIFACT_LABELS[dominant_key]
# Affected area: percentage of pixels where anomaly exceeds threshold
affected_mask = (anomaly_norm > 0.15).float()
affected_area = round(float(affected_mask.mean().item()) * 100, 1)
return {
"dominant_artifact": dominant_artifact,
"severity_scores": severity_scores,
"affected_area": affected_area,
}
# ======================================================================
# Quality label from score
# ======================================================================
def score_label(score: float) -> str:
"""Return a human-readable quality label for a FR-IQA score."""
if score >= 0.9:
return "Excellent quality"
elif score >= 0.7:
return "Good quality"
elif score >= 0.5:
return "Moderate degradation"
elif score >= 0.3:
return "Poor quality"
else:
return "Severe degradation"
# ======================================================================
# Main pipeline
# ======================================================================
def run_pipeline(args: argparse.Namespace) -> None:
"""Execute the full UPIQAL pipeline and write results to disk."""
# ── Header ──────────────────────────────────────────────────────
sep = "\u2500" * 30
print(f"UPIQAL CLI v{UPIQAL_VERSION}")
print(sep)
print(f"Reference : {args.reference}")
print(f"Target : {args.target}")
# ── Load images ─────────────────────────────────────────────────
raw_kw = dict(
width=getattr(args, "width", 0) or 0,
height=getattr(args, "height", 0) or 0,
pixel_format=getattr(args, "pixel_format", "RGB888") or "RGB888",
)
# Multi-scale pyramid (Phase 1 of the paper): load at FULL resolution,
# then downsample a separate copy for the deep-feature branch. The
# heuristics (blocking/ringing/noise/blur) run on the original pixels
# so grid-aligned artefacts aren't blurred away; Modules 1-4 use the
# 256-ish-sided pyramid level for speed and to match the paper's
# "minimum dimension ~ 256 px for standard feature extraction".
# Opt out via --no-pyramid to restore single-scale behaviour.
use_pyramid = bool(getattr(args, "pyramid", True))
feature_side = int(getattr(args, "feature_side", 256))
if use_pyramid:
ref_full = load_image_as_tensor(args.reference, max_side=args.max_side, **raw_kw)
tgt_full = load_image_as_tensor(args.target, max_side=args.max_side, **raw_kw)
# Deep-feature copy: aggressively shrunk so VGG16 runs fast and
# sees the same effective spatial frequencies as in the paper.
ref_tensor = (
F.interpolate(
ref_full, size=_pyramid_target(ref_full.shape[-2:], feature_side),
mode="bicubic", align_corners=False, antialias=True,
).clamp(0.0, 1.0)
if max(ref_full.shape[-2:]) > feature_side
else ref_full
)
tgt_tensor = (
F.interpolate(
tgt_full, size=_pyramid_target(tgt_full.shape[-2:], feature_side),
mode="bicubic", align_corners=False, antialias=True,
).clamp(0.0, 1.0)
if max(tgt_full.shape[-2:]) > feature_side
else tgt_full
)
else:
ref_full = load_image_as_tensor(args.reference, max_side=args.max_side, **raw_kw)
tgt_full = load_image_as_tensor(args.target, max_side=args.max_side, **raw_kw)
ref_tensor = ref_full
tgt_tensor = tgt_full
# Ensure matching spatial dimensions.
#
# Previously this bilinear-upsampled the target to the reference size,
# which silently injected blur into the target and caused false "blur"/
# "color shift" severity on same-content pairs at mismatched native
# sizes (validation probe T7: score 0.95 -> 0.84).
#
# Fix: pick the SMALLER resolution (preserves sharpness of the lower-
# res image; never upsamples) and downsample the larger one via bicubic
# with antialiasing. If both already match, this is a no-op.
# Reconcile ref/tgt spatial sizes at BOTH pyramid levels so the rest
# of the pipeline sees matching shapes.
def _match(a: torch.Tensor, b: torch.Tensor):
_, _, ah, aw = a.shape
_, _, bh, bw = b.shape
if (ah, aw) == (bh, bw):
return a, b
ch, cw = min(ah, bh), min(aw, bw)
if (ah, aw) != (ch, cw):
a = F.interpolate(
a, size=(ch, cw), mode="bicubic",
align_corners=False, antialias=True,
).clamp(0.0, 1.0)
if (bh, bw) != (ch, cw):
b = F.interpolate(
b, size=(ch, cw), mode="bicubic",
align_corners=False, antialias=True,
).clamp(0.0, 1.0)
return a, b
ref_tensor, tgt_tensor = _match(ref_tensor, tgt_tensor)
ref_full, tgt_full = _match(ref_full, tgt_full)
B, C, H, W = ref_tensor.shape
print(f"Resolution: {H} x {W}")
print(sep)
# ── Initialize modules (before creating output dir, so failures
# like VGG16 download errors don't leave empty directories) ───
timestamp = datetime.now()
normalizer = Normalizer(mode="imagenet")
chromatic = ChromaticTransportEvaluator(patch_size=16, sinkhorn_iters=20)
# Deep feature extractor — try pretrained from local weights, fallback to random init
try:
deep_stats = DeepStatisticalExtractor(pretrained=True)
vgg_status = "ImageNet (pretrained, local)"
except FileNotFoundError:
print(" [warn] Local VGG16 weights not found. Run: python weights/download_vgg16.py")
deep_stats = DeepStatisticalExtractor(pretrained=False)
vgg_status = "random initialization (pretrained weights unavailable)"
print(f" VGG16 weights: {vgg_status}")
uncertainty_weights_path = getattr(args, "uncertainty_weights", None)
if uncertainty_weights_path:
uncertainty = ProbabilisticUncertaintyMapper(parameterization="blockdiag")
state = torch.load(
uncertainty_weights_path, map_location="cpu", weights_only=True
)
if isinstance(state, dict) and "state_dict" in state:
state = state["state_dict"]
uncertainty.load_state_dict(state, strict=False)
uncertainty.eval()
print(f" Uncertainty weights: {uncertainty_weights_path} (blockdiag, loaded)")
else:
uncertainty = ProbabilisticUncertaintyMapper()
print(" Uncertainty weights: identity diagonal (untrained)")
heuristics = SpatialHeuristicsEngine()
# Aggregation weights (defaults from upiqal/model.py). Optionally
# overridden by a MOS-tuned ckpt produced by train_aggregation.py.
w_color = 0.1
w_anomaly = 0.3
w_structure = 0.5
w_heuristic = 0.1
score_scale = 10.0
score_center = 0.2
sigmoid_gain_override: Optional[float] = None