-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
177 lines (139 loc) · 6.66 KB
/
inference.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
import time
import numpy as np
import torch
import torch.nn.functional as F
from ops.utils import AverageMeter, accuracy, get_one_hot
from tqdm import tqdm
def inductive(val_loader, model, ata_helper, args, log=None):
batch_time = AverageMeter()
top1 = AverageMeter()
# switch to evaluate mode
model.eval()
end = time.time()
target = torch.arange(args.way, dtype=torch.int16).repeat(args.shot)
target = target.type(torch.LongTensor).cuda()
target_1h = get_one_hot(target, args.way)
target_1h = target_1h.cuda()
support_idx, query_idx = (
torch.Tensor(np.arange(args.way * args.shot)).long().view(1, args.shot, args.way),
torch.Tensor(np.arange(args.way * args.shot, args.way * (args.shot + args.n_query)))
.long()
.view(1, args.n_query, args.way),
)
for i, (input, _) in tqdm(enumerate(val_loader)):
# compute output
with torch.no_grad():
outputepi = model(input, feature=True)
support, query = outputepi[support_idx.flatten()].contiguous(), outputepi[query_idx.flatten()].contiguous()
support = support.view(args.way * args.shot, args.num_segments, -1).detach()
query = query.view(args.way * args.n_query, args.num_segments, -1).detach()
support = F.normalize(support, dim=-1)
query = F.normalize(query, dim=-1)
weights = support.view(args.shot, args.way, args.num_segments, -1).mean(0).clone().detach().cuda()
scale = torch.FloatTensor(1).fill_(10.0).cuda()
weights.requires_grad_()
scale.requires_grad_()
optimizer = torch.optim.Adam([weights, scale])
for step in range(args.iter):
D, norm_D = ata_helper.similarity_matrix(support, weights, scale)
sim_a = ata_helper.appearance_score(D, scale=1.0)
ent_cond = -(norm_D * torch.log(norm_D + 1e-12)).sum(2).mean(1)
ent = -(norm_D.mean(1) * torch.log(norm_D.mean(1) + 1e-12)).sum(1)
loss = -(target_1h * torch.log(sim_a.softmax(-1) + 1e-12)).sum(1).mean(0)
loss += (target_1h * ent_cond).sum(1).mean(0) * 0.1
loss += -(target_1h * ent).sum(1).mean(0) * 1
optimizer.zero_grad()
loss.backward()
optimizer.step()
D, norm_D = ata_helper.similarity_matrix(query, weights, scale)
sim_a = ata_helper.appearance_score(D)
sim_d = ata_helper.temporal_score(norm_D)
sim = sim_a.softmax(1) + sim_d.softmax(1)
# measure accuracy
target = torch.arange(args.way, dtype=torch.int16).repeat(args.n_query).type(torch.LongTensor).cuda()
(prec1,) = accuracy(sim.data, target.cuda(), topk=(1,))
top1.update(prec1.item(), input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
output = (
"Test: [{0}/{1}]\t"
"Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t"
"Prec@1 {top1.val:.3f} ({top1.avg:.3f})".format(i, len(val_loader), batch_time=batch_time, top1=top1)
)
print(output)
if log is not None:
log.write(output + "\n")
log.flush()
output = "Testing Results: Prec@1 {top1.avg:.3f}".format(top1=top1)
print(output)
if log is not None:
log.write(output + "\n")
log.flush()
return top1.avg
def transductive(val_loader, model, ata_helper, args, log=None):
batch_time = AverageMeter()
top1 = AverageMeter()
# switch to evaluate mode
model.eval()
end = time.time()
target = torch.arange(args.way, dtype=torch.int16).repeat(args.shot)
target = target.type(torch.LongTensor).cuda()
target_1h = get_one_hot(target, args.way)
target_1h = target_1h.cuda()
support_idx, query_idx = (
torch.Tensor(np.arange(args.way * args.shot)).long().view(1, args.shot, args.way),
torch.Tensor(np.arange(args.way * args.shot, args.way * (args.shot + args.n_query)))
.long()
.view(1, args.n_query, args.way),
)
with torch.no_grad():
for i, (input, _) in tqdm(enumerate(val_loader)):
# compute output
outputepi = model(input, feature=True)
support, query = outputepi[support_idx.flatten()].contiguous(), outputepi[query_idx.flatten()].contiguous()
support = support.view(args.way * args.shot, args.num_segments, -1).detach()
query = query.view(args.way * args.n_query, args.num_segments, -1).detach()
support = F.normalize(support, dim=-1)
query = F.normalize(query, dim=-1)
weights = support.view(args.shot, args.way, args.num_segments, -1).mean(0).clone().detach().cuda()
scale = torch.FloatTensor(1).fill_(10.0).cuda()
for step in range(args.iter):
D, norm_D = ata_helper.similarity_matrix(query, weights, scale)
sim_a = ata_helper.appearance_score(D)
sim_d = ata_helper.temporal_score(norm_D)
sim = sim_a.softmax(1) + sim_d.softmax(1)
weights = (sim.unsqueeze(-1).unsqueeze(-1) * query.unsqueeze(1)).sum(0) / sim.unsqueeze(-1).unsqueeze(
-1
).sum(0) + support.view(args.shot, args.way, args.num_segments, -1).mean(0).clone().detach().cuda()
weights = F.normalize(weights, dim=-1)
D, norm_D = ata_helper.similarity_matrix(query, weights, scale)
sim_a = ata_helper.appearance_score(D)
sim_d = ata_helper.temporal_score(norm_D)
sim = sim_a.softmax(1) # + sim_d.softmax(1)
# measure accuracy
target = torch.arange(args.way, dtype=torch.int16).repeat(args.n_query).type(torch.LongTensor).cuda()
(prec1,) = accuracy(sim.data, target.cuda(), topk=(1,))
top1.update(prec1.item(), input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
output = (
"Test: [{0}/{1}]\t"
"Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t"
"Prec@1 {top1.val:.3f} ({top1.avg:.3f})".format(
i, len(val_loader), batch_time=batch_time, top1=top1
)
)
print(output)
if log is not None:
log.write(output + "\n")
log.flush()
output = "Testing Results: Prec@1 {top1.avg:.3f}".format(top1=top1)
print(output)
if log is not None:
log.write(output + "\n")
log.flush()
return top1.avg