TypeError:添加的图层必须是类Layer的实例.找到:< keras.engine.training.Model对象,位于0x7fa5bee17ac8>. [英] TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.training.Model object at 0x7fa5bee17ac8>

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

问题描述

我尝试使用keras库的Xception/Inception Model训练模型,但是我面对价值错误

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

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

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.

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

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