-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaption.py
More file actions
97 lines (62 loc) · 2.51 KB
/
caption.py
File metadata and controls
97 lines (62 loc) · 2.51 KB
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
# from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input
import tensorflow as tf
from keras.models import load_model, Model
import pickle
import numpy as np
import warnings
warnings.filterwarnings("ignore")
model = load_model("C:/Users/mandi/Downloads/ML-Models-Flask-master/Deploy Image Captioning/model_weights/model_10.h5")
model.make_predict_function()
#
# incept_model = ResNet50(include_top=True)
# last = incept_model.layers[-2].output
# modele = Model(inputs = incept_model.input,outputs = last)
# #modele.summary()
#
model_temp = ResNet50(weights="imagenet", input_shape=(224,224,3))
# model_temp = ResNet50(include_top=True)
# Create a new model, by removing the last layer (output layer of 1000 classes) from the resnet50
model_resnet = Model(model_temp.input, model_temp.layers[-2].output)
model_resnet.make_predict_function()
# Load the word_to_idx and idx_to_word from disk
with open("./storage/word_to_idx.pkl", "rb") as w2i:
word_to_idx = pickle.load(w2i)
with open("./storage/idx_to_word.pkl", "rb") as i2w:
idx_to_word = pickle.load(i2w)
max_len = 35
def preprocess_image(img):
img = tf.keras.utils.load_img(img, target_size=(224,224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
return img
def encode_image(img):
img = preprocess_image(img)
feature_vector = model_resnet.predict(img)
feature_vector = feature_vector.reshape(1, feature_vector.shape[1])
return feature_vector
def predict_caption(photo):
in_text = "startseq"
for i in range(max_len):
sequence = [word_to_idx[w] for w in in_text.split() if w in word_to_idx]
sequence = pad_sequences([sequence], maxlen=max_len, padding='post')
ypred = model.predict([photo,sequence])
ypred = ypred.argmax()
word = idx_to_word[ypred]
in_text+= ' ' +word
if word =='endseq':
break
final_caption = in_text.split()
final_caption = final_caption[1:-1]
final_caption = ' '.join(final_caption)
return final_caption
def caption_this_image(input_img):
photo = encode_image(input_img)
print("photos", photo)
caption = predict_caption(photo)
# keras.backend.clear_session()
return caption