File size: 23,690 Bytes
3625530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e15351
 
 
 
 
3625530
3e15351
 
 
 
 
 
 
3625530
 
 
 
 
 
 
 
 
 
 
 
3e15351
3625530
 
 
 
 
 
3e15351
3625530
3e15351
 
 
 
3625530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e15351
3625530
 
 
 
 
 
 
 
 
 
3e15351
3625530
 
 
 
 
 
 
 
 
 
 
 
 
3e15351
 
3625530
3e15351
3625530
 
 
 
 
 
3e15351
 
3625530
3e15351
 
3625530
 
3e15351
3625530
 
 
 
 
3e15351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3625530
 
 
 
 
3e15351
3625530
 
 
 
 
 
3e15351
3625530
3e15351
 
 
3625530
 
 
 
 
 
 
3e15351
 
 
 
 
 
 
 
 
 
3625530
 
3e15351
 
3625530
3e15351
 
3625530
3e15351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3625530
 
 
3e15351
3625530
 
 
3e15351
 
 
 
 
 
 
3625530
3e15351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3625530
3e15351
 
 
 
 
 
 
 
 
 
 
3625530
3e15351
 
 
 
3625530
3e15351
 
 
 
 
 
3625530
 
 
 
 
 
 
3e15351
3625530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e15351
 
 
 
 
3625530
 
3e15351
 
 
 
 
 
 
 
 
3625530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e15351
3625530
3e15351
 
3625530
3e15351
3625530
3e15351
 
3625530
 
 
 
 
 
 
 
 
 
 
 
3e15351
 
 
 
 
 
 
3625530
 
 
 
 
 
 
 
3e15351
3625530
 
3e15351
3625530
3e15351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3625530
3e15351
3625530
 
 
 
3e15351
3625530
 
 
 
3e15351
3625530
 
 
 
 
 
 
3e15351
 
3625530
 
3e15351
 
3625530
 
 
3e15351
 
 
3625530
 
 
 
 
 
 
3e15351
3625530
 
3e15351
3625530
 
3e15351
3625530
 
 
3e15351
3625530
3e15351
 
3625530
 
 
 
 
 
 
 
3e15351
3625530
 
3e15351
 
 
 
 
 
 
 
3625530
3e15351
3625530
 
3e15351
 
 
 
3625530
 
 
 
 
3e15351
 
 
 
 
 
3625530
3e15351
 
 
3625530
 
 
 
 
 
 
 
3e15351
 
 
 
 
 
3625530
 
 
 
 
 
3e15351
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
"""DINOv3 ViT-H/16+ Tagger β€” Fully Standalone Inference Script

Zero dependency on transformers, trainer code, or any internal module.
Only requires: torch, torchvision, safetensors, Pillow, requests.

  pip install torch torchvision safetensors Pillow requests

The DINOv3 ViT-H/16+ architecture is implemented directly here, with weights
loaded from a .safetensors checkpoint.  The state-dict key names match the
HuggingFace transformers layout exactly so checkpoints are interchangeable.

Usage
-----
# Single image, top-30 tags:
python inference_tagger_standalone.py \
    --checkpoint tagger_checkpoints/2026-03-28_22-57-47.safetensors \
    --vocab     tagger_vocab.json \
    --images    photo.jpg \
    --topk      30

# URL input:
python inference_tagger_standalone.py \
    --checkpoint tagger_checkpoints/2026-03-28_22-57-47.safetensors \
    --vocab     tagger_vocab.json \
    --images    https://example.com/photo.jpg

# Threshold instead of top-k:
python inference_tagger_standalone.py ... --threshold 0.4

# Pipe-friendly comma-separated tags (one line per image):
python inference_tagger_standalone.py ... --format tags

# JSON output:
python inference_tagger_standalone.py ... --format json

Output formats (--format)
-------------------------
  pretty  (default) β€” human-readable table with scores
  tags              β€” comma-separated tag string, one line per image
  json              β€” JSON array of {file, tags: [{tag, score}]} objects
"""

from __future__ import annotations

import argparse
import json
import math
import sys
from functools import lru_cache
from io import BytesIO
from pathlib import Path

import requests
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms.v2 as v2
from PIL import Image
from safetensors.torch import load_file


# =============================================================================
# DINOv3 ViT-H/16+ β€” hardcoded architecture
# All hyperparameters match facebook/dinov3-vith16plus-pretrain-lvd1689m
# =============================================================================

