File size: 12,809 Bytes
159500c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
DINOv3 Encoder for Semantic-Guided Surface Normal Estimation

This module provides a simplified DINOv3 encoder that extracts semantic features
from RGB images for cross-attention in the TransNormal pipeline.

The encoder is particularly effective for transparent objects, as DINOv3's
strong semantic features can "see through" refraction artifacts.
"""

import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Optional, Dict


# DINOv3 model configurations
DINOV3_CONFIGS = {
    "dinov3_vits16": {
        "embed_dim": 384,
        "patch_size": 16,
        "n_storage_tokens": 4,
    },
    "dinov3_vitb16": {
        "embed_dim": 768,
        "patch_size": 16,
        "n_storage_tokens": 4,
    },
    "dinov3_vitl16": {
        "embed_dim": 1024,
        "patch_size": 16,
        "n_storage_tokens": 4,
    },
    "dinov3_vith16plus": {
        "embed_dim": 1280,
        "patch_size": 16,
        "n_storage_tokens": 4,
    },
}


class DINOv3Encoder(nn.Module):
    """
    DINOv3 Encoder for extracting semantic features from RGB images.
    
    This encoder provides projected patch tokens for cross-attention in the UNet,
    replacing CLIP text embeddings with visual semantic features.
    
    Args:
        model_name: DINOv3 model name (e.g., "dinov3_vith16plus")
        cross_attention_dim: Target dimension for cross-attention (1024 for SD 2.x)
        weights_path: Path to DINOv3 pretrained weights (HuggingFace format)
        freeze_encoder: Whether to freeze the DINOv3 backbone
    """
    
    def __init__(
        self,
        model_name: str = "dinov3_vith16plus",
        cross_attention_dim: int = 1024,
        weights_path: Optional[str] = None,
        freeze_encoder: bool = True,
    ):
        super().__init__()
        
        self.model_name = model_name
        self.cross_attention_dim = cross_attention_dim
        self.weights_path = weights_path
        self.freeze_encoder = freeze_encoder
        
        # Get model configuration
        if model_name not in DINOV3_CONFIGS:
            raise ValueError(f"Unknown DINOv3 model: {model_name}. Available: {list(DINOV3_CONFIGS.keys())}")
        
        self.config = DINOV3_CONFIGS[model_name]
        self.dino_hidden_dim = self.config["embed_dim"]
        self.patch_size = self.config["patch_size"]
        self.n_storage_tokens = self.config["n_storage_tokens"]
        
        # DINOv3 backbone (loaded later)
        self.dino_backbone = None
        self._use_hf_interface = False
        self._is_loaded = False
        
        # Cross-attention projector: DINO hidden_dim -> SD cross_attention_dim
        self.cross_attention_projector = nn.Linear(self.dino_hidden_dim, cross_attention_dim)
        self._init_projector()
        
        # ImageNet normalization for DINOv3
        self.register_buffer(
            "imagenet_mean",
            torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1),
            persistent=False
        )
        self.register_buffer(
            "imagenet_std",
            torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1),
            persistent=False
        )
    
    @property
    def dtype(self) -> torch.dtype:
        """Return the dtype of the encoder (for diffusers compatibility)."""
        return self.cross_attention_projector.weight.dtype
    
    @property
    def device(self) -> torch.device:
        """Return the device of the encoder."""
        return self.cross_attention_projector.weight.device
    
    def _init_projector(self):
        """Initialize the cross-attention projector with Xavier initialization."""
        nn.init.xavier_uniform_(self.cross_attention_projector.weight)
        nn.init.zeros_(self.cross_attention_projector.bias)
    
    def _preprocess_image(self, pixel_values: torch.Tensor) -> torch.Tensor:
        """
        Preprocess image from [-1, 1] to ImageNet normalized format.
        
        Args:
            pixel_values: Input images, shape (B, 3, H, W), normalized to [-1, 1]
        
        Returns:
            Preprocessed images with ImageNet normalization
        """
        # Convert from [-1, 1] to [0, 1]
        pixel_values = (pixel_values + 1.0) / 2.0
        
        # Ensure mean/std are on the same device and dtype
        mean = self.imagenet_mean.to(device=pixel_values.device, dtype=pixel_values.dtype)
        std = self.imagenet_std.to(device=pixel_values.device, dtype=pixel_values.dtype)
        
        # Apply ImageNet normalization
        pixel_values = (pixel_values - mean) / std
        
        return pixel_values
    
    def load_dino_model(self, device: torch.device = None, dtype: torch.dtype = None):
        """
        Load the DINOv3 model from HuggingFace format.
        
        Args:
            device: Device to load the model on
            dtype: Data type for the model weights
        """
        if self._is_loaded:
            return
        
        if self.weights_path is None:
            raise ValueError("weights_path must be provided to load DINOv3 model")
        
        try:
            from transformers import AutoModel
            
            print(f"[DINOv3] Loading from: {self.weights_path}")
            self.dino_backbone = AutoModel.from_pretrained(
                self.weights_path,
                trust_remote_code=True,
            )
            
            # Update config from loaded model
            hf_config = getattr(self.dino_backbone, "config", None)
            if hf_config is not None:
                self.dino_hidden_dim = getattr(hf_config, "hidden_size", self.dino_hidden_dim)
                self.patch_size = getattr(hf_config, "patch_size", self.patch_size)
                self.n_storage_tokens = getattr(hf_config, "num_register_tokens", self.n_storage_tokens)
                
                # Reinitialize projector if hidden dim changed
                if self.cross_attention_projector.in_features != self.dino_hidden_dim:
                    self.cross_attention_projector = nn.Linear(
                        self.dino_hidden_dim, self.cross_attention_dim
                    )
                    self._init_projector()
            
            self._use_hf_interface = True
            
            # Move to device/dtype
            if device is not None:
                self.dino_backbone = self.dino_backbone.to(device)
                self.cross_attention_projector = self.cross_attention_projector.to(device)
            
            if dtype is not None:
                self.dino_backbone = self.dino_backbone.to(dtype)
                self.cross_attention_projector = self.cross_attention_projector.to(dtype)
            
            # Freeze backbone
            if self.freeze_encoder:
                self.dino_backbone.requires_grad_(False)
                self.dino_backbone.eval()
            
            self._is_loaded = True
            print(f"[DINOv3] Successfully loaded {self.model_name}")
            print(f"  - Hidden dim: {self.dino_hidden_dim}")
            print(f"  - Patch size: {self.patch_size}")
            print(f"  - Cross-attention dim: {self.cross_attention_dim}")
            
        except Exception as e:
            raise RuntimeError(
                f"Failed to load DINOv3 model from {self.weights_path}.\n"
                f"Error: {e}"
            )
    
    def _ensure_loaded(self):
        """Ensure the model is loaded before forward pass."""
        if not self._is_loaded:
            raise RuntimeError(
                "DINOv3 model not loaded. Call load_dino_model() first."
            )
    
    def extract_patch_tokens(self, pixel_values: torch.Tensor) -> torch.Tensor:
        """
        Extract patch tokens from DINOv3.
        
        Args:
            pixel_values: Input images, shape (B, 3, H, W), normalized to [-1, 1]
        
        Returns:
            patch_tokens: Shape (B, N, D) where N is number of patches, D is hidden_dim
        """
        self._ensure_loaded()
        
        # Preprocess image
        preprocessed = self._preprocess_image(pixel_values)
        
        # Ensure dimensions are multiples of patch_size
        _, _, H, W = preprocessed.shape
        new_H = (H // self.patch_size) * self.patch_size
        new_W = (W // self.patch_size) * self.patch_size
        if new_H != H or new_W != W:
            preprocessed = F.interpolate(
                preprocessed,
                size=(new_H, new_W),
                mode='bilinear',
                align_corners=False
            )
        
        # Forward through DINOv3
        with torch.no_grad() if self.freeze_encoder else torch.enable_grad():
            if self._use_hf_interface:
                outputs = self.dino_backbone(
                    pixel_values=preprocessed,
                    output_hidden_states=True
                )
                last_hidden = outputs.last_hidden_state
                # Remove CLS and register tokens
                n_special = 1 + self.n_storage_tokens
                patch_tokens = last_hidden[:, n_special:, :]
            else:
                outputs = self.dino_backbone.forward_features(preprocessed, masks=None)
                patch_tokens = outputs['x_norm_patchtokens']
        
        return patch_tokens
    
    def forward(self, pixel_values: torch.Tensor) -> Dict[str, torch.Tensor]:
        """
        Forward pass to extract features for cross-attention.
        
        Args:
            pixel_values: Input images, shape (B, 3, H, W), normalized to [-1, 1]
        
        Returns:
            dict with 'cross_attention_features': Projected features, shape (B, N, cross_attention_dim)
        """
        self._ensure_loaded()
        
        # Extract patch tokens
        patch_tokens = self.extract_patch_tokens(pixel_values)
        
        # Project to cross-attention dimension
        projector_dtype = next(self.cross_attention_projector.parameters()).dtype
        if patch_tokens.dtype != projector_dtype:
            patch_tokens = patch_tokens.to(dtype=projector_dtype)
        
        cross_attention_features = self.cross_attention_projector(patch_tokens)
        
        return {'cross_attention_features': cross_attention_features}
    
    def get_cross_attention_features(self, pixel_values: torch.Tensor) -> torch.Tensor:
        """
        Convenience method to get only cross-attention features.
        
        Args:
            pixel_values: Input images, shape (B, 3, H, W), normalized to [-1, 1]
        
        Returns:
            cross_attention_features: Shape (B, N, cross_attention_dim)
        """
        return self.forward(pixel_values)['cross_attention_features']
    
    def load_projector(self, projector_path: str, device: torch.device = None):
        """
        Load pretrained projector weights.
        
        Args:
            projector_path: Path to projector weights file (.pt)
            device: Device to load weights on
        """
        if not os.path.exists(projector_path):
            raise FileNotFoundError(f"Projector weights not found: {projector_path}")
        
        state_dict = torch.load(projector_path, map_location=device or "cpu")
        self.cross_attention_projector.load_state_dict(state_dict)
        print(f"[DINOv3] Loaded projector weights from {projector_path}")


def create_dino_encoder(
    model_name: str = "dinov3_vith16plus",
    cross_attention_dim: int = 1024,
    weights_path: Optional[str] = None,
    projector_path: Optional[str] = None,
    device: torch.device = None,
    dtype: torch.dtype = None,
    freeze_encoder: bool = True,
) -> DINOv3Encoder:
    """
    Factory function to create and initialize a DINOv3 encoder.
    
    Args:
        model_name: DINOv3 model name
        cross_attention_dim: Target dimension for cross-attention
        weights_path: Path to DINOv3 pretrained weights
        projector_path: Path to projector weights (optional)
        device: Device to load the model on
        dtype: Data type for the model
        freeze_encoder: Whether to freeze the backbone
    
    Returns:
        Initialized DINOv3Encoder
    """
    encoder = DINOv3Encoder(
        model_name=model_name,
        cross_attention_dim=cross_attention_dim,
        weights_path=weights_path,
        freeze_encoder=freeze_encoder,
    )
    
    # Load DINO backbone
    if weights_path is not None:
        encoder.load_dino_model(device=device, dtype=dtype)
    
    # Load projector weights if provided
    if projector_path is not None:
        encoder.load_projector(projector_path, device=device)
    
    # Move to device
    if device is not None:
        encoder = encoder.to(device)
    
    if dtype is not None:
        encoder = encoder.to(dtype)
    
    return encoder