mimus
mimus

Reputation: 367

Cannot use vggface-keras in Tensorflow 2.0

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.

Upvotes: 2

Views: 9142

Answers (4)

duhaime
duhaime

Reputation: 27603

I also needed to use vggface in Tensorflow 2 so created this fork of keras-vggface. You should be able to use python setup.py install to install it (after cloning).

Upvotes: 0

sefiks
sefiks

Reputation: 1650

VGG-Face is wrapped in deepface framework for python. Just pass VGG-Face string to model name variable.

#!pip install deepface
from deepface import DeepFace
obj = DeepFace.verify([
      ["img1.jpg", "img2.jpg"],
      ["img1.jpg", "img3.jpg"],
      ["img1.jpg", "img4.jpg"],
   ]
   , model_name = "VGG-Face")
print(obj)

This block will check img1 among img2, img3, img4.

Upvotes: -1

mimus
mimus

Reputation: 367

here is a page, where you can download the .h5 file with the weights of the vggface model, so we can use it to train in tensorflow with higher versions than 1.15

https://sefiks.com/2018/09/03/face-recognition-with-facenet-in-keras/

Upvotes: 1

Dr. Snoopy
Dr. Snoopy

Reputation: 56377

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.

Upvotes: 4

Related Questions