-
Notifications
You must be signed in to change notification settings - Fork 1
/
referential_game_env.py
286 lines (268 loc) · 12 KB
/
referential_game_env.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
from random import sample
from typing import Dict, List, Tuple
import numpy as np
from listener import Listener
from math import log
from find_image import find_image_multi
from collections import Counter
import torch
import pdb
def H(n):
#move somewhere else! calculates nth harmonic number
# Euler-Mascheroni constant
gamma = 0.57721566490153286060651209008240243104215933593992
return gamma + log(n) + 0.5/n - 1./(12*n**2) + 1./(120*n**4)
class ReferentialGameEnv(object):
def __init__(self, *,
max_len: int,
eos_id: int,
noop_penalty: float,
length_penalty: float,
batch_size: int,
n_distr: int, # this might not be general
distribution: str = "uniform",
game_file_path: str = "game_file.pt",
captions_file: str,
**kwargs) -> None:
super().__init__()
self.max_len = max_len
self.eos_id = eos_id
self.noop_penalty = noop_penalty
self.length_penalty = length_penalty
self.batch_size = batch_size
self.n_distr = n_distr
self.distribution = distribution
self.game_file_path = game_file_path
self.image_size = (2048,)
self.listener = Listener(**kwargs).cuda()
self.target_ids = None
self.captions_file_path = captions_file
self.caption_length = 9
self._get_game_file()
def _get_game_file(self) -> np.array:
import torch
self.game_file = torch.load(self.game_file_path)
for i in self.game_file:
try:
self.game_file[i] = torch.from_numpy(np.array(self.game_file[i]))
except:
pass
filter_ids = []
for idx, cap in enumerate(self.game_file["captions"]):
if max(cap) < 199:
filter_ids.append(idx)
filter_id_set = set(filter_ids)
filter_id_to_idx = {filter_ids[i]: i for i in range(len(filter_ids))}
# pdb.set_trace()
self.game_file["captions"] = self.game_file["captions"][filter_ids]
self.game_file["images"] = self.game_file["images"][filter_ids]
if "similarity_rank" not in self.game_file:
self.game_file["sample_candidates"] = torch.from_numpy(
np.random.randint(low=0,
high=len(filter_ids), size=(len(filter_ids), 100)
)
)
else:
sample_candidates = []
import tqdm
for i in tqdm.tqdm(filter_ids):
tmp = []
for j in self.game_file["similarity_rank"][i]:
j = j.item()
if j in filter_id_set and j != i:
tmp.append(filter_id_to_idx[j])
if len(tmp) == 1000:
break
sample_candidates.append(tmp)
self.game_file["sample_candidates"] = torch.LongTensor(sample_candidates)
self.captions_file = torch.load(self.captions_file_path)
self.captions_file = torch.from_numpy(np.array([[k[:self.caption_length] for k in j] for j in self.captions_file]))
self.game_file["all_captions"] = self.captions_file[filter_ids]
def _find_eos(self, actions: np.array) -> List[int]:
eos_loc = [-1 for _ in range(len(actions))]
for idx, i in enumerate(actions):
for j in range(self.max_len):
if i[j] == self.eos_id:
eos_loc[idx] = j+1
break
return eos_loc
def _new_game(self) -> np.array:
import torch # using old code here; change to numpy later
sample_candidates = self.game_file["sample_candidates"]
n = sample_candidates.size()[1]
n_img = sample_candidates.size()[0]
target_images = torch.randint(n_img, size=(self.batch_size,))
if self.distribution == 'zipf':
zipf_weights = np.array([1/(i*H(n)) for i in range(1, n+1)])
distr_array = np.random.choice(n, (self.batch_size, self.n_distr+1), False, zipf_weights)
distr_images = torch.from_numpy(distr_array)
else:
distr_images = torch.randint(n, size=(
self.batch_size, self.n_distr + 1
))
target_candidates = torch.index_select(
sample_candidates, 0, target_images.view(-1)
).view(self.batch_size, n)
distr_images = torch.gather(
target_candidates, 1, distr_images).view(
self.batch_size, self.n_distr+1
)
target_indices = torch.randint(
self.n_distr + 1, size=(self.batch_size,))
distr_images[range(self.batch_size), target_indices] \
= target_images.view(self.batch_size)
self.distr_images = distr_images.numpy()
self.target_ids = target_indices.numpy()
self.images = torch.index_select(
self.game_file["images"], 0,
distr_images.view(-1)
).view(self.batch_size, self.n_distr+1, *self.image_size).numpy()
return dict(
images=self.images,
images_ids=distr_images.numpy(),
goal=target_indices.numpy()
)
# def _render(self, actions, return_dict):
# import torch
# i2w = torch.load("i2w")
# for i in range(self.batch_size):
# print("goal\tchoice\timages")
# goal = [" " for i in range(self.n_distr+1)]
# goal[self.target_ids[i]] = "→"
# choice = [" " for i in range(self.n_distr+1)]
# choice[return_dict["choice"][i].item()] = "→"
# captions = torch.index_select(
# self.game_file["captions"], 0,
# torch.from_numpy(self.distr_images[i])
# ).cpu().tolist()
# captions = [
# ' '.join(i2w[i] for i in j) for j in captions
# ]
# for j in range(self.n_distr+1):
# print(f"{goal[j]}\t{choice[j]}\t{captions[j]}")
# print(f"sentence: {' '.join(i2w[j] for j in actions[i])}")
def _render(self, actions, return_dict, name):
import torch
i2w = torch.load("i2w")
for i in range(self.batch_size):
with open(name + "_" + str(i) + ".html", "w") as html:
goal = [" " for i in range(self.n_distr+1)]
goal[self.target_ids[i]] = "→"
choice = [" " for i in range(self.n_distr+1)]
choice[return_dict["choice"][i].item()] = "→"
captions = torch.index_select(
self.game_file["captions"], 0,
torch.from_numpy(self.distr_images[i])
).cpu().tolist()
all_captions_torch = torch.index_select(
self.game_file["all_captions"], 0,
torch.from_numpy(self.distr_images[i])
).cpu().tolist()
captions = [
' '.join(i2w[i] for i in j) for j in captions
]
all_captions = []
for l in all_captions_torch:
tmp = []
for j in l:
caption_sentence = ' '.join(i2w[i] for i in j)
caption_no_tags = ' '.join(caption_sentence.split(' ')[1:-1])
tmp.append(caption_no_tags.split('<EOS>')[0])
all_captions.append(tmp)
# write images + goal/choice status
html.write("<html>\n<table>\n\t<tr>\n")
for j in range(self.n_distr + 1):
header_str = "\t\t<th>Image " + str(j)
if goal[j] == "→":
header_str += " (Goal)"
if choice[j] == "→":
header_str += " (Choice)"
header_str += "</th>\n"
html.write(header_str)
# write captions
html.write("\t</tr>\n\t<tr>\n")
for j in range(self.n_distr + 1):
html.write("\t\t<th>" + captions[j] + "</th>\n")
# write images
html.write("\t</tr>\n\t<tr>\n")
for j in range(self.n_distr+1):
try:
img_link = find_image_multi(all_captions[j])
html.write("\t\t<td><img src='" + img_link + "'></td>\n")
except IndexError:
html.write("\t\t<td>IMAGE NOT FOUND</td>\n")
# write output sentence
html.write("\t</tr>\n\t<tr>\n")
output = ' '.join(i2w[j] for j in actions[i])
output = output.split('<EOS>')[0]
html.write("\t\t<td colspan='" + str(self.n_distr+1) + f"'><center>output sentence: {output}</center></td>\n")
# print(f"sentence: {' '.join(i2w[j] for j in actions[i])}")
html.write("\t</tr>\n</table>\n</html>\n")
def step(self, actions: np.array, render=False, name="None") -> Tuple[
Dict[np.array, np.array], np.array
]:
B = actions.shape[0]
acc = []
# for gold standard runs
# import torch
# actions = torch.index_select(self.game_file["captions"], 0, torch.from_numpy(self.distr_images[range(B), self.target_ids])).view(self.batch_size, -1).numpy()
# actions = actions[:, :self.max_len]
# listener act
if True:
action_len = self._find_eos(actions)
for idx, i in enumerate(action_len):
if i == -1:
action_len[idx] = self.max_len
return_dict = self.listener.act(self.images, actions, action_len)
if render:
self._render(actions, return_dict, name)
# reward calculation
if True:
reward = np.zeros_like(actions).astype(np.float32)
eos_loc = self._find_eos(actions)
# game reward
for idx, i in enumerate(eos_loc):
if (i != -1) and (return_dict["control"][idx] in [0, 1]):
if return_dict["choice"][idx] == self.target_ids[idx]:
reward[idx][i-1] = 1.0
acc.append(1)
else:
reward[idx][i-1] = -1.0
acc.append(0)
else:
reward[idx][i-1] = -self.noop_penalty
# length penalty
for idx, i in enumerate(eos_loc):
if i == -1:
reward[idx] -= self.length_penalty
elif i < self.max_len - 1:
reward[idx][:i] -= self.length_penalty
# observation
if True:
obs = dict()
# feedback
import torch
# add choices and controls
obs["choices"] = return_dict["choice"]
obs["controls"] = return_dict["control"]
obs["feedback"] = torch.index_select(
self.game_file["captions"], 0,
torch.from_numpy(self.distr_images[range(B), return_dict["choice"].cpu()])
).view(self.batch_size, -1).numpy()
obs["feedback"] = obs["feedback"][:, :self.max_len]
# obs["feedback_mask"] = (return_dict["control"] == 1).float()
obs["ground_truth"] = torch.index_select(
self.game_file["captions"], 0,
torch.from_numpy(self.distr_images[range(B), self.target_ids])
).view(self.batch_size, -1).numpy()
obs["ground_truth"] = obs["ground_truth"][:, :self.max_len]
obs["accuracy"] = sum(acc) / len(acc) if len(acc) else 1/(self.n_distr + 1)
# new game
obs.update(self._new_game())
return obs, reward
def reset(self):
return self._new_game()
def close(self):
pass
def get_most_frequent_words(self, vocab_size) -> List[int]:
return [tup[0] for tup in Counter(torch.flatten(self.game_file["captions"]).tolist()).most_common(vocab_size)]