类型错误:添加的图层必须是类图层的实例.发现:<keras.engine.training.Model 对象在 0x7fa5bee17ac8> [英] TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.training.Model object at 0x7fa5bee17ac8>

查看:23
本文介绍了类型错误:添加的图层必须是类图层的实例.发现:<keras.engine.training.Model 对象在 0x7fa5bee17ac8>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am try to train a model using Xception /Inception Model of keras library but I face value error

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

Dataset which I use it from kaggle commuinity and Notebook which I refer Notebook But I am try to use different Model like Xception /Inception but silmilar idea not work for me

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>

谢谢

推荐答案

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

You are mixing imports between keras and tf.keras libraries, they are not the same library and this combination is not supported.

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

You can import tf.keras.applications to get access to InceptionV3.

这篇关于类型错误:添加的图层必须是类图层的实例.发现:&lt;keras.engine.training.Model 对象在 0x7fa5bee17ac8&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