-
Notifications
You must be signed in to change notification settings - Fork 16
/
train.py
140 lines (101 loc) · 5.27 KB
/
train.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
import os
import tqdm
import time
import wandb
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from common.meter import Meter
from common.utils import detect_grad_nan, compute_accuracy, set_seed, setup_run
from models.dataloader.samplers import CategoriesSampler
from models.dataloader.data_utils import dataset_builder
from models.renet import RENet
from test import test_main, evaluate
def train(epoch, model, loader, optimizer, args=None):
model.train()
train_loader = loader['train_loader']
train_loader_aux = loader['train_loader_aux']
# label for query set, always in the same pattern
label = torch.arange(args.way).repeat(args.query).cuda() # 012340123401234...
loss_meter = Meter()
acc_meter = Meter()
k = args.way * args.shot
tqdm_gen = tqdm.tqdm(train_loader)
for i, ((data, train_labels), (data_aux, train_labels_aux)) in enumerate(zip(tqdm_gen, train_loader_aux), 1):
data, train_labels = data.cuda(), train_labels.cuda()
data_aux, train_labels_aux = data_aux.cuda(), train_labels_aux.cuda()
# Forward images (3, 84, 84) -> (C, H, W)
model.module.mode = 'encoder'
data = model(data)
data_aux = model(data_aux) # I prefer to separate feed-forwarding data and data_aux due to BN
# loss for batch
model.module.mode = 'cca'
data_shot, data_query = data[:k], data[k:]
logits, absolute_logits = model((data_shot.unsqueeze(0).repeat(args.num_gpu, 1, 1, 1, 1), data_query))
epi_loss = F.cross_entropy(logits, label)
absolute_loss = F.cross_entropy(absolute_logits, train_labels[k:])
# loss for auxiliary batch
model.module.mode = 'fc'
logits_aux = model(data_aux)
loss_aux = F.cross_entropy(logits_aux, train_labels_aux)
loss_aux = loss_aux + absolute_loss
loss = args.lamb * epi_loss + loss_aux
acc = compute_accuracy(logits, label)
loss_meter.update(loss.item())
acc_meter.update(acc)
tqdm_gen.set_description(f'[train] epo:{epoch:>3} | avg.loss:{loss_meter.avg():.4f} | avg.acc:{acc_meter.avg():.3f} (curr:{acc:.3f})')
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 2.0)
detect_grad_nan(model)
optimizer.step()
optimizer.zero_grad()
return loss_meter.avg(), acc_meter.avg(), acc_meter.confidence_interval()
def train_main(args):
Dataset = dataset_builder(args)
trainset = Dataset('train', args)
train_sampler = CategoriesSampler(trainset.label, len(trainset.data) // args.batch, args.way, args.shot + args.query)
train_loader = DataLoader(dataset=trainset, batch_sampler=train_sampler, num_workers=8, pin_memory=True)
trainset_aux = Dataset('train', args)
train_loader_aux = DataLoader(dataset=trainset_aux, batch_size=args.batch, shuffle=True, num_workers=8, pin_memory=True)
train_loaders = {'train_loader': train_loader, 'train_loader_aux': train_loader_aux}
valset = Dataset('val', args)
val_sampler = CategoriesSampler(valset.label, args.val_episode, args.way, args.shot + args.query)
val_loader = DataLoader(dataset=valset, batch_sampler=val_sampler, num_workers=8, pin_memory=True)
''' fix val set for all epochs '''
val_loader = [x for x in val_loader]
set_seed(args.seed)
model = RENet(args).cuda()
model = nn.DataParallel(model, device_ids=args.device_ids)
if not args.no_wandb:
wandb.watch(model)
print(model)
optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=0.9, nesterov=True, weight_decay=0.0005)
lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=args.milestones, gamma=args.gamma)
max_acc, max_epoch = 0.0, 0
set_seed(args.seed)
for epoch in range(1, args.max_epoch + 1):
start_time = time.time()
train_loss, train_acc, _ = train(epoch, model, train_loaders, optimizer, args)
val_loss, val_acc, _ = evaluate(epoch, model, val_loader, args, set='val')
if not args.no_wandb:
wandb.log({'train/loss': train_loss, 'train/acc': train_acc, 'val/loss': val_loss, 'val/acc': val_acc}, step=epoch)
if val_acc > max_acc:
print(f'[ log ] *********A better model is found ({val_acc:.3f}) *********')
max_acc, max_epoch = val_acc, epoch
torch.save(dict(params=model.state_dict(), epoch=epoch), os.path.join(args.save_path, 'max_acc.pth'))
torch.save(optimizer.state_dict(), os.path.join(args.save_path, 'optimizer_max_acc.pth'))
if args.save_all:
torch.save(dict(params=model.state_dict(), epoch=epoch), os.path.join(args.save_path, f'epoch_{epoch}.pth'))
torch.save(optimizer.state_dict(), os.path.join(args.save_path, f'optimizer_epoch_{epoch}.pth'))
epoch_time = time.time() - start_time
print(f'[ log ] saving @ {args.save_path}')
print(f'[ log ] roughly {(args.max_epoch - epoch) / 3600. * epoch_time:.2f} h left\n')
lr_scheduler.step()
return model
if __name__ == '__main__':
args = setup_run(arg_mode='train')
model = train_main(args)
test_acc, test_ci = test_main(model, args)
if not args.no_wandb:
wandb.log({'test/acc': test_acc, 'test/confidence_interval': test_ci})