D_MODEL = 1280
N_HEADS = 20
HEAD_DIM = D_MODEL // N_HEADS  # 64
N_LAYERS = 32
D_FFN = 5120
N_REGISTERS = 4
PATCH_SIZE = 16
ROPE_THETA = 100.0
ROPE_RESCALE = 2.0
LN_EPS = 1e-5
LAYERSCALE = 1.0

FEATURE_DIM = (1 + N_REGISTERS) * D_MODEL  # 6400


# ---------------------------------------------------------------------------
# RoPE helpers
# ---------------------------------------------------------------------------

@lru_cache(maxsize=32)
def _patch_coords_cached(h: int, w: int, device_str: str) -> torch.Tensor:
    device = torch.device(device_str)
    cy = torch.arange(0.5, h, dtype=torch.float32, device=device) / h
    cx = torch.arange(0.5, w, dtype=torch.float32, device=device) / w
    coords = torch.stack(torch.meshgrid(cy, cx, indexing="ij"), dim=-1).flatten(0, 1)
    coords = 2.0 * coords - 1.0
    coords = coords * ROPE_RESCALE
    return coords  # [h*w, 2]


def _build_rope(h_patches: int, w_patches: int,
                dtype: torch.dtype, device: torch.device):
    coords = _patch_coords_cached(h_patches, w_patches, str(device))
    inv_freq = 1.0 / (ROPE_THETA ** torch.arange(
        0, 1, 4 / HEAD_DIM, dtype=torch.float32, device=device))
    angles = 2 * math.pi * coords[:, :, None] * inv_freq[None, None, :]
    angles = angles.flatten(1, 2).tile(2)
    cos = torch.cos(angles).to(dtype).unsqueeze(0).unsqueeze(0)
    sin = torch.sin(angles).to(dtype).unsqueeze(0).unsqueeze(0)
    return cos, sin


def _rotate_half(x: torch.Tensor) -> torch.Tensor:
    h = x.shape[-1] // 2
    return torch.cat((-x[..., h:], x[..., :h]), dim=-1)


def _apply_rope(q: torch.Tensor, k: torch.Tensor,
                cos: torch.Tensor, sin: torch.Tensor):
    n_pre = 1 + N_REGISTERS
    q_pre, q_pat = q[..., :n_pre, :], q[..., n_pre:, :]
    k_pre, k_pat = k[..., :n_pre, :], k[..., n_pre:, :]
    q_pat = q_pat * cos + _rotate_half(q_pat) * sin
    k_pat = k_pat * cos + _rotate_half(k_pat) * sin
    return torch.cat([q_pre, q_pat], dim=-2), torch.cat([k_pre, k_pat], dim=-2)


# ---------------------------------------------------------------------------
# Transformer blocks
# ---------------------------------------------------------------------------

class _Attention(nn.Module):
    def __init__(self):
        super().__init__()
        self.q_proj = nn.Linear(D_MODEL, D_MODEL, bias=True)
        self.k_proj = nn.Linear(D_MODEL, D_MODEL, bias=False)
        self.v_proj = nn.Linear(D_MODEL, D_MODEL, bias=True)
        self.o_proj = nn.Linear(D_MODEL, D_MODEL, bias=True)

    def forward(self, x, cos, sin):
        B, S, _ = x.shape
        q = self.q_proj(x).view(B, S, N_HEADS, HEAD_DIM).transpose(1, 2)
        k = self.k_proj(x).view(B, S, N_HEADS, HEAD_DIM).transpose(1, 2)
        v = self.v_proj(x).view(B, S, N_HEADS, HEAD_DIM).transpose(1, 2)
        q, k = _apply_rope(q, k, cos, sin)
        out = F.scaled_dot_product_attention(q, k, v, scale=HEAD_DIM ** -0.5)
        return self.o_proj(out.transpose(1, 2).reshape(B, S, D_MODEL))


class _GatedMLP(nn.Module):
    def __init__(self):
        super().__init__()
        self.gate_proj = nn.Linear(D_MODEL, D_FFN, bias=True)
        self.up_proj = nn.Linear(D_MODEL, D_FFN, bias=True)
        self.down_proj = nn.Linear(D_FFN, D_MODEL, bias=True)

    def forward(self, x):
        return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))


class _Block(nn.Module):
    def __init__(self):
        super().__init__()
        self.norm1 = nn.LayerNorm(D_MODEL, eps=LN_EPS)
        self.attention = _Attention()
        self.layer_scale1 = nn.Parameter(torch.full((D_MODEL,), LAYERSCALE))
        self.norm2 = nn.LayerNorm(D_MODEL, eps=LN_EPS)
        self.mlp = _GatedMLP()
        self.layer_scale2 = nn.Parameter(torch.full((D_MODEL,), LAYERSCALE))

    def forward(self, x, cos, sin):
        x = x + self.attention(self.norm1(x), cos, sin) * self.layer_scale1
        x = x + self.mlp(self.norm2(x)) * self.layer_scale2
        return x


class _Embeddings(nn.Module):
    def __init__(self):
        super().__init__()
        # zeros() rather than empty() so a forgotten checkpoint key fails
        # predictably instead of producing undefined outputs.
        self.cls_token = nn.Parameter(torch.zeros(1, 1, D_MODEL))
        self.mask_token = nn.Parameter(torch.zeros(1, 1, D_MODEL))
        self.register_tokens = nn.Parameter(torch.zeros(1, N_REGISTERS, D_MODEL))
        self.patch_embeddings = nn.Conv2d(
            3, D_MODEL, kernel_size=PATCH_SIZE, stride=PATCH_SIZE)

    def forward(self, pixel_values):
        B = pixel_values.shape[0]
        dtype = self.patch_embeddings.weight.dtype
        patches = self.patch_embeddings(
            pixel_values.to(dtype)).flatten(2).transpose(1, 2)
        cls = self.cls_token.expand(B, -1, -1)
        regs = self.register_tokens.expand(B, -1, -1)
        return torch.cat([cls, regs, patches], dim=1)


class DINOv3ViTH(nn.Module):
    """DINOv3 ViT-H/16+ backbone.

    Token layout: [CLS, reg_0..reg_3, patch_0..patch_N].
    Returns last_hidden_state [B, 1+R+P, D_MODEL].
    """

    def __init__(self):
        super().__init__()
        self.embeddings = _Embeddings()
        self.layer = nn.ModuleList([_Block() for _ in range(N_LAYERS)])
        self.norm = nn.LayerNorm(D_MODEL, eps=LN_EPS)

    def forward(self, pixel_values):
        _, _, H, W = pixel_values.shape
        x = self.embeddings(pixel_values)
        h_p, w_p = H // PATCH_SIZE, W // PATCH_SIZE
        cos, sin = _build_rope(h_p, w_p, x.dtype, pixel_values.device)
        for block in self.layer:
            x = block(x, cos, sin)
        return self.norm(x)


# =============================================================================
# Head β€” auto-detected from the checkpoint
# =============================================================================

class _LowRankHead(nn.Module):
    """Two-matrix low-rank projection head.

        features (in_dim)
          β†’ Linear(in_dim, rank, bias=?)
          β†’ Linear(rank, num_tags, bias=?)
    """

    def __init__(self, in_dim: int, rank: int, num_tags: int,
                 down_bias: bool, up_bias: bool):
        super().__init__()
        self.proj_down = nn.Linear(in_dim, rank, bias=down_bias)
        self.proj_up = nn.Linear(rank, num_tags, bias=up_bias)

    def forward(self, x):
        return self.proj_up(self.proj_down(x))


