简体   繁体   中英

How can I resolve ValueError: Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata).?

I'm trying to implement a 3Dcnn model in keras, but I have a problem with how my model is called. Running the following code:

...
...
...
input_layer = Input((16, 16, 16, 3))
x = inception_v4_stem(input_layer)
for i in range(num_A_blocks):
    x = inception_v4_A(x)
x = inception_v4_reduction_A(x)
for i in range(num_B_blocks):
    x = inception_v4_B(x)
x = inception_v4_reduction_B(x)
for i in range(num_C_blocks):
    x = inception_v4_C(x)

x = AveragePooling3D((4, 4, 4), strides=(1, 1, 1), padding="same", data_format="channels_last")
x = Dropout(0.5)
x = Flatten()

x = Dense(nb_classes, activation='softmax')

## define the model with input layer and output layer
model = Model(inputs = input_layer, outputs = x)

model.summary()

model.compile(loss=categorical_crossentropy, optimizer=Adadelta(lr=0.1), metrics=['acc'])
model.fit(x=xtrain, y=y_train, batch_size=128, epochs=50, validation_split=0.2)

I get the following error:

Traceback (most recent call last):
  File "kI3DV2y.py", line 275, in <module>
    model = Model(inputs = [input_layer], outputs = x)
  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 94, in __init__
    self._init_graph_network(*args, **kwargs)
  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 198, in _init_graph_network
    'Found: ' + str(x))
ValueError: Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata). Found: <keras.layers.core.Dense object at 0x000001EDAEC47348>

Can someone please tell me what i'm doing wrong here and how can I correctly define my model? Thanks in anticipation.

Window 10
Python 3.7.6
Tensorflow-gpu==1.14
Keras==2.3.1
Wrote the code based on keras 2 API

You are not correctly using your layer with the Functional API, as you are not giving inputs to layers. This is the correct way:

x = AveragePooling3D((4, 4, 4), strides=(1, 1, 1), padding="same", data_format="channels_last")(x)
x = Dropout(0.5)(x)
x = Flatten()(x)

x = Dense(nb_classes, activation='softmax')(x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM