-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtip-adapter.py
executable file
·313 lines (262 loc) · 11.7 KB
/
tip-adapter.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim
import datetime
import time
import copy
from pathlib import Path
from qpth.qp import QPFunction
import clip
from clip.simple_tokenizer import SimpleTokenizer as _Tokenizer
import json
from dataset import SetDataManager
from options import parse_args
import random
import numpy as np
_tokenizer = _Tokenizer()
seed = 0
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
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 = 16
dtype = clip_model.dtype
ctx_dim = clip_model.ln_final.weight.shape[0]
clip_imsize = clip_model.visual.input_resolution
cfg_imsize = 224
ctx_vectors = torch.empty(n_ctx, ctx_dim, dtype=dtype)
nn.init.normal_(ctx_vectors, std=0.02)
prompt_prefix = " ".join(["X"] * n_ctx)
self.ctx = nn.Parameter(ctx_vectors) # to be optimized
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.n_cls = n_cls
self.n_ctx = n_ctx
self.tokenized_prompts = tokenized_prompts # torch.Tensor
self.name_lens = name_lens
self.class_token_position = "end"
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
if self.class_token_position == "end":
prompts = torch.cat(
[
prefix, # (n_cls, 1, dim)
ctx, # (n_cls, n_ctx, dim)
suffix, # (n_cls, *, dim)
],
dim=1,
)
else:
raise ValueError
return prompts
class CustomCLIP(nn.Module):
def __init__(self, classnames, clip_model):
super().__init__()
self.clip = clip_model
self.classnames = classnames
self.image_encoder = clip_model.visual
self.logit_scale = clip_model.logit_scale
self.dtype = clip_model.dtype
def forward(self, image):
image_features = self.clip.encode_image(image.type(self.dtype))
text = self.classnames
text = clip.tokenize(text).cuda()
text_features = self.clip.encode_text(text)
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
logit_scale = self.logit_scale.exp()
logits = logit_scale * image_features @ text_features.t()
return logits
def encode_image(self, image):
image_features = self.clip.encode_image(image.type(self.dtype))
return image_features / image_features.norm(dim=-1, keepdim=True)
def encode_text(self):
text = self.classnames
text = clip.tokenize(text).cuda()
text_features = self.clip.encode_text(text)
return text_features / text_features.norm(dim=-1, keepdim=True)
def one_hot(indices, depth=20):
"""
Returns a one-hot tensor.
This is a PyTorch equivalent of Tensorflow's tf.one_hot.
Parameters:
indices: a (n_batch, m) Tensor or (m) Tensor.
depth: a scalar. Represents the depth of the one hot dimension.
Returns: a (n_batch, m, depth) Tensor or (m, depth) Tensor.
"""
encoded_indicies = torch.zeros(indices.size() + torch.Size([depth])).cuda() # (n_batch, m, depth) or (m, depth)
index = indices.reshape(indices.size()+torch.Size([1])) # (n_batch, m, 1) or (m, 1)
if len(indices.size()) < 2:
encoded_indicies = encoded_indicies.scatter_(1, index, 1)
else:
encoded_indicies = encoded_indicies.scatter_(2, index, 1)
return encoded_indicies
def finetune(novel_loader, n_way=5, n_support=5, n_query=15, class_name=()):
iter_num = len(novel_loader)
acc_all = []
name = 'FT'
if params.reset_head:
name += '_H'
test_log_file = open(os.path.join(params.output, f'{name}_{params.n_way}w_{params.n_shot}s.txt'), "w")
print(params, file=test_log_file)
model_clip, _ = clip.load("ViT-B/16", device="cpu")
start_time = time.time()
for ti, (x, _) in enumerate(novel_loader):
# prepare data
x = x.cuda()
xs = x[:, :n_support].reshape(-1, *x.size()[2:]) # (n_way*n_support, 3, H, W)
ys = torch.from_numpy(np.repeat(range(n_way), n_support)).cuda()
xq = x[:, n_support:].reshape(-1, *x.size()[2:]) # (n_way*query, 3, H, W)
yq = np.repeat(range(n_way), n_query)
task_class_name = [class_name[i] for i in _[:, 0].numpy()]
# Model
model = CustomCLIP(task_class_name, copy.deepcopy(model_clip)).float().cuda()
# Finetune
model.train()
batch_size = n_way
support_size = n_way * n_support
loss_fn = nn.CrossEntropyLoss().cuda()
cache_keys = []
cache_values = []
for j in range(0, xs.size(0), 20):
cache_key = model.encode_image(xs[j:j+20]).data
cache_value = one_hot(ys[j:j+20])
cache_keys += [cache_key]
cache_values += [cache_value]
cache_keys = nn.Parameter(torch.cat(cache_keys, dim=0), requires_grad=True)
cache_values = torch.cat(cache_values, dim=0)
opt = torch.optim.AdamW([cache_keys], lr=params.ft_lr)
for epoch in range(params.ft_epoch):
rand_id = np.random.permutation(support_size)
for j in range(0, support_size, batch_size):
opt.zero_grad()
selected_id = torch.from_numpy(rand_id[j: min(j+batch_size, support_size)]).cuda()
x_batch = xs[selected_id] # (batch_size, 3, 224, 224)
y_batch = ys[selected_id] # (batch_size)
alpha = 3
beta = 1
image_features = model.encode_image(x_batch).data
text_features = model.encode_text().data
clip_logits = 100. * image_features @ text_features.t()
affinity = image_features @ cache_keys.t()
cache_logits = ((-1) * (beta - beta * affinity)).exp() @ cache_values
logits = clip_logits + cache_logits * alpha
loss = loss_fn(logits, y_batch)
loss.backward()
opt.step()
del opt, xs
torch.cuda.empty_cache()
# Test
model.eval()
with torch.no_grad():
alpha = 3
beta = 1
image_features = model.encode_image(xq)
text_features = model.encode_text()
clip_logits = 100. * image_features @ text_features.t()
affinity = image_features @ cache_keys.t()
cache_logits = ((-1) * (beta - beta * affinity)).exp() @ cache_values
tip_logits = clip_logits + cache_logits * alpha
_, topk_labels = tip_logits.data.topk(1, 1, True, True)
topk_ind = topk_labels.cpu().numpy()
top1_correct = np.sum(topk_ind[:, 0] == yq)
correct_this, count_this = float(top1_correct), len(yq)
acc = correct_this * 100. / count_this
acc_all.append(acc)
print('Task %d: %4.2f%%' % (ti, acc))
print("Task %d: %4.2f%%" % (ti, acc), file=test_log_file)
del xq, model
torch.cuda.empty_cache()
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
acc_all = np.asarray(acc_all)
acc_mean = np.mean(acc_all)
acc_std = np.std(acc_all)
print('Test Acc = %4.2f +- %4.2f%%' % (acc_mean, 1.96*acc_std/np.sqrt(iter_num)))
print('Test Acc = %4.2f +- %4.2f%%' % (acc_mean, 1.96*acc_std/np.sqrt(iter_num)), file=test_log_file)
print('Total time: {}'.format(total_time_str))
print('Total time: {}'.format(total_time_str), file=test_log_file)
test_log_file.close()
if __name__ == '__main__':
np.random.seed(10)
params = parse_args()
print(params)
image_size = 224
eposide_num = 2000
n_query = 15
print('Loading target dataset!')
novel_file = os.path.join(params.data_dir, params.dataset, 'all.json')
if params.dataset == 'cub':
with open(novel_file, 'r') as f:
class_name = json.load(f)['label_names']
for i in range(len(class_name)):
class_name[i] = class_name[i].split('.')[-1].replace('_', ' ').lower()
class_name = ['a photo of a %s, a type of bird.'%name for name in class_name]
elif params.dataset == 'cars':
with open(novel_file, 'r') as f:
class_name = json.load(f)['label_names']
for i in range(len(class_name)):
class_name[i] = class_name[i].lower()
class_name = ['a photo of a %s.'%name for name in class_name]
elif params.dataset == 'places':
with open(novel_file, 'r') as f:
class_name = json.load(f)['label_names']
for i in range(len(class_name)):
class_name[i] = class_name[i].replace('_', ' ').lower()
class_name = ['a photo of a %s.'%name for name in class_name]
else:
with open('./filelists/plantae/name.json', 'r') as f:
js = json.load(f)
d = {}
for _ in js:
d[int(_['id'])] = _['name']
with open(novel_file, 'r') as f:
class_name = json.load(f)['label_names']
for i in range(len(class_name)):
class_name[i] = d[int(class_name[i])].replace('_', ' ').lower()
class_name = ['a photo of a %s.'%name for name in class_name]
datamgr = SetDataManager(image_size, n_query=n_query, n_way=params.n_way, n_support=params.n_shot, n_eposide=eposide_num)
novel_loader = datamgr.get_data_loader(novel_file, aug=False)
params.output = os.path.join(params.output, params.dataset)
Path(params.output).mkdir(parents=True, exist_ok=True)
finetune(novel_loader, n_way=params.n_way, n_support=params.n_shot, n_query=n_query, class_name=class_name)