This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
exp28_scenenn.py
177 lines (127 loc) · 4.57 KB
/
exp28_scenenn.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
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
"""
Scene-level nearest-neighbor teller
"""
from interactivity import INTERACTIVE, try_magic, try_cd
try_cd('~/dev/drawmodel/nkcodraw')
#%%
import numpy as np
from pathlib import Path
import codraw_data
from codraw_data import AbstractScene, Clipart
import abs_render
from abs_metric import scene_similarity, clipart_similarity
from episode import Episode, Transcriber, respond_to
import model
from model import make_fns, eval_fns
from model import Model
from baseline2_models import load_baseline2
# %%
scenes_and_scripts_dev = codraw_data.get_scenes_and_scripts('dev')
transcribe = Transcriber(
'exp28_scenenn.py' if INTERACTIVE else __file__,
scenes_and_scripts=scenes_and_scripts_dev[::110],
scenes_description="scenes_and_scripts_dev[::110]")
# %%
models_baseline2 = load_baseline2()
# %%
drawer_lstmaddonly_a = models_baseline2['drawer_lstmaddonly_a']
drawer_lstmaddonly_b = models_baseline2['drawer_lstmaddonly_b']
# %%
from datagen import Datagen
class SceneNearestNeighborData(Datagen):
def init_full(self):
self.build_dicts()
def init_from_spec(self):
self.build_dicts()
def build_dicts(self):
self.scene_to_msgs = {}
# calculate events
events = codraw_data.get_contextual_place_many(self.split)
scene = None
msgs = None
it = iter(events)
for event in it:
if isinstance(event, codraw_data.ObserveTruth):
if scene is not None and msgs is not None:
self.scene_to_msgs[tuple(scene)] = msgs
scene = event.scene
msgs = []
elif isinstance(event, codraw_data.TellGroup):
msgs.append(event.msg)
if scene is not None and msgs is not None:
self.scene_to_msgs[tuple(scene)] = msgs
# %%
class SceneNearestNeighborTeller(Model):
datagen_cls = SceneNearestNeighborData
def prepare(self, episode):
scene = episode.get_last(codraw_data.ObserveTruth).scene
best_similarity = -1
best_msgs = []
best_scene_tuple = None
for cand_scene_tuple in self.datagen.scene_to_msgs:
cand_sim = scene_similarity(cand_scene_tuple, scene)
if cand_sim > best_similarity:
best_similarity = cand_sim
best_msgs = self.datagen.scene_to_msgs[cand_scene_tuple]
best_scene_tuple = cand_scene_tuple
# display(AbstractScene(scene))
# display(AbstractScene(best_scene_tuple))
# display(best_similarity)
episode.to_tell = best_msgs[::] # make a copy!
@respond_to(codraw_data.ObserveTruth)
@respond_to(codraw_data.ReplyGroup)
def tell(self, episode):
if not hasattr(episode, 'to_tell'):
self.prepare(episode)
if episode.to_tell:
msg = episode.to_tell.pop(0)
episode.append(codraw_data.TellGroup(msg))
def get_action_fns(self):
return [self.tell]
# %%
data_scenenn_a = SceneNearestNeighborData('a')
data_scenenn_b = SceneNearestNeighborData('b')
# %%
teller_scenenn_a = SceneNearestNeighborTeller(data_scenenn_a)
teller_scenenn_b = SceneNearestNeighborTeller(data_scenenn_b)
# %%
# Episode.run(codraw_data.get_scenes('dev')[0], make_fns('aa', (teller_scenenn_a, teller_scenenn_b), (drawer_lstmaddonly_a, drawer_lstmaddonly_b))).display()
# %%
# %%
# %%
print()
print()
print("Final evaluation on full dev set")
# %%
for splits in ('aa', 'ab', 'ba', 'bb'):
sims = eval_fns(make_fns(splits, (teller_scenenn_a, teller_scenenn_b), (drawer_lstmaddonly_a, drawer_lstmaddonly_b)), limit=None)
print(splits, sims.mean())
# aa 1.3095491909624886
# ab 1.3115692170881366
# nohier aa 2.229799264350204
# nohier ab 2.255167911899865
# %%
for splits in ('ba', 'bb'):
sims = eval_fns(make_fns(splits, (teller_scenenn_a, teller_scenenn_b), (drawer_lstmaddonly_a, drawer_lstmaddonly_b)), limit=None)
print(splits, sims.mean())
# %%
transcribe("exp28_scenenn",
aa=make_fns('aa', (teller_scenenn_a, teller_scenenn_b), (drawer_lstmaddonly_a, drawer_lstmaddonly_b)),
ab=make_fns('ab', (teller_scenenn_a, teller_scenenn_b), (drawer_lstmaddonly_a, drawer_lstmaddonly_b)),
)
# %%
# hieraddonlyseq = dict(
# drawer_hieraddonlyseq_a = drawer_hieraddonlyseq_a.spec,
# drawer_hieraddonlyseq_b = drawer_hieraddonlyseq_b.spec,
# )
#%%
# torch.save(hieraddonlyseq, Path('models/hieraddonlyseq.pt'))
# %%
# %%
# %%
# %%