1

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 ?

sibiyes
  • 61
  • 7
  • It is advised not to mix ```keras``` and ```tensorflow.keras``` imports. All keras imports should be ```from tensorflow.keras.layers import Dense```. Maybe try that? – theastronomist Nov 17 '20 at 20:25
  • I made a change to not save the model as '.h5' file and allow tensorflow to save it in its won way by just specifying a directory path. And it worked when I did that. The new save command will be `model.save(model_folder + '/model')` – sibiyes Nov 18 '20 at 21:40

1 Answers1

1

You may need to specify the input_shape as per tf docs where it states

input_shape: optional shape tuple, only to be specified if include_top is False (otherwise the input shape has to be (224, 224, 3)

In your case vgg_model = VGG16(weights='imagenet', include_top=False, input_shape=((320, 640, 3))) may help?

Thomas Wright
  • 124
  • 2
  • 6