I am working on an image caption model. While training the model I am getting a compile error. I tried many solutions but can't seem to solve the problem. Also, my code was in old Keras version. I am using Keras 2.2.4: Please see below the code that in written in old Keras version:
import numpy as np
import pandas as pd
from keras.models import Sequential, Model
from keras.layers import LSTM, Embedding, TimeDistributed, Dense, RepeatVector, Activation
from keras.layers.merge import Concatenate
from keras.preprocessing import image, sequence
import pickle as pickle
def create_model(self, ret_model = False):
image_model = Sequential()
image_model.add(Dense(EMBEDDING_DIM, input_dim = 4096, activation='relu'))
image_model.add(RepeatVector(self.max_length))
lang_model = Sequential()
lang_model.add(Embedding(self.vocab_size, 256, input_length=self.max_length))
lang_model.add(LSTM(256,return_sequences=True))
lang_model.add(TimeDistributed(Dense(EMBEDDING_DIM)))
model = Sequential()
model.add(Merge([image_model, lang_model], mode='concat'))
model.add(LSTM(1000,return_sequences=False))
model.add(Dense(self.vocab_size))
model.add(Activation('softmax'))
print ("Model created!")
if(ret_model==True):
return model
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
return model
I changed the code to like this. The libraries are the same as i showed in the first code. Please see below the changed code:
def create_model(self, ret_model = False):
image_model = Sequential()
image_model.add(Dense(EMBEDDING_DIM, input_dim = 4096, activation='relu'))
image_model.add(RepeatVector(self.max_length))
lang_model = Sequential()
lang_model.add(Embedding(self.vocab_size, 256, input_length=self.max_length))
lang_model.add(LSTM(256, return_sequences=True))
lang_model.add(TimeDistributed(Dense(EMBEDDING_DIM)))
model = Sequential()
#model.add(Merge([image_model, lang_model], mode='concat'))
model.add(Concatenate([image_model, lang_model])) #changed the above commented line to this
model.add(LSTM(1000, return_sequences=False))
model.add(Dense(self.vocab_size))
model.add(Activation('softmax'))
print ("Model created!")
if(ret_model==True):
return model
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
return model
When I use this above-changed code and train my model I get a runtime error which says I must compile my code. Though I am compiling my code. Please see the error below:
Using TensorFlow backend.
413439
WARNING:tensorflow:From C:\Users\UserName\Anaconda3\lib\site-
packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from
tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Model created!
Traceback (most recent call last):
File "D:\Image-caption\Image-Captioning-master\train.py", line 14, in <module>
train(int(sys.argv[1]))
File "D:\Image-caption\Image-Captioning-master\train.py", line 9, in train
model.fit_generator(sd.data_process(batch_size=batch_size),
steps_per_epoch=sd.no_samples/batch_size, epochs=epoch, verbose=2, callbacks=None)
File "C:\Users\UserName\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in
wrapper
return func(*args, **kwargs)
File "C:\Users\UserName\Anaconda3\lib\site-packages\keras\engine\training.py", line 1418, in
fit_generator
initial_epoch=initial_epoch)
File "C:\Users\UserName\Anaconda3\lib\site-packages\keras\engine\training_generator.py", line 40,
in fit_generator
model._make_train_function()
File "C:\Users\UserName\Anaconda3\lib\site-packages\keras\engine\training.py", line 496, in
_make_train_function
raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.
This is my train.py file code:
import SceneDesc
import sys
def train(epoch):
sd = SceneDesc.scenedesc()
model = sd.create_model()
batch_size = 512
model.fit_generator(sd.data_process(batch_size=batch_size),
steps_per_epoch=sd.no_samples/batch_size, epochs=epoch, verbose=2, callbacks=None)
model.save('Output/Model.h5', overwrite=True)
model.save_weights('Output/Weights.h5',overwrite=True)
if __name__=="__main__":
train(int(sys.argv[1]))
I am a beginner and in a learning phase. I am trying to solve this probe for quite some time but can't seem to solve the issue. I have also provide the link of my files in dpaste. Here is the link: https://dpaste.de/t2yU and https://dpaste.de/cVGP ... Though these are only two files. I tried my best to ask a clear question. I hope to get a positive response. :) Any help will be apreciated.