-
Notifications
You must be signed in to change notification settings - Fork 16
/
question_answer.py
62 lines (56 loc) · 1.91 KB
/
question_answer.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
import numpy as np
import embedding as ebd
import prepare_data
import models
import argparse
import sys
import keras.backend as K
from nltk import word_tokenize
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
from keras.models import load_model
def extract_image_features(img_path):
model = models.VGG_16('weights/vgg16_weights.h5')
img = image.load_img(img_path,target_size=(224,224))
x = image.img_to_array(img)
x = np.expand_dims(x,axis=0)
x = preprocess_input(x)
last_layer_output = K.function([model.layers[0].input,K.learning_phase()],
[model.layers[-1].input])
features = last_layer_output([x,0])[0]
return features
def preprocess_question(question):
word_idx = ebd.load_idx()
tokens = word_tokenize(question)
seq = []
for token in tokens:
seq.append(word_idx.get(token,0))
seq = np.reshape(seq,(1,len(seq)))
return seq
def generate_answer(img_path, question, model):
model_path = 'weights/model_'+str(model)+'.h5'
model = load_model(model_path)
img_features = extract_image_features(img_path)
seq = preprocess_question(question)
if model == 1:
x = [img_features, seq]
else:
x = [img_features, seq, img_features]
probabilities = model.predict(x)[0]
answers = np.argsort(probabilities[:1000])
top_answers = [prepare_data.top_answers[answers[-1]],
prepare_data.top_answers[answers[-2]],
prepare_data.top_answers[answers[-3]]]
return top_answers
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-image', type=str, required=True)
parser.add_argument('-question', type=str, required=True)
parser.add_argument('-model', type=int, default=2)
args = parser.parse_args()
if args.model != 1 and args.model != 2:
print('Invalid model selection.')
sys.exit()
top_answers = generate_answer(args.image, args.question, args.model)
print('Top answers: %s, %s, %s.' % (top_answers[0],top_answers[1],top_answers[2]))
if __name__ == '__main__':main()