Jyothirmai
commited on
Update cnnrnn.py
Browse files
cnnrnn.py
CHANGED
@@ -28,19 +28,17 @@ from tensorflow.keras.models import Model
|
|
28 |
from tensorflow.keras.layers import Dense, LSTM, Input, Embedding, Conv2D, Concatenate, Flatten, Add, Dropout, GRU
|
29 |
import random
|
30 |
import datetime
|
31 |
-
from nltk.translate.bleu_score import sentence_bleu
|
32 |
|
33 |
|
34 |
|
35 |
-
def getModel(image):
|
36 |
-
embedding_matrix_vocab = np.load('my_embedding_matrix.npy')
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
41 |
dense1 = Dense(256, kernel_initializer=tf.keras.initializers.glorot_uniform(seed = 56), name='dense_encoder')(input1)
|
42 |
|
43 |
-
input2 = Input(shape=
|
44 |
embedding_layer = Embedding(input_dim = 1427, output_dim = 300, input_length=153, mask_zero=True, trainable=False,
|
45 |
weights=[embedding_matrix_vocab], name="Embedding_layer")
|
46 |
emb = embedding_layer(input2)
|
@@ -71,9 +69,9 @@ def getModel(image):
|
|
71 |
|
72 |
encoder_decoder = Model(inputs = [input1, input2], outputs = output)
|
73 |
|
74 |
-
encoder_decoder.load_weights("encoder_decoder_epoch_5.h5")
|
75 |
# encoder
|
76 |
-
|
77 |
encoder_input = encoder_decoder.input[0]
|
78 |
encoder_output = encoder_decoder.get_layer('dense_encoder').output
|
79 |
encoder_model = Model(encoder_input, encoder_output)
|
@@ -89,13 +87,69 @@ def getModel(image):
|
|
89 |
decoder_model = Model(inputs = [text_input, enc_output], outputs = decoder_output)
|
90 |
|
91 |
return encoder_model,decoder_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
|
94 |
def greedysearch(image):
|
95 |
# Open the pickle file for reading
|
96 |
train_data = pd.read_csv('Final_Train_Data.csv')
|
97 |
y_train = train_data['Report']
|
98 |
-
encoder_model, decoder_model = getModel(
|
99 |
input_ = 'startseq'
|
100 |
image_features = encoder_model.predict(image)
|
101 |
result = []
|
|
|
28 |
from tensorflow.keras.layers import Dense, LSTM, Input, Embedding, Conv2D, Concatenate, Flatten, Add, Dropout, GRU
|
29 |
import random
|
30 |
import datetime
|
|
|
31 |
|
32 |
|
33 |
|
|
|
|
|
34 |
|
35 |
+
def getModel():
|
36 |
+
|
37 |
+
embedding_matrix_vocab = np.load('my_embedding_matrix.npy')
|
38 |
+
input1 = Input(shape=(2048,), name='Image_input')
|
39 |
dense1 = Dense(256, kernel_initializer=tf.keras.initializers.glorot_uniform(seed = 56), name='dense_encoder')(input1)
|
40 |
|
41 |
+
input2 = Input(shape=(153,), name='Text_Input')
|
42 |
embedding_layer = Embedding(input_dim = 1427, output_dim = 300, input_length=153, mask_zero=True, trainable=False,
|
43 |
weights=[embedding_matrix_vocab], name="Embedding_layer")
|
44 |
emb = embedding_layer(input2)
|
|
|
69 |
|
70 |
encoder_decoder = Model(inputs = [input1, input2], outputs = output)
|
71 |
|
72 |
+
encoder_decoder.load_weights("/content/encoder_decoder_epoch_5.h5")
|
73 |
# encoder
|
74 |
+
|
75 |
encoder_input = encoder_decoder.input[0]
|
76 |
encoder_output = encoder_decoder.get_layer('dense_encoder').output
|
77 |
encoder_model = Model(encoder_input, encoder_output)
|
|
|
87 |
decoder_model = Model(inputs = [text_input, enc_output], outputs = decoder_output)
|
88 |
|
89 |
return encoder_model,decoder_model
|
90 |
+
|
91 |
+
# def getModel(image):
|
92 |
+
# embedding_matrix_vocab = np.load('my_embedding_matrix.npy')
|
93 |
+
|
94 |
+
# input1 = Input(shape=(2048), name='Image_input')
|
95 |
+
# dense1 = Dense(256, kernel_initializer=tf.keras.initializers.glorot_uniform(seed = 56), name='dense_encoder')(input1)
|
96 |
+
|
97 |
+
# input2 = Input(shape=(153), name='Text_Input')
|
98 |
+
# embedding_layer = Embedding(input_dim = 1427, output_dim = 300, input_length=153, mask_zero=True, trainable=False,
|
99 |
+
# weights=[embedding_matrix_vocab], name="Embedding_layer")
|
100 |
+
# emb = embedding_layer(input2)
|
101 |
+
|
102 |
+
# LSTM1 = LSTM(units=256, activation='tanh', recurrent_activation='sigmoid', use_bias=True,
|
103 |
+
# kernel_initializer=tf.keras.initializers.glorot_uniform(seed=23),
|
104 |
+
# recurrent_initializer=tf.keras.initializers.orthogonal(seed=7),
|
105 |
+
# bias_initializer=tf.keras.initializers.zeros(), return_sequences=True, name="LSTM1")(emb)
|
106 |
+
# #LSTM1_output = LSTM1(emb)
|
107 |
+
|
108 |
+
# LSTM2 = LSTM(units=256, activation='tanh', recurrent_activation='sigmoid', use_bias=True,
|
109 |
+
# kernel_initializer=tf.keras.initializers.glorot_uniform(seed=23),
|
110 |
+
# recurrent_initializer=tf.keras.initializers.orthogonal(seed=7),
|
111 |
+
# bias_initializer=tf.keras.initializers.zeros(), name="LSTM2")
|
112 |
+
# LSTM2_output = LSTM2(LSTM1)
|
113 |
+
|
114 |
+
# dropout1 = Dropout(0.5, name='dropout1')(LSTM2_output)
|
115 |
+
|
116 |
+
# dec = tf.keras.layers.Add()([dense1, dropout1])
|
117 |
+
|
118 |
+
# fc1 = Dense(256, activation='relu', kernel_initializer=tf.keras.initializers.he_normal(seed = 63), name='fc1')
|
119 |
+
# fc1_output = fc1(dec)
|
120 |
+
|
121 |
+
# dropout2 = Dropout(0.4, name='dropout2')(fc1_output)
|
122 |
+
|
123 |
+
# output_layer = Dense(1427, activation='softmax', name='Output_layer')
|
124 |
+
# output = output_layer(dropout2)
|
125 |
+
|
126 |
+
# encoder_decoder = Model(inputs = [input1, input2], outputs = output)
|
127 |
+
|
128 |
+
# encoder_decoder.load_weights("encoder_decoder_epoch_5.h5")
|
129 |
+
# # encoder
|
130 |
+
|
131 |
+
# encoder_input = encoder_decoder.input[0]
|
132 |
+
# encoder_output = encoder_decoder.get_layer('dense_encoder').output
|
133 |
+
# encoder_model = Model(encoder_input, encoder_output)
|
134 |
+
|
135 |
+
# # decoder#
|
136 |
+
# text_input = encoder_decoder.input[1]
|
137 |
+
# enc_output = Input(shape=(256,), name='Enc_Output')
|
138 |
+
# text_output = encoder_decoder.get_layer('LSTM2').output
|
139 |
+
# add1 = tf.keras.layers.Add()([text_output, enc_output])
|
140 |
+
# fc_1 = fc1(add1)
|
141 |
+
# decoder_output = output_layer(fc_1)
|
142 |
+
|
143 |
+
# decoder_model = Model(inputs = [text_input, enc_output], outputs = decoder_output)
|
144 |
+
|
145 |
+
# return encoder_model,decoder_model
|
146 |
|
147 |
|
148 |
def greedysearch(image):
|
149 |
# Open the pickle file for reading
|
150 |
train_data = pd.read_csv('Final_Train_Data.csv')
|
151 |
y_train = train_data['Report']
|
152 |
+
encoder_model, decoder_model = getModel()
|
153 |
input_ = 'startseq'
|
154 |
image_features = encoder_model.predict(image)
|
155 |
result = []
|