-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoader.py
55 lines (40 loc) · 1.42 KB
/
Loader.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
import pickle
def load_doc(filename):
file = open(filename,'r')
text = file.read()
file.close()
return text
#Image Identifier --- TrainImage.txt
#2513260012_03d33305cf.jpg
def load_set(filename):
doc = load_doc(filename)
dataset = list()
for line in doc.split('\n'):
if len(line)<1:
continue
identifier = line.split('.')[0]
dataset.append(identifier)
return set(dataset)
#load cleaned description
#1000268201_693b08cb0e child in pink dress is climbing up set of stairs in an entry way
#assign start and end token to the description
def load_clean_descriptions(filename, dataset):
doc = load_doc(filename)
descriptions = dict()
for line in doc.split('\n'):
tokens = line.split()
image_id, image_desc = tokens[0],tokens[1:]
#if image_id not in dataset ignore
if image_id in dataset:
if image_id not in descriptions:
descriptions[image_id] = list()
desc = 'startseq ' + ' '.join(image_desc) + ' endseq'
descriptions[image_id].append(desc)
return descriptions
# load photo features from features.pkl
def load_photo_features(filename, dataset):
# load all features
all_features = pickle.load(open(filename, 'rb'))
# filter features
features = {k: all_features[k] for k in dataset}
return features