-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_manager.py
344 lines (250 loc) · 10 KB
/
json_manager.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
import json
from random import randrange
import re
import random
from args import Args
class JSONManager(object):
"""
This class handles everything about json description file
"""
word_frequencies = {}
word_to_idx = {}
idx_to_word = {}
vocab_size = -1
def __init__(self, json_path):
self.json_path = json_path
self.data = self.read_json(json_path)
self.preprocess_text_data()
self.frequencies = self.get_word_frequencies()
self.word_to_idx = self.get_word_to_idx_mapping()
self.idx_to_word = self.get_idx_to_word_mapping()
self.vocab_size = self.get_vocab_size()
def read_json(self, path):
with open(path) as json_file:
data = json.load(json_file)
return data
def get_number_of_descriptions_at_idx(self, index):
return len(self.data[index]["descriptions"])
def get_description_from_idx(self, index):
"""
This method returns a random description for given image index.
"""
idx = random.randint(0, len(self.data[index]["descriptions"])-1)
return self.data[index]["descriptions"][idx]["text"]
def get_description_from_idx_with_idx(self, index, index2):
"""
This method return description number "index2" for data instance number "index".
"""
return self.data[index]["descriptions"][index2]["text"]
def get_imagename_from_idx(self, idx):
return self.data[idx]["image"]
def get_random_description(self):
"""
Returns description of random image
Used for giving images with mismatched description to discriminator
"""
rand_idx = randrange(len(self.data))
return self.get_description_from_idx(rand_idx)
def preprocess_text_data(self):
"""
Insert space before special characters such as "," or "." etc.
"""
#desc = "A woman with a chiselled jaw, prominent cheekbones, a long, narrow nose and thin eyebrows. She has long, messy, black hair and she is wearing makeup."
for i in range(len(self.data)):
for j in range(len(self.data[i]["descriptions"])):
desc = self.get_description_from_idx_with_idx(i,j)
desc = re.sub(r"([^a-zA-Z])", r" \1 ", desc)
desc = re.sub('\s{2,}', ' ', desc)
self.data[i]["descriptions"][j]["text"] = desc
def get_word_frequencies(self):
frequencies = {}
for i in range(len(self.data)):
for j in range(len(self.data[i]["descriptions"])):
desc = self.get_description_from_idx_with_idx(i, j)
words = desc.split(" ")
for word in words:
try:
frequencies[word] += 1
except:
frequencies[word] = 1
# sort according to frequency
frequencies = {k: v for k, v in sorted(frequencies.items(), key=lambda item: item[1], reverse=True)}
return frequencies
def get_word_to_idx_mapping(self):
mapping = {}
count = 2 # 0 and 1 are reserved for pad and unknown
for k, v in self.frequencies.items():
word = k
mapping[word] = count
count += 1
return mapping
def get_idx_to_word_mapping(self):
idx_to_word = {}
for k, v in self.word_to_idx.items():
idx_to_word[v] = k
return idx_to_word
def get_vocab_size(self):
return len(self.frequencies)
def transform_sentence(self, tokenized_sentence, desired_length):
# Transforms sentence to word indexes
unknown = 1
pad = 0
transformed = []
for word in tokenized_sentence:
if word in self.word_to_idx:
transformed.append(self.word_to_idx[word])
else:
transformed.append(unknown)
if len(transformed) < desired_length:
while(len(transformed) < desired_length):
transformed.append(pad)
else:
transformed = transformed[0:desired_length]
# print("len(transformed)", len(transformed))
return transformed
def get_shortest_sentence_length(self):
result = 9e9
for item in self.data:
for desc in item["descriptions"]:
sentence_len = len(desc["text"].split(" "))
if sentence_len < result:
result = sentence_len
print(result)
return result
def get_average_sentence_length(self):
result = 0
count = 0
for item in self.data:
for desc in item["descriptions"]:
sentence_len = len(desc["text"].split(" "))
result += sentence_len
count += 1
result = result / count
print(result)
return result
class SimpleShapesDescriptionReader(object):
data_dir = Args.shapes_dataset_dir
descriptions_dir = Args.shapes_descriptions_dir
data = None
def __init__(self):
self.data = self.get_descriptions_data()
def get_descriptions_data(self):
result = {}
filepath = self.descriptions_dir
with open(filepath) as fp:
line = fp.readline()
item = line.split(":")
image_name = item[0][:-1] + ".jpg"
description = item[1]
description = description[1:-2] # remove unnecessary space at the beginning of each description and \n at the end.
result[image_name] = description
cnt = 1
while line:
line = fp.readline()
if(not line):
break
item = line.split(":")
image_name = item[0][:-1] + ".jpg"
description = item[1]
description = description[1:-2] # remove unnecessary space at the beginning of each description and \n at the end.
cnt += 1
result[image_name] = description
return result
def get_description_from_image_name(self, image_name):
return self.data[image_name]
def get_imagename_from_idx(self, idx):
return str(idx) + ".jpg"
def get_description_from_idx(self, idx):
img_name = self.get_imagename_from_idx(idx)
return self.data[img_name]
def get_random_description(self):
rand_idx = randrange(len(self.data))
return self.get_description_from_idx(rand_idx)
class JSONManager_V2(object):
"""
This class is used to parse json data generated by combining 5685 data and
descriptions generated from celebA annotations.
"""
json_path = None
data = None
word_frequencies = {}
word_to_idx = {}
idx_to_word = {}
vocab_size = -1
def __init__(self, json_path):
self.json_path = json_path
self.data = self.read_json(self.json_path)
self.preprocess_text_data()
self.frequencies = self.get_word_frequencies()
self.word_to_idx = self.get_word_to_idx_mapping()
self.idx_to_word = self.get_idx_to_word_mapping()
self.vocab_size = self.get_vocab_size()
def read_json(self, path):
with open(path) as json_file:
temp_data = json.load(json_file)
data = []
for k, v in temp_data.items():
data.append({"imagename": k, "descriptions": v})
return data
def get_number_of_descriptions_at_idx(self, index):
return len(self.data[index]["descriptions"])
def get_description_from_idx(self, index):
"""
This method returns a random description for given image index.
"""
idx = random.randint(0, len(self.data[index]["descriptions"])-1)
return self.data[index]["descriptions"][idx]
def get_description_from_idx_with_idx(self, index, index2):
"""
This method return description number "index2" for data instance number "index".
"""
return self.data[index]["descriptions"][index2]
def get_imagename_from_idx(self, idx):
return self.data[idx]["imagename"]
def get_random_description(self):
"""
Returns description of random image
Used for giving images with mismatched description to discriminator
"""
rand_idx = randrange(len(self.data))
return self.get_description_from_idx(rand_idx)
def preprocess_text_data(self):
"""
Insert space before special characters such as "," or "." etc.
"""
#desc = "A woman with a chiselled jaw, prominent cheekbones, a long, narrow nose and thin eyebrows. She has long, messy, black hair and she is wearing makeup."
for i in range(len(self.data)):
for j in range(len(self.data[i]["descriptions"])):
desc = self.get_description_from_idx_with_idx(i,j)
desc = re.sub(r"([^a-zA-Z])", r" \1 ", desc)
desc = re.sub('\s{2,}', ' ', desc)
self.data[i]["descriptions"][j] = desc
def get_word_frequencies(self):
frequencies = {}
for i in range(len(self.data)):
for j in range(len(self.data[i]["descriptions"])):
desc = self.get_description_from_idx_with_idx(i, j)
words = desc.split(" ")
for word in words:
try:
frequencies[word] += 1
except:
frequencies[word] = 1
# sort according to frequency
frequencies = {k: v for k, v in sorted(frequencies.items(), key=lambda item: item[1], reverse=True)}
return frequencies
def get_word_to_idx_mapping(self):
mapping = {}
count = 2 # 0 and 1 are reserved for pad and unknown
for k, v in self.frequencies.items():
word = k
mapping[word] = count
count += 1
return mapping
def get_idx_to_word_mapping(self):
idx_to_word = {}
for k, v in self.word_to_idx.items():
idx_to_word[v] = k
return idx_to_word
def get_vocab_size(self):
return len(self.frequencies)