Spaces:
Running on Zero
Running on Zero
Upload models/dinov3_hf_extractor.py with huggingface_hub
Browse files
models/dinov3_hf_extractor.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
"""DINOv3 feature extraction via HuggingFace, handling CLS and register tokens."""
|
| 2 |
|
|
|
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
| 5 |
from transformers import AutoImageProcessor, AutoModel
|
|
@@ -22,14 +23,15 @@ class DINOv3HFExtractor(nn.Module):
|
|
| 22 |
def __init__(self, repo_id="facebook/dinov3-vitb16-pretrain-lvd1689m", take_last=None, take_indices=None, trainable=False):
|
| 23 |
super().__init__()
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
# Disable resizing/cropping so native 512x512 maps to 32x32 patches
|
| 28 |
for k in ("do_resize", "do_center_crop"):
|
| 29 |
if hasattr(self.proc, k):
|
| 30 |
setattr(self.proc, k, False)
|
| 31 |
|
| 32 |
-
self.model = AutoModel.from_pretrained(repo_id)
|
| 33 |
self.model.config.output_hidden_states = True
|
| 34 |
|
| 35 |
# trainable=True is used by the `dino_only` ablation (fine-tune the backbone);
|
|
|
|
| 1 |
"""DINOv3 feature extraction via HuggingFace, handling CLS and register tokens."""
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
import torch
|
| 5 |
import torch.nn as nn
|
| 6 |
from transformers import AutoImageProcessor, AutoModel
|
|
|
|
| 23 |
def __init__(self, repo_id="facebook/dinov3-vitb16-pretrain-lvd1689m", take_last=None, take_indices=None, trainable=False):
|
| 24 |
super().__init__()
|
| 25 |
|
| 26 |
+
token = os.environ.get("HF_TOKEN")
|
| 27 |
+
self.proc = AutoImageProcessor.from_pretrained(repo_id, token=token)
|
| 28 |
|
| 29 |
# Disable resizing/cropping so native 512x512 maps to 32x32 patches
|
| 30 |
for k in ("do_resize", "do_center_crop"):
|
| 31 |
if hasattr(self.proc, k):
|
| 32 |
setattr(self.proc, k, False)
|
| 33 |
|
| 34 |
+
self.model = AutoModel.from_pretrained(repo_id, token=token)
|
| 35 |
self.model.config.output_hidden_states = True
|
| 36 |
|
| 37 |
# trainable=True is used by the `dino_only` ablation (fine-tune the backbone);
|