我正在尝试在keras中实现3Dcnn模型,但是我如何调用模型存在问题。运行以下代码:
...
...
...
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)
我收到以下错误:
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>
有人可以告诉我我在这里做错了什么,如何正确定义模型?谢谢。
Window 10
Python 3.7.6
Tensorflow-gpu==1.14
Keras==2.3.1
Wrote the code based on keras 2 API
答案 0 :(得分:0)
您未正确将图层与功能性API配合使用,因为您未向图层提供输入。这是正确的方法:
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)