def _build_head_from_checkpoint(
    head_sd: dict,
    in_dim: int,
    num_tags: int,
) -> tuple[nn.Module, dict]:
    """Inspect head_sd and build a matching Module.

    Supports two layouts, in order of preference:
      1. Single linear          β€” any ``*.weight`` with shape [num_tags, in_dim]
      2. Low-rank pair (2 mats) β€” one ``*.weight`` [rank, in_dim] plus
                                   one ``*.weight`` [num_tags, rank]

    Returns (module, remapped_state_dict) where the remapped state dict
    matches the module's own key names so strict loading works.
    """
    weights_2d = [(k, v) for k, v in head_sd.items()
                  if k.endswith(".weight") and v.ndim == 2]

    # --- Case 1: single dense linear ---------------------------------------
    singles = [(k, v) for k, v in weights_2d
               if tuple(v.shape) == (num_tags, in_dim)]
    if len(weights_2d) <= 2 and len(singles) == 1:
        wkey, wval = singles[0]
        base = wkey[:-len(".weight")]
        bias_key = base + ".bias"
        has_bias = bias_key in head_sd
        module = nn.Linear(in_dim, num_tags, bias=has_bias)
        remapped = {"weight": wval}
        if has_bias:
            remapped["bias"] = head_sd[bias_key]
        # Sanity check: no extra keys we don't understand
        expected_src = {wkey} | ({bias_key} if has_bias else set())
        extra = set(head_sd) - expected_src
        if extra:
            raise RuntimeError(
                f"Head has single-linear shape but extra unknown keys: {sorted(extra)}")
        return module, remapped

    # --- Case 2: low-rank pair ---------------------------------------------
    down = None  # (key, tensor) with shape [rank, in_dim]
    up = None    # (key, tensor) with shape [num_tags, rank]
    for k, v in weights_2d:
        if v.shape[1] == in_dim and v.shape[0] != num_tags:
            down = (k, v)
        elif v.shape[0] == num_tags and v.shape[1] != in_dim:
            up = (k, v)

    if down is not None and up is not None:
        rank_down = down[1].shape[0]
        rank_up = up[1].shape[1]
        if rank_down != rank_up:
            raise RuntimeError(
                f"Low-rank head: inner dims disagree "
                f"(down out={rank_down}, up in={rank_up})")

        down_key, down_w = down
        up_key, up_w = up
        down_base = down_key[:-len(".weight")]
        up_base = up_key[:-len(".weight")]
        down_bias_key = down_base + ".bias"
        up_bias_key = up_base + ".bias"
        has_down_bias = down_bias_key in head_sd
        has_up_bias = up_bias_key in head_sd

        module = _LowRankHead(
            in_dim=in_dim,
            rank=rank_down,
            num_tags=num_tags,
            down_bias=has_down_bias,
            up_bias=has_up_bias,
        )
        remapped = {
            "proj_down.weight": down_w,
            "proj_up.weight": up_w,
        }
        if has_down_bias:
            remapped["proj_down.bias"] = head_sd[down_bias_key]
        if has_up_bias:
            remapped["proj_up.bias"] = head_sd[up_bias_key]

        # Sanity check
        expected_src = {down_key, up_key}
        if has_down_bias:
            expected_src.add(down_bias_key)
        if has_up_bias:
            expected_src.add(up_bias_key)
        extra = set(head_sd) - expected_src
        if extra:
            raise RuntimeError(
                f"Low-rank head detected but checkpoint has extra unknown "
                f"head keys: {sorted(extra)}")

        print(f"[Tagger] Detected low-rank head: "
              f"in_dim={in_dim}, rank={rank_down}, num_tags={num_tags} "
              f"(down_bias={has_down_bias}, up_bias={has_up_bias})")
        return module, remapped

    raise RuntimeError(
        "Could not infer head architecture from checkpoint. "
        f"Non-backbone keys found: {sorted(head_sd.keys())}"
    )


# =============================================================================
# Tagger wrapper module
# =============================================================================

class DINOv3Tagger(nn.Module):
    """Backbone + head. The head is attached after the checkpoint is
    inspected (so we can build the right shape)."""

    def __init__(self):
        super().__init__()
        self.backbone = DINOv3ViTH()
        self.head: nn.Module | None = None  # attached by Tagger

    def forward(self, pixel_values):
        hidden = self.backbone(pixel_values)
        cls = hidden[:, 0, :]
        regs = hidden[:, 1: 1 + N_REGISTERS, :].flatten(1)
        features = torch.cat([cls, regs], dim=-1).float()  # fp32 for head
        return self.head(features)


# =============================================================================
# Checkpoint loading helpers
# =============================================================================

def _split_and_clean_state_dict(sd: dict) -> tuple[dict, dict]:
    """Split full state dict into (backbone_sd, head_sd), stripping the
    ``backbone.`` prefix and applying the remaps needed to match
    ``DINOv3ViTH``'s parameter layout:

      1. ``backbone.model.layer.N.*`` β†’ ``layer.N.*``
         (the checkpoint has an HF-style intermediate ``model`` wrapper
         that our flat backbone class does not)
      2. ``...layer_scale{1,2}.lambda1`` β†’ ``...layer_scale{1,2}``
         (HF stores layer_scale as a sub-module with a ``lambda1``
         parameter; we use a plain ``nn.Parameter``)
      3. Drop any ``rope_embeddings`` buffers (recomputed on the fly)
    """
    backbone_sd: dict = {}
    head_sd: dict = {}
    for k, v in sd.items():
        if k.startswith("backbone."):
            nk = k[len("backbone."):]
            # Remap (1): strip intermediate "model." before "layer."
            if nk.startswith("model.layer."):
                nk = nk[len("model."):]
            backbone_sd[nk] = v
        else:
            head_sd[k] = v

    # Remap (2): layer.N.layer_scale{1,2}.lambda1 β†’ layer.N.layer_scale{1,2}
    for k in list(backbone_sd.keys()):
        if ".layer_scale" in k and k.endswith(".lambda1"):
            backbone_sd[k[:-len(".lambda1")]] = backbone_sd.pop(k)

    # Remap (3): drop rope buffers (recomputed on the fly)
    for k in list(backbone_sd.keys()):
        if "rope_embeddings" in k:
            backbone_sd.pop(k)

    return backbone_sd, head_sd


