I am trying to use the keras-vggface library from https://github.com/rcmalli/keras-vggface to train a CNN. I have installed tensorflow 2.0.0-rc1, keras 2.3.1, cuda 10.1, cudnn 7.6.5 and the driver’s version is 418, the problem is that when i try to use the vggface model, as a convolutional base, i get an error, here is the code and the error
from keras_vggface.vggface import VGGFace conv_base = VGGFace(model='vgg16', include_top=False) model = models.Sequential() model.add(conv_base) model.add(layers.Flatten()) model.add(layers.Dense(1024, activation='relu')) model.add(layers.Dense(800, activation='softmax'))
Error!
TypeError Traceback (most recent call last) <ipython-input-4-f6b5cad8f44b> in <module> 1 #arquitectura 2 model = models.Sequential() ----> 3 model.add(conv_base) 4 model.add(layers.Flatten()) 5 model.add(layers.Dense(1024, activation='relu')) ~/anaconda3/envs/vggface/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access ~/anaconda3/envs/vggface/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/sequential.py in add(self, layer) 156 raise TypeError('The added layer must be ' 157 'an instance of class Layer. ' --> 158 'Found: ' + str(layer)) 159 160 tf_utils.assert_no_legacy_layers([layer]) TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.training.Model object at 0x7f0bf03db210>
I hope you can tell me why i get this error and how to solve it, thanks for reading.
Advertisement
Answer
The problem is incompatibility between keras
and tf.keras
. The library you are using (vggface-keras), uses keras
, while your code uses tf.keras
. This won’t work.
The only possible solutions is you to use keras
for your whole pipeline, or for you to modify the vggface-keras
library to use tf.keras
, including modifying all imports and fixing any bugs that appear.