-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevaluate_disc.py
171 lines (146 loc) · 7.17 KB
/
evaluate_disc.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from dataloader.dataloader_visdial_disc import VisdialDataset
from models.visual_dialog_encoder import VisualDialogEncoder
import options
import pprint
import os
import json
from train_disc import forward
from utils.visdial_metrics import SparseGTMetrics, NDCG, scores_to_ranks
from pytorch_transformers.tokenization_bert import BertTokenizer
from utils.data_utils import sequence_mask, batch_iter
from utils.logger import Logger
def evaluate(dataloader, params, eval_batch_size, mode='vd_eval_val'):
sparse_metrics = SparseGTMetrics()
ndcg = NDCG()
ranks_json = []
dialog_encoder.eval()
batch_idx = 0
with torch.no_grad():
batch_size = 200
print("batch size for evaluation", batch_size)
for epoch_id, _, batch in batch_iter(dataloader, params):
if epoch_id == 1:
break
tokens = batch['tokens']
num_rounds = tokens.shape[1]
num_options = tokens.shape[2]
tokens = tokens.view(-1, tokens.shape[-1])
segments = batch['segments']
segments = segments.view(-1, segments.shape[-1])
sep_indices = batch['sep_indices']
sep_indices = sep_indices.view(-1, sep_indices.shape[-1])
mask = batch['mask']
mask = mask.view(-1, mask.shape[-1])
hist_len = batch['hist_len']
hist_len = hist_len.view(-1)
# get image features
features = batch['image_feat']
spatials = batch['image_loc']
image_mask = batch['image_mask']
# expand the image features to match those of tokens etc.
max_num_regions = features.shape[-2]
features = features.unsqueeze(1).unsqueeze(1).expand(eval_batch_size, num_rounds, num_options, max_num_regions, 2048).contiguous()
spatials = spatials.unsqueeze(1).unsqueeze(1).expand(eval_batch_size, num_rounds, num_options, max_num_regions, 5).contiguous()
image_mask = image_mask.unsqueeze(1).unsqueeze(1).expand(eval_batch_size, num_rounds, num_options, max_num_regions).contiguous()
features = features.view(-1, max_num_regions, 2048)
spatials = spatials.view(-1, max_num_regions, 5)
image_mask = image_mask.view(-1, max_num_regions)
assert tokens.shape[0] == segments.shape[0] == sep_indices.shape[0] == mask.shape[0] == \
hist_len.shape[0] == features.shape[0] == spatials.shape[0] == \
image_mask.shape[0] == num_rounds * num_options * eval_batch_size
output = []
assert (eval_batch_size * num_rounds * num_options)//batch_size == (eval_batch_size * num_rounds * num_options)/batch_size
for j in range((eval_batch_size * num_rounds * num_options)//batch_size):
# create chunks of the original batch
item = {}
item['tokens'] = tokens[j*batch_size:(j+1)*batch_size,:]
item['segments'] = segments[j*batch_size:(j+1)*batch_size,:]
item['sep_indices'] = sep_indices[j*batch_size:(j+1)*batch_size,:]
item['mask'] = mask[j*batch_size:(j+1)*batch_size,:]
item['hist_len'] = hist_len[j*batch_size:(j+1)*batch_size]
item['image_feat'] = features[j*batch_size:(j+1)*batch_size, : , :]
item['image_loc'] = spatials[j*batch_size:(j+1)*batch_size, : , :]
item['image_mask'] = image_mask[j*batch_size:(j+1)*batch_size, :]
_, _, _, _, nsp_scores, _ = forward(dialog_encoder, item, params)
# normalize nsp scores
nsp_probs = F.softmax(nsp_scores, dim=1)
assert nsp_probs.shape[-1] == 2
output.append(nsp_probs[:,0])
# print("output shape",torch.cat(output,0).shape)
output = torch.cat(output,0).view(eval_batch_size, num_rounds, num_options)
if mode == 'vd_eval_val':
gt_option_inds = batch['gt_option_inds']
gt_relevance = batch['gt_relevance']
gt_relevance_round_id = batch['round_id'].squeeze(1)
sparse_metrics.observe(output, gt_option_inds)
output = output[torch.arange(output.size(0)), gt_relevance_round_id - 1, :]
ndcg.observe(output, gt_relevance)
else:
ranks = scores_to_ranks(output)
ranks = ranks.squeeze(1)
for i in range(eval_batch_size):
ranks_json.append(
{
"image_id": batch["image_id"][i].item(),
"round_id": int(batch["round_id"][i].item()),
"ranks": [
rank.item()
for rank in ranks[i][:]
],
}
)
batch_idx += 1
if mode == 'vd_eval_val':
all_metrics = {}
all_metrics.update(sparse_metrics.retrieve(reset=True))
all_metrics.update(ndcg.retrieve(reset=True))
for metric_name, metric_value in all_metrics.items():
logger.write(f"{metric_name}: {metric_value}")
return ranks_json
if __name__ == '__main__':
params = options.read_command_line()
if not os.path.exists(params['save_path']):
os.makedirs(params['save_path'], exist_ok=True)
pprint.pprint(params)
dataset = VisdialDataset(params)
eval_batch_size = 20
save_name = params['save_name'] if params['save_name'] else 'performance_log.txt'
logger = Logger(os.path.join(params['save_path'], save_name))
mode = params['mode']
assert mode == 'vd_eval_val' or mode == 'vd_eval_test'
dataset.mode = mode
params['model'] = 'enc_only_a'
dataloader = DataLoader(
dataset,
batch_size=eval_batch_size,
shuffle=False,
num_workers=params['num_workers'],
drop_last=False,
pin_memory=False
)
device = (
torch.device("cuda", params["gpu_ids"][0])
if params["gpu_ids"][0] >= 0
else torch.device("cpu")
)
params['device'] = device
dialog_encoder = VisualDialogEncoder(params)
if params['start_path']:
pretrained_dict = torch.load(params['start_path'], map_location=device)
if 'model_state_dict' in pretrained_dict:
pretrained_dict = pretrained_dict['model_state_dict']
model_dict = dialog_encoder.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
print("number of keys transferred", len(pretrained_dict))
assert len(pretrained_dict.keys()) > 0
model_dict.update(pretrained_dict)
dialog_encoder.load_state_dict(model_dict)
dialog_encoder = dialog_encoder.to(device)
dialog_encoder = nn.DataParallel(dialog_encoder, params["gpu_ids"])
ranks_json = evaluate(dataloader, params, eval_batch_size, mode=mode)
if mode == 'vd_eval_test':
json.dump(ranks_json, open(params['save_name'] + '_predictions.txt', "w"))