# =============================================================================
# Image preprocessing
# =============================================================================

_IMAGENET_MEAN = [0.485, 0.456, 0.406]
_IMAGENET_STD = [0.229, 0.224, 0.225]


def _snap(x: int, m: int) -> int:
    return max(m, (x // m) * m)


def _open_image(source) -> Image.Image:
    s = str(source)
    if s.startswith("http://") or s.startswith("https://"):
        r = requests.get(s, timeout=30)
        r.raise_for_status()
        return Image.open(BytesIO(r.content)).convert("RGB")
    return Image.open(source).convert("RGB")


def preprocess_image(source, max_size: int = 1024) -> torch.Tensor:
    """Load and preprocess an image β†’ [1, 3, H, W] float32, ImageNet-normalised.

    Aspect ratio is preserved: a single scale factor is chosen so that the
    long edge fits inside max_size after snapping to a PATCH_SIZE multiple.
    """
    img = _open_image(source)
    w, h = img.size

    # Target long-edge (snapped to patch multiple).
    long_edge = max(w, h)
    target_long = _snap(min(long_edge, max_size), PATCH_SIZE)
    scale = target_long / long_edge

    new_w = _snap(max(PATCH_SIZE, round(w * scale)), PATCH_SIZE)
    new_h = _snap(max(PATCH_SIZE, round(h * scale)), PATCH_SIZE)

    return v2.Compose([
        v2.Resize((new_h, new_w), interpolation=v2.InterpolationMode.LANCZOS),
        v2.ToImage(),
        v2.ToDtype(torch.float32, scale=True),
        v2.Normalize(mean=_IMAGENET_MEAN, std=_IMAGENET_STD),
    ])(img).unsqueeze(0)


# =============================================================================
# Tagger wrapper
# =============================================================================

class Tagger:
    """Inference wrapper for DINOv3Tagger (ViT-H/16+).

    Parameters
    ----------
    checkpoint_path : str
        Path to a .safetensors or .pt/.pth checkpoint.
    vocab_path : str
        Path to tagger_vocab.json or tagger_vocab_with_categories.json
        (either must contain an ``idx2tag`` list).
    device : str
        "cuda", "cuda:0", "cpu", ...
    dtype : torch.dtype
        Backbone precision. bfloat16 recommended on Ampere+, float16 for
        older GPUs, float32 for CPU. The head always runs in fp32.
    max_size : int
        Long-edge cap in pixels before feeding to the model.
    """

    def __init__(
        self,
        checkpoint_path: str,
        vocab_path: str,
        device: str = "cuda",
        dtype: torch.dtype = torch.bfloat16,
        max_size: int = 1024,
    ):
        want_cuda = device.startswith("cuda")
        if want_cuda and not torch.cuda.is_available():
            print("[Tagger] CUDA not available, falling back to CPU")
            device = "cpu"
            dtype = torch.float32
        self.device = torch.device(device)
        self.dtype = dtype
        self.max_size = max_size

        with open(vocab_path) as f:
            data = json.load(f)
        self.idx2tag: list[str] = data["idx2tag"]
        self.num_tags = len(self.idx2tag)
        print(f"[Tagger] Vocabulary: {self.num_tags:,} tags")

        # --- Load checkpoint to CPU first so we can inspect shapes ---------
        print(f"[Tagger] Loading checkpoint: {checkpoint_path}")
        if checkpoint_path.endswith((".safetensors", ".sft")):
            sd = load_file(checkpoint_path, device="cpu")
        else:
            sd = torch.load(checkpoint_path, map_location="cpu")

        backbone_sd, head_sd = _split_and_clean_state_dict(sd)

        if not head_sd:
            raise RuntimeError(
                "Checkpoint contains no non-backbone keys β€” cannot build head.")

        # --- Build model, inferring head shape from the checkpoint --------
        self.model = DINOv3Tagger()
        head_module, head_sd_remapped = _build_head_from_checkpoint(
            head_sd, in_dim=FEATURE_DIM, num_tags=self.num_tags,
        )
        self.model.head = head_module

        # --- Strict load β€” mismatches raise instead of silently passing ----
        self.model.backbone.load_state_dict(backbone_sd, strict=True)
        self.model.head.load_state_dict(head_sd_remapped, strict=True)

        # --- Move to device. Backbone β†’ bf16/fp16; head stays fp32. --------
        self.model.backbone = self.model.backbone.to(
            device=self.device, dtype=dtype)
        self.model.head = self.model.head.to(
            device=self.device, dtype=torch.float32)
        self.model.eval()
        print(f"[Tagger] Ready on {self.device} (backbone={dtype}, head=fp32)")

    @torch.no_grad()
    def predict(self, image, topk: int | None = 30,
                threshold: float | None = None) -> list[tuple[str, float]]:
        """Tag a single image (local path or URL)."""
        if topk is None and threshold is None:
            topk = 30

        pv = preprocess_image(image, max_size=self.max_size).to(self.device)
        logits = self.model(pv)[0]
        scores = torch.sigmoid(logits.float())

        if topk is not None:
            values, indices = scores.topk(min(topk, self.num_tags))
        else:
            assert threshold is not None
            indices = (scores >= threshold).nonzero(as_tuple=True)[0]
            values = scores[indices]
            order = values.argsort(descending=True)
            indices, values = indices[order], values[order]

        return [(self.idx2tag[i], float(v))
                for i, v in zip(indices.tolist(), values.tolist())]

    @torch.no_grad()
    def predict_batch(self, images, topk: int | None = 30,
                      threshold: float | None = None):
        return [self.predict(img, topk=topk, threshold=threshold)
                for img in images]


# =============================================================================
# Output formatters
# =============================================================================

def _fmt_pretty(path: str, results) -> str:
    lines = [f"\n{'─' * 60}", f" {path}", f"{'─' * 60}"]
    for rank, (tag, score) in enumerate(results, 1):
        bar = "β–ˆ" * int(score * 20)
        lines.append(f" {rank:>3}. {score:.3f} {bar:<20} {tag}")
    return "\n".join(lines)


def _fmt_tags(results) -> str:
    return ", ".join(tag for tag, _ in results)


def _fmt_json(path: str, results) -> dict:
    return {"file": path,
            "tags": [{"tag": t, "score": round(s, 4)} for t, s in results]}


# =============================================================================
# CLI
# =============================================================================

def main():
    parser = argparse.ArgumentParser(
        description="DINOv3 ViT-H/16+ tagger inference (standalone)",
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument("--checkpoint", required=True,
                        help="Path to .safetensors or .pt checkpoint")
    parser.add_argument("--vocab", required=True,
                        help="Path to tagger_vocab*.json")
    parser.add_argument("--images", nargs="+", required=True,
                        help="Image paths and/or http(s) URLs")
    parser.add_argument("--device", default="cuda",
                        help="Device: cuda, cuda:0, cpu (default: cuda)")
    parser.add_argument("--max-size", type=int, default=1024,
                        help="Long-edge cap in pixels (default: 1024)")

    mode = parser.add_mutually_exclusive_group()
    mode.add_argument("--topk", type=int, default=30,
                      help="Return top-k tags (default: 30)")
    mode.add_argument("--threshold", type=float,
                      help="Return all tags with score >= threshold")

    parser.add_argument("--format", choices=["pretty", "tags", "json"],
                        default="pretty", help="Output format (default: pretty)")
    args = parser.parse_args()

    tagger = Tagger(
        checkpoint_path=args.checkpoint,
        vocab_path=args.vocab,
        device=args.device,
        max_size=args.max_size,
    )

    topk, threshold = (
        (None, args.threshold) if args.threshold else (args.topk, None)
    )
    json_out = []

    for src in args.images:
        is_url = str(src).startswith("http://") or str(src).startswith("https://")
        if not is_url and not Path(src).exists():
            print(f"[warning] File not found: {src}", file=sys.stderr)
            continue
        results = tagger.predict(src, topk=topk, threshold=threshold)
        if args.format == "pretty":
            print(_fmt_pretty(src, results))
        elif args.format == "tags":
            print(_fmt_tags(results))
        elif args.format == "json":
            json_out.append(_fmt_json(src, results))

    if args.format == "json":
        print(json.dumps(json_out, indent=2, ensure_ascii=False))


if __name__ == "__main__":
    main()