我试图用我的自定义数据修改Resnet50,如下所示:
X = [[1.85, 0.460,... -0.606] ... [0.229, 0.543,... 1.342]]
y = [2, 4, 0, ... 4, 2, 2]X是一个长度为2000的特征向量,用于784幅图像。Y是一个大小为784的数组,包含标签的二进制表示形式。
以下是代码:
def __classifyRenet(self, X, y):
image_input = Input(shape=(2000,1))
num_classes = 5
model = ResNet50(weights='imagenet',include_top=False)
model.summary()
last_layer = model.output
# add a global spatial average pooling layer
x = GlobalAveragePooling2D()(last_layer)
# add fully-connected & dropout layers
x = Dense(512, activation='relu',name='fc-1')(x)
x = Dropout(0.5)(x)
x = Dense(256, activation='relu',name='fc-2')(x)
x = Dropout(0.5)(x)
# a softmax layer for 5 classes
out = Dense(num_classes, activation='softmax',name='output_layer')(x)
# this is the model we will train
custom_resnet_model2 = Model(inputs=model.input, outputs=out)
custom_resnet_model2.summary()
for layer in custom_resnet_model2.layers[:-6]:
layer.trainable = False
custom_resnet_model2.layers[-1].trainable
custom_resnet_model2.compile(loss='categorical_crossentropy',
optimizer='adam',metrics=['accuracy'])
clf = custom_resnet_model2.fit(X, y,
batch_size=32, epochs=32, verbose=1,
validation_data=(X, y))
return clf我打电话是为了发挥以下作用:
clf = self.__classifyRenet(X_train, y_train)它犯了一个错误:
ValueError: Error when checking input: expected input_24 to have 4 dimensions, but got array with shape (785, 2000)请帮帮忙。谢谢!
发布于 2018-03-09 19:01:06
1.首先,理解错误.
您的输入与ResNet的输入不匹配,对于ResNet,输入应该是(n_sample,224,224,3),但是您有(785,2000)。从您的问题中,您有784幅大小为2000的图像,这与原始的ResNet50输入形状(224x224)并不一致,不管您如何重塑它。这意味着您不能对数据直接使用ResNet50。您在代码中所做的唯一的事情就是获取ResNet50的最后一层,并添加输出层以与输出类大小保持一致。
2。那么,你能做什么。。
如果坚持使用ResNet架构,则需要更改输入层而不是输出层。此外,您将需要重塑您的图像数据,以利用卷积层。这意味着,您不能将它放在(2000,)数组中,而是需要类似于(height, width, channel),就像ResNet和其他体系结构所做的那样。当然,您还需要更改输出层,就像您所做的那样,以便您为您的类进行预测。试一试如下:
model = ResNet50(input_tensor=image_input_shape, include_top=True,weights='imagenet')这样,您就可以指定自定义的输入图像形状。您可以检查github代码以获得更多信息(https://github.com/keras-team/keras/blob/master/keras/applications/resnet50.py)。以下是docstring的一部分:
input_shape: optional shape tuple, only to be specified
if `include_top` is False (otherwise the input shape
has to be `(224, 224, 3)` (with `channels_last` data format)
or `(3, 224, 224)` (with `channels_first` data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 197.
E.g. `(200, 200, 3)` would be one valid value.https://stackoverflow.com/questions/49197132
复制相似问题