-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate_masked.py
152 lines (104 loc) · 5.34 KB
/
evaluate_masked.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
import torch
import torch.nn.functional as F
def evaluate_model_masked(model, dataloader, device, max_batches = None):
model.eval()
with torch.no_grad():
num_correct = 0
num_samples = 0
for idx, (inputs, candidates, targets) in enumerate(dataloader):
# move images to the device
inputs = inputs.to(device) # shape (B,9,model_dim)
candidates = candidates.to(device) # shape (B, 8, embed_dim)
targets = targets.to(device) # shape (B,)
# forward pass
outputs = model(inputs).unsqueeze(1) # (batch_size,1,embed_dim)
guesses = torch.argmin(torch.sum((candidates - outputs)**2, dim=-1), dim = -1)
num_correct += torch.eq(guesses, targets).sum().item()
num_samples += inputs.size(0)
if max_batches is not None and idx + 1 == max_batches:
break
return 100*(num_correct / num_samples)
def evaluate_model_masked_BERT_embed(model, dataloader, device, max_batches = None):
model.eval()
with torch.no_grad():
num_correct = 0
num_samples = 0
for idx, (inputs, cands_image, target_nums, targets) in enumerate(dataloader):
batch_size = inputs.size(0)
# move images to the device
inputs = inputs.to(device) # shape (B,9,1,160,160)
target_nums = target_nums.to(device)
# forward pass
outputs, _, cands_embed = model(inputs, cands_image)
# get take "guesses" as closest in MSE
outputs = outputs.unsqueeze(1) # (B, 1, embed_dim)
guesses = torch.argmin(torch.sum((cands_embed - outputs) ** 2, dim=-1), dim=-1)
# tally the correct answers
num_correct += torch.eq(guesses, target_nums).sum().item()
num_samples += inputs.size(0)
if max_batches is not None and idx + 1 == max_batches:
break
return 100*(num_correct / num_samples)
def evaluate_model_masked_BERT_dist(model, dataloader, device, max_batches = None):
model.eval()
with torch.no_grad():
num_correct = 0
num_samples = 0
for idx, (inputs, cands, target_nums) in enumerate(dataloader):
batch_size = inputs.size(0)
# move images to the device
inputs = inputs.to(device) # shape (B,9,1,160,160)
target_nums = target_nums.to(device)
# forward pass
dists, _, _ = model(inputs, cands)
dists_softmax = F.softmax(dists, dim = 1)
guesses = torch.argmax(dists, dim = -1) # take highest probability guess
num_correct += torch.eq(guesses, target_nums).sum().item()
num_samples += inputs.size(0)
if max_batches is not None and idx + 1 == max_batches:
break
return 100*(num_correct / num_samples)
def evaluate_model_masked_BERT_v13(model, dataloader, device, max_batches = None):
model.eval()
with torch.no_grad():
num_correct = 0
num_samples = 0
for idx, (inputs, mask_tensors, _, target_nums, imagetensors) in enumerate(dataloader):
batch_size = inputs.size(0)
# move images to the device
inputs = inputs.to(device) # shape (B,9,1,160,160)
candidates = imagetensors[:,8:,:,:,:].to(device) # shape (B, 8, 1, 160, 160)
target_nums = target_nums.to(device)
# mask_tensors = mask_tensors.to(device) # 1s where the mask is, 0s elsewhere
candidates_embed = model.encode(candidates.reshape(batch_size*8, 1, 160, 160)).reshape(batch_size, 8, -1)
# forward pass
outputs, _ = model(inputs)
outputs = outputs.unsqueeze(1)
guesses = torch.argmin(torch.sum((candidates_embed - outputs)**2, dim=2), dim = -1) # take least squares guess
num_correct += torch.eq(guesses, target_nums).sum().item()
num_samples += inputs.size(0)
if max_batches is not None and idx + 1 == max_batches:
break
return 100*(num_correct / num_samples)
def evaluate_model_masked_BERT(model, dataloader, device, max_batches = None):
model.eval()
with torch.no_grad():
num_correct = 0
num_samples = 0
for idx, (inputs, mask_tensors, _, target_nums, imagetensors) in enumerate(dataloader):
# move images to the device
inputs = inputs.to(device) # shape (B,9,1,160,160)
candidates = imagetensors[:,8:,:,:,:].to(device) # shape (B, 8, 1, 160, 160)
target_nums = target_nums.to(device)
mask_tensors = mask_tensors.to(device) # 1s where the mask is, 0s elsewhere
# forward pass
outputs, _ = model(inputs)
outputs = outputs.unsqueeze(1)
# outputs = torch.sum(outputs*mask_tensors, dim=1).unsqueeze(1) # extract only guess
guesses = torch.argmin(torch.sum((candidates - outputs)**2, dim=[2,3,4]), dim = -1) # take least squares guess
# guesses = torch.argmin(torch.mean(torch.abs(candidates - outputs), dim=[2,3,4]), dim = -1) # take closest L1 guess
num_correct += torch.eq(guesses, target_nums).sum().item()
num_samples += inputs.size(0)
if max_batches is not None and idx + 1 == max_batches:
break
return 100*(num_correct / num_samples)