简体   繁体   English

尝试在ResNet50(notop)上添加Flatten图层并出现错误

[英]Try adding a Flatten layer on ResNet50(notop) and get an error

I am trying to add a Flatten layer, a Dense layer(relu) and a Dense layer(softmax) on ResNet50 for a multi-classification task using Keras 2.0.2 Theano 0.9.0 py2.7 on Win10.Here is my code: 我试图在Win10上使用Keras 2.0.2 Theano 0.9.0 py2.7在ResNet50上添加Flatten层,Dense层(relu)和Dense层(softmax)进行多分类任务,这是我的代码:

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

I have set image_dim_ordering: 我设置了image_dim_ordering:

from keras import backend as K
K.set_image_dim_ordering('th')

And here is my Keras.json file: 这是我的Keras.json文件:

{

"backend": "theano", ``"image_data_format": "channels_first", ``"epsilon": 1e-07, ``"floatx": "float32" } "backend": "theano", ``"image_data_format": "channels_first", ``"epsilon": 1e-07, ``"floatx": "float32" }

Here is the error message: 这是错误消息:

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.

You should 你应该

Pass an input_shape argument to the first layer. 将input_shape参数传递给第一层。 This is a shape tuple (a tuple of integers or None entries, where None indicates that any positive integer may be expected). 这是一个形状元组(整数元组或None条目,其中None表示可能期望任何正整数)。 In input_shape, the batch dimension is not included. 在input_shape中,不包括批次尺寸。

In your case, the first layer is Flatten() layer. 在您的情况下,第一层是Flatten()层。 It should be like 应该像

your_input = Input(shape=output_shape_of_resnet)
x = Flatten(your_input)

As for feeding the output of resnet50 into your own layers, consider defining a new model that incorporates your own layers and resnet, like 至于将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)

I had some error in case, when size of input image was too small for model of network. 当输入图像的大小对于网络模型而言太小时,我遇到了一些错误。 If the size of output data of layer become 0, this error appear. 如果图层的输出数据大小变为0,则会出现此错误。 Your may use model.summary() to see how your network looks. 您可以使用model.summary()来查看您的网络外观。 This is example of model.summary() output: 这是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
_________________________________________________________________

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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