mirror of
https://github.com/SikongJueluo/Mini-Nav.git
synced 2026-03-12 12:25:32 +08:00
feat(benchmarks): add evaluation framework for DINO-based compressors
This commit is contained in:
29
mini-nav/compressors/dino_compressor.py
Normal file
29
mini-nav/compressors/dino_compressor.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from typing import Optional, cast
|
||||
|
||||
import torch.nn.functional as F
|
||||
from torch import nn
|
||||
from transformers import AutoModel, Dinov2Model
|
||||
|
||||
|
||||
class DinoCompressor(nn.Module):
|
||||
def __init__(self, compressor: Optional[nn.Module] = None):
|
||||
super().__init__()
|
||||
|
||||
self.dino = cast(
|
||||
Dinov2Model,
|
||||
AutoModel.from_pretrained("facebook/dinov2-large"),
|
||||
)
|
||||
|
||||
self.compressor = compressor
|
||||
|
||||
def forward(self, inputs):
|
||||
teacher_tokens = self.dino(**inputs).last_hidden_state # [B,N,1024]
|
||||
|
||||
teacher_embed = teacher_tokens.mean(dim=1)
|
||||
teacher_embed = F.normalize(teacher_embed, dim=-1) # [B,1024]
|
||||
|
||||
if self.compressor is None:
|
||||
return teacher_embed
|
||||
|
||||
feats, recon = self.compressor(teacher_tokens)
|
||||
return feats
|
||||
Reference in New Issue
Block a user