This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
/
question_engine.py
289 lines (235 loc) · 8.93 KB
/
question_engine.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
# Copyright 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
import json, os, math
from collections import defaultdict
"""
Utilities for working with function program representations of questions.
Some of the metadata about what question node types are available etc are stored
in a JSON metadata file.
"""
# Handlers for answering questions. Each handler receives the scene structure
# that was output from Blender, the node, and a list of values that were output
# from each of the node's inputs; the handler should return the computed output
# value from this node.
def scene_handler(scene_struct, inputs, side_inputs):
# Just return all objects in the scene
return list(range(len(scene_struct['objects'])))
def make_filter_handler(attribute):
def filter_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 1
assert len(side_inputs) == 1
value = side_inputs[0]
output = []
for idx in inputs[0]:
atr = scene_struct['objects'][idx][attribute]
if value == atr or value in atr:
output.append(idx)
return output
return filter_handler
def unique_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 1
if len(inputs[0]) != 1:
return '__INVALID__'
return inputs[0][0]
def vg_relate_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 1
assert len(side_inputs) == 1
output = set()
for rel in scene_struct['relationships']:
if rel['predicate'] == side_inputs[0] and rel['subject_idx'] == inputs[0]:
output.add(rel['object_idx'])
return sorted(list(output))
def relate_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 1
assert len(side_inputs) == 1
relation = side_inputs[0]
return scene_struct['relationships'][relation][inputs[0]]
def union_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 2
assert len(side_inputs) == 0
return sorted(list(set(inputs[0]) | set(inputs[1])))
def intersect_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 2
assert len(side_inputs) == 0
return sorted(list(set(inputs[0]) & set(inputs[1])))
def count_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 1
return len(inputs[0])
def make_same_attr_handler(attribute):
def same_attr_handler(scene_struct, inputs, side_inputs):
cache_key = '_same_%s' % attribute
if cache_key not in scene_struct:
cache = {}
for i, obj1 in enumerate(scene_struct['objects']):
same = []
for j, obj2 in enumerate(scene_struct['objects']):
if i != j and obj1[attribute] == obj2[attribute]:
same.append(j)
cache[i] = same
scene_struct[cache_key] = cache
cache = scene_struct[cache_key]
assert len(inputs) == 1
assert len(side_inputs) == 0
return cache[inputs[0]]
return same_attr_handler
def make_query_handler(attribute):
def query_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 1
assert len(side_inputs) == 0
idx = inputs[0]
obj = scene_struct['objects'][idx]
assert attribute in obj
val = obj[attribute]
if type(val) == list and len(val) != 1:
return '__INVALID__'
elif type(val) == list and len(val) == 1:
return val[0]
else:
return val
return query_handler
def exist_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 1
assert len(side_inputs) == 0
return len(inputs[0]) > 0
def equal_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 2
assert len(side_inputs) == 0
return inputs[0] == inputs[1]
def less_than_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 2
assert len(side_inputs) == 0
return inputs[0] < inputs[1]
def greater_than_handler(scene_struct, inputs, side_inputs):
assert len(inputs) == 2
assert len(side_inputs) == 0
return inputs[0] > inputs[1]
# Register all of the answering handlers here.
# TODO maybe this would be cleaner with a function decorator that takes
# care of registration? Not sure. Also what if we want to reuse the same engine
# for different sets of node types?
execute_handlers = {
'scene': scene_handler,
'filter_color': make_filter_handler('color'),
'filter_shape': make_filter_handler('shape'),
'filter_material': make_filter_handler('material'),
'filter_size': make_filter_handler('size'),
'filter_objectcategory': make_filter_handler('objectcategory'),
'unique': unique_handler,
'relate': relate_handler,
'union': union_handler,
'intersect': intersect_handler,
'count': count_handler,
'query_color': make_query_handler('color'),
'query_shape': make_query_handler('shape'),
'query_material': make_query_handler('material'),
'query_size': make_query_handler('size'),
'exist': exist_handler,
'equal_color': equal_handler,
'equal_shape': equal_handler,
'equal_integer': equal_handler,
'equal_material': equal_handler,
'equal_size': equal_handler,
'equal_object': equal_handler,
'less_than': less_than_handler,
'greater_than': greater_than_handler,
'same_color': make_same_attr_handler('color'),
'same_shape': make_same_attr_handler('shape'),
'same_size': make_same_attr_handler('size'),
'same_material': make_same_attr_handler('material'),
}
def answer_question(question, metadata, scene_struct, all_outputs=False,
cache_outputs=True):
"""
Use structured scene information to answer a structured question. Most of the
heavy lifting is done by the execute handlers defined above.
We cache node outputs in the node itself; this gives a nontrivial speedup
when we want to answer many questions that share nodes on the same scene
(such as during question-generation DFS). This will NOT work if the same
nodes are executed on different scenes.
"""
all_input_types, all_output_types = [], []
node_outputs = []
for node in question['nodes']:
if cache_outputs and '_output' in node:
node_output = node['_output']
else:
node_type = node['type']
msg = 'Could not find handler for "%s"' % node_type
assert node_type in execute_handlers, msg
handler = execute_handlers[node_type]
node_inputs = [node_outputs[idx] for idx in node['inputs']]
side_inputs = node.get('side_inputs', [])
node_output = handler(scene_struct, node_inputs, side_inputs)
if cache_outputs:
node['_output'] = node_output
node_outputs.append(node_output)
if node_output == '__INVALID__':
break
if all_outputs:
return node_outputs
else:
return node_outputs[-1]
def insert_scene_node(nodes, idx):
# First make a shallow-ish copy of the input
new_nodes = []
for node in nodes:
new_node = {
'type': node['type'],
'inputs': node['inputs'],
}
if 'side_inputs' in node:
new_node['side_inputs'] = node['side_inputs']
new_nodes.append(new_node)
# Replace the specified index with a scene node
new_nodes[idx] = {'type': 'scene', 'inputs': []}
# Search backwards from the last node to see which nodes are actually used
output_used = [False] * len(new_nodes)
idxs_to_check = [len(new_nodes) - 1]
while idxs_to_check:
cur_idx = idxs_to_check.pop()
output_used[cur_idx] = True
idxs_to_check.extend(new_nodes[cur_idx]['inputs'])
# Iterate through nodes, keeping only those whose output is used;
# at the same time build up a mapping from old idxs to new idxs
old_idx_to_new_idx = {}
new_nodes_trimmed = []
for old_idx, node in enumerate(new_nodes):
if output_used[old_idx]:
new_idx = len(new_nodes_trimmed)
new_nodes_trimmed.append(node)
old_idx_to_new_idx[old_idx] = new_idx
# Finally go through the list of trimmed nodes and change the inputs
for node in new_nodes_trimmed:
new_inputs = []
for old_idx in node['inputs']:
new_inputs.append(old_idx_to_new_idx[old_idx])
node['inputs'] = new_inputs
return new_nodes_trimmed
def is_degenerate(question, metadata, scene_struct, answer=None, verbose=False):
"""
A question is degenerate if replacing any of its relate nodes with a scene
node results in a question with the same answer.
"""
if answer is None:
answer = answer_question(question, metadata, scene_struct)
for idx, node in enumerate(question['nodes']):
if node['type'] == 'relate':
new_question = {
'nodes': insert_scene_node(question['nodes'], idx)
}
new_answer = answer_question(new_question, metadata, scene_struct)
if verbose:
print('here is truncated question:')
for i, n in enumerate(new_question['nodes']):
name = n['type']
if 'side_inputs' in n:
name = '%s[%s]' % (name, n['side_inputs'][0])
print(i, name, n['_output'])
print('new answer is: ', new_answer)
if new_answer == answer:
return True
return False