-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
154 lines (94 loc) · 4.15 KB
/
app.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
from flask import Flask, render_template, request
import cv2
from keras.models import load_model
import numpy as np
from keras.applications import ResNet50
from keras.optimizers import Adam
from keras.layers import Dense, Flatten,Input, Convolution2D, Dropout, LSTM, TimeDistributed, Embedding, Bidirectional, Activation, RepeatVector,Concatenate
from keras.models import Sequential, Model
from keras.utils import np_utils
from keras.preprocessing import image, sequence
import cv2
from keras.preprocessing.sequence import pad_sequences
import tensorflow
from tqdm import tqdm
from keras.applications import resnet50
from werkzeug.utils import secure_filename
import os
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
config = ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
vocab = np.load('vocab.npy', allow_pickle=True)
#vocab = np.load('C:\\Users\\Balaji\\Documents\\Machine Learning\\Deep Learning\\Image Captioning\\Flickr_Data\\Flickr_Data\vocab.npy',allow_pickle=True)
vocab = vocab.item()
inv_vocab = {v:k for k,v in vocab.items()}
#print("+"*50)
#print("vocabulary loaded")
embedding_size = 128
vocab_size = len(vocab)
max_len = 40
image_model = Sequential()
image_model.add(Dense(embedding_size, input_shape=(2048,), activation='relu'))
image_model.add(RepeatVector(max_len))
language_model = Sequential()
language_model.add(Embedding(input_dim=vocab_size, output_dim=embedding_size, input_length=max_len))
language_model.add(LSTM(256, return_sequences=True))
language_model.add(TimeDistributed(Dense(embedding_size)))
conca = Concatenate()([image_model.output, language_model.output])
x = LSTM(128, return_sequences=True)(conca)
x = LSTM(512, return_sequences=False)(x)
x = Dense(vocab_size)(x)
out = Activation('softmax')(x)
model = Model(inputs=[image_model.input, language_model.input], outputs = out)
model.compile(loss='categorical_crossentropy', optimizer='RMSprop', metrics=['accuracy'])
model.load_weights('C:\\Users\\Balaji\\Documents\\Machine Learning\\Deep Learning\\Image Captioning\\Flickr_Data\\Flickr_Data\\model.h5')
print("="*150)
print("MODEL LOADED")
resnet = ResNet50(include_top=False,weights='imagenet',input_shape=(224,224,3),pooling='avg')
#resnet = load_model('resnet.h5')
print("="*150)
print("RESNET MODEL LOADED")
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1
@app.route('/')
def index():
return render_template('index.html')
@app.route('/after', methods=['GET', 'POST'])
def after():
global model, resnet, vocab, inv_vocab
f = request.files['file1']
#'C:\\Users\\Balaji\\Documents\\Machine Learning\Deep Learning\Image Captioning\\Flickr_Data\\Flickr_Data
basepath = "C:\\Users\\Balaji\\Documents\\Machine Learning\Deep Learning\\Image Captioning\\Flickr_Data\\Flickr_Data\\Images"
file_path = os.path.join(basepath, secure_filename(f.filename))
f.save(file_path)
print("="*50)
print("IMAGE SAVED")
image = cv2.imread(file_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (224,224))
image = np.reshape(image, (1,224,224,3))
incept = resnet.predict(image).reshape(1,2048)
print("="*50)
print("Predict Features")
text_in = ['startofseq']
final = ''
print("="*50)
print("GETING Captions")
count = 0
while tqdm(count < 20):
count += 1
encoded = []
for i in text_in:
encoded.append(vocab[i])
padded = pad_sequences([encoded], maxlen=max_len, padding='post', truncating='post').reshape(1,max_len)
sampled_index = np.argmax(model.predict([incept, padded]))
sampled_word = inv_vocab[sampled_index]
if sampled_word != 'endofseq':
final = final + ' ' + sampled_word
text_in.append(sampled_word)
return render_template('after.html', data=final)
if __name__ == "__main__":
app.run(debug=False,threaded=False)