-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraints_splitA.py
1055 lines (905 loc) · 38.6 KB
/
constraints_splitA.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
author: Adnen Abdessaied
maintainer: "Adnen Abdessaied"
website: adnenabdessaied.de
version: 1.0.1
"""
# --------------------------------------------------------
# adapted from https://github.com/satwikkottur/clevr-dialog/blob/master/constraints.py
# --------------------------------------------------------
import copy
import json
import random
import numpy as np
import global_vars as gvars
# Some quick methods.
def apply_immediate(hist): return (len(hist['objects']) == 1 and
hist['mergeable'] and
'exist' not in hist['template'])
def apply_group(hist): return (len(hist['objects']) >= 2 and
hist['mergeable'] and
'count' not in prev_group)
def caption(scene, templates):
"""Constraints for caption generation.
Args:
scene: CLEVR Scene graphs to generate captions with constraints
template: List of caption templates
Returns:
sample_captions: Samples from caption hypotheses
"""
caption_hypotheses = {}
# Sweep through all templates to extract 'interesting' captions.
n_objs = len(scene['objects'])
rels = scene['relationships']
# Caption Type 1: Extreme locations.
ext_loc_templates = [ii for ii in templates if ii['type'] == 'extreme-loc']
# number of objects in the scene
filter_objs = copy.deepcopy(scene['objects'])
attr_counts = get_attribute_counts_for_objects(scene, filter_objs)
hypotheses = []
for template in ext_loc_templates:
# absolute location based constraint
constraint = template['constraints'][0]
extreme_type = constraint['args'][0]
# check if there is an object that is at the center of the image
# roughly in the middle along front-back and right-left dim
if extreme_type == 'center':
for ii, obj in enumerate(filter_objs):
bla = [len(rels[kk][ii]) <= n_objs / 2
for kk in ['front', 'behind', 'right', 'left']]
matches = np.sum([len(rels[kk][ii]) <= n_objs / 2
for kk in ['front', 'behind', 'right', 'left']])
if matches == 4:
hypotheses.append((extreme_type, copy.deepcopy(obj)))
else:
for ii, obj in enumerate(filter_objs):
if len(rels[extreme_type][ii]) == 0:
hypotheses.append((extreme_type, copy.deepcopy(obj)))
# sample one at random, and create the graph item
# Filter hypothesis which are ambiguous otherwise.
for index, (_, hypothesis) in enumerate(hypotheses):
uniq_attr = [attr for attr in gvars.METAINFO['attributes']
if attr_counts[(attr, hypothesis[attr])] == 1]
for attr in uniq_attr:
del hypotheses[index][1][attr]
hypotheses = [ii for ii in hypotheses if len(ii[1]) > 1]
caption_hypotheses['extreme-loc'] = hypotheses
# Caption Type 2: Unique object and attribute.
filter_objs = copy.deepcopy(scene['objects'])
# each hypothesis is (object, attribute) pair
hypotheses = []
for ii, obj in enumerate(filter_objs):
# get unique set of attributes
uniq_attrs = [ii for ii in gvars.METAINFO['attributes']
if attr_counts[(ii, obj[ii])] == 1]
# for each, add it to hypothesis
for attr in uniq_attrs:
hypotheses.append((obj, attr))
caption_hypotheses['unique-obj'] = hypotheses
# Caption Type 3: Unique attribute count based caption.
# count unique object based constraint
# Each hypothesis is object collection.
caption_hypotheses['count-attr'] = [(attr_val, count)
for attr_val, count in attr_counts.items()
if count > 1]
# Caption Type 4: Relation between two objects.
# Out of the two, one has a unique attribute.
# find a pair of objects sharing a relation, unique
# filter_objs = copy.deepcopy(scene['objects'])
# n_objs = len(filter_objs)
# # get a dict of unique attributes for each object
# uniq_attr = [[] for ii in range(n_objs)]
# non_uniq_attr = [[] for ii in range(n_objs)]
# for ind, obj in enumerate(filter_objs):
# uniq_attr[ind] = [attr for attr in gvars.METAINFO['attributes']
# if attr_counts[(attr, obj[attr])] == 1]
# non_uniq_attr[ind] = [attr for attr in gvars.METAINFO['attributes']
# if attr_counts[(attr, obj[attr])] > 1]
# uniqueness = [len(ii) > 0 for ii in uniq_attr]
# # Hypothesis is a uniq object and non-unique obj2 sharing relation R
# # global ordering for uniqueness
# hypotheses = []
# for rel, order in scene['relationships'].items():
# num_rel = [(ii, len(order[ii])) for ii in range(n_objs)]
# num_rel = sorted(num_rel, key=lambda x: x[1], reverse=True)
# # take only the ids
# num_rel = [ii[0] for ii in num_rel]
# for index, obj_id in enumerate(num_rel[:-1]):
# next_obj_id = num_rel[index + 1]
# # if unique, check if the next one has non-unique attributes
# if uniqueness[obj_id]:
# if len(non_uniq_attr[next_obj_id]) > 0:
# obj1 = (obj_id, random.choice(uniq_attr[obj_id]))
# obj2 = (next_obj_id, random.choice(non_uniq_attr[next_obj_id]))
# hypotheses.append((obj1, rel, obj2))
# # if not unique, check if the next one has unique attributes
# else:
# if len(uniq_attr[next_obj_id]) > 0:
# obj1 = (obj_id, random.choice(non_uniq_attr[obj_id]))
# obj2 = (next_obj_id, random.choice(uniq_attr[next_obj_id]))
# hypotheses.append((obj1, rel, obj2))
# caption_hypotheses['obj-relation'] = hypotheses
sample_captions = sample_from_hypotheses(
caption_hypotheses, scene, templates)
return sample_captions
def question(scene, dialog, template):
"""Constraints question generation.
Inputs:
scene:Partial scene graphs on CLEVR images with generated captions
template: List of question templates to use
Output:
list of object groups
"""
ques_round = len(dialog['graph']['history']) - 1
graph = dialog['graph']
# check for constraints and answer question
if 'group' in template['label']:
groups = []
# Pick a group hypothesis
for ii in graph['history']:
if 'count' in ii or len(ii['objects']) == 0:
groups.append(ii)
if template['label'] == 'count-all':
# Preliminary checks:
# (A) count-all cannot follow count-all, count-other
for prev_history in graph['history'][1:]:
if prev_history['template'] in ['count-all', 'count-other']:
return []
# create object group
obj_group = []
new_obj = {'required': [], 'optional': []}
for obj_id, ii in enumerate(scene['objects']):
obj_copy = copy.deepcopy(new_obj)
obj_copy['id'] = ii['id']
obj_group.append(obj_copy)
# create graph item
graph_item = {'round': ques_round + 1,
'objects': copy.deepcopy(obj_group),
'template': template['label'],
'mergeable': True, 'count': len(obj_group)}
# clean graph item
graph_item = clean_graph_item(graph_item)
# no constraints, count the number of objects in true scene
return [{'answer': len(obj_group), 'group_id': ques_round + 1,
'objects': [], 'graph': graph_item}]
elif (template['label'] == 'count-other' or
template['label'] == 'exist-other'):
# preliminary checks:
# (A) exist-other cannot follow exist-other, count-all, count-other
# (B) count-other cannot follow count-all, count-other
for prev_history in graph['history'][1:]:
if prev_history['template'] in ['count-all', 'count-other']:
return []
if (prev_history['template'] == 'exist-other' and
template['label'] == 'exist-other'):
return []
# get a list of all objects we know
known_ids = [jj['id'] for ii in graph['history'] for jj in ii['objects']]
known_ids = list(set(known_ids))
n_objs = len(scene['objects'])
difference = n_objs - len(known_ids)
diff_ids = [ii for ii in range(n_objs) if ii not in known_ids]
# create empty objects for these
obj_group = [{'id': ii} for ii in diff_ids]
# create graph item
graph_item = {'round': ques_round + 1, 'objects': obj_group,
'template': template['label'], 'mergeable': False}
if 'count' in template['label']:
graph_item['count'] = difference
graph_item['mergeable'] = True # merge if count is known
answer = difference
elif 'exist' in template['label']:
# If heads (> 0.5) -- difference > 0
if random.random() > 0.5:
if difference > 0:
answer = 'yes'
else:
return []
else:
if difference == 0:
answer = 'no'
else:
return []
# no constraints, count the number of objects in true scene
return [{'answer': answer, 'group_id': ques_round + 1,
'objects': [], 'graph': graph_item}]
elif template['label'] == 'count-all-group':
# we need a group in the previous round
prev_group = graph['history'][-1]
prev_label = prev_group['template']
if not (len(prev_group['objects']) > 1 and
'count' not in prev_group and
'obj-relation' not in prev_label):
return []
# check if count is not given before
attrs = [ii for ii in gvars.METAINFO['attributes'] if ii in prev_group]
count = 0
for obj in prev_group['objects']:
count += all([obj[ii] == prev_group['objects'][0][ii] for ii in attrs])
# create object group
obj_group = []
new_obj = {'required': [], 'optional': []}
for obj_id, ii in enumerate(scene['objects']):
obj_copy = copy.deepcopy(new_obj)
obj_copy['id'] = ii['id']
obj_group.append(obj_copy)
# create graph item
graph_item = {'round': ques_round + 1, 'objects': copy.deepcopy(obj_group),
'template': template['label'],
'mergeable': True, 'count': count}
# clean graph item
graph_item = clean_graph_item(graph_item)
# no constraints, count the number of objects in true scene
return [{'answer': count, 'group_id': ques_round + 1,
'objects': [], 'graph': graph_item}]
elif ('count-obj-exclude' in template['label'] or
'exist-obj-exclude' in template['label']):
# placeholder for object description, see below
obj_desc = None
prev_history = graph['history'][-1]
scene_counts = get_attribute_counts_for_objects(scene)
if 'imm' in template['label']:
# we need an immediate group in the previous round
if apply_immediate(prev_history):
focus_id = prev_history['objects'][0]['id']
else:
return []
elif 'early' in template['label']:
# search through history for an object with unique attribute
attr_counts = get_known_attribute_counts(graph)
# get attributes with just one count
single_count = [ii for ii, count in attr_counts.items() if count == 1]
# remove attributes that point to objects in the previous round
# TODO: re-think this again
obj_ids = get_unique_attribute_objects(graph, single_count)
prev_history_obj_ids = [ii['id'] for ii in prev_history['objects']]
single_count = [ii for ii in single_count if
obj_ids[ii] not in prev_history_obj_ids]
if len(single_count) == 0:
return []
# give preference to attributes with multiple counts in scene graph
#scene_counts = get_attribute_counts_for_objects(scene)
ambiguous_attrs = [ii for ii in single_count if scene_counts[ii] > 1]
if len(ambiguous_attrs) > 0:
focus_attr = random.choice(ambiguous_attrs)
else:
focus_attr = random.choice(single_count)
focus_id = obj_ids[focus_attr]
# unique object description
obj_desc = {'required': [focus_attr[0]], 'optional': [],
focus_attr[0]: focus_attr[1]}
# get the known attributes for the current object
focus_obj = graph['objects'][focus_id]
known_attrs = [attr for attr in gvars.METAINFO['attributes']
if attr in focus_obj and
'%s_exclude_count' % attr not in focus_obj]
# for count: only if existence if True, else count it trivially zero
if 'count' in template['label']:
for attr in known_attrs[::-1]:
if not focus_obj.get('%s_exclude_exist' % attr, True):
known_attrs.remove(attr)
# for exist: get relations without exist before
elif 'exist' in template['label']:
known_attrs = [attr for attr in known_attrs
if '%s_exclude_exist' % attr not in focus_obj]
# select an attribute
if len(known_attrs) == 0:
return[]
# split this into zero and non-zero
if 'exist' in template['label']:
focus_attrs = [(ii, scene['objects'][focus_id][ii])
for ii in known_attrs]
zero_count = [ii for ii in focus_attrs if scene_counts[ii] == 1]
nonzero_count = [ii for ii in focus_attrs if scene_counts[ii] > 1]
if random.random() > 0.5:
if len(zero_count) > 0:
attr = random.choice(zero_count)[0]
else:
return []
else:
if len(nonzero_count) > 0:
attr = random.choice(nonzero_count)[0]
else:
return []
else:
attr = random.choice(known_attrs)
# create the object group
obj_group = []
new_obj = {'required': ['attribute'], 'optional': []}
for obj in scene['objects']:
# add if same attribute value and not focus object
if obj[attr] == focus_obj[attr] and obj['id'] != focus_id:
obj_copy = copy.deepcopy(new_obj)
obj_copy['id'] = obj['id']
obj_copy[attr] = focus_obj[attr]
obj_group.append(obj_copy)
answer = len(obj_group)
ref_obj = copy.deepcopy(new_obj)
ref_obj['id'] = focus_id
ref_obj['volatile'] = True
if 'exist' in template['label']:
answer = 'yes' if answer > 0 else 'no'
ref_obj['%s_exclude_exist' % attr] = answer
elif 'count' in template['label']:
ref_obj['%s_exclude_count' % attr] = answer
obj_group.append(ref_obj)
graph_item = {'round': ques_round+1, 'objects': copy.deepcopy(obj_group),
'template': template['label'], 'mergeable': True,
'focus_id': focus_id, 'focus_desc': obj_desc}
if 'count' in template['label']:
graph_item['count'] = answer
graph_item = clean_graph_item(graph_item)
ref_obj['attribute'] = attr
return [{'answer': answer, 'group_id': ques_round + 1,
'required': [], 'optional': [],
'objects': [ref_obj, obj_desc], 'graph': graph_item}]
elif ('count-obj-rel' in template['label'] or
'exist-obj-rel' in template['label']):
# placeholder for object description, see below
obj_desc = None
prev_history = graph['history'][-1]
# we need a single object in the previous round
if 'imm2' in template['label']:
# we need a obj-rel-imm in previous label, same as the current one
prev_label = prev_history['template']
cur_label = template['label']
if 'obj-rel-imm' not in prev_label or cur_label[:5] != prev_label[:5]:
return []
else:
focus_id = prev_history['focus_id']
elif 'imm' in template['label']:
# we need an immediate group in the previous round
if apply_immediate(prev_history):
focus_id = prev_history['objects'][0]['id']
else:
return []
elif 'early' in template['label']:
# search through history for an object with unique attribute
attr_counts = get_known_attribute_counts(graph)
# get attributes with just one count
single_count = [ii for ii, count in attr_counts.items() if count == 1]
# remove attributes that point to objects in the previous round
# TODO: re-think this again
obj_ids = get_unique_attribute_objects(graph, single_count)
prev_history_obj_ids = [ii['id'] for ii in prev_history['objects']]
single_count = [ii for ii in single_count if
obj_ids[ii] not in prev_history_obj_ids]
if len(single_count) == 0:
return []
focus_attr = random.choice(single_count)
for focus_id, obj in graph['objects'].items():
if obj.get(focus_attr[0], None) == focus_attr[1]:
break
# unique object description
obj_desc = {'required': [focus_attr[0]], 'optional': [],
focus_attr[0]: focus_attr[1]}
# get relations with unknown counts
unknown_rels = [rel for rel in gvars.METAINFO['relations']
if '%s_count' % rel not in graph['objects'][focus_id]]
# for count: only if existence if True, else count it trivially zero
if 'count' in template['label']:
for ii in unknown_rels[::-1]:
if not graph['objects'][focus_id].get('%s_exist' % ii, True):
unknown_rels.remove(ii)
# for exist: get relations without exist before
elif 'exist' in template['label']:
unknown_rels = [rel for rel in unknown_rels
if '%s_exist' % rel not in graph['objects'][focus_id]]
# select an object with some known objects
if len(unknown_rels) == 0:
return []
# pick between yes/no for exist questions, 50% of times
if 'exist' in template['label']:
zero_count = [ii for ii in unknown_rels
if len(scene['relationships'][ii][focus_id]) == 0]
nonzero_count = [ii for ii in unknown_rels
if len(scene['relationships'][ii][focus_id]) > 0]
if random.random() > 0.5:
if len(zero_count) > 0:
rel = random.choice(zero_count)
else:
return []
else:
if len(nonzero_count) > 0:
rel = random.choice(nonzero_count)
else:
return []
else:
rel = random.choice(unknown_rels)
# create the object group
obj_group = []
new_obj = {'required': ['relation'], 'optional': []}
obj_pool = scene['relationships'][rel][focus_id]
for obj_id in obj_pool:
obj_copy = copy.deepcopy(new_obj)
obj_copy['id'] = obj_id
obj_group.append(obj_copy)
answer = len(obj_pool)
ref_obj = copy.deepcopy(new_obj)
ref_obj['id'] = focus_id
ref_obj['volatile'] = True
if 'exist' in template['label']:
answer = 'yes' if answer > 0 else 'no'
ref_obj['%s_exist' % rel] = answer
elif 'count' in template['label']:
ref_obj['%s_count' % rel] = answer
obj_group.append(ref_obj)
graph_item = {'round': ques_round+1, 'objects': copy.deepcopy(obj_group),
'template': template['label'], 'mergeable': True,
'focus_id': focus_id, 'focus_desc': obj_desc}
if 'count' in template['label']:
graph_item['count'] = answer
graph_item = clean_graph_item(graph_item)
#ref_obj['relation'] = rel
# add attribute as argument
return [{'answer': answer, 'group_id': ques_round + 1,
'required': [], 'optional': [], 'relation': rel,
'objects': [ref_obj, obj_desc], 'graph': graph_item}]
elif ('count-attribute' in template['label'] or
'exist-attribute' in template['label']):
if 'group' in template['label']:
# we need an immediate group in the previous round
prev_history = graph['history'][-1]
prev_label = prev_history['template']
# if exist: > 0 is good, else > 1 is needed
min_count = 0 if 'exist' in prev_label else 1
if (len(prev_history['objects']) > min_count and
prev_history['mergeable'] and
'obj-relation' not in prev_label):
obj_pool = graph['history'][-1]['objects']
else:
return []
else:
obj_pool = scene['objects']
# get counts for attributes, and sample evenly with 0 and other numbers
counts = get_attribute_counts_for_objects(scene, obj_pool)
# if exist, choose between zero and others wiht 0.5 probability
zero_prob = 0.5 if 'exist' in template['label'] else 0.7
if random.random() > zero_prob:
pool = [ii for ii in counts if counts[ii] == 0]
else:
pool = [ii for ii in counts if counts[ii] != 0]
# check if count is already known
attr_pool = filter_attributes_with_known_counts(graph, pool)
# for exist: get known attributes and remove them
if 'exist' in template['label']:
known_attr = get_known_attributes(graph)
attr_pool = [ii for ii in attr_pool if ii not in known_attr]
# if non-empty, sample it
if len(attr_pool) == 0:
return []
attr, value = random.choice(attr_pool)
# add a hypothesi, and return the answer
count = 0
obj_group = []
new_obj = {attr: value, 'required': [attr], 'optional': []}
for index, obj in enumerate(obj_pool):
if scene['objects'][obj['id']][attr] == value:
obj_copy = copy.deepcopy(new_obj)
obj_copy['id'] = obj['id']
obj_group.append(obj_copy)
count += 1
graph_item = {'round': ques_round + 1, 'objects': copy.deepcopy(obj_group),
'template': template['label'], 'mergeable': True, attr: value}
if 'count' in template['label']:
graph_item['count'] = count
answer = count
elif 'exist' in template['label']:
answer = 'yes' if count > 0 else 'no'
# Clean graph item.
graph_item = clean_graph_item(graph_item)
if count == 0:
# Fake object group, to serve for arguments.
obj_group = [{attr: value, 'required': [attr], 'optional': []}]
return [{'answer': answer, 'group_id': ques_round + 1,
'required': [attr], 'optional': [],
'count': 9999, 'objects': obj_group, 'graph': graph_item}]
elif 'seek-attr-rel' in template['label']:
# Placeholder for object description, see below.
obj_desc = None
prev_history = graph['history'][-1]
if 'imm' in template['label']:
# we need an immediate group in the previous round
if apply_immediate(prev_history):
focus_id = prev_history['objects'][0]['id']
else:
return []
elif 'early' in template['label']:
# search through history for an object with unique attribute
attr_counts = get_known_attribute_counts(graph)
# get attributes with just one count
single_count = [ii for ii, count in attr_counts.items() if count == 1]
# remove attributes that point to objects in the previous round
# TODO: re-think this again
obj_ids = get_unique_attribute_objects(graph, single_count)
prev_history_obj_ids = [ii['id'] for ii in prev_history['objects']]
single_count = [ii for ii in single_count if
obj_ids[ii] not in prev_history_obj_ids]
if len(single_count) == 0:
return []
# give preference to attributes with multiple counts in scene graph
scene_counts = get_attribute_counts_for_objects(scene)
ambiguous_attrs = [ii for ii in single_count if scene_counts[ii] > 1]
if len(ambiguous_attrs) > 0:
focus_attr = random.choice(ambiguous_attrs)
else:
focus_attr = random.choice(single_count)
focus_id = obj_ids[focus_attr]
# unique object description
obj_desc = {'required': [focus_attr[0]], 'optional': [],
focus_attr[0]: focus_attr[1]}
# for each relation, get the object, sample an attribute, and sample
hypotheses = []
for rel in gvars.METAINFO['relations']:
gt_relations = scene['relationships'][rel]
objs = [(ii, len(gt_relations[ii])) for ii in gt_relations[focus_id]]
objs = sorted(objs, key=lambda x: x[1], reverse=True)
if len(objs) == 0:
# add a null hypotheses
# check if the object is known to be extreme
if ('%s_count' % rel not in graph['objects'][focus_id] and
'%s_exist' % rel not in graph['objects'][focus_id]):
random_attr = random.choice(gvars.METAINFO['attributes'])
hypotheses.append((None, rel, random_attr))
continue
closest_obj = objs[0][0]
# check what attributes are known/unknown
known_info = graph['objects'].get(closest_obj, {})
for attr in gvars.METAINFO['attributes']:
if attr not in known_info:
hypotheses.append((closest_obj, rel, attr))
if len(hypotheses) == 0:
return []
sample_id, rel, attr = random.choice(hypotheses)
# add the new attribute to object
new_obj = {'required': ['attribute', 'relation'],
'optional': [], 'id': sample_id}
if sample_id is not None:
answer = scene['objects'][sample_id][attr]
else:
answer = 'none'
new_obj[attr] = answer
graph_item = {'round': ques_round+1, 'objects': [copy.deepcopy(new_obj)],
'template': template['label'], 'mergeable': True,
'focus_id': focus_id, 'focus_desc': obj_desc}
# remove objects if none
if sample_id is None:
graph_item['objects'] = []
graph_item = clean_graph_item(graph_item)
# Add attribute as argument.
new_obj['attribute'] = attr
return [{'answer': new_obj[attr], 'group_id': ques_round + 1,
'required': [], 'optional': [], 'relation': rel,
'objects': [new_obj, obj_desc], 'graph': graph_item}]
elif 'seek-attr' in template['label']:
# placeholder for object description, see below
obj_desc = None
prev_history = graph['history'][-1]
prev_label = prev_history['template']
implicit_attr = None
# we need a single object in the previous round
if 'imm2' in template['label']:
# we need a seek-attr-imm/seek-attr-rel-imm in previous label
if ('seek-attr-imm' not in prev_label and
'seek-attr-rel-imm' not in prev_label):
return []
elif len(prev_history['objects']) == 0:
return []
else:
focus_id = prev_history['objects'][0]['id']
elif 'imm' in template['label']:
# we need an immediate group in the previous round
if apply_immediate(prev_history):
focus_id = prev_history['objects'][0]['id']
else:
return []
elif 'sim' in template['label']:
if 'seek-attr-imm' not in prev_label:
return[]
else:
prev_obj = prev_history['objects'][0]
focus_id = prev_obj['id']
attr = [ii for ii in gvars.METAINFO['attributes'] if ii in prev_obj]
assert len(attr) == 1, 'Something wrong in previous history!'
implicit_attr = attr[0]
if 'early' in template['label']:
# search through history for an object with unique attribute
attr_counts = get_known_attribute_counts(graph)
# get attributes with just one count
single_count = [ii for ii, count in attr_counts.items() if count == 1]
# remove attributes that point to objects in the previous round
# TODO: re-think this again
obj_ids = get_unique_attribute_objects(graph, single_count)
prev_history_obj_ids = [ii['id'] for ii in prev_history['objects']]
single_count = [ii for ii in single_count if
obj_ids[ii] not in prev_history_obj_ids]
# if there is an attribute, eliminate those options
if implicit_attr is not None:
single_count = [ii for ii in single_count if ii[0] != implicit_attr]
obj_ids = get_unique_attribute_objects(graph, single_count)
# again rule out objects whose implicit_attr is known
single_count = [ii for ii in single_count
if implicit_attr not in graph['objects'][obj_ids[ii]]]
if len(single_count) == 0:
return []
# give preference to attributes with multiple counts in scene graph
scene_counts = get_attribute_counts_for_objects(scene)
ambiguous_attrs = [ii for ii in single_count if scene_counts[ii] > 1]
if len(ambiguous_attrs) > 0:
focus_attr = random.choice(ambiguous_attrs)
else:
focus_attr = random.choice(single_count)
focus_id = get_unique_attribute_objects(graph, [focus_attr])[focus_attr]
# unique object description
obj_desc = {'required': [focus_attr[0]], 'optional': [],
focus_attr[0]: focus_attr[1]}
# get unknown attributes, randomly sample one
if implicit_attr is None:
unknown_attrs = [attr for attr in gvars.METAINFO['attributes']
if attr not in graph['objects'][focus_id]]
# TODO: select an object with some known objects
if len(unknown_attrs) == 0:
return []
attr = random.choice(unknown_attrs)
else:
attr = implicit_attr
# add the new attribute to object
new_obj = {'required': ['attribute'], 'optional': [], 'id': focus_id}
if 'sim' in template['label']:
new_obj['required'] = []
new_obj[attr] = scene['objects'][focus_id][attr]
graph_item = {'round': ques_round+1, 'objects': [copy.deepcopy(new_obj)],
'template': template['label'], 'mergeable': True,
'focus_id': focus_id, 'focus_desc': obj_desc}
graph_item = clean_graph_item(graph_item)
# add attribute as argument
new_obj['attribute'] = attr
return [{'answer': new_obj[attr], 'group_id': ques_round + 1,
'required': [], 'optional': [],
'objects': [new_obj, obj_desc], 'graph': graph_item}]
return []
def sample_from_hypotheses(caption_hypotheses, scene, cap_templates):
"""Samples from caption hypotheses given the scene and caption templates.
Args:
caption_hypotheses: List of hypotheses for objects/object pairs
scene: CLEVR image scene graph
cap_templates: List of caption templates to sample captions
Returns:
obj_groups: List of object groups and corresponding sampled captions
"""
obj_groups = []
# Caption Type 1: Extreme location.
hypotheses = caption_hypotheses['extreme-loc']
if len(hypotheses) > 0:
# extreme location hypotheses
extreme_type, focus_obj = random.choice(hypotheses)
# sample optional attributes
obj_attrs = [attr for attr in gvars.METAINFO['attributes']
if attr in focus_obj]
focus_attr = random.choice(obj_attrs)
optional_attrs = [ii for ii in obj_attrs if ii != focus_attr]
sampled_attrs = sample_optional_tags(optional_attrs,
gvars.METAINFO['probabilities'])
# add additional attributes
req_attrs = sampled_attrs + [focus_attr]
filter_obj = {attr: val for attr, val in focus_obj.items()
if attr in req_attrs}
filter_obj['required'] = req_attrs
filter_obj['optional'] = req_attrs
filter_obj['id'] = focus_obj['id']
obj_group = {'required': req_attrs, 'optional': [], 'group_id': 0,
'objects': [filter_obj]}
# also create a clean graph object
graph_item = copy.deepcopy(obj_group)
graph_item = clean_graph_item(graph_item)
graph_item['mergeable'] = True
graph_item['objects'][0]['%s_count' % extreme_type] = 0
graph_item['objects'][0]['%s_exist' % extreme_type] = False
graph_item['template'] = 'extreme-%s' % extreme_type
obj_group['graph'] = graph_item
obj_groups.append([obj_group])
# Caption Type 2: Unique object.
hypotheses = caption_hypotheses['unique-obj']
if len(hypotheses) > 0:
# sample one at random, and create the graph item
focus_obj, focus_attr = random.choice(hypotheses)
# sample optional attributes
optional_attrs = [ii for ii in gvars.METAINFO['attributes']
if ii != focus_attr]
sampled_attrs = sample_optional_tags(optional_attrs,
gvars.METAINFO['probabilities'])
# add additional attributes
req_attrs = sampled_attrs + [focus_attr]
filter_obj = {attr: val for attr, val in focus_obj.items()
if attr in req_attrs}
filter_obj['required'] = req_attrs
filter_obj['optional'] = req_attrs
filter_obj['id'] = focus_obj['id']
obj_group = {'required': req_attrs, 'optional': [], 'group_id': 0,
'objects': [filter_obj]}
# also create a clean graph object
graph_item = copy.deepcopy(obj_group)
graph_item = clean_graph_item(graph_item)
graph_item['mergeable'] = True
graph_item['objects'][0]['unique'] = True
graph_item['template'] = 'unique-obj'
obj_group['graph'] = graph_item
obj_groups.append([obj_group])
# Caption Type 3: Unique attribute count based caption.
hypotheses = caption_hypotheses['count-attr']
if len(hypotheses) > 0:
# Randomly sample one hypothesis and one template.
(attr, value), count = random.choice(hypotheses)
# Segregate counting templates.
count_templates = [ii for ii in cap_templates if 'count' in ii['type']]
template = random.choice(count_templates)
obj_group = {'group_id': 0, 'count': count, attr: value,
'optional': [], 'required': [], 'objects': []}
# get a list of objects which are part of this collection
for ii, obj in enumerate(scene['objects']):
if obj[attr] == value:
new_obj = {'id': obj['id'], attr: value}
new_obj['required'] = [attr]
new_obj['optional'] = []
obj_group['objects'].append(new_obj)
if 'no' in template['label']:
# Count is not mentioned.
del obj_group['count']
graph_item = copy.deepcopy(obj_group)
graph_item['mergeable'] = False
else:
# Count is mentioned.
for index, ii in enumerate(obj_group['objects']):
obj_group['objects'][index]['required'].append('count')
graph_item = copy.deepcopy(obj_group)
graph_item['mergeable'] = True
# clean up graph item
graph_item['template'] = template['label']
graph_item = clean_graph_item(graph_item)
obj_group['graph'] = graph_item
obj_group['use_plural'] = True
obj_groups.append([obj_group])
# Caption Type 4: Relation between two objects (one of them is unique).
# hypotheses = caption_hypotheses['obj-relation']
# if len(hypotheses) > 0:
# (obj_id1, attr1), rel, (obj_id2, attr2) = random.choice(hypotheses)
# obj_group = {'group_id': 0, 'relation': rel}
# # create object dictionaries
# obj1 = {'optional': [], 'required': [attr1], 'id': obj_id1,
# attr1: scene['objects'][obj_id1][attr1]}
# obj2 = {'optional': [], 'required': [attr2], 'id': obj_id2,
# attr2: scene['objects'][obj_id2][attr2]}
# obj_group['objects'] = [obj2, obj1]
# # also create a clean graph object
# graph_item = copy.deepcopy(obj_group)
# graph_item = clean_graph_item(graph_item)
# graph_item['mergeable'] = True
# graph_item['template'] = 'obj-relation'
# obj_group['graph'] = graph_item
# obj_groups.append([obj_group])
return obj_groups
def get_known_attributes(graph):
"""Fetches a list of known attributes given the scene graph.
Args:
graph: Scene graph to check unique attributes from
Returns:
known_attrs: List of known attributes from the scene graph
"""
known_attrs = []
for obj_id, obj_info in graph['objects'].items():
# The attribute is unique already.
# if obj_info.get('unique', False): continue
for attr in gvars.METAINFO['attributes']:
if attr in obj_info:
known_attrs.append((attr, obj_info[attr]))
# also go over the groups
for ii in graph['history']:
# a group of objects, with unknown count
#if 'count' not in ii: continue
for attr in gvars.METAINFO['attributes']:
if attr in ii:
known_attrs.append((attr, ii[attr]))
known_attrs = list(set(known_attrs))
return known_attrs
def get_known_attribute_counts(graph):
"""Fetches a count of known attributes given the scene graph.
Calls get_known_attributes method internally.
Args:
graph: Scene graph to check unique attributes from
Returns:
counts: Count of known attributes from the scene graph
"""
known_attrs = get_known_attributes(graph)
# Go through objects and count.
counts = {ii: 0 for ii in known_attrs}
for _, obj in graph['objects'].items():
for attr, val in known_attrs:
if obj.get(attr, None) == val:
counts[(attr, val)] += 1
return counts
def filter_attributes_with_known_counts(graph, known_attrs):
"""Filters attributes whose counts are known, given the scene graph.
Args:
graph: Scene graph from the dialog generated so far
known_attrs: List of known attributes from the ground truth scene graph
Returns:
known_attrs: List of attributes with unknown counts removed inplace
"""
for attr, val in known_attrs[::-1]:
for ii in graph['history']:
# A group of objects, with unknown count.
if 'count' not in ii:
continue
# Count is absent.
if ii.get(attr, None) == val:
known_attrs.remove((attr, val))
return known_attrs
def clean_graph_item(graph_item):
"""Cleans up graph item (remove 'required' and 'optional' tags).
Args:
graph_item: Input graph item to be cleaned.
Returns:
clean_graph_item: Copy of the graph item after cleaning.
"""
clean_graph_item = copy.deepcopy(graph_item)
if 'optional' in clean_graph_item:
del clean_graph_item['optional']
if 'required' in clean_graph_item:
del clean_graph_item['required']
for index, ii in enumerate(clean_graph_item['objects']):
if 'optional' in ii:
del clean_graph_item['objects'][index]['optional']
if 'required' in ii:
del clean_graph_item['objects'][index]['required']
return clean_graph_item
def get_attribute_counts_for_objects(scene, objects=None):
"""Counts attributes for a given set of objects.
Args:
scene: Scene graph for the dialog generated so far