TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.training.Model object at 0x7fa5bee17ac8>

家住魔仙堡 提交于 2021-02-02 09:56:45

问题


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

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

Error WHich I Face


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

Thanks


回答1:


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

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



来源:https://stackoverflow.com/questions/60349042/typeerror-the-added-layer-must-be-an-instance-of-class-layer-found-keras-eng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!