-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_features.py
219 lines (187 loc) · 9.87 KB
/
get_features.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
from args import get_parser
args = get_parser().parse_args()
from nltk import tokenize
from flair.embeddings import FlairEmbeddings, BertEmbeddings
from flair.data import Sentence
import glob
from utils import *
import os
import logging
import numpy as np
import sys
import tensorflow as tf
from keras.models import Model
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing import image
class features:
def __init__(self):
model_path_sc = args.model_path_scene
model_path_obj = args.model_path_object
model_path_loc = args.model_path_location
hierarchy = args.scene_hierarchy
labels = args.scene_labels
self.scence_obj = SceneClassificator(model_path=model_path_sc, labels_file=labels, hierarchy_file=hierarchy)
self.object_obj = ObjectDetector(model_path=model_path_obj)
self.location_obj = GeoEstimator(model_path_loc, use_cpu=True)
self.flair_forward_embedding = FlairEmbeddings('multi-forward')
self.flair_backward_embedding = FlairEmbeddings('multi-backward')
self.bert_embedding = BertEmbeddings('bert-base-multilingual-cased')
def save_void_file_features(self, articles, lang):
whole_features = []
events = articles.keys()
for event in events:
docs = articles[event]
for doc in docs:
id = doc['uri']
whole_features.append( {'article_id':id, 'features':{} } )
np.save('features/features_'+lang, whole_features)
def entities_main_vector(self, articles_in, nel_tool, ner_tool):
vector = []
events = articles_in.keys()
for event in events:
articles = articles_in[event]
for art in articles:
for ent in art['nel'][nel_tool][ner_tool]:
if ent not in vector:
vector.append(ent)
return vector
def save_bert(self, name_whole_features, articles):
if os.path.exists("features/" + name_whole_features):
whole_features = np.load("features/" + name_whole_features, allow_pickle=True)
else:
'features file not found!!!'
events = articles.keys()
indplus = 0
for event in events:
docs = articles[event]
for doc in docs:
text = doc['body'][:1500]
id = doc['uri']
##
for f in whole_features:
if f['article_id'] == id:
features_doc = f
sentences = tokenize.sent_tokenize(text)
len_sent = len(sentences)
vec_embs = []
for ind in range(len_sent):
if len(sentences[ind]) > 0:
if ind == len_sent - 1:
text = sentences[ind]
elif ind == len_sent - 2:
text = sentences[ind] + " " + sentences[ind + 1]
else:
text = sentences[ind] + " " + sentences[ind + 1] + " " + sentences[ind + 2]
sentence = Sentence(text)
self.bert_embedding.embed(sentence)
avg_vector = np.zeros(768)
counter = 0
# now check out the embedded tokens.
for token in sentence:
vector = token.embedding.cpu().detach().numpy()
avg_vector = np.add(avg_vector, vector[0:768])
counter += 1
avg_vector /= counter
vec_embs.append(avg_vector)
features_doc['features']['t_bert'] = vec_embs
indplus += 1
print("****************** " + str(indplus))
np.save("features/" +name_whole_features, whole_features)
print("bert saved successfully!!!")
def save_object_scene(self,name_whole_features, articles, image_path, image_names, img_format):
if img_format == "png":
till = -4
else:
till = -5
if os.path.exists("features/" + name_whole_features):
whole_features = np.load("features/" + name_whole_features, allow_pickle=True)
else:
'file features not found!!!'
events = articles.keys()
for event in events:
print("event " + event + " ...")
docs = articles[event]
for i in range(len(docs)): # img_name == article_id
img = ""
for image_n, image in zip(image_names, glob.glob(image_path + "*." + img_format)):
# o = image_n[0:till]
if image_n[0:till] == docs[i]['uri']:
img = image
break
if img != "":
print("article " + str(i) + " ...")
v_scene = self.scence_obj.get_img_embedding(img)
v_object = self.object_obj.get_img_embedding(img)
article_id = docs[i]['uri']
for f in whole_features:
if f['article_id'] == article_id:
features_id = f
features_id['features']['v_scene'] = v_scene
features_id['features']['v_object'] = v_object
np.save("features/" + name_whole_features, whole_features)
print("features obj and scene saved successfully!!!")
def save_entity_overlap(self, name_whole_features, articles, lang):
all_entities_vector = self.entities_main_vector(articles, 'wikifier', 'spacy')
np.save("features/entities_vector_" + lang, all_entities_vector)
if os.path.exists("features/" + name_whole_features):
whole_features = np.load("features/" + name_whole_features, allow_pickle=True)
else:
'file features not found!!!'
events = articles.keys()
indplus = 0
for event in events:
docs = articles[event]
for doc in docs:
id = doc['uri']
features = ""
for f in whole_features:
if f['article_id'] == id:
features = f
t_entity_overlap = self.map_entities_to_main_vector(doc['nel']['wikifier']['spacy'], all_entities_vector)
if features != "":
features['features']['t_entity_overlap'] = t_entity_overlap
indplus += 1
print("****************** " + str(indplus))
np.save("features/" + name_whole_features, whole_features)
print("features entity overlap saved successfully!!!")
def save_location(self, pre_path, name_whole_features, image_format):
if os.path.exists("features/" + name_whole_features):
whole_features = np.load("features/" + name_whole_features, allow_pickle=True)
else:
'file features not found!!!'
for wf in whole_features:
img = pre_path+wf['article_id']+"."+image_format
location_features = self.location_obj.get_img_embedding(img)
if len(location_features)<1:
print(wf['article_id'])
# wf['features']['v_location'] = location_features
# print(location_features.shape)
wf['features']['v_location'] = location_features
# whole_features.append({'article_id': id, 'features': {'v_location': location_features}})
np.save("features/"+name_whole_features, whole_features)
print("features location saved successfully!!!")
def get_features(features_obj, feature, articles_with_nel, images_path, image_names, lang, image_format):
features_obj.save_void_file_features(articles_with_nel, lang)
name_saved_features = 'features_'+lang
if not os.path.exists("features"):
os.mkdir("features")
if feature == 'scene_obj':
if not os.path.exists("features/"+lang+"features_obj_scene.npy"):
features_obj.save_object_scene(articles_with_nel, name_saved_features, images_path, image_names, image_format)
if feature=='bert':
if not os.path.exists("features/"+lang+"features_bert.npy"):
features_obj.save_bert( name_saved_features, articles_with_nel)
if feature=='entity':
if not os.path.exists("features/"+lang+"features_entity_overlap.npy"):
features_obj.save_entity_overlap(name_saved_features, articles_with_nel, lang)
if feature=='location':
if not os.path.exists("features/"+lang+"features_location.npy"):
features_obj.save_location(images_path, name_saved_features, image_format)
lang = args.language
articles_with_nel = open_json(args.docs_path + "/nel_" + lang + ".json")
images_path = args.images_path
image_names = [ get_file_name(img) for img in glob.glob(images_path + "/*.jpeg") ]
feature_type = args.feature
features_obj = features()
get_features(features_obj, feature_type, articles_with_nel, images_path, image_names, lang, "jpeg")