-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
134 lines (109 loc) · 5.71 KB
/
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
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
import timm
import torch
import torch.nn as nn
from transformers import BertTokenizer, BertForMaskedLM
from models.blip import blip_feature_extractor
class PromptEncoder(nn.Module):
'''
modified learnable token generator from P-tuning: https://github.com/THUDM/P-tuning
'''
def __init__(self, prompt_token_len, hidden_size, device, lstm_dropout):
super().__init__()
print("[#] Init prompt encoder...")
# Input [0, 1, 2, 3, ...]
self.seq_indices = torch.LongTensor(list(range(prompt_token_len))).to(device)
# Embedding
self.embedding = nn.Embedding(prompt_token_len, hidden_size)
# LSTM
self.lstm_head = nn.LSTM(input_size=hidden_size,
hidden_size=hidden_size // 2,
num_layers=2,
dropout=lstm_dropout,
bidirectional=True,
batch_first=True)
self.mlp_head = nn.Sequential(nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, hidden_size))
def forward(self):
input_embeds = self.embedding(self.seq_indices).unsqueeze(0)
output_embeds = self.mlp_head(self.lstm_head(input_embeds)[0]).squeeze()
return output_embeds
class VisualEncoder(nn.Module):
def __init__(self, model_name, pooling_scale, embedding_dim):
'''for ResNet only'''
super().__init__()
self.backbone = timm.create_model(model_name, pretrained=True)
x = int(pooling_scale[0])
y = int(pooling_scale[1])
print(f'[#] Image Tokens: (49, 2048) -> ({x}×{y}, {embedding_dim})')
self.local_pool = nn.Sequential(
nn.AdaptiveAvgPool2d((x,y)), # (bs, 2048, 7, 7) -> (bs, 2048, x, y)
nn.Flatten(2, -1) # -> (bs, 2048, xy)
)
self.visual_mlp = nn.Linear(2048, embedding_dim, bias=False)
def forward(self, imgs_tensor):
visual_embeds = self.backbone.forward_features(imgs_tensor) # (bs, 3, 224, 224) -> (bs, 2048, 7, 7)
visual_embeds = self.local_pool(visual_embeds) # (bs, 2048, xy)
visual_embeds = visual_embeds.permute(0, 2, 1) # (bs, xy, 2048)
visual_embeds = self.visual_mlp(visual_embeds) # (bs, xy, 768)
return visual_embeds
class MSAModel(torch.nn.Module):
'''main model'''
def __init__(self, args, label_id_list):
super().__init__()
self.args = args
self.device = args.device
self.label_id_list = label_id_list
self.tokenizer = BertTokenizer.from_pretrained(args.model_name)
self.lm_model = BertForMaskedLM.from_pretrained(args.model_name)
self.embeddings = self.lm_model.bert.get_input_embeddings()
self.embedding_dim = self.embeddings.embedding_dim#768
if not args.no_img:
self.img_token_id = self.tokenizer.get_vocab()[args.img_token]
#print(self.img_token_id)#3
self.img_token_len = args.img_token_len
model_url = 'D://PyCharm/FS1/model_base_capfilt_large.pth'
model = blip_feature_extractor(pretrained=model_url, image_size=224, vit='base')
model.eval()
self.model = model.to(self.device)
#print(args.img_token_len)#1
# print(self.embedding_dim)#768
self.visual_encoder = VisualEncoder(args.visual_model_name, args.pooling_scale, self.embedding_dim)
if args.template == 1:
self.prompt_token_id = self.tokenizer.get_vocab()[args.prompt_token]
self.prompt_token_len = sum([int(i) for i in args.prompt_shape])
self.prompt_encoder = PromptEncoder(self.prompt_token_len, self.embedding_dim, args.device, args.lstm_dropout)
def embed_input(self, input_ids, imgs=None):
#print(imgs.shape)#([34, 3, 224, 224])
bs = input_ids.shape[0]
#print(bs)#34
embeds = self.embeddings(input_ids)
#print(embeds.shape)#([34, 84, 768])
if self.args.template == 1:
prompt_token_position = torch.nonzero(input_ids == self.prompt_token_id).reshape((bs, self.prompt_token_len, 2))[:, :, 1]
prompt_embeds = self.prompt_encoder()
for bidx in range(bs):
for i in range(self.prompt_token_len):
embeds[bidx, prompt_token_position[bidx, i], :] = prompt_embeds[i, :]
if not self.args.no_img:
visual_embeds = self.visual_encoder(imgs)
#visual_embeds = self.model(imgs, '', mode='image')[:, 0].unsqueeze(1)
#print(input_ids)
#print(visual_embeds.shape) #[34, 1, 768]
img_token_position = torch.nonzero(input_ids == self.img_token_id).reshape((bs, self.img_token_len, 2))[:, :, 1]
for bidx in range(bs):
for i in range(self.img_token_len):
embeds[bidx, img_token_position[bidx, i], :] = visual_embeds[bidx, i, :]
return embeds
def forward(self, input_ids, attention_mask, labels, imgs=None):
inputs_embeds = self.embed_input(input_ids, imgs)
# print(input_ids.shape)#([34, 84])
# print(inputs_embeds.shape)#([34, 84, 768])
output = self.lm_model(inputs_embeds=inputs_embeds, attention_mask=attention_mask, labels=labels)
loss, logits = output.loss, output.logits
pred = logits[labels != -100]
probs = pred[:, self.label_id_list]
pred_labels_idx = torch.argmax(probs, dim=-1).tolist()
y_ = [self.label_id_list[i] for i in pred_labels_idx]
y = labels[labels != -100]
return loss, y_, y.tolist()