-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_and_test.py
179 lines (148 loc) · 6.78 KB
/
train_and_test.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
import torch
import logging
import torch.nn.functional as F
import util.utils as utils
from util.local_parts import train_pos_weights
from util.rotate_tensor import multiple_rotate_all, mask_tensor
def _train_or_test(model, epoch, dataloader, tb_writer, iteration, optimizer=None, use_l1_mask=True,
coefs=None, args=None, log=print):
'''
model: the multi-gpu model
dataloader:
optimizer: if None, will be test evaluation
'''
is_train = optimizer is not None
n_examples = 0
n_correct = 0
n_batches = 0
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
header = 'Epoch: [{}]'.format(epoch)
print_freq = 60
pos_weight = torch.from_numpy(train_pos_weights).cuda()
logger = logging.getLogger("train")
logger.info("Start train one epoch")
it = 0
for data_item in metric_logger.log_every(dataloader, print_freq, header):
if len(data_item) == 2:
image, label = data_item
else:
image, label, attributes = data_item
attributes = torch.stack(attributes).permute(1, 0).type(torch.FloatTensor).cuda()
attributes_criterion = torch.nn.BCEWithLogitsLoss()
# attributes_criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
input = image.cuda()
target = label.cuda()
bz = target.shape[0]
# Augment Images
if epoch < args.proto_epochs and args.use_mse_loss:
input = multiple_rotate_all(input, all_rotate_times=[0, 1])
grad_req = torch.enable_grad() if is_train else torch.no_grad()
with grad_req:
output, (_, proto_acts, shallow_feas, deep_feas, all_feas) = model(input)
if isinstance(output, tuple):
logits, logits_attri, attributes_logits = output
# Select the top output
logits, logits_attri, attributes_logits, proto_acts, shallow_feas, deep_feas = \
logits[:bz], logits_attri[:bz], attributes_logits[:bz], proto_acts[:bz], shallow_feas[:bz], deep_feas[:bz]
del input
# Compute loss
attributes_cost = attributes_criterion(attributes_logits, attributes)
logits = logits if epoch < args.proto_epochs else logits_attri
cross_entropy = torch.nn.functional.cross_entropy(logits, target)
model_without_ddp = model.module if hasattr(model, 'module') else model
if epoch < args.proto_epochs:
ortho_cost = model_without_ddp.get_ortho_loss()
consis_cost = model_without_ddp.get_CLA_loss(shallow_feas, deep_feas, scales=[1, 2], consis_thresh=args.consis_thresh)
mse_cost = model_without_ddp.get_CIA_loss(all_feas, bz, layer_idx=3)
else:
cls_dis_cost, sep_dis_cost = model_without_ddp.get_PA_loss(proto_acts)
# Evaluation statistics
_, predicted = torch.max(logits.data, 1)
n_examples += target.size(0)
n_correct += (predicted == target).sum().item()
n_batches += 1
# Compute gradient and do SGD step
if is_train:
if epoch < args.warmup_epochs: # Freeze the backbone
loss = (coefs['crs_ent'] * cross_entropy
+ coefs['orth'] * ortho_cost)
elif epoch >= args.warmup_epochs and epoch < args.proto_epochs: # Unfreeze the backbone
loss = (coefs['crs_ent'] * cross_entropy
+ coefs['orth'] * ortho_cost
+ coefs['consis'] * consis_cost
+ coefs['mse'] * mse_cost)
elif epoch >= args.proto_epochs: # Only train the predictor
loss = (coefs['crs_ent'] * cross_entropy
+ coefs['attri'] * attributes_cost
+ coefs['cls_dis'] * cls_dis_cost
+ coefs['sep_dis'] * sep_dis_cost)
loss_value = loss.item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Normalize basis vectors
model_without_ddp.prototype_vectors.data = F.normalize(model_without_ddp.prototype_vectors, p=2, dim=1).data
metric_logger.update(loss=loss_value)
metric_logger.update(lr=optimizer.param_groups[0]["lr"])
tb_writer.add_scalars(
main_tag="train/loss",
tag_scalar_dict={
"cls": loss.item(),
},
global_step=iteration+it
)
it += 1
# Del input
del target
del output
del predicted
results_loss = {'accu' : n_correct/n_examples}
return n_correct / n_examples, results_loss
def train(model, epoch, dataloader, optimizer, tb_writer, iteration, coefs=None, args=None, log=print):
assert(optimizer is not None)
model.train()
return _train_or_test(model=model, epoch=epoch, dataloader=dataloader, optimizer=optimizer, tb_writer=tb_writer,
iteration=iteration, coefs=coefs, args=args, log=log)
def test(model, epoch, dataloader, tb_writer, iteration, args=None, log=print):
model.eval()
return _train_or_test(model=model, epoch=epoch, dataloader=dataloader, optimizer=None, tb_writer=tb_writer,
iteration=iteration, args=args, log=log)
def warm_only_new(model):
if hasattr(model, 'module'):
model = model.module
for p in model.features.parameters():
p.requires_grad = False
for p in model.add_on_layers.parameters():
p.requires_grad = True
model.prototype_vectors.requires_grad = True
for p in model.attributes_predictor.parameters():
p.requires_grad = False
for p in model.class_predictor.parameters():
p.requires_grad = False
def joint_new(model):
if hasattr(model, 'module'):
model = model.module
for p in model.features.parameters():
p.requires_grad = True
for p in model.add_on_layers.parameters():
p.requires_grad = True
model.prototype_vectors.requires_grad = True
for p in model.attributes_predictor.parameters():
p.requires_grad = False
for p in model.class_predictor.parameters():
p.requires_grad = False
def final_new(model):
if hasattr(model, 'module'):
model = model.module
# for p in model.features.parameters():
# p.requires_grad = False
for p in model.features.parameters():
p.requires_grad = False
for p in model.add_on_layers.parameters():
p.requires_grad = True
for p in model.attributes_predictor.parameters():
p.requires_grad = True
for p in model.class_predictor.parameters():
p.requires_grad = True
model.prototype_vectors.requires_grad = True