|
import torch |
|
import torch.nn as nn |
|
from transformers import CLIPTextModel, RobertaModel, CLIPVisionModel |
|
from timm import create_model |
|
EMBEDDING_DIM = 512 |
|
class ImageEncoder(nn.Module): |
|
def __init__(self): |
|
super(ImageEncoder, self).__init__() |
|
|
|
self.swin = create_model("swin-tiny-patch4-window7-224", pretrained=True, features_only=True) |
|
for param in self.swin.parameters(): |
|
param.requires_grad = True |
|
|
|
|
|
self.swin_output_dim = self.swin.feature_info.channels()[-1] |
|
|
|
|
|
self.fc1 = nn.Linear(self.swin_output_dim * 7 * 7, EMBEDDING_DIM) |
|
nn.init.xavier_uniform_(self.fc1.weight) |
|
nn.init.zeros_(self.fc1.bias) |
|
for param in self.fc1.parameters(): |
|
param.requires_grad = True |
|
|
|
|
|
def forward(self, x): |
|
|
|
swin_features = self.swin(x)[-1] |
|
|
|
|
|
swin_features = swin_features.view(swin_features.size(0), -1) |
|
|
|
|
|
output = self.fc1(swin_features) |
|
return output |
|
|
|
from transformers import RobertaModel |
|
|
|
class RobertaEncoder(nn.Module): |
|
def __init__(self, roberta_model_path="roberta-base"): |
|
super(RobertaEncoder, self).__init__() |
|
|
|
self.roberta = RobertaModel.from_pretrained(roberta_model_path) |
|
|
|
|
|
self.projection = nn.Linear(self.roberta.config.hidden_size, EMBEDDING_DIM) |
|
|
|
|
|
nn.init.xavier_uniform_(self.projection.weight) |
|
nn.init.zeros_(self.projection.bias) |
|
|
|
|
|
for param in self.roberta.parameters(): |
|
param.requires_grad = True |
|
|
|
def forward(self, input_ids, attention_mask): |
|
""" |
|
Forward pass through RoBERTa. |
|
Args: |
|
input_ids: Tensor of shape (batch_size, seq_length) |
|
attention_mask: Tensor of shape (batch_size, seq_length) |
|
|
|
Returns: |
|
Embedding: Tensor of shape (batch_size, EMBEDDING_DIM) |
|
""" |
|
roberta_output = self.roberta(input_ids=input_ids, attention_mask=attention_mask) |
|
cls_token = roberta_output.last_hidden_state[:, 0, :] |
|
pooled_output = torch.mean(roberta_output.last_hidden_state, dim=1) |
|
|
|
return self.projection(cls_token+pooled_output) |
|
|
|
from transformers import AutoTokenizer, Siglip2TextModel,AutoModel |
|
|
|
|
|
class SigLIP2TextEncoder(nn.Module): |
|
def __init__(self, embedding_dim=512): |
|
super(SigLIP2TextEncoder, self).__init__() |
|
model = AutoModel.from_pretrained("google/siglip2-base-patch16-224") |
|
self.text_encoder = model.text_model |
|
hidden_size = self.text_encoder.config.hidden_size |
|
self.projection = nn.Linear(hidden_size, embedding_dim) |
|
|
|
nn.init.xavier_uniform_(self.projection.weight) |
|
nn.init.zeros_(self.projection.bias) |
|
|
|
for param in self.text_encoder.parameters(): |
|
param.requires_grad = True |
|
for param in self.projection.parameters(): |
|
param.requires_grad = True |
|
|
|
def forward(self, tokens): |
|
""" |
|
Args: |
|
tokens: |
|
|
|
Returns: |
|
Tensor of shape (batch_size, embedding_dim) |
|
""" |
|
|
|
outputs = self.text_encoder(**tokens) |
|
|
|
return self.projection(outputs.pooler_output) |
|
|