-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaFTer.py
368 lines (308 loc) · 11.6 KB
/
LaFTer.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import argparse
import torch
import datetime
from dassl.utils import setup_logger, set_random_seed, collect_env_info
from dassl.config import get_cfg_default
from dassl.engine import build_trainer
from utils.utils import *
# custom
import datasets.oxford_flowers
import datasets.fgvc_aircraft
import datasets.dtd
import datasets.eurosat
import datasets.food101
import datasets.sun397
import datasets.ucf101
import datasets.imagenet_r
import datasets.imagenet
import datasets.imagenet_s
import datasets.imagenet_a
import datasets.caltech101
import datasets.cifar
import trainers.LaFTer as lafter_uft
from utils.utils import *
import os
def print_args(args, cfg):
print("***************")
print("** Arguments **")
print("***************")
optkeys = list(args.__dict__.keys())
optkeys.sort()
for key in optkeys:
print("{}: {}".format(key, args.__dict__[key]))
print("************")
print("** Config **")
print("************")
print(cfg)
def reset_cfg(cfg, args):
if args.root:
cfg.DATASET.ROOT = args.root
if args.output_dir:
cfg.OUTPUT_DIR = args.output_dir
if args.resume:
cfg.RESUME = args.resume
if args.seed:
cfg.SEED = args.seed
if args.source_domains:
cfg.DATASET.SOURCE_DOMAINS = args.source_domains
if args.target_domains:
cfg.DATASET.TARGET_DOMAINS = args.target_domains
if args.transforms:
cfg.INPUT.TRANSFORMS = args.transforms
if args.trainer:
cfg.TRAINER.NAME = args.trainer
if args.backbone:
cfg.MODEL.BACKBONE.NAME = args.backbone
if args.head:
cfg.MODEL.HEAD.NAME = args.head
def extend_cfg(cfg):
"""
Add new config variables.
E.g.
from yacs.config import CfgNode as CN
cfg.TRAINER.MY_MODEL = CN()
cfg.TRAINER.MY_MODEL.PARAM_A = 1.
cfg.TRAINER.MY_MODEL.PARAM_B = 0.5
cfg.TRAINER.MY_MODEL.PARAM_C = False
"""
from yacs.config import CfgNode as CN
cfg.TRAINER.COOP = CN()
cfg.TRAINER.COOP.N_CTX = 16 # number of context vectors
cfg.TRAINER.COOP.CSC = False # class-specific context
cfg.TRAINER.COOP.CTX_INIT = "" # initialization words
cfg.TRAINER.COOP.PREC = "fp16" # fp16, fp32, amp
cfg.TRAINER.COOP.CLASS_TOKEN_POSITION = "end" # 'middle' or 'end' or 'front'
cfg.TRAINER.COCOOP = CN()
cfg.TRAINER.COCOOP.N_CTX = 16 # number of context vectors
cfg.TRAINER.COCOOP.CTX_INIT = "" # initialization words
cfg.TRAINER.COCOOP.PREC = "fp16" # fp16, fp32, amp
cfg.DATASET.SUBSAMPLE_CLASSES = "all" # all, base or new
cfg.txt_cls = args.txt_cls
cfg.gpt_prompts = args.gpt_prompts
def setup_cfg(args):
cfg = get_cfg_default()
extend_cfg(cfg)
# 1. From the dataset config file
if args.dataset_config_file:
cfg.merge_from_file(args.dataset_config_file)
# 2. From the method config file
if args.config_file:
cfg.merge_from_file(args.config_file)
# 3. From input arguments
reset_cfg(cfg, args)
# 4. From optional input arguments
cfg.merge_from_list(args.opts)
return cfg
class lossmeter:
"""Compute and store the average and current value.
Examples::
>>> # 1. Initialize a meter to record loss
>>> losses = AverageMeter()
>>> # 2. Update meter after every mini-batch update
>>> losses.update(loss_value, batch_size)
"""
def __init__(self, ema=False):
"""
Args:
ema (bool, optional): apply exponential moving average.
"""
self.ema = ema
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
if isinstance(val, torch.Tensor):
val = val.item()
self.val = val
self.sum += val * n
self.count += n
if self.ema:
self.avg = self.avg * 0.9 + self.val * 0.1
else:
self.avg = self.sum / self.count
def test(args, teloader, model):
model.eval()
top1 = AverageMeter('Acc@1', ':6.2f')
top1_pl = AverageMeter('Acc@1', ':6.2f')
one_hot = []
one_hot_pl = []
for i, (inputs) in enumerate(tqdm(teloader)):
img = inputs["img"]
labels = inputs["label"]
if args.zero_shot:
with torch.no_grad():
output_pseudo_label = model(inputs.cuda(), zero_shot=True)
_, predicted_pl = output_pseudo_label.max(1)
one_hot_pl.append(predicted_pl.eq(labels.cuda()).cpu())
acc1_pl = one_hot_pl[-1].sum().item() / len(labels)
top1_pl.update(acc1_pl, len(labels))
else:
with torch.no_grad():
inputs, labels = img.cuda(), labels.cuda()
outputs = model(inputs, clip_eval=True)
_, predicted = outputs.max(1)
one_hot.append(predicted.eq(labels).cpu())
acc1 = one_hot[-1].sum().item() / len(labels)
top1.update(acc1, len(labels))
if not args.zero_shot:
return top1.avg * 100, top1_pl.avg * 100
else:
return top1_pl.avg * 100
def train_txt_cls(args, model):
optimizer, _, _ = setup_text_training_utils(args, model)
criteria = torch.nn.CrossEntropyLoss(label_smoothing=0.1)
for i in tqdm(range(args.txt_epochs)):
loss = model.train_txt_clas(criteria)
optimizer.zero_grad()
loss.backward()
optimizer.step()
model.txt_cls_init()
def train_lafter(args, model, tr_loader, val_loader):
# first train text classifier
train_txt_cls(args, model)
all_acc = list()
optimizer, scheduler, criteria = setup_lafter_training_utils(args, model)
batch_time = lossmeter()
data_time = lossmeter()
for epoch in range(args.epochs):
print(f'Epoch: {epoch}')
model.eval()
model.adapter.train()
end = time.time()
for i, batch in enumerate((tr_loader)):
data_time.update(time.time() - end)
batch_time.update(time.time() - end)
input = batch["img"]
input = torch.stack(input) # two views from dataloader
input = input.to(model.device)
optimizer.zero_grad()
pl = model.forward_normal_for_pl(input[0])
out = model.forward_aug_with_prompts(input[1].float().cuda())
pseudo_label = F.softmax(pl, dim=-1) # / 0.04
pseudo_label = pseudo_label.argmax(dim=1, keepdim=True)
pseudo_label = pseudo_label.flatten().cuda()
loss = criteria(out.squeeze(), pseudo_label)
if i % args.print_freq == 0:
print(
"epoch [{0}/{1}][{2}/{3}]\t"
"loss {losses}\t"
"lr {lr:.6e}".format(
epoch + 1,
args.epochs,
i + 1,
len(tr_loader),
losses=loss.item(),
lr=optimizer.param_groups[0]["lr"],
))
loss.backward()
optimizer.step()
scheduler.step()
print(f'Evaluation: {epoch}')
acc = test_prompting(val_loader, model)
print(f'TOP-1 Accuracy: {acc}')
all_acc.append(acc)
print(f'-------------------------------- Best Accuracy: {max(all_acc)} --------------------------------')
def main(args):
cfg = setup_cfg(args)
cfg.DATALOADER.TRAIN_X.BATCH_SIZE = args.batch_size
cfg.DATALOADER.TEST.BATCH_SIZE = args.batch_size
cfg.SEED = args.seed
dataset_name = cfg.DATASET.NAME
setup_txt_epochs(args, dataset_name)
if cfg.SEED >= 0:
print("Setting fixed seed: {}".format(cfg.SEED))
set_random_seed(cfg.SEED)
setup_logger(cfg.OUTPUT_DIR)
print_args(args, cfg)
if torch.cuda.is_available() and cfg.USE_CUDA:
torch.backends.cudnn.benchmark = True
trainer = build_trainer(cfg)
model = trainer.model
model.args = args
test_loader = trainer.test_loader
train_loader = trainer.train_loader_x
if args.zero_shot:
zero_shot(model, test_loader)
else:
train_lafter(args, model,train_loader, test_loader)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--root", type=str, default="", help="path to dataset")
parser.add_argument("--output-dir", type=str, default="", help="output directory")
parser.add_argument(
"--resume",
type=str,
default="",
help="checkpoint directory (from which the training resumes)",
)
parser.add_argument(
"--seed", type=int, default=7777, help="only positive value enables a fixed seed"
)
parser.add_argument(
"--print_freq", type=int, default=10, help="only positive value enables a fixed seed"
)
parser.add_argument(
"--source-domains", type=str, nargs="+", help="source domains for DA/DG"
)
parser.add_argument(
"--target-domains", type=str, nargs="+", help="target domains for DA/DG"
)
parser.add_argument(
"--transforms", type=str, nargs="+", help="data augmentation methods"
)
parser.add_argument(
"--config-file", type=str, default="", help="path to config file"
)
parser.add_argument(
"--dataset-config-file",
type=str,
default="",
help="path to config file for dataset setup",
)
parser.add_argument("--trainer", type=str, default="", help="name of trainer")
parser.add_argument("--backbone", type=str, default="", help="name of CNN backbone")
parser.add_argument("--head", type=str, default="", help="name of head")
parser.add_argument("--eval-only", action="store_true", help="evaluation only")
parser.add_argument(
"--model-dir",
type=str,
default="",
help="load model from this directory for eval-only mode",
)
parser.add_argument(
"--load-epoch", type=int, help="load model weights at this epoch for evaluation"
)
parser.add_argument(
"--no-train", action="store_true", help="do not call trainer.train()"
)
parser.add_argument(
"opts",
default=None,
nargs=argparse.REMAINDER,
help="modify config options using the command-line",
)
parser.add_argument('--exp-name', type=str, required=False)
parser.add_argument('--scheduler', default='cosine')
parser.add_argument('--scheduler-epochs', type=int, default=15)
parser.add_argument('--scheduler-gamma', type=float, default=0.3)
parser.add_argument('--weight-decay', type=float, default=0.0001)
parser.add_argument('--acc-batches', type=int, default=1)
parser.add_argument('--arch', type=str, default='ViT-B/32', required=False)
parser.add_argument('--gpt_prompts', action='store_true')
parser.add_argument('--text_prompts', action='store_true')
parser.add_argument('--zero_shot', action='store_true')
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--txt_cls', type=str, default='tap', required=True, choices=['cls_only',
'templates_only', 'lafter', 'zero_shot'])
parser.add_argument('--batch_size', type=int, default=50)
parser.add_argument('--workers', type=int, default=4)
parser.add_argument('--lr', type=float, default=0.001)
parser.add_argument('--epochs', type=int, default=50)
parser.add_argument('--txt_epochs', type=int, default=1000)
parser.add_argument('--logfolder', default='logs', type=str)
args = parser.parse_args()
args.mile_stones = None
main(args)