-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeature_extractor.py
More file actions
354 lines (291 loc) · 11.9 KB
/
feature_extractor.py
File metadata and controls
354 lines (291 loc) · 11.9 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
"""
Generic FeatureExtractor wrapper class hierarchy.
Replaces the args.model if/elif chain in distillation_v3.py.
Each model gets its own subclass that wraps the corresponding get_pixel_features_* function.
"""
import torch
import gc
class FeatureExtractor:
"""Base class for all feature extraction models."""
def __init__(self, model_name, device, **kwargs):
self.model_name = model_name
self.device = device
self.model = None
self._kwargs = kwargs
def init_model(self):
"""Initialize the underlying model. Called once."""
raise NotImplementedError
def get_pixel_features(self, renderings, H, W, **kwargs):
"""
Extract per-pixel features from rendered views.
Args:
renderings: (B, H, W, 4) rendered images
H, W: image dimensions
**kwargs: model-specific args (batch_size, normalize, debug, etc.)
Returns:
view_features: list of feature tensors or a single tensor
"""
raise NotImplementedError
def cleanup(self):
"""Free GPU memory after feature extraction."""
if self.model is not None:
try:
self.model = self.model.to("cpu")
except Exception:
pass
del self.model
self.model = None
gc.collect()
torch.cuda.empty_cache()
@property
def needs_normal_map(self):
"""Whether this model requires normal map renderings."""
return False
@property
def supports_sam_refinement(self):
"""Whether SAM-based feature refinement is applicable to this model."""
return False
class DINOv2Extractor(FeatureExtractor):
"""DINOv2 feature extraction."""
def init_model(self, arch=None):
if self.model is not None:
return
from dino import init_dino
arch = self._kwargs["arch"]
if arch is None:
print("Warning: arch is not provided for dino2. Using default arch: dinov2_vitg14_reg")
arch = "dinov2_vitg14_reg"
self.model = init_dino(self.device, archtype=arch)
self.model_name = arch
@property
def supports_sam_refinement(self):
return True
def get_pixel_features(self, renderings, H, W, *,
batch_size=10, normalize=True,
debug=False, **kwargs):
from pixel_features import get_pixel_features_dino
return get_pixel_features_dino(
self.device, self.model, renderings,
H=H, W=W,
normalize=normalize,
debug=debug,
batch_size=batch_size,
)
class DINOv3Extractor(FeatureExtractor):
""" DINOv3 feature extraction.
Required kwargs: repodir, checkpoint
"""
def init_model(self, arch=None, checkpoint=None, repodir=None):
if self.model is not None:
return
from dino import init_dino
arch = self._kwargs["arch"]
if arch is None:
print("Warning: arch is not provided for dino3. Using default arch: dinov3_vit7b16")
arch = "dinov3_vit7b16"
checkpoint = self._kwargs["checkpoint"]
if checkpoint is None:
print("Warning: checkpoint is not provided for dino3. Using default checkpoint: /net/projects2/ranalab/dinov3_vit7b16_pretrain_lvd1689m-a955f4ea.pth")
checkpoint = "/net/projects2/ranalab/dinov3_vit7b16_pretrain_lvd1689m-a955f4ea.pth"
repodir = self._kwargs["repodir"]
if repodir is None:
print("Warning: repodir is not provided for dino3. Using default repodir: /net/projects/ranalab/guanzhi/dinov3")
repodir = "/net/projects/ranalab/guanzhi/dinov3"
self.model = init_dino(
self.device,
repodir=repodir,
archtype=arch,
source="local",
weights=checkpoint,
)
self.model_name = arch
@property
def supports_sam_refinement(self):
return True
def get_pixel_features(self, renderings, H, W, *,
batch_size=10, normalize=True, half=True,
debug=False, **kwargs):
from pixel_features import get_pixel_features_dino3
return get_pixel_features_dino3(
self.device, self.model, renderings,
H=H, W=W,
normalize=normalize,
half=half,
debug=debug,
batch_size=batch_size,
)
class RADIOExtractor(FeatureExtractor):
"""RADIO feature extraction."""
def init_model(self, arch=None):
if self.model is not None and getattr(self, "image_processor", None) is not None:
return
from transformers import AutoModel, CLIPImageProcessor
arch = self._kwargs["arch"]
if arch is None:
print("Warning: arch is not provided for radio. Using default arch: C-RADIOv3-g")
arch = "C-RADIOv3-g"
self.image_processor = CLIPImageProcessor.from_pretrained(f"nvidia/{arch}")
self.model = AutoModel.from_pretrained(f"nvidia/{arch}", trust_remote_code=True)
self.model.eval().to(self.device)
self.model_name = f"radio_{arch}"
@property
def supports_sam_refinement(self):
return True
def get_pixel_features(self, renderings, H, W, *,
batch_size=10, normalize=True, half=True,
debug=False, **kwargs):
from pixel_features import get_pixel_features_radio
return get_pixel_features_radio(
self.device,
self.model,
self.image_processor,
renderings,
H=H, W=W,
normalize=normalize,
half=half,
debug=debug,
batch_size=batch_size,
)
def cleanup(self):
# `image_processor` is CPU-only but can hold large configs/caches; clear it too.
if hasattr(self, "image_processor"):
del self.image_processor
self.image_processor = None
super().cleanup()
class SAMExtractor(FeatureExtractor):
"""SAM v1 feature extraction."""
def init_model(self, arch=None, checkpoint=None):
if self.model is not None:
return
from segment_anything import SamPredictor, sam_model_registry
arch = self._kwargs["arch"]
if arch is None:
print("Warning: arch is not provided for sam. Using default arch: vit_l")
arch = "vit_l"
checkpoint = self._kwargs["checkpoint"]
if checkpoint is None:
print("Warning: checkpoint is not provided for sam. Using default checkpoint: /net/scratch/rliu/SAMmodels/sam_vit_l_0b3195.pth")
checkpoint = "/net/scratch/rliu/SAMmodels/sam_vit_l_0b3195.pth"
sam = sam_model_registry[arch](checkpoint=checkpoint)
self.model = SamPredictor(sam)
self.model.model = self.model.model.to(self.device)
self.model_name = f"sam_{arch}"
def get_pixel_features(self, renderings, H, W, *,
batch_size=5, normalize=True, half=True,
debug=False, **kwargs):
from pixel_features import get_pixel_features_sam
return get_pixel_features_sam(
self.device, self.model, renderings,
normalize=normalize,
half=half,
debug=debug,
batch_size=batch_size,
)
def cleanup(self):
super().cleanup()
class SAM2Extractor(FeatureExtractor):
"""SAM2 feature extraction."""
def init_model(self, repodir=None, checkpoint=None, model_cfg=None):
import sys
import os
if self.model is not None:
return
repodir = self._kwargs["repodir"]
if repodir is None:
print("Warning: repodir is not provided for sam2. Using default repodir: /net/projects/ranalab/guanzhi/DFD/sam2_repo")
repodir = "/net/projects/ranalab/guanzhi/DFD/sam2_repo"
if not os.path.isdir(repodir):
raise FileNotFoundError(
f"SAM2 repo not found at {repodir}. "
f"Set repodir=... when creating the extractor, or create/clone sam2_repo."
)
if repodir not in sys.path:
sys.path.append(repodir)
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
checkpoint = self._kwargs["checkpoint"]
if checkpoint is None:
print("Warning: checkpoint is not provided for sam2. Using default checkpoint: /net/projects/ranalab/guanzhi/DFD/sam2_repo/checkpoints/sam2.1_hiera_large.pt")
checkpoint = os.path.join(repodir, "checkpoints/sam2.1_hiera_large.pt")
model_cfg = self._kwargs["model_cfg"]
if model_cfg is None:
print("Warning: model_cfg is not provided for sam2. Using default local model_cfg: configs/sam2.1/sam2.1_hiera_l.yaml")
model_cfg = "configs/sam2.1/sam2.1_hiera_l.yaml"
self.model = SAM2ImagePredictor(build_sam2(model_cfg, checkpoint))
def get_pixel_features(self, renderings, H, W, *,
batch_size=20, normalize=True, half=True,
debug=False, concat_hr=False, **kwargs):
from pixel_features import get_pixel_features_sam2
return get_pixel_features_sam2(
self.device, self.model, renderings,
normalize=normalize,
half=half,
debug=debug,
batch_size=batch_size,
concat_hr=concat_hr,
)
def cleanup(self):
super().cleanup()
class CLIPExtractor(FeatureExtractor):
"""CLIP feature extraction."""
def init_model(self, arch=None, checkpoint=None):
if self.model is not None:
return
from clip_stuff import CLIPConvFeatures
arch = self._kwargs["arch"]
if arch is None:
print("Warning: arch is not provided for clip. Using default arch: ViT-L-14")
arch = "ViT-L-14"
checkpoint = self._kwargs["checkpoint"]
if checkpoint is None:
print("Warning: checkpoint is not provided for clip. Using default checkpoint: /net/scratch/rliu/CLIPmodels/ViT-L-14.pt")
checkpoint = "/net/scratch/rliu/CLIPmodels/ViT-L-14.pt"
self.model = CLIPConvFeatures(
device=self.device,
clip_model_name=arch,
clip_model_path=checkpoint,
num_augs=0,
)
self.model_name = f"clip_{arch}"
def get_pixel_features(self, renderings, H, W, *,
batch_size=20, normalize=True, half=False,
debug=False, **kwargs):
from pixel_features import get_pixel_features_clip
return get_pixel_features_clip(
self.device, self.model, renderings,
normalize=normalize,
half=half,
debug=debug,
batch_size=batch_size,
)
def cleanup(self):
super().cleanup()
# --- Registry and factory ---
EXTRACTORS = {
"dino2": DINOv2Extractor,
"dino3": DINOv3Extractor,
"radio": RADIOExtractor,
"sam": SAMExtractor,
"sam2": SAM2Extractor,
"clip": CLIPExtractor,
}
def create_extractor(model_name, device, arch=None, checkpoint=None, repodir=None, model_cfg=None):
"""
Factory function to create and initialize a FeatureExtractor.
Args:
model_name: one of 'dino2', 'dino3', 'radio', 'sam', 'sam2', 'clip'
device: torch device
arch: architecture name
checkpoint: checkpoint path
repodir: repository directory
model_cfg: model configuration file
Returns:
FeatureExtractor instance with model loaded
"""
if model_name not in EXTRACTORS:
raise ValueError(
f"Unknown model: {model_name}. Available: {sorted(set(list(EXTRACTORS.keys())))}"
)
extractor = EXTRACTORS[model_name](model_name, device, arch=arch, checkpoint=checkpoint, repodir=repodir, model_cfg=model_cfg)
extractor.init_model()
return extractor