Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...