无法在 Tensorflow 2.0 中使用 vggface-keras

Cannot use vggface-keras in Tensorflow 2.0

我正在尝试使用 https://github.com/rcmalli/keras-vggface 中的 keras-vggface 库来训练 CNN。我已经安装了 tensorflow 2.0.0-rc1、keras 2.3.1、cuda 10.1、cudnn 7.6.5 并且驱动程序的版本是 418,问题是当我尝试使用 vggface 模型作为卷积基时,我得到一个错误,这是代码和错误

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'))

错误!

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>

我希望你能告诉我为什么会出现这个错误以及如何解决它,感谢阅读。

问题是 kerastf.keras 之间不兼容。您正在使用的库 (vggface-keras) 使用 keras,而您的代码使用 tf.keras。这行不通。

唯一可能的解决方案是对整个管道使用 keras,或者修改 vggface-keras 库以使用 tf.keras,包括修改所有导入并修复任何出现的错误。

这是一个页面,你可以在这里下载带有vggface模型权重的.h5文件,所以我们可以用它来训练1.15以上版本的tensorflow

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

VGG-Face 封装在 python 的深度面部框架中。只需将 VGG-Face 字符串传递给模型名称变量即可。

#!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)

此块将检查 img2、img3、img4 中的 img1。

我还需要在如此创建的 Tensorflow 2 中使用 vggface this fork of keras-vggface。您应该能够使用 python setup.py install 来安装它(在克隆之后)。