Image Feature Extraction
Transformers
Safetensors
English
gr_lite
fashion
image-retrieval
vision-transformer
dino
custom_code
Instructions to use srpone/gr-lite with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use srpone/gr-lite with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="srpone/gr-lite", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("srpone/gr-lite", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Add HuggingFace-compatible model (config + safetensors + model code)
Browse files- config.json +20 -0
- configuration_gr_lite.py +35 -0
- model.safetensors +3 -0
- modeling_gr_lite.py +146 -0
config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_attn_implementation_autoset": true,
|
| 3 |
+
"hidden_size": 1024,
|
| 4 |
+
"image_size": 336,
|
| 5 |
+
"intermediate_size": 4096,
|
| 6 |
+
"k_bias": false,
|
| 7 |
+
"layer_norm_eps": 1e-06,
|
| 8 |
+
"model_type": "gr_lite",
|
| 9 |
+
"num_attention_heads": 16,
|
| 10 |
+
"num_channels": 3,
|
| 11 |
+
"num_hidden_layers": 24,
|
| 12 |
+
"num_register_tokens": 4,
|
| 13 |
+
"patch_size": 16,
|
| 14 |
+
"qkv_bias": true,
|
| 15 |
+
"transformers_version": "4.49.0",
|
| 16 |
+
"auto_map": {
|
| 17 |
+
"AutoConfig": "configuration_gr_lite.GRLiteConfig",
|
| 18 |
+
"AutoModel": "modeling_gr_lite.GRLiteModel"
|
| 19 |
+
}
|
| 20 |
+
}
|
configuration_gr_lite.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""GR-Lite model configuration."""
|
| 2 |
+
|
| 3 |
+
from transformers import PretrainedConfig
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class GRLiteConfig(PretrainedConfig):
|
| 7 |
+
model_type = "gr_lite"
|
| 8 |
+
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
hidden_size=1024,
|
| 12 |
+
num_hidden_layers=24,
|
| 13 |
+
num_attention_heads=16,
|
| 14 |
+
intermediate_size=4096,
|
| 15 |
+
patch_size=16,
|
| 16 |
+
image_size=336,
|
| 17 |
+
num_channels=3,
|
| 18 |
+
num_register_tokens=4,
|
| 19 |
+
layer_norm_eps=1e-6,
|
| 20 |
+
qkv_bias=True,
|
| 21 |
+
k_bias=False,
|
| 22 |
+
**kwargs,
|
| 23 |
+
):
|
| 24 |
+
super().__init__(**kwargs)
|
| 25 |
+
self.hidden_size = hidden_size
|
| 26 |
+
self.num_hidden_layers = num_hidden_layers
|
| 27 |
+
self.num_attention_heads = num_attention_heads
|
| 28 |
+
self.intermediate_size = intermediate_size
|
| 29 |
+
self.patch_size = patch_size
|
| 30 |
+
self.image_size = image_size
|
| 31 |
+
self.num_channels = num_channels
|
| 32 |
+
self.num_register_tokens = num_register_tokens
|
| 33 |
+
self.layer_norm_eps = layer_norm_eps
|
| 34 |
+
self.qkv_bias = qkv_bias
|
| 35 |
+
self.k_bias = k_bias
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256: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""
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
from transformers import PreTrainedModel
|
| 7 |
+
from transformers.modeling_outputs import BaseModelOutputWithPooling
|
| 8 |
+
from .configuration_gr_lite import GRLiteConfig
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class GRLiteEmbeddings(nn.Module):
|
| 12 |
+
def __init__(self, config: GRLiteConfig):
|
| 13 |
+
super().__init__()
|
| 14 |
+
self.cls_token = nn.Parameter(torch.zeros(1, 1, config.hidden_size))
|
| 15 |
+
self.mask_token = nn.Parameter(torch.zeros(1, 1, config.hidden_size))
|
| 16 |
+
self.register_tokens = nn.Parameter(
|
| 17 |
+
torch.zeros(1, config.num_register_tokens, config.hidden_size)
|
| 18 |
+
)
|
| 19 |
+
self.patch_embeddings = nn.Conv2d(
|
| 20 |
+
config.num_channels,
|
| 21 |
+
config.hidden_size,
|
| 22 |
+
kernel_size=config.patch_size,
|
| 23 |
+
stride=config.patch_size,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
def forward(self, pixel_values: torch.Tensor) -> torch.Tensor:
|
| 27 |
+
B = pixel_values.shape[0]
|
| 28 |
+
x = self.patch_embeddings(pixel_values) # [B, D, H/P, W/P]
|
| 29 |
+
x = x.flatten(2).transpose(1, 2) # [B, N, D]
|
| 30 |
+
cls = self.cls_token.expand(B, -1, -1)
|
| 31 |
+
reg = self.register_tokens.expand(B, -1, -1)
|
| 32 |
+
x = torch.cat([cls, reg, x], dim=1) # [B, 1+R+N, D]
|
| 33 |
+
return x
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class GRLiteAttention(nn.Module):
|
| 37 |
+
def __init__(self, config: GRLiteConfig):
|
| 38 |
+
super().__init__()
|
| 39 |
+
self.num_heads = config.num_attention_heads
|
| 40 |
+
self.head_dim = config.hidden_size // config.num_attention_heads
|
| 41 |
+
self.scale = self.head_dim**-0.5
|
| 42 |
+
|
| 43 |
+
self.q_proj = nn.Linear(config.hidden_size, config.hidden_size, bias=config.qkv_bias)
|
| 44 |
+
self.k_proj = nn.Linear(config.hidden_size, config.hidden_size, bias=config.k_bias)
|
| 45 |
+
self.v_proj = nn.Linear(config.hidden_size, config.hidden_size, bias=config.qkv_bias)
|
| 46 |
+
self.o_proj = nn.Linear(config.hidden_size, config.hidden_size, bias=True)
|
| 47 |
+
|
| 48 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 49 |
+
B, N, _ = x.shape
|
| 50 |
+
q = self.q_proj(x).reshape(B, N, self.num_heads, self.head_dim).permute(0, 2, 1, 3)
|
| 51 |
+
k = self.k_proj(x).reshape(B, N, self.num_heads, self.head_dim).permute(0, 2, 1, 3)
|
| 52 |
+
v = self.v_proj(x).reshape(B, N, self.num_heads, self.head_dim).permute(0, 2, 1, 3)
|
| 53 |
+
|
| 54 |
+
attn = (q @ k.transpose(-2, -1)) * self.scale
|
| 55 |
+
attn = attn.softmax(dim=-1)
|
| 56 |
+
|
| 57 |
+
x = (attn @ v).transpose(1, 2).reshape(B, N, -1)
|
| 58 |
+
return self.o_proj(x)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class GRLiteLayerScale(nn.Module):
|
| 62 |
+
def __init__(self, dim: int):
|
| 63 |
+
super().__init__()
|
| 64 |
+
self.lambda1 = nn.Parameter(torch.ones(dim))
|
| 65 |
+
|
| 66 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 67 |
+
return x * self.lambda1
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class GRLiteMLP(nn.Module):
|
| 71 |
+
def __init__(self, config: GRLiteConfig):
|
| 72 |
+
super().__init__()
|
| 73 |
+
self.up_proj = nn.Linear(config.hidden_size, config.intermediate_size)
|
| 74 |
+
self.down_proj = nn.Linear(config.intermediate_size, config.hidden_size)
|
| 75 |
+
self.act = nn.GELU()
|
| 76 |
+
|
| 77 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 78 |
+
return self.down_proj(self.act(self.up_proj(x)))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class GRLiteLayer(nn.Module):
|
| 82 |
+
def __init__(self, config: GRLiteConfig):
|
| 83 |
+
super().__init__()
|
| 84 |
+
self.norm1 = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 85 |
+
self.attention = GRLiteAttention(config)
|
| 86 |
+
self.layer_scale1 = GRLiteLayerScale(config.hidden_size)
|
| 87 |
+
self.norm2 = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 88 |
+
self.mlp = GRLiteMLP(config)
|
| 89 |
+
self.layer_scale2 = GRLiteLayerScale(config.hidden_size)
|
| 90 |
+
|
| 91 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 92 |
+
x = x + self.layer_scale1(self.attention(self.norm1(x)))
|
| 93 |
+
x = x + self.layer_scale2(self.mlp(self.norm2(x)))
|
| 94 |
+
return x
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class GRLitePreTrainedModel(PreTrainedModel):
|
| 98 |
+
config_class = GRLiteConfig
|
| 99 |
+
base_model_prefix = "model"
|
| 100 |
+
supports_gradient_checkpointing = False
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
class GRLiteModel(GRLitePreTrainedModel):
|
| 104 |
+
"""GR-Lite: Fashion image retrieval model (DINOv2 ViT-L/16 backbone).
|
| 105 |
+
|
| 106 |
+
Produces L2-normalized 1024-dim image embeddings.
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
def __init__(self, config: GRLiteConfig):
|
| 110 |
+
super().__init__(config)
|
| 111 |
+
self.embeddings = GRLiteEmbeddings(config)
|
| 112 |
+
self.layer = nn.ModuleList(
|
| 113 |
+
[GRLiteLayer(config) for _ in range(config.num_hidden_layers)]
|
| 114 |
+
)
|
| 115 |
+
self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 116 |
+
|
| 117 |
+
def forward(
|
| 118 |
+
self,
|
| 119 |
+
pixel_values: torch.Tensor,
|
| 120 |
+
output_hidden_states: bool = False,
|
| 121 |
+
return_dict: bool = True,
|
| 122 |
+
**kwargs,
|
| 123 |
+
):
|
| 124 |
+
x = self.embeddings(pixel_values)
|
| 125 |
+
|
| 126 |
+
hidden_states = () if output_hidden_states else None
|
| 127 |
+
for layer_module in self.layer:
|
| 128 |
+
if output_hidden_states:
|
| 129 |
+
hidden_states += (x,)
|
| 130 |
+
x = layer_module(x)
|
| 131 |
+
|
| 132 |
+
x = self.norm(x)
|
| 133 |
+
if output_hidden_states:
|
| 134 |
+
hidden_states += (x,)
|
| 135 |
+
|
| 136 |
+
# CLS token embedding, L2-normalized
|
| 137 |
+
pooled = F.normalize(x[:, 0], p=2, dim=-1)
|
| 138 |
+
|
| 139 |
+
if not return_dict:
|
| 140 |
+
return (x, pooled, hidden_states) if output_hidden_states else (x, pooled)
|
| 141 |
+
|
| 142 |
+
return BaseModelOutputWithPooling(
|
| 143 |
+
last_hidden_state=x,
|
| 144 |
+
pooler_output=pooled,
|
| 145 |
+
hidden_states=hidden_states,
|
| 146 |
+
)
|