-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
157 lines (120 loc) · 5.21 KB
/
utils.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
import os
import torch
import torch.nn as nn
from collections import OrderedDict
from typing import Tuple, TypeVar
from torch import Tensor
from clip import clip
from trainers.apt import PromptLearner, TextEncoder
mu = (0.48145466, 0.4578275, 0.40821073)
std = (0.26862954, 0.26130258, 0.27577711)
class ImageNormalizer(nn.Module):
def __init__(self, mean: Tuple[float, float, float],
std: Tuple[float, float, float]) -> None:
super(ImageNormalizer, self).__init__()
self.register_buffer('mean', torch.as_tensor(mean).view(1, 3, 1, 1))
self.register_buffer('std', torch.as_tensor(std).view(1, 3, 1, 1))
def forward(self, input: Tensor) -> Tensor:
return (input - self.mean) / self.std
def __repr__(self):
return f'ImageNormalizer(mean={self.mean.squeeze()}, std={self.std.squeeze()})' # type: ignore
class CustomCLIP(nn.Module):
def __init__(self,
model,
classnames,
cls_prompt='a photo of a {}',
atk_prompt=None,
cfg=None):
super().__init__()
self.cfg = cfg
self.logit_scale = model.logit_scale
self.classnames = classnames
self.model = model
self.mode = 'classification'
self.normalize = ImageNormalizer(mu, std).cuda()
self.set_prompts(cls_prompt, atk_prompt)
def _prompt_text_features(self, prompt):
if '{}' in prompt:
# manual prompt template
prompts = torch.cat([clip.tokenize(prompt.format(c))
for c in self.classnames])
self.model = self.model
text_features = self.model.encode_text(prompts)
else:
# optimized prompt vector
prompter_ckp = prompt
assert os.path.isfile(prompter_ckp)
prompter = PromptLearner(self.cfg, self.classnames, self.model)
state_dict = torch.load(prompter_ckp)["state_dict"]
# Ignore fixed token vectors
if "token_prefix" in state_dict:
del state_dict["token_prefix"]
if "token_suffix" in state_dict:
del state_dict["token_suffix"]
prompter.load_state_dict(state_dict, strict=False)
text_encoder = TextEncoder(self.model)
prompts = prompter()
text_features = text_encoder(prompts, prompter.tokenized_prompts)
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
return text_features.detach()
def set_prompts(self, cls_prompt, atk_prompt=None):
print(f'classification prompt: {cls_prompt}')
self.cls_tfeatures = self._prompt_text_features(cls_prompt).cuda()
if atk_prompt is None or cls_prompt == atk_prompt:
print(f'attack prompt: {cls_prompt}')
self.atk_tfeatures = self.cls_tfeatures
else:
print(f'attack prompt: {atk_prompt}')
self.atk_tfeatures = self._prompt_text_features(atk_prompt).cuda()
def forward(self, image):
image_features = self.model.encode_image(self.normalize(image))
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
logit_scale = self.logit_scale.exp()
text_features = self.cls_tfeatures if self.mode == 'classification' else self.atk_tfeatures
logits = logit_scale * image_features @ text_features.t()
return logits
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
class ProgressMeter(object):
def __init__(self, num_batches, meters, prefix=""):
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
self.meters = meters
self.prefix = prefix
def display(self, batch):
entries = [self.prefix + self.batch_fmtstr.format(batch)]
entries += [str(meter) for meter in self.meters]
print('\t'.join(entries))
def _get_batch_fmtstr(self, num_batches):
num_digits = len(str(num_batches // 1))
fmt = '{:' + str(num_digits) + 'd}'
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res