-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathevaluate.py
360 lines (314 loc) · 12.9 KB
/
evaluate.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import os
import gc
import random
import pprint
from six.moves import range
from markdown2 import markdown
from time import gmtime, strftime
from timeit import default_timer as timer
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import options
from dataloader import VisDialDataset
from torch.utils.data import DataLoader
from eval_utils.dialog_generate import dialogDump
from eval_utils.rank_answerer import rankABot
from eval_utils.rank_questioner import rankQBot, rankQABots
from utils import utilities as utils
from utils.visualize import VisdomVisualize
import visdom
import numpy as np
from eval_utils.aqm_runner import AQMRunner
try:
from nsml import Visdom
print('able to use nsml Visdom')
except ImportError:
pass
def getAQMSetting(params):
if params["aqmstartFrom"]:
strategy = "depA"
qBot = os.path.splitext(os.path.basename(params["qstartFrom"]))[0]
aBot = os.path.splitext(os.path.basename(params["startFrom"]))[0]
aprxABot = os.path.splitext(os.path.basename(params["aqmstartFrom"]))[0]
else:
aBot = os.path.splitext(os.path.basename(params["startFrom"]))[0]
aprxABot = os.path.splitext(os.path.basename(params["aqmAStartFrom"]))[0]
strategy = "trueA" if aBot == aprxABot else "indA"
qBot = os.path.splitext(os.path.basename(params["aqmQStartFrom"]))[0]
if "delta" in qBot:
assert "delta" in aBot and "delta" in aprxABot
else:
assert "delta" not in aBot and "delta" not in aprxABot
if "delta" in qBot:
hpSetting = "delta"
else:
hpSetting = "nondelta"
aqmSetting = {
"hpSetting": hpSetting,
"strategy": strategy,
"qBot": qBot,
"aBot": aBot,
"aprxABot": aprxABot,
}
return aqmSetting
def main(params):
aqmSetting = None
if ("AQMBotRank" in params["evalModeList"]
or "AQMdialog" in params["evalModeList"]
or "AQMdemo" in params["evalModeList"]):
aqmSetting = getAQMSetting(params)
# setup dataloader
dlparams = params.copy()
dlparams['useIm'] = True
dlparams['useHistory'] = True
dlparams['numRounds'] = 10
splits = ['val', 'test']
dataset = VisDialDataset(dlparams, splits)
# Transferring dataset parameters
transfer = ['vocabSize', 'numOptions', 'numRounds']
for key in transfer:
if hasattr(dataset, key):
params[key] = getattr(dataset, key)
if 'numRounds' not in params:
params['numRounds'] = 10
# Always load checkpoint parameters with continue flag
params['continue'] = True
excludeParams = ['batchSize', 'visdomEnv', 'startFrom', 'qstartFrom', 'trainMode', \
'evalModeList', 'inputImg', 'inputQues', 'inputJson', 'evalTitle', 'beamSize', \
'enableVisdom', 'visdomServer', 'visdomServerPort', 'randomCaption', 'zeroCaption',
'numImg', 'numQ', 'numA', 'alpha',
'qbeamSize', 'gamma', 'delta', 'lambda',
'onlyGuesser', 'randQ', 'gen1Q', 'gtQ', 'randA', 'noHistory',
'slGuesser', 'resampleEveryDialog']
aBot = None
qBot = None
aqmBot = None
# load aBot
print('load aBot')
if params['startFrom']:
aBot, loadedParams, _ = utils.loadModel(params, 'abot', overwrite=True)
assert aBot.encoder.vocabSize == dataset.vocabSize, "Vocab size mismatch!"
for key in loadedParams:
params[key] = loadedParams[key]
aBot.eval()
# Retaining certain dataloder parameters
for key in excludeParams:
params[key] = dlparams[key]
print('load qBot')
# load qBot
if params['qstartFrom'] and not params['aqmstartFrom']:
qBot, loadedParams, _ = utils.loadModel(params, 'qbot', overwrite=True)
assert qBot.encoder.vocabSize == params[
'vocabSize'], "Vocab size mismatch!"
for key in loadedParams:
params[key] = loadedParams[key]
qBot.eval()
# Retaining certain dataloder parameters
for key in excludeParams:
params[key] = dlparams[key]
print('load AQM-Bot')
# load aqmBot
if params['aqmstartFrom']: # abot of AQM
assert params['qstartFrom'] # qbot of AQM
aqmBot, loadedParams, _ = utils.loadModel(params, 'AQM-qbot', overwrite=True)
assert aqmBot.questioner.encoder.vocabSize == params[
'vocabSize'], "Vocab size mismatch!"
for key in loadedParams:
params[key] = loadedParams[key]
aqmBot.eval()
# load qBot
for key in excludeParams:
params[key] = dlparams[key]
aqmQ, loadedParams, _ = utils.loadModel(params, 'qbot', overwrite=True)
assert aqmQ.encoder.vocabSize == params[
'vocabSize'], "Vocab size mismatch!"
for key in loadedParams:
params[key] = loadedParams[key]
aqmQ.eval()
for key in excludeParams:
params[key] = dlparams[key]
aqmBot.setQuestioner(aqmQ)
elif params['aqmQStartFrom']:
from visdial.models.aqm_questioner import AQMQuestioner
aqmBot = AQMQuestioner()
aqmBot.eval()
params['qstartFrom'] = params['aqmQStartFrom']
aqmQ, loadedParams, _ = utils.loadModel(params, 'qbot', overwrite=True)
assert aqmQ.encoder.vocabSize == params[
'vocabSize'], "Vocab size mismatch!"
for key in loadedParams:
params[key] = loadedParams[key]
aqmQ.eval()
for key in excludeParams:
params[key] = dlparams[key]
aqmBot.setQuestioner(aqmQ)
params['startFrom'] = params['aqmAStartFrom']
aqmA, loadedParams, _ = utils.loadModel(params, 'abot', overwrite=True)
assert aqmA.encoder.vocabSize == dataset.vocabSize, "Vocab size mismatch!"
for key in loadedParams:
params[key] = loadedParams[key]
aqmA.eval()
aqmBot.setAppAnswerer(aqmA)
for key in excludeParams:
params[key] = dlparams[key]
pprint.pprint(params)
#viz.addText(pprint.pformat(params, indent=4))
print("Running evaluation!")
numRounds = params['numRounds']
if 'ckpt_iterid' in params:
iterId = params['ckpt_iterid'] + 1
else:
iterId = -1
if 'test' in splits:
split = 'test'
splitName = 'test - {}'.format(params['evalTitle'])
else:
split = 'val'
splitName = 'full Val - {}'.format(params['evalTitle'])
print("Using split %s" % split)
dataset.split = split
if 'ABotRank' in params['evalModeList']:
if params['aqmstartFrom']:
aBot = aqmBot.appAnswerer
print('evaluating appBot of AQM')
print("Performing ABotRank evaluation")
rankMetrics = rankABot(
aBot, dataset, split, scoringFunction=utils.maskedNll,
expLowerLimit=params['expLowerLimit'],
expUpperLimit=params['expUpperLimit'])
print(rankMetrics)
for metric, value in rankMetrics.items():
plotName = splitName + ' - ABot Rank'
#viz.linePlot(iterId, value, plotName, metric, xlabel='Iterations')
if 'QBotRank' in params['evalModeList']:
print("Performing QBotRank evaluation")
rankMetrics, roundRanks = rankQBot(qBot, dataset, split,
expLowerLimit=params['expLowerLimit'],
expUpperLimit=params['expUpperLimit'],
verbose=1)
for metric, value in rankMetrics.items():
plotName = splitName + ' - QBot Rank'
#viz.linePlot(iterId, value, plotName, metric, xlabel='Iterations')
for r in range(numRounds + 1):
for metric, value in roundRanks[r].items():
plotName = '[Iter %d] %s - QABots Rank Roundwise' % \
(iterId, splitName)
#viz.linePlot(r, value, plotName, metric, xlabel='Round')
if 'QABotsRank' in params['evalModeList']:
print("Performing QABotsRank evaluation")
outputPredFile = "data/visdial/visdial/output_predictions_rollout.h5"
rankMetrics, roundRanks = rankQABots(
qBot, aBot, dataset, split, beamSize=params['beamSize'],
expLowerLimit=params['expLowerLimit'],
expUpperLimit=params['expUpperLimit'],
zeroCaption=params['zeroCaption'],
randomCaption=params['randomCaption'],
numRounds=params['runRounds'])
for metric, value in rankMetrics.items():
plotName = splitName + ' - QABots Rank'
#viz.linePlot(iterId, value, plotName, metric, xlabel='Iterations')
for r in range(numRounds + 1):
for metric, value in roundRanks[r].items():
plotName = '[Iter %d] %s - QBot All Metrics vs Round'%\
(iterId, splitName)
#viz.linePlot(r, value, plotName, metric, xlabel='Round')
if 'AQMBotRank' in params['evalModeList']:
print("Performing AQMBotRank evaluation")
outputPredFile = "data/visdial/visdial/output_predictions_rollout.h5"
rankMetrics, roundRanks = AQMRunner(
aqmBot, aBot, dataset, split, beamSize=params['beamSize'], realQA=params['aqmRealQA'],
saveLogs=params['saveLogs'], showQA=params['showQA'],
expLowerLimit=params['expLowerLimit'],
expUpperLimit=params['expUpperLimit'],
selectedBatchIdxs=params['selectedBatchIdxs'],
numRounds=params['runRounds'],
lda=params['lambda'],
onlyGuesser=params['onlyGuesser'],
numQ=params['numQ'],
qbeamSize=params['qbeamSize'],
numImg=params['numImg'],
alpha=params['alpha'],
numA=params['numA'],
randQ=params['randQ'],
randA=params['randA'],
zeroCaption=params['zeroCaption'],
randomCaption=params['randomCaption'],
gamma=params['gamma'],
delta=params['delta'],
gen1Q=params['gen1Q'],
gtQ=params['gtQ'],
noHistory=params['noHistory'],
slGuesser=params['slGuesser'],
resampleEveryDialog=params['resampleEveryDialog'],
aqmSetting=aqmSetting,
).rankQuestioner()
for metric, value in rankMetrics.items():
plotName = splitName + ' - QABots Rank'
#viz.linePlot(iterId, value, plotName, metric, xlabel='Iterations')
for r in range(numRounds + 1):
for metric, value in roundRanks[r].items():
plotName = '[Iter %d] %s - QBot All Metrics vs Round'%\
(iterId, splitName)
#viz.linePlot(r, value, plotName, metric, xlabel='Round')
if 'dialog' in params['evalModeList']:
print("Performing dialog generation...")
split = 'test'
outputFolder = "dialog_output/results"
os.makedirs(outputFolder, exist_ok=True)
outputPath = os.path.join(outputFolder, "results.json")
dialogDump(
params,
dataset,
split,
aBot=aBot,
qBot=qBot,
expLowerLimit=params['expLowerLimit'],
expUpperLimit=params['expUpperLimit'],
beamSize=params['beamSize'],
savePath=outputPath)
if 'AQMdialog' in params['evalModeList']:
print("Performing AQM dialog generation...")
split = 'test'
AQMRunner(
aqmBot, aBot, dataset, split, beamSize=params['beamSize'], realQA=params['aqmRealQA'],
saveLogs=params['saveLogs'], showQA=params['showQA'],
expLowerLimit=params['expLowerLimit'],
expUpperLimit=params['expUpperLimit'],
selectedBatchIdxs=params['selectedBatchIdxs'],
numRounds=params['runRounds'],
lda=params['lambda'],
onlyGuesser=params['onlyGuesser'],
numQ=params['numQ'],
qbeamSize=params['qbeamSize'],
numImg=params['numImg'],
alpha=params['alpha'],
numA=params['numA'],
randQ=params['randQ'],
randA=params['randA'],
zeroCaption=params['zeroCaption'],
randomCaption=params['randomCaption'],
gamma=params['gamma'],
delta=params['delta'],
gen1Q=params['gen1Q'],
gtQ=params['gtQ'],
noHistory=params['noHistory'],
slGuesser=params['slGuesser'],
resampleEveryDialog=params['resampleEveryDialog'],
aqmSetting=aqmSetting,
).dialogDump(params)
#viz.addText("Evaluation run complete!")
if __name__ == '__main__':
# read the command line options
params = options.readCommandLine()
# seed rng for reproducibility
manualSeed = 1234
# manualSeed = params['randomSeed']
if "AQMdemo" not in params["evalModeList"]:
random.seed(manualSeed)
np.random.seed(manualSeed)
torch.manual_seed(manualSeed)
if params['useGPU']:
torch.cuda.manual_seed_all(manualSeed)
main(params)