forked from gaopengcuhk/Tip-Adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_imagenet_ood.py
237 lines (178 loc) · 9.65 KB
/
main_imagenet_ood.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
import os
import random
import argparse
import yaml
from tqdm import tqdm
import torch
import torch.nn.functional as F
import torch.nn as nn
from datasets import build_ood_dataset
from datasets.imagenet import ImageNet
import clip
from datasets.utils import build_data_loader
from utils import *
from log_utils import *
from datetime import datetime
def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--id_config', dest='id_config', help='in domain dataset settings in yaml format')
parser.add_argument('--ood_config', dest="ood_config", help="out of domain dataset settings in yaml format")
args = parser.parse_args()
return args
def run_tip_adapter(cfg, cache_keys, cache_values, test_features, test_labels, clip_weights):
# Zero-shot CLIP
clip_logits = 100. * test_features @ clip_weights
acc = cls_acc(clip_logits, test_labels)
print("\n**** Zero-shot CLIP's test accuracy: {:.2f}. ****\n".format(acc))
# Tip-Adapter
beta, alpha = cfg['init_beta'], cfg['init_alpha']
affinity = test_features @ cache_keys
cache_logits = ((-1) * (beta - beta * affinity)).exp() @ cache_values
tip_logits = clip_logits + cache_logits * alpha
acc = cls_acc(tip_logits, test_labels)
print("**** Tip-Adapter's test accuracy: {:.2f}. ****\n".format(acc))
# Search Hyperparameters
_ = search_hp(cfg, cache_keys, cache_values, test_features, test_labels, clip_weights)
def run_tip_adapter_F_ood(log, cfg, cache_keys, cache_values, test_features, test_labels, clip_weights, clip_model,
train_loader_F, open_features, open_labels):
# Enable the cached keys to be learnable
adapter = nn.Linear(cache_keys.shape[0], cache_keys.shape[1], bias=False).to(clip_model.dtype).cuda()
adapter.weight = nn.Parameter(cache_keys.t())
optimizer = torch.optim.AdamW(adapter.parameters(), lr=cfg['lr'], eps=1e-4)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, cfg['train_epoch'] * len(train_loader_F))
beta, alpha = cfg['init_beta'], cfg['init_alpha']
best_acc, best_epoch = 0.0, 0
for train_idx in range(cfg['train_epoch']):
# Train
adapter.train()
correct_samples, all_samples = 0, 0
loss_list = []
log.debug('Train Epoch: {:} / {:}'.format(train_idx, cfg['train_epoch']))
for i, (images, target) in enumerate(tqdm(train_loader_F)):
images, target = images.cuda(), target.cuda()
with torch.no_grad():
image_features = clip_model.encode_image(images)
image_features /= image_features.norm(dim=-1, keepdim=True)
affinity = adapter(image_features)
cache_logits = ((-1) * (beta - beta * affinity)).exp() @ cache_values
clip_logits = 100. * image_features @ clip_weights
tip_logits = clip_logits + cache_logits * alpha
loss = F.cross_entropy(tip_logits, target)
# todo: add auroc loss
acc = cls_acc(tip_logits, target)
correct_samples += acc / 100 * len(tip_logits)
all_samples += len(tip_logits)
loss_list.append(loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
current_lr = scheduler.get_last_lr()[0]
log.debug('LR: {:.6f}, Acc: {:.4f} ({:}/{:}), Loss: {:.4f}'.format(current_lr, correct_samples / all_samples,
correct_samples, all_samples,
sum(loss_list) / len(loss_list)))
# Eval
adapter.eval()
affinity = adapter(test_features)
cache_logits = ((-1) * (beta - beta * affinity)).exp() @ cache_values
clip_logits = 100. * test_features @ clip_weights
tip_logits = clip_logits + cache_logits * alpha
acc = cls_acc(tip_logits, test_labels)
log.debug("**** Tip-Adapter-F's test accuracy: {:.2f}. ****\n".format(acc))
open_affinity = adapter(open_features)
open_cache_logits = ((-1) * (beta - beta * open_affinity)).exp() @ cache_values
open_logits = 100. * open_features @ clip_weights
open_tip_logits = open_logits + open_cache_logits * alpha
auroc, aupr, fpr = cls_auroc_mcm(tip_logits, open_tip_logits, 1)
log.debug("**** Tip-Adapter's test auroc, aupr, fpr: {:.2f}, {:.2f}, {:.2f}. ****\n".format(auroc, aupr, fpr))
if acc > best_acc:
best_acc = acc
best_epoch = train_idx
torch.save(adapter.weight, cfg['cache_dir'] + "/best_F_" + str(cfg['shots']) + "shots.pt")
adapter.weight = torch.load(cfg['cache_dir'] + "/best_F_" + str(cfg['shots']) + "shots.pt")
log.debug(f"**** After fine-tuning, Tip-Adapter-F's best test accuracy: {best_acc:.2f}, at epoch: {best_epoch}. ****\n")
# Search Hyperparameters
# todo: cache_keys? affinity?
_ = search_hp_ood(log, cfg, cache_keys, cache_values, test_features, test_labels, open_features, open_labels, clip_weights, adapter=adapter)
def run_tip_adapter_ood(log, cfg, cache_keys, cache_values, test_features, test_labels, clip_weights, open_features,
open_labels):
# Zero-shot CLIP
clip_logits = 100. * test_features @ clip_weights
acc = cls_acc(clip_logits, test_labels)
log.debug("\n**** Zero-shot CLIP's test accuracy: {:.2f}. ****\n".format(acc))
# Tip-Adapter
beta, alpha = cfg['init_beta'], cfg['init_alpha']
affinity = test_features @ cache_keys
cache_logits = ((-1) * (beta - beta * affinity)).exp() @ cache_values
tip_logits = clip_logits + cache_logits * alpha
acc = cls_acc(tip_logits, test_labels)
log.debug("**** Tip-Adapter's test accuracy: {:.2f}. ****\n".format(acc))
# Tip-Adapter-Auroc
open_logits = 100. * open_features @ clip_weights
open_affinity = open_features @ cache_keys
open_cache_logits = ((-1) * (beta - beta * open_affinity)).exp() @ cache_values
open_tip_logits = open_logits + open_cache_logits * alpha
auroc, aupr, fpr = cls_auroc_mcm(tip_logits, open_tip_logits, 1)
log.debug("**** Tip-Adapter's val auroc, aupr, fpr: {:.2f}, {:.2f}, {:.2f}. ****\n".format(auroc, aupr, fpr))
# Search Hyperparameters
#_ = search_hp_ood(log, cfg, cache_keys, cache_values, test_features, test_labels, open_features, open_labels,
# clip_weights)
def main():
# Load config file
args = get_arguments()
assert (os.path.exists(args.id_config))
assert (os.path.exists(args.ood_config))
# Load configuration
id_cfg = yaml.load(open(args.id_config, 'r'), Loader=yaml.Loader)
ood_cfg = yaml.load(open(args.ood_config, 'r'), Loader=yaml.Loader)
# Set logging
current = datetime.now()
formatted_time = current.strftime("%Y_%m_%d_%H_%M_%S")
args.log_directory = f"logs/{id_cfg['dataset']}/{id_cfg['backbone']}/{ood_cfg['dataset']}/{str(formatted_time)}"
args.name = "TRAIN_EVAL_INFO"
os.makedirs(args.log_directory, exist_ok=True)
log = setup_log(args)
# Set cache
id_cache_dir = os.path.join('/home/nfs03/zengtc/tip/caches', id_cfg['dataset'])
os.makedirs(id_cache_dir, exist_ok=True)
id_cfg['cache_dir'] = id_cache_dir
log.debug("\nRunning in-domain dataset configs.")
log.debug(id_cfg)
# CLIP
clip_model, preprocess = clip.load(id_cfg['backbone'])
clip_model.eval()
# ImageNet dataset
random.seed(1)
torch.manual_seed(1)
log.debug("Preparing ImageNet dataset.")
imagenet = ImageNet(id_cfg['root_path'], id_cfg['shots'], preprocess)
test_loader = torch.utils.data.DataLoader(imagenet.test, batch_size=64, num_workers=8, shuffle=False)
train_loader_cache = torch.utils.data.DataLoader(imagenet.train, batch_size=256, num_workers=8, shuffle=False)
train_loader_F = torch.utils.data.DataLoader(imagenet.train, batch_size=256, num_workers=8, shuffle=True)
# Textual features
log.debug("Getting textual features as CLIP's classifier.")
clip_weights = clip_classifier(imagenet.classnames, imagenet.template, clip_model)
# Construct the cache model by few-shot training set
log.debug("\nConstructing cache model by few-shot visual features and labels.")
cache_keys, cache_values = build_cache_model(log, id_cfg, clip_model, train_loader_cache)
# Pre-load test features
log.debug("\nLoading visual features and labels from test set.")
test_features, test_labels = pre_load_features(id_cfg, "test", clip_model, test_loader)
# Load open-set dataset
log.debug("\nRunning out-domain dataset configs.")
log.debug(ood_cfg)
ood_cache_dir = os.path.join('/home/nfs03/zengtc/tip/caches', ood_cfg['dataset'])
os.makedirs(ood_cache_dir, exist_ok=True)
ood_cfg['cache_dir'] = ood_cache_dir
ood_dataset = build_ood_dataset(ood_cfg['dataset'], ood_cfg['root_path'], log)
ood_loader = build_data_loader(data_source=ood_dataset.all, batch_size=64, is_train=False, tfm=preprocess,
shuffle=False)
ood_features, ood_labels = pre_load_features(ood_cfg, "ood", clip_model, ood_loader)
# ------------------------------------------ Tip-Adapter ------------------------------------------
run_tip_adapter_ood(log, id_cfg, cache_keys, cache_values, test_features, test_labels, clip_weights, ood_features,
ood_labels)
# ------------------------------------------ Tip-Adapter-F ------------------------------------------
run_tip_adapter_F_ood(log, id_cfg, cache_keys, cache_values, test_features, test_labels, clip_weights, clip_model,
train_loader_F, ood_features, ood_labels)
if __name__ == '__main__':
main()