Python 尝试在ResNet50(notop)上添加一个展平层,并得到一个错误
我正在尝试在ResNet50上为使用Win10上的Keras 2.0.2 Theano 0.9.0 py2.7的多分类任务添加展平层、密集层(relu)和密集层(softmax)。以下是我的代码:Python 尝试在ResNet50(notop)上添加一个展平层,并得到一个错误,python,neural-network,theano,keras,Python,Neural Network,Theano,Keras,我正在尝试在ResNet50上为使用Win10上的Keras 2.0.2 Theano 0.9.0 py2.7的多分类任务添加展平层、密集层(relu)和密集层(softmax)。以下是我的代码: def create_model(): base_model = ResNet50(include_top=False, weights=None, input_tensor=None, input_shape=(3,224,224),
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
我已设置图像尺寸顺序:
from keras import backend as K
K.set_image_dim_ordering('th')
这是我的Keras.json文件:
{
“后端”:“theano”,
``“图像数据格式”:“通道优先”,
``“ε”: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.
你应该
将输入_形状参数传递给第一层。这是一个形状元组(整数或无项的元组,其中无表示可能需要任何正整数)。在输入形状中,不包括批次维度
在您的例子中,第一层是flatte()层。应该是
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)
我有一些错误的情况下,当输入图像的大小太小的网络模型。如果图层输出数据的大小变为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
_________________________________________________________________
错误堆栈跟踪是什么?我可能应该提到,如果我不添加行,那么一切都可以正常工作:
base\u model.load\u weights(weight\u path+”/resnet50\u weights\u dim\u ordering\u the kernels\u notop.