Spaces:
Sleeping
Sleeping
Upload models/dinov3_hf_extractor.py with huggingface_hub
Browse files2 unmodified lines345678910111213141516171819202122232425262728293031323334353637383940413 unmodified lines454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361372 unmodified linesimport osimport torchimport torch.nn as nnfrom transformers import AutoImageProcessor, AutoModelclass DINOv3HFExtractor(nn.Module):"""Extracts intermediate features from DINOv3 via HuggingFace transformers.Properly handles DINOv3 register tokens:- Output shape: [B, 1 + P + R, C] where:- 1 = CLS token- P = spatial patches (1024 for 512x512 with 16x16 patches)- R = register tokens (typically 4 for DINOv3)Returns 4 feature maps of shape [B, C_dino, 32, 32] from last 4 layers.Input images must be [B, 3, 512, 512] in [0, 1] range."""def __init__(self, repo_id="facebook/dinov3-vitb16-pretrain-lvd1689m", take_last=None, take_indices=None, trainable=False):super().__init__()token = os.environ.get("HF_TOKEN")self.proc = AutoImageProcessor.from_pretrained(repo_id, token=token)# Disable resizing/cropping so native 512x512 maps to 32x32 patchesfor k in ("do_resize", "do_center_crop"):if hasattr(self.proc, k):setattr(self.proc, k, False)self.model = AutoModel.from_pretrained(repo_id, token=token)self.model.config.output_hidden_states = True# trainable=True is used by the `dino_only` ablation (fine-tune the backbone);# otherwise the backbone is frozen and kept in eval mode.self._frozen = not trainableif self._frozen:self.model.eval()3 unmodified linesself.model.train()for p in self.model.parameters():p.requires_grad = True# ImageNet normalization stats: mean [0.485, 0.456, 0.406], std [0.229, 0.224, 0.225]mean = torch.tensor(self.proc.image_mean).view(1, 3, 1, 1)std = torch.tensor(self.proc.image_std).view(1, 3, 1, 1)self.register_buffer("mean", mean, persistent=False)self.register_buffer("std", std, persistent=False)if take_indices is not None:self.take_indices = take_indicesself.take_last = Noneelse:self.take_last = take_last if take_last is not None else 4self.take_indices = Noneself.patch_size = getattr(self.model.config, "patch_size", 16)self.num_register_tokens = getattr(self.model.config, "num_register_tokens", 0)hidden_size = getattr(self.model.config, "hidden_size", 768)layers = self.take_indices if self.take_indices is not None else f"last {self.take_last}"trainable_str = "trainable" if not self._frozen else "frozen"print(f"[DINOv3] {repo_id.split('/')[-1]}: dim={hidden_size}, patch={self.patch_size}, "f"layers={layers} ({trainable_str})")def train(self, mode: bool = True):"""Keep a frozen backbone in eval mode; otherwise follow `mode`."""self.training = modeif self._frozen:self.model.eval()else:self.model.train(mode)return selfdef forward(self, images_512: torch.Tensor):"""Extract DINOv3 features from [B, 3, H, W] images in [0, 1]; returns maps [B, C, H//16, W//16]."""# Disable grad only when the backbone is frozen; otherwise allow fine-tuningwith torch.set_grad_enabled(not self._frozen):return self._forward(images_512)def _forward(self, images_512: torch.Tensor):x = (images_512 - self.mean) / self.stdout = self.model(pixel_values=x, output_hidden_states=True)hidden_states = out.hidden_states # Tuple: [emb, blk1, ..., blkL]B, _, H, W = images_512.shapeH_patches = H // self.patch_sizeW_patches = W // self.patch_sizeP = H_patches * W_patches # total spatial patchesR = self.num_register_tokens# Each hidden state is [B, 1 + P + R, C], laid out as [CLS][P spatial patches][R register tokens]maps = []if self.take_indices is not None:for idx in self.take_indices:hidden = hidden_states[idx]spatial = hidden[:, 1:1+P, :] # [B, P, C], drop CLS and register tokensC = spatial.shape[-1]spatial_map = spatial.transpose(1, 2).reshape(B, C, H_patches, W_patches).contiguous()maps.append(spatial_map)else:for hidden in hidden_states[-self.take_last:]:spatial = hidden[:, 1:1+P, :] # [B, P, C], drop CLS and register tokensC = spatial.shape[-1]spatial_map = spatial.transpose(1, 2).reshape(B, C, H_patches, W_patches).contiguous()maps.append(spatial_map)return mapsdef create_dinov3_hf_extractor(repo_id="facebook/dinov3-vitb16-pretrain-lvd1689m", take_last=None, take_indices=None, trainable=False):"""Factory for DINOv3HFExtractor (frozen in eval mode unless trainable=True).repo_id options: vits16 (384-dim), vitb16 (768-dim, recommended), vitl16 (1024-dim)."""return DINOv3HFExtractor(repo_id=repo_id, take_last=take_last, take_indices=take_indices, trainable=trainable)if __name__ == "__main__":extractor = create_dinov3_hf_extractor().cuda()dummy_input = torch.randn(2, 3, 512, 512).cuda()print(f"\nInput: {dummy_input.shape}")features = extractor(dummy_input)print(f"\nExtracted {len(features)} feature maps:")for i, feat in enumerate(features):print(f" Layer {i}: {feat.shape}")2 unmodified lines34567891011121314151617181920212223242526272829303132333435363738394041424344454647484950513 unmodified lines5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301312 unmodified linesimport osimport torchimport torch.nn as nnfrom transformers import DINOv3ViTConfig, DINOv3ViTModel, DINOv3ViTImageProcessorFastclass DINOv3HFExtractor(nn.Module):"""Extracts intermediate features from DINOv3 via HuggingFace transformers.Builds the model from config (no gated download required); weights are loadedfrom the MMDiff checkpoint which bundles the DINOv3 backbone.Returns 4 feature maps of shape [B, C_dino, H//16, W//16] from selected layers.Input images must be [B, 3, H, W] in [0, 1] range."""def __init__(self, repo_id="facebook/dinov3-vitb16-pretrain-lvd1689m",take_last=None, take_indices=None, trainable=False,hidden_size=768, num_hidden_layers=12, num_attention_heads=12,intermediate_size=3072, patch_size=16, image_size=512,num_register_tokens=4):super().__init__()# Build image processor from default config (no gated download needed)self.proc = DINOv3ViTImageProcessorFast()# Disable resizing/cropping so native resolution maps to patchesfor k in ("do_resize", "do_center_crop"):if hasattr(self.proc, k):setattr(self.proc, k, False)# Build model from config (random weights; real weights loaded from checkpoint)config = DINOv3ViTConfig(hidden_size=hidden_size,num_hidden_layers=num_hidden_layers,num_attention_heads=num_attention_heads,intermediate_size=intermediate_size,patch_size=patch_size,image_size=image_size,num_register_tokens=num_register_tokens,hidden_act="gelu",)self.model = DINOv3ViTModel(config)self.model.config.output_hidden_states = Trueself._frozen = not trainableif self._frozen:self.model.eval()3 unmodified linesself.model.train()for p in self.model.parameters():p.requires_grad = True# ImageNet normalization statsmean = torch.tensor(self.proc.image_mean).view(1, 3, 1, 1)std = torch.tensor(self.proc.image_std).view(1, 3, 1, 1)self.register_buffer("mean", mean, persistent=False)self.register_buffer("std", std, persistent=False)if take_indices is not None:self.take_indices = take_indicesself.take_last = Noneelse:self.take_last = take_last if take_last is not None else 4self.take_indices = Noneself.patch_size = getattr(self.model.config, "patch_size", 16)self.num_register_tokens = getattr(self.model.config, "num_register_tokens", 0)hidden_size = getattr(self.model.config, "hidden_size", 768)layers = self.take_indices if self.take_indices is not None else f"last {self.take_last}"trainable_str = "trainable" if not self._frozen else "frozen"print(f"[DINOv3] Built from config: dim={hidden_size}, patch={self.patch_size}, "f"layers={layers} ({trainable_str})")def train(self, mode: bool = True):self.training = modeif self._frozen:self.model.eval()else:self.model.train(mode)return selfdef forward(self, images_512: torch.Tensor):with torch.set_grad_enabled(not self._frozen):return self._forward(images_512)def _forward(self, images_512: torch.Tensor):x = (images_512 - self.mean) / self.stdout = self.model(pixel_values=x, output_hidden_states=True)hidden_states = out.hidden_statesB, _, H, W = images_512.shapeH_patches = H // self.patch_sizeW_patches = W // self.patch_sizeP = H_patches * W_patchesR = self.num_register_tokensmaps = []if self.take_indices is not None:for idx in self.take_indices:hidden = hidden_states[idx]spatial = hidden[:, 1:1+P, :]C = spatial.shape[-1]spatial_map = spatial.transpose(1, 2).reshape(B, C, H_patches, W_patches).contiguous()maps.append(spatial_map)else:for hidden in hidden_states[-self.take_last:]:spatial = hidden[:, 1:1+P, :]C = spatial.shape[-1]spatial_map = spatial.transpose(1, 2).reshape(B, C, H_patches, W_patches).contiguous()maps.append(spatial_map)return mapsdef create_dinov3_hf_extractor(repo_id="facebook/dinov3-vitb16-pretrain-lvd1689m", take_last=None, take_indices=None, trainable=False):"""Factory for DINOv3HFExtractor (frozen in eval mode unless trainable=True).Builds from config — no gated download required."""return DINOv3HFExtractor(repo_id=repo_id, take_last=take_last, take_indices=take_indices, trainable=trainable,)
models/dinov3_hf_extractor.py+52-58