-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomtest.py
67 lines (64 loc) · 1.89 KB
/
customtest.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
from model import *
from keras.utils.np_utils import to_categorical
from nltk.tokenize import word_tokenize
from keras.callbacks import ModelCheckpoint
from keras.applications.imagenet_utils import preprocess_input
from keras.preprocessing import image
import warnings
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
"""
Program to custom check value for images
"""
def cust_data():
imgin = raw_input("Image to test: ")
img = np.array(image.load_img(imgin, target_size=(224,224)))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
img = preprocess_input(x)
question_in = raw_input("Question:")
ques = [x.lower() for x in word_tokenize(question_in)]
que = [0 for x in range(0,26)]
que_len = []
que_len.append(len(ques))
loc = 0
data = json.load(open("data_prepro.json"))
idx2word = data['ix_to_word']
for j in ques:
for k in idx2word.keys():
if idx2word[k] == j:
que[loc] = int(k)
loc+=1
break
que = np.array(que)
que_len = np.array(que_len)
que_check = move_right(que, que_len)
que_check = np.reshape(que_check,(1,26))
meta_data = json.load(open('data_prepro.json', 'r'))
meta_data['ix_to_word'] = {str(word):int(i) for i,word in meta_data['ix_to_word'].items()}
num_words = len(meta_data['ix_to_word'])
num_classes = len(meta_data['ix_to_ans'])
vqa = model(num_words, 300, num_classes)
vqa.load_weights('weights-22.hdf5')
value = vqa.predict([img, que_check])
th = max(value[0])
ans = []
data = json.load(open("data_prepro.json"))
idx2ans = data['ix_to_ans']
for i in range(0,1000):
if value[0][i] == th:
cnt = 0
for k in idx2ans.keys():
if cnt == i:
ans = idx2ans[k]
cnt+=1
img=mpimg.imread(imgin)
imgplot = plt.imshow(img)
print("Question: {}".format(question_in))
print("Answer: {}".format(ans))
plt.show()
if __name__ == "__main__":
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
warnings.filterwarnings("ignore")
cust_data()