3

I'm using the following code to load an imagenet pre-trained VGG19 model and fit to my custom dataset.

from keras.applications.vgg19 import VGG19


optim = tf.keras.optimizers.RMSprop(momentum=0.9)
vgg19 = VGG19(include_top=False, weights='imagenet', input_tensor=tf.keras.layers.Input(shape=(224, 224, 3)))
vgg19.trainable = False
# x = keras.layers.GlobalAveragePooling2D()(model_vgg19_pt.output)
x = keras.layers.Flatten()(vgg19.output)
output = keras.layers.Dense(n_classes, activation='softmax')(x)
model_vgg19_pt = keras.models.Model(inputs=[vgg19.input], outputs=[output])
model_vgg19_pt.compile(optimizer=optim,
                       loss='categorical_crossentropy', metrics=['categorical_accuracy'])
callback = tf.keras.callbacks.LearningRateScheduler(scheduler)
model_vgg19_pt.fit(x_train, y_train, batch_size=20,
                             epochs=50, callbacks=[callback]
                             )

on model.fit() line, I get the following error

KeyError: 'The optimizer cannot recognize variable dense_1/kernel:0. This usually means you are trying to call the optimizer to update different parts of the model separately. Please call optimizer.build(variables) with the full list of trainable variables before the training loop or use legacy optimizer `tf.keras.optimizers.legacy.{self.class.name}.'

What does it mean and how can I fix it?

I get the same errors for

keras.applications.inception_v3

too, when using the same implementation method.

Additionally, this was working with jupyter notebook file on tensorflow cpu, but when running on a remote machine with tensorflow-gpu installed, I'm getting these errors.

This works fine with optimizer SGD, but not with RMSprop. why?

Additional Using this:

model_vgg19_pt.compile(optimizer=tf.keras.optimizers.RMSprop(momentum=0.9),
                           loss='categorical_crossentropy', metrics=['categorical_accuracy'])

instead as used above works. But can somebody explain why....

shey
  • 33
  • 1
  • 7
  • Are you running this on Python 3.11? I am getting this error too since upgrading... – wildcat89 Dec 29 '22 at 19:51
  • Hello, did you manage to solve this and would you know how to solve it for the Adam optimiser? Thanks – sirj Jan 19 '23 at 03:32
  • 1
    please check the implementation below *Additional* in the question itself, thats the only way it worked for me. I think defining the optimizer in a variable didn't work, but it works as long as I initialize *optimizer* parameter in compile() – shey Jan 19 '23 at 21:53
  • Yes I faced the same issue, and the error goes away by not defining the optimizer as a variable. This was not an issue two months ago, when I ran the SAME code. Very strange. – Danny Feb 20 '23 at 16:41

3 Answers3

6

Use a legacy optimizer

 tf.keras.optimizers.legacy.SGD(learning_rate=0.1)
niran
  • 1,920
  • 8
  • 34
  • 60
2

When I upgraded from Python 3.9.5 to 3.10.9 with tensorflow==2.11.0,

I got the similar error for Keras Sequential model

KeyError: 'The optimizer cannot recognize variable dense_24/kernel:0. This usually means you are trying to call the optimizer to update different parts of the model separately. Please call `optimizer.build(variables)` with the full list of trainable variables before the training loop or use legacy optimizer `tf.keras.optimizers.legacy.{self.__class__.__name__}.'

Renamed the library to legacy optimizer

from

from tensorflow.keras.optimizers import SGD

to

from tensorflow.keras.optimizers.legacy import SGD

It worked. #niran comment is correct

hanzgs
  • 1,498
  • 17
  • 44
0

Which version of Tensorflow GPU have you installed? TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows. Please check the link to install TensorFlow by following all the Hardware/Software requirements for the GPU support.

The LearningRateScheduler arguments in callback is not defined which you are passing while model compilation.

I was able to train the model after removing the callback from model.fit(). (Attaching the gist here for your reference)

  • I couldn't share the full code, but thanks for pointing it, should've removed the part for easier reproducibility. – shey Apr 17 '23 at 15:02