-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
71 lines (47 loc) · 1.53 KB
/
preprocess.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
import json
import os
image_folder_path = "flickr30k-images"
caption_folder_path = "flickr30k/results_20130124.token"
image_list = os.listdir(image_folder_path)
with open(caption_folder_path,'r') as f:
content = f.read().split('\n')
dictionary = {}
for i in range(0, len(content), 5):
image_name = content[i].split('#')[0]
ls = []
for j in range(i, i+5):
caption = content[j].split(maxsplit=1)[-1]
ls.append(caption)
ls.append(os.path.join(image_folder_path, image_name))
dictionary[image_name] = ls
f.close()
# train set
with open('train.txt', 'r') as f:
content = f.read().split('\n')
train_list = [i + '.jpg' for i in content]
train_dict = {k:dictionary[k] for k in train_list if k in dictionary}
f1 = open("train.json", "w")
json.dump(train_dict, f1, indent=4)
f1.close()
f.close()
print("Successfully split train set!")
# validation set
with open('validation.txt', 'r') as f:
content = f.read().split('\n')
validation_list = [i + '.jpg' for i in content]
validation_dict = {k:dictionary[k] for k in validation_list if k in dictionary}
f1 = open("validation.json", "w")
json.dump(validation_dict, f1, indent=4)
f1.close()
f.close()
print("Successfully split validation set!")
# test set
with open('test.txt', 'r') as f:
content = f.read().split('\n')
test_list = [i + '.jpg' for i in content]
test_dict = {k:dictionary[k] for k in test_list if k in dictionary}
f1 = open("test.json", "w")
json.dump(test_dict, f1, indent=4)
f1.close()
f.close()
print("Successfully split test set!")