-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
221 lines (182 loc) · 8.11 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from collections import OrderedDict
import torch
import torch.nn as nn
from torch.nn import functional as F
from clip import clip
from clip.simple_tokenizer import SimpleTokenizer as _Tokenizer
from config import cfg
from log import logger
_tokenizer = _Tokenizer()
def load_clip_to_cpu():
backbone_name = 'RN50'
url = clip._MODELS[backbone_name]
model_path = clip._download(url)
try:
# loading JIT archive
model = torch.jit.load( # type: ignore
model_path, map_location="cpu").eval()
state_dict = None
except RuntimeError:
state_dict = torch.load(model_path, map_location="cpu")
model = clip.build_model(state_dict or model.state_dict()) # type: ignore
return model
class TextEncoder(nn.Module):
def __init__(self, clip_model):
super().__init__()
self.transformer = clip_model.transformer
self.positional_embedding = clip_model.positional_embedding
self.ln_final = clip_model.ln_final
self.text_projection = clip_model.text_projection
self.dtype = clip_model.dtype
def forward(self, prompts, tokenized_prompts):
x = prompts + self.positional_embedding.type(self.dtype)
x = x.permute(1, 0, 2) # NLD -> LND
x = self.transformer(x)
x = x.permute(1, 0, 2) # LND -> NLD
x = self.ln_final(x).type(self.dtype)
# x.shape = [batch_size, n_ctx, transformer.width]
# take features from the eot embedding (eot_token is the highest number in each sequence)
x = x[torch.arange(x.shape[0]),
tokenized_prompts.argmax(dim=-1)] @ self.text_projection
return x
class PromptLearner(nn.Module):
def __init__(self, classnames, clip_model):
super().__init__()
n_cls = len(classnames)
n_ctx = cfg.n_ctx
dtype = clip_model.dtype
clip_imsize = clip_model.visual.input_resolution
cfg_imsize = 224
assert cfg_imsize == clip_imsize, f"cfg_imsize ({cfg_imsize}) must equal to clip_imsize ({clip_imsize})"
# use given words to initialize context vectors
ctx_init = cfg.ctx_init.replace("_", " ")
assert (n_ctx == len(ctx_init.split(" ")))
prompt = clip.tokenize(ctx_init)
with torch.no_grad():
embedding = clip_model.token_embedding(prompt).type(dtype)
ctx_vectors = embedding[0, 1:1 + n_ctx, :]
prompt_prefix = ctx_init
self.ctx = nn.Parameter(ctx_vectors) # type: ignore
classnames = [name.replace("_", " ") for name in classnames]
name_lens = [len(_tokenizer.encode(name)) for name in classnames]
prompts = [prompt_prefix + " " + name + "." for name in classnames]
tokenized_prompts = torch.cat([clip.tokenize(p) for p in prompts])
with torch.no_grad():
embedding = clip_model.token_embedding(tokenized_prompts).type(
dtype)
# These token vectors will be saved when in save_model(),
# but they should be ignored in load_model() as we want to use
# those computed using the current class names
self.register_buffer("token_prefix", embedding[:, :1, :]) # SOS
self.register_buffer("token_suffix",
embedding[:, 1 + n_ctx:, :]) # CLS, EOS
self.register_buffer("token_middle", embedding[:, 1:(1 + n_ctx), :])
self.n_cls = n_cls
self.n_ctx = n_ctx
self.tokenized_prompts = tokenized_prompts # torch.Tensor
self.name_lens = name_lens
def forward(self):
ctx = self.ctx
if ctx.dim() == 2:
ctx = ctx.unsqueeze(0).expand(self.n_cls, -1, -1)
prefix = self.token_prefix
suffix = self.token_suffix
prompts = torch.cat(
[
prefix, # (n_cls, 1, dim)
ctx, # (n_cls, n_ctx, dim)
suffix, # (n_cls, *, dim)
], # type: ignore
dim=1,
)
return prompts
def load_clip_model():
clip_model = load_clip_to_cpu()
# CLIP's default precision is fp16
clip_model.float()
return clip_model, clip._transform(clip_model.visual.input_resolution)
import math
import numpy as np
class GraphConvolution(nn.Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, in_features, out_features, bias=False):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = nn.parameter.Parameter(torch.Tensor(in_features, out_features))
if bias:
self.bias = nn.parameter.Parameter(torch.Tensor(1, 1, out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input, adj):
support = torch.matmul(input, self.weight)
output = torch.matmul(adj, support)
if self.bias is not None:
return output + self.bias
else:
return output
def __repr__(self):
return self.__class__.__name__ + ' (' \
+ str(self.in_features) + ' -> ' \
+ str(self.out_features) + ')'
from timm.models.vision_transformer import resize_pos_embed
class SCPNet(nn.Module):
def __init__(self, classnames, clip_model):
super().__init__()
self.prompt_learner = PromptLearner(classnames, clip_model)
self.tokenized_prompts = self.prompt_learner.tokenized_prompts
self.image_encoder = clip_model.visual
self.text_encoder = TextEncoder(clip_model)
self.logit_scale = clip_model.logit_scale
self.dtype = clip_model.dtype
self.gc1 = GraphConvolution(1024, 2048)
self.gc2 = GraphConvolution(2048, 2048)
self.gc3 = GraphConvolution(2048, 1024)
self.relu = nn.LeakyReLU(0.2)
self.relu2 = nn.LeakyReLU(0.2)
self.relation = torch.Tensor(np.load(cfg.relation_file))
_ ,max_idx = torch.topk(self.relation, cfg.sparse_topk)
mask = torch.ones_like(self.relation).type(torch.bool)
for i, idx in enumerate(max_idx):
mask[i][idx] = 0
self.relation[mask] = 0
sparse_mask = mask
dialog = torch.eye(cfg.num_classes).type(torch.bool)
self.relation[dialog] = 0
self.relation = self.relation / torch.sum(self.relation, dim=1).reshape(-1, 1) * cfg.reweight_p
self.relation[dialog] = 1-cfg.reweight_p
self.gcn_relation = self.relation.clone()
assert(self.gcn_relation.requires_grad == False)
self.relation = torch.exp(self.relation/cfg.T) / torch.sum(torch.exp(self.relation/cfg.T), dim=1).reshape(-1,1)
self.relation[sparse_mask] = 0
self.relation = self.relation / torch.sum(self.relation, dim=1).reshape(-1, 1)
def forward(self, image):
tokenized_prompts = self.tokenized_prompts
image_features = self.image_encoder(image.type(self.dtype))
image_features = image_features / image_features.norm(dim=-1,
keepdim=True)
logit_scale = self.logit_scale.exp()
if cfg.scale != 'clip':
assert(isinstance(cfg.scale, int))
logit_scale = cfg.scale
prompts = self.prompt_learner()
text_features = self.text_encoder(prompts, tokenized_prompts)
identity = text_features
text_features = self.gc1(text_features, self.gcn_relation.cuda())
text_features = self.relu(text_features)
text_features = self.gc2(text_features, self.gcn_relation.cuda())
text_features = self.relu2(text_features)
text_features = self.gc3(text_features, self.gcn_relation.cuda())
text_features += identity
text_features = text_features / text_features.norm(dim=-1,
keepdim=True)
logits = logit_scale * image_features @ text_features.t()
return logits