【问题标题】:Try adding a Flatten layer on ResNet50(notop) and get an error尝试在 ResNet50(notop) 上添加一个 Flatten 层并得到一个错误
【发布时间】:2017-09-04 09:00:39
【问题描述】:

我正在尝试在 ResNet50 上添加一个 Flatten 层、一个 Dense 层(relu)和一个 Dense 层(softmax),以便在 Win10 上使用 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.

【问题讨论】:

  • 错误堆栈跟踪是什么?
  • 我可能应该提到,如果我不添加该行,那么一切正常:base_model.load_weights(weight_path+'/resnet50_weights_th_dim_ordering_th_kernels_notop.

标签: python neural-network theano keras


【解决方案1】:

你应该

将 input_shape 参数传递给第一层。这是一个形状元组(整数或 None 条目的元组,其中 None 表示可能需要任何正整数)。 input_shape中不包含batch维度。

在您的情况下,第一层是 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)

【讨论】:

    【解决方案2】:

    当输入图像的大小对于网络模型来说太小时,我遇到了一些错误。如果层的输出数据大小变为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
    _________________________________________________________________
    

    【讨论】:

      猜你喜欢
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多