我试图在ResNet50上添加一个Flatten图层,一个Dense图层(relu)和一个Dense图层(softmax),用于在Win10.Here上使用Keras 2.0.2 Theano 0.9.0 py2.7进行多分类任务我的代码:
def create_model():
base_model = ResNet50(include_top=False, weights=None,
input_tensor=None, input_shape=(3,224,224),
pooling=None)
base_model.load_weights(weight_path+'/resnet50_weights_th_dim_ordering_th_kernels_notop.h5')
x = base_model.output
x = Flatten()(x)
x = Dense(128,activation='relu',kernel_initializer='random_normal',
kernel_regularizer=regularizers.l2(0.1),
activity_regularizer=regularizers.l2(0.1))(x)
x=Dropout(0.3)(x)
y = Dense(8, activation='softmax')(x)
model = Model(base_model.input, y)
for layer in base_model.layers:
layer.trainable = False
model.compile(optimizer='adadelta',
loss='categorical_crossentropy')
return model
我设置了image_dim_ordering:
from keras import backend as K
K.set_image_dim_ordering('th')
这是我的Keras.json文件:
{
"backend": "theano",
``"image_data_format": "channels_first",
``"epsilon": 1e-07,
``"floatx": "float32"
}
以下是错误消息:
ValueError: The shape of the input to "Flatten" is not fully defined (got (2048, None, None). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
答案 0 :(得分:0)
你应该
将input_shape参数传递给第一层。这是一个形状元组(整数或无条目的元组,其中None表示可以预期任何正整数)。在input_shape中,不包括批量维度。
在您的情况下,第一层是Flatten()图层。它应该像
your_input = Input(shape=output_shape_of_resnet)
x = Flatten(your_input)
至于将resnet50的输出提供给您自己的图层,请考虑定义一个包含您自己的图层和resnet的新模型,例如
new_model = Sequential()
new_model.add(resnet_model) #Of course you need the definition and weights of resnet
resnet_model.trainable = False #I guess?
new_model.add(your_own_layers_model)
答案 1 :(得分:0)
如果输入图像的大小对于网络模型来说太小,我遇到了一些错误。如果图层的输出数据大小变为0,则会出现此错误。您可以使用model.summary()
查看您的网络外观。这是model.summary()
输出的示例:
Layer (type) Output Shape Param #
=================================================================
conv2d_78 (Conv2D) (None, 16, 21, 21) 160
_________________________________________________________________
max_pooling2d_62 (MaxPooling (None, 16, 5, 5) 0
_________________________________________________________________
...
flatten_25 (Flatten) (None, 32) 0
_________________________________________________________________
dense_28 (Dense) (None, 2) 1026
=================================================================
Total params: 31,970
Trainable params: 31,970
Non-trainable params: 0
_________________________________________________________________