-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_fewshot.py
304 lines (234 loc) · 10.4 KB
/
main_fewshot.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
'''
* Adapted from ULIP (https://github.com/salesforce/ULIP)
* By Hongyu Sun
'''
from collections import OrderedDict
import os
import math
import time
import wandb
import torch
import torch.nn.parallel
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.data.distributed import DistributedSampler
from torch.nn import CrossEntropyLoss
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import collections
from data.dataset_3d import *
from utils.utils import get_dataset, accuracy, set_random_seed, AverageMeter, ProgressMeter
import models.ULIP_models as models
from utils.tokenizer import SimpleTokenizer
from utils import utils
def main(args):
utils.init_distributed_mode(args)
if utils.is_main_process() and args.wandb:
os.environ["WANDB_BASE_URL"] = args.wb_url
wandb.login(key=args.wb_key)
wandb.init(project=args.proj_name, name=args.exp_name)
# fix the seed for reproducibility
seed = args.seed + utils.get_rank()
set_random_seed(seed)
# create model
print("=> creating model: {}".format(args.model))
model = getattr(models, args.model)(args)
model.cuda(args.gpu)
if args.distributed:
# `find_unused_parameters=False`
model = DDP(model, device_ids=[args.gpu], find_unused_parameters=False)
# define loss function (criterion) and optimizer
criterion = CrossEntropyLoss(label_smoothing=args.label_smoothing).cuda(args.gpu)
if args.optim == 'sgd':
optimizer = torch.optim.SGD(model.parameters(), lr=args.lr)
elif args.optim == 'adam':
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
elif args.optim == 'adamw':
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr, betas=args.betas,
eps=args.eps, weight_decay=args.wd)
cudnn.benchmark = True
# Data loading code
print(f"=> creating a {args.nshots}-Shot dataset")
tokenizer = SimpleTokenizer()
# do not use `train_transform`
train_dataset = get_dataset(None, tokenizer, args, 'train')
val_dataset = get_dataset(None, tokenizer, args, 'test')
print('------ len(train_dataset)', len(train_dataset))
print('------ len(val_dataset)', len(val_dataset))
if args.distributed:
train_sampler = DistributedSampler(train_dataset)
val_sampler = DistributedSampler(val_dataset)
else:
train_sampler = None
val_sampler = None
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.batch_size, shuffle=(train_sampler is None),
num_workers=args.workers, pin_memory=True, sampler=train_sampler, drop_last=False)
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=args.batch_size, shuffle=(val_sampler is None),
num_workers=args.workers, pin_memory=True, sampler=val_sampler, drop_last=False)
# --- option 1
lr_scheduler = utils.cosine_scheduler(args.lr, args.lr_end, args.epochs,
len(train_loader) // args.update_freq, warmup_epochs=args.warmup_epochs, start_warmup_value=args.lr_start)
# --- option 2
# lr_scheduler = utils.cosine_annealing_warmup(optimizer, first_cycle_epochs=args.first_cycle_epochs,
# max_lr=args.lr, min_lr=args.min_lr, warmup_epochs=args.warmup_epochs, gamma=args.gamma)
print(args)
print("=> beginning finetuning")
best_acc = 0
best_epoch = -1
for epoch in range(args.start_epoch, args.epochs):
if args.distributed:
train_sampler.set_epoch(epoch)
train_stats = train(train_loader, model, criterion, optimizer, epoch, lr_scheduler, args)
val_stats = {"acc": -1}
# lr_scheduler.step()
val_stats = validate(val_loader, model, criterion, args)
acc = val_stats["acc"]
print(val_stats)
is_best = acc > best_acc
best_acc = max(acc, best_acc)
if args.distributed:
state_dict_prompt = model.module.prompt_learner.state_dict()
# last transformer block
state_dict_block = model.module.point_encoder.blocks.blocks[-1].state_dict()
else:
state_dict_prompt = model.prompt_learner.state_dict()
# last transformer block
state_dict_block = model.point_encoder.blocks.blocks[-1].state_dict()
if is_best:
best_epoch = epoch
print("=> saving best checkpoint")
saved_data = {'epoch': epoch + 1, 'state_dict': state_dict_prompt, # save `prompt_learner` only
'optimizer' : optimizer.state_dict(),
'best_acc': best_acc, 'args': args,}
saved_data['last_block'] = state_dict_block if args.head_type > 0 else None
utils.save_on_master(saved_data, is_best, os.path.join(args.output_dir, args.proj_name, args.exp_name))
log_stats = {**{f'train_{k}': v for k, v in train_stats.items()},
**{f'test_{k}': v for k, v in val_stats.items()},
'epoch': epoch,
'best_acc': best_acc,
'best_epoch': best_epoch}
if utils.is_main_process():
if args.wandb:
wandb.log(log_stats)
if utils.is_main_process():
wandb.finish()
# copy log file from pueue to outputs/${proj_name}/${exp_name}
utils.copy_log_from_pueue(args.output_dir, args.proj_name, args.exp_name, 'run.log')
def train(train_loader, model, criterion, optimizer, epoch, lr_scheduler, args):
batch_time = AverageMeter('Time', ':6.2f')
data_time = AverageMeter('Data', ':6.2f')
mem = AverageMeter('Mem (GB)', ':6.1f')
metric_names = models.get_metric_names()
iters_per_epoch = len(train_loader) // args.update_freq
# loss & acc
metrics = OrderedDict([(name, AverageMeter(name, ':.2e')) for name in metric_names])
progress = ProgressMeter(
iters_per_epoch,
[batch_time, data_time, mem, *metrics.values()],
prefix="Epoch: [{}]".format(epoch))
# switch to train mode
model.train()
end = time.time()
for data_iter, inputs in enumerate(train_loader):
if data_iter > len(train_loader) * args.data_ratio: # dr: data_ratio
break
optimizer.zero_grad()
optim_iter = data_iter // args.update_freq
# measure data loading time
data_time.update(time.time() - end)
# update weight decay and learning rate according to their schedule
it = iters_per_epoch * epoch + optim_iter # global training iteration
for _, param_group in enumerate(optimizer.param_groups):
param_group['lr'] = lr_scheduler[it]
# pc: [batch, npoints, 3]
pc = inputs[0]
pc = pc.to(args.gpu)
# label: [batch, ]
label = inputs[1].long().to(args.gpu)
# [batch, num_classes]
pred = model(pc)
loss = criterion(pred, label)
loss.backward(retain_graph=True)
optimizer.step()
pred_idx = pred.argmax(dim=1)
correct = torch.eq(pred_idx, label).sum()
acc = correct / len(label)
# NOTE check whether `loss` is exploded
if not math.isfinite(loss.item()):
print("Loss is {}, stopping training".format(loss.item()))
sys.exit(1)
if (data_iter + 1) % args.update_freq != 0:
continue
# clamp logit scale to [0, 100]
utils.get_model(model).logit_scale.data.clamp_(0, 4.6052)
logit_scale = utils.get_model(model).logit_scale.exp().item()
metrics['loss'].update(loss.item())
metrics['acc'].update(acc.item())
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
mem.update(torch.cuda.max_memory_allocated() // 1e9)
if optim_iter % args.print_freq == 0:
# if utils.is_main_process() and args.wandb:
# wandb.log({**{'loss': loss.item(), 'acc': acc.item()},
# 'logit': logit_scale})
progress.display(optim_iter)
progress.synchronize()
return {**{k: v.avg for k, v in metrics.items()},
'lr': optimizer.param_groups[0]['lr'],
'logit_scale': logit_scale}
def validate(test_loader, model, criterion, args):
batch_time = AverageMeter('Time', ':6.3f')
val_top1 = AverageMeter('Acc@1', ':6.2f')
val_loss = AverageMeter('Acc@5', ':6.2f')
progress = ProgressMeter(
len(test_loader),
[batch_time, val_top1, val_loss],
prefix='Test: ')
# switch to evaluate mode
model.eval()
with torch.no_grad():
end = time.time()
per_class_stats = collections.defaultdict(int)
per_class_correct_top1 = collections.defaultdict(int)
for i, inputs in enumerate(test_loader):
pc = inputs[0] # [batch, npoints, 3]
target = inputs[1] # [batch,]
target_name = inputs[2] # [batch]
for name in target_name:
per_class_stats[name] += 1
pc = pc.cuda(args.gpu)
target = target.long().cuda(args.gpu)
# [batch, num_classes]
pred = model(pc)
loss = criterion(pred, target)
# compute top1 only
res, correct = accuracy(pred, target, topk=(1,))
acc = res[0]
val_loss.update(loss.item(), pc.size(0))
val_top1.update(acc.item(), pc.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# [batch]
top1_accurate = correct[:1].squeeze()
for idx, name in enumerate(target_name):
if top1_accurate[idx].item():
per_class_correct_top1[name] += 1
if i % args.print_freq == 0:
progress.display(i)
top1_accuracy_per_class = {}
for name in per_class_stats.keys():
top1_accuracy_per_class[name] = per_class_correct_top1[name] / per_class_stats[name]
top1_accuracy_per_class = collections.OrderedDict(top1_accuracy_per_class)
print(','.join(top1_accuracy_per_class.keys()))
print(','.join([str(value) for value in top1_accuracy_per_class.values()]))
progress.synchronize()
print('Test * Acc@1 {top1.avg:.3f} Loss {val_loss.avg:.3f}')
return {'acc': val_top1.avg, 'loss': val_loss.avg}
if __name__ == '__main__':
from parser import args
main(args)