【问题标题】:TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.training.Model object at 0x7fa5bee17ac8>TypeError:添加的层必须是类Layer的实例。找到:<keras.engine.training.Model object at 0x7fa5bee17ac8>
【发布时间】:2020-06-06 11:55:22
【问题描述】:

我正在尝试使用 keras 库的 Xception / Inception 模型训练模型,但我面临价值错误

我从 kaggle 社区和 Notebook 中使用的数据集 Notebook 但我尝试使用不同的模型,如 Xception /Inception 但类似的想法对我不起作用

with strategy.scope():
    enet = keras.applications.inception_v3.InceptionV3(
        input_shape=(512, 512, 3),
        weights='imagenet',
        include_top=False
)

model = tf.keras.Sequential([
    enet,
    tf.keras.layers.GlobalAveragePooling2D(),
    tf.keras.layers.Dense(len(CLASSES), activation='softmax')
])

model.compile(
    optimizer=tf.keras.optimizers.Adam(lr=0.0001),
    loss = 'sparse_categorical_crossentropy',
    metrics=['sparse_categorical_accuracy']
)
 model.summary()

我面临的错误


--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-30d5c6cc8c12> in <module>
     11         enet,
     12         tf.keras.layers.GlobalAveragePooling2D(),
---> 13         tf.keras.layers.Dense(len(CLASSES), activation='softmax')
     14     ])
     15 

/opt/conda/lib/python3.6/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

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/sequential.py in 
 __init__(self, layers, name)
    114       tf_utils.assert_no_legacy_layers(layers)
    115       for layer in layers:
--> 116         self.add(layer)
    117 
    118   @property

  /opt/conda/lib/python3.6/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

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/sequential.py in add(self, 
layer)
    159       raise TypeError('The added layer must be '
    160                       'an instance of class Layer. '
--> 161                       'Found: ' + str(layer))
    162 
    163     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 0x7fa5bee17ac8>

谢谢

【问题讨论】:

    标签: python tensorflow keras deep-learning


    【解决方案1】:

    您在 kerastf.keras 库之间混合导入,它们不是同一个库,并且不支持这种组合。

    您可以导入tf.keras.applications 以访问InceptionV3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2019-08-14
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多