-
Notifications
You must be signed in to change notification settings - Fork 4
/
CLIP_model.py
71 lines (50 loc) · 2.2 KB
/
CLIP_model.py
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
import torch
from torch import nn
import torch.nn.functional as F
from torchvision import models, transforms
from transformers import AutoTokenizer, AutoModel
import config as CFG
import cv2
class CLIPModel(nn.Module):
"""CLIP model for Bangla"""
def __init__(self):
super(CLIPModel, self).__init__()
self.image_encoder = models.efficientnet_b2(weights = "EfficientNet_B2_Weights.DEFAULT")
self.image_encoder.fc = nn.Identity()
self.image_out = nn.Sequential(
nn.Linear(CFG.image_embedding, 256), nn.ReLU(), nn.Linear(256, 256)
)
self.text_encoder = AutoModel.from_pretrained(CFG.text_encoder_model)
self.target_token_idx = 0
self.text_out = nn.Sequential(
nn.Linear(768, 256), nn.ReLU(), nn.Linear(256, 256)
)
def forward(self, image, text, mask):
image_vec = self.image_encoder(image)
image_vec = self.image_out(image_vec)
text_out = self.text_encoder(text, mask)
last_hidden_states = text_out.last_hidden_state
last_hidden_states = last_hidden_states[:,self.target_token_idx,:]
text_vec = self.text_out(last_hidden_states.view(-1,768))
return image_vec, text_vec
def get_image_embeddings(self, image):
image_vec = self.image_encoder(image)
image_vec = self.image_out(image_vec)
return image_vec
def get_text_embeddings(self, text, mask):
text_out = self.text_encoder(text, mask)
last_hidden_states = text_out.last_hidden_state
last_hidden_states = last_hidden_states[:,self.target_token_idx,:]
text_vec = self.text_out(last_hidden_states.view(-1,768))
return text_vec
if __name__ == '__main__':
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
images = torch.randn(40, 3, 224, 224).to(device)
input_ids = torch.randint(5, 300, size=(40, 200)).to(device)
attention_mask = torch.ones(40, 200).to(device)
print("Building CLIP")
clip_model = CLIPModel().to(device)
print(clip_model)
img_vec, text_vec = clip_model(images, input_ids, attention_mask)
print(img_vec.shape)
print(text_vec.shape)