forked from cfchen-duke/ProtoPNet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_and_test.py
176 lines (144 loc) · 6.72 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
import time
import torch
from helpers import list_of_distances, make_one_hot
def _train_or_test(model, dataloader, optimizer=None, class_specific=True, use_l1_mask=True,
coefs=None, log=print):
'''
model: the multi-gpu model
dataloader:
optimizer: if None, will be test evaluation
'''
is_train = optimizer is not None
start = time.time()
n_examples = 0
n_correct = 0
n_batches = 0
total_cross_entropy = 0
total_cluster_cost = 0
# separation cost is meaningful only for class_specific
total_separation_cost = 0
total_avg_separation_cost = 0
for i, (image, label) in enumerate(dataloader):
input = image.cuda()
target = label.cuda()
# torch.enable_grad() has no effect outside of no_grad()
grad_req = torch.enable_grad() if is_train else torch.no_grad()
with grad_req:
# nn.Module has implemented __call__() function
# so no need to call .forward
output, min_distances = model(input)
# compute loss
cross_entropy = torch.nn.functional.cross_entropy(output, target)
if class_specific:
max_dist = (model.module.prototype_shape[1]
* model.module.prototype_shape[2]
* model.module.prototype_shape[3])
# prototypes_of_correct_class is a tensor of shape batch_size * num_prototypes
# calculate cluster cost
prototypes_of_correct_class = torch.t(model.module.prototype_class_identity[:,label]).cuda()
inverted_distances, _ = torch.max((max_dist - min_distances) * prototypes_of_correct_class, dim=1)
cluster_cost = torch.mean(max_dist - inverted_distances)
# calculate separation cost
prototypes_of_wrong_class = 1 - prototypes_of_correct_class
inverted_distances_to_nontarget_prototypes, _ = \
torch.max((max_dist - min_distances) * prototypes_of_wrong_class, dim=1)
separation_cost = torch.mean(max_dist - inverted_distances_to_nontarget_prototypes)
# calculate avg cluster cost
avg_separation_cost = \
torch.sum(min_distances * prototypes_of_wrong_class, dim=1) / torch.sum(prototypes_of_wrong_class, dim=1)
avg_separation_cost = torch.mean(avg_separation_cost)
if use_l1_mask:
l1_mask = 1 - torch.t(model.module.prototype_class_identity).cuda()
l1 = (model.module.last_layer.weight * l1_mask).norm(p=1)
else:
l1 = model.module.last_layer.weight.norm(p=1)
else:
min_distance, _ = torch.min(min_distances, dim=1)
cluster_cost = torch.mean(min_distance)
l1 = model.module.last_layer.weight.norm(p=1)
# evaluation statistics
_, predicted = torch.max(output.data, 1)
n_examples += target.size(0)
n_correct += (predicted == target).sum().item()
n_batches += 1
total_cross_entropy += cross_entropy.item()
total_cluster_cost += cluster_cost.item()
total_separation_cost += separation_cost.item()
total_avg_separation_cost += avg_separation_cost.item()
# compute gradient and do SGD step
if is_train:
if class_specific:
if coefs is not None:
loss = (coefs['crs_ent'] * cross_entropy
+ coefs['clst'] * cluster_cost
+ coefs['sep'] * separation_cost
+ coefs['l1'] * l1)
else:
loss = cross_entropy + 0.8 * cluster_cost - 0.08 * separation_cost + 1e-4 * l1
else:
if coefs is not None:
loss = (coefs['crs_ent'] * cross_entropy
+ coefs['clst'] * cluster_cost
+ coefs['l1'] * l1)
else:
loss = cross_entropy + 0.8 * cluster_cost + 1e-4 * l1
optimizer.zero_grad()
loss.backward()
optimizer.step()
del input
del target
del output
del predicted
del min_distances
end = time.time()
log('\ttime: \t{0}'.format(end - start))
log('\tcross ent: \t{0}'.format(total_cross_entropy / n_batches))
log('\tcluster: \t{0}'.format(total_cluster_cost / n_batches))
if class_specific:
log('\tseparation:\t{0}'.format(total_separation_cost / n_batches))
log('\tavg separation:\t{0}'.format(total_avg_separation_cost / n_batches))
log('\taccu: \t\t{0}%'.format(n_correct / n_examples * 100))
log('\tl1: \t\t{0}'.format(model.module.last_layer.weight.norm(p=1).item()))
p = model.module.prototype_vectors.view(model.module.num_prototypes, -1).cpu()
with torch.no_grad():
p_avg_pair_dist = torch.mean(list_of_distances(p, p))
log('\tp dist pair: \t{0}'.format(p_avg_pair_dist.item()))
return n_correct / n_examples
def train(model, dataloader, optimizer, class_specific=False, coefs=None, log=print):
assert(optimizer is not None)
log('\ttrain')
model.train()
return _train_or_test(model=model, dataloader=dataloader, optimizer=optimizer,
class_specific=class_specific, coefs=coefs, log=log)
def test(model, dataloader, class_specific=False, log=print):
log('\ttest')
model.eval()
return _train_or_test(model=model, dataloader=dataloader, optimizer=None,
class_specific=class_specific, log=log)
def last_only(model, log=print):
for p in model.module.features.parameters():
p.requires_grad = False
for p in model.module.add_on_layers.parameters():
p.requires_grad = False
model.module.prototype_vectors.requires_grad = False
for p in model.module.last_layer.parameters():
p.requires_grad = True
log('\tlast layer')
def warm_only(model, log=print):
for p in model.module.features.parameters():
p.requires_grad = False
for p in model.module.add_on_layers.parameters():
p.requires_grad = True
model.module.prototype_vectors.requires_grad = True
for p in model.module.last_layer.parameters():
p.requires_grad = True
log('\twarm')
def joint(model, log=print):
for p in model.module.features.parameters():
p.requires_grad = True
for p in model.module.add_on_layers.parameters():
p.requires_grad = True
model.module.prototype_vectors.requires_grad = True
for p in model.module.last_layer.parameters():
p.requires_grad = True
log('\tjoint')