I built a model that consists of a pre-learnt model followed by some layers that are added to the end. Everything works well and I can train the model on my dataset and save the trained model to file. But when I try to load the saved model, I get the following error. I get that this points to the Dense layer shape being undefined. However I specified the input shape to the model before training and made sure that the resulting Dense layer will have a predetermined shape. Not sure how to address the issue
This is the model that was designed
import tensorflow as tf
from tensorflow.keras.utils import Sequence
from tensorflow.keras.applications.vgg16 import VGG16
from keras.layers.core import Dense, Dropout, Activation, Flatten
from tensorflow.keras.applications.vgg16 import VGG16
def create_model():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Input((320, 640, 3)))
vgg_model = VGG16(weights='imagenet', include_top=False)
model.add(vgg_model)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(256))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(128))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(1))
model.add(tf.keras.layers.Activation('linear'))
optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001)
model.compile(loss = 'mean_squared_error', optimizer = optimizer)
return model
model = create_model()
model.fit(data_generator, epochs = 1)
model.save(model_folder + '/model.h5')
After training the model on my dataset I am trying to load into in an other script
model = tf.keras.models.load_model(model_folder + '/model.h5')
This leads to the following error
raise ValueError('The last dimension of the inputs to `Dense` '
ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.
I am using tensorflow version 2.1.0 and keras version 2.3.1
Any idea, why this is happening and how to resolve this issue ?