-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgenerate.py
95 lines (61 loc) · 2.36 KB
/
generate.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
import json
from keras.models import load_model
import pickle
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.preprocessing.sequence import pad_sequences
import collections
from keras.preprocessing import image
from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from keras.models import Model
# Read the files word_to_idx.pkl and idx_to_word.pkl to get the mappings between word and index
word_to_index = {}
with open ("data/textFiles/word_to_idx.pkl", 'rb') as file:
word_to_index = pd.read_pickle(file)
index_to_word = {}
with open ("data/textFiles/idx_to_word.pkl", 'rb') as file:
index_to_word = pd.read_pickle(file)
print("Loading the model...")
model = load_model('model_checkpoints/model_19.h5')
resnet50_model = ResNet50 (weights = 'imagenet', input_shape = (224, 224, 3))
resnet50_model = Model (resnet50_model.input, resnet50_model.layers[-2].output)
# Generate Captions for a random image in test dataset
def predict_caption(photo):
inp_text = "startseq"
for i in range(38):
sequence = [word_to_index[w] for w in inp_text.split() if w in word_to_index]
sequence = pad_sequences([sequence], maxlen=38, padding='post')
ypred = model.predict([photo, sequence])
ypred = ypred.argmax()
word = index_to_word[ypred]
inp_text += (' ' + word)
if word == 'endseq':
break
final_caption = inp_text.split()[1:-1]
final_caption = ' '.join(final_caption)
return final_caption
def preprocess_image (img):
img = image.load_img(img, target_size=(224, 224))
img = image.img_to_array(img)
# Convert 3D tensor to a 4D tendor
img = np.expand_dims(img, axis=0)
#Normalize image accoring to ResNet50 requirement
img = preprocess_input(img)
return img
# A wrapper function, which inputs an image and returns its encoding (feature vector)
def encode_image (img):
img = preprocess_image(img)
feature_vector = resnet50_model.predict(img)
# feature_vector = feature_vector.reshape((-1,))
return feature_vector
print("Encoding the image ...")
img_name = "input.jpg"
photo = encode_image(img_name).reshape((1, 2048))
print("Running model to generate the caption...")
caption = predict_caption(photo)
img_data = plt.imread(img_name)
plt.imshow(img_data)
plt.axis("off")
plt.show()
print(caption)