-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtraining.py
150 lines (121 loc) · 5.9 KB
/
training.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
import torch.utils.data.dataset
from torch.utils.data import DataLoader
from tqdm import tqdm
from losses import compute_contrastive_loss_from_feats
from models import *
from utils import * # bad practice, nvm
ckpt_dir = 'exp_data'
def train_model(model1, model2, train_set, val_set, tqdm_on, id, num_epochs, batch_size, learning_rate, c1, c2, t):
# cuda side setup
model1 = nn.DataParallel(model1).cuda()
model2 = nn.DataParallel(model2).cuda()
# training side
optimizer = torch.optim.AdamW(params=list(model1.parameters()) + list(model2.parameters()),
lr=learning_rate, weight_decay=3e-4)
criterion = nn.CrossEntropyLoss()
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=num_epochs)
# load the training data
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True,
num_workers=16, pin_memory=True, drop_last=True)
val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=False, num_workers=16,
pin_memory=True, drop_last=True)
# training loop
for epoch in range(num_epochs):
loss1_model1 = AverageMeter()
loss1_model2 = AverageMeter()
loss2_model1 = AverageMeter()
loss2_model2 = AverageMeter()
loss3_combined = AverageMeter()
acc_model1 = AverageMeter()
acc_model2 = AverageMeter()
model1.train()
model2.train()
pg = tqdm(train_loader, leave=False, total=len(train_loader), disable=not tqdm_on)
for i, (x1, y1, x2, y2) in enumerate(pg):
# doodle, label, real, label
x1, y1, x2, y2 = x1.cuda(), y1.cuda(), x2.cuda(), y2.cuda()
# train model1 (doodle)
pred1, feats1 = model1(x1, return_feats=True)
loss_1 = criterion(pred1, y1) # classification loss
loss_2 = compute_contrastive_loss_from_feats(feats1, y1, t)
loss1_model1.update(loss_1)
loss2_model1.update(loss_2)
loss_model1 = loss_1 + c1 * loss_2
# train model2 (real)
pred2, feats2 = model2(x2, return_feats=True)
loss_1 = criterion(pred2, y2) # classification loss
loss_2 = compute_contrastive_loss_from_feats(feats2, y2, t)
loss1_model2.update(loss_1)
loss2_model2.update(loss_2)
loss_model2 = loss_1 + c1 * loss_2
# the third loss
combined_feat = feats1 * feats2
loss_3 = compute_contrastive_loss_from_feats(combined_feat, y1, t)
loss3_combined.update(loss_3)
loss = loss_model1 + loss_model2 + c2 * loss_3
# statistics
acc_model1.update(compute_accuracy(pred1, y1))
acc_model2.update(compute_accuracy(pred2, y2))
# optimization
loss.backward()
optimizer.step()
optimizer.zero_grad()
# display
pg.set_postfix({
'acc 1': '{:.6f}'.format(acc_model1.avg),
'acc 2': '{:.6f}'.format(acc_model2.avg),
'l1m1': '{:.6f}'.format(loss1_model1.avg),
'l2m1': '{:.6f}'.format(loss2_model1.avg),
'l1m2': '{:.6f}'.format(loss1_model2.avg),
'l2m2': '{:.6f}'.format(loss2_model2.avg),
'train epoch': '{:03d}'.format(epoch)
})
print(
f'train epoch {epoch}, acc 1={acc_model1.avg:.3f}, acc 2={acc_model2.avg:.3f}, l1m1={loss1_model1.avg:.3f},'
f'l1m2={loss1_model2.avg:.3f}, l2m1={loss2_model1.avg:.3f}, l2m2={loss2_model2.avg:.3f}, '
f'l3={loss3_combined.avg:.3f}')
# validation
model1.eval(), model1.eval()
acc_model1.reset(), acc_model2.reset()
pg = tqdm(val_loader, leave=False, total=len(val_loader), disable=not tqdm_on)
with torch.no_grad():
for i, (x1, y1, x2, y2) in enumerate(pg):
pred1, feats1 = model1(x1, return_feats=True)
pred2, feats2 = model2(x2, return_feats=True)
acc_model1.update(compute_accuracy(pred1, y1))
acc_model2.update(compute_accuracy(pred2, y2))
# display
pg.set_postfix({
'acc 1': '{:.6f}'.format(acc_model1.avg),
'acc 2': '{:.6f}'.format(acc_model2.avg),
'val epoch': '{:03d}'.format(epoch)
})
print(f'validation epoch {epoch}, acc 1 (doodle) = {acc_model1.avg:.3f}, acc 2 (real) = {acc_model2.avg:.3f}')
scheduler.step()
print(f'training finished')
# save checkpoint
exp_dir = f'exp_data/{id}'
save_model(exp_dir, f'{id}_model1.pt', model1)
save_model(exp_dir, f'{id}_model2.pt', model2)
fix_seed(0) # zero seed by default
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
if __name__ == "__main__":
from dataset import ImageDataset
from training_config import doodles, reals, doodle_size, real_size, NUM_CLASSES
train_set = ImageDataset(doodles, reals, doodle_size, real_size, train=True)
val_set = ImageDataset(doodles, reals, doodle_size, real_size, train=False)
# tunable hyper params.
use_cnn = True
num_epochs, base_bs, base_lr = 20, 512, 2e-2
c1, c2, t = 1, 1, 0.1 # contrastive learning. if you want vanilla (cross-entropy) training, set c1 and c2 to 0.
dropout = 0.2
add_layer = True
# models
doodle_model = ExampleCNN(1, NUM_CLASSES, dropout, add_layer) if use_cnn \
else ExampleMLP(doodle_size * doodle_size, 128, NUM_CLASSES)
real_model = ExampleCNN(3, NUM_CLASSES, dropout, add_layer) if use_cnn \
else ExampleMLP(doodle_size * doodle_size, 128, NUM_CLASSES)
# just some logistics
tqdm_on = False # progress bar
id = 0 # change to the id of each experiment accordingly
train_model(doodle_model, real_model, train_set, val_set, tqdm_on, id, num_epochs, base_bs, base_lr, c1, c2, t)