首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有自定义数据的Resnet

具有自定义数据的Resnet
EN

Stack Overflow用户
提问于 2018-03-09 15:34:16
回答 1查看 4.6K关注 0票数 2

我试图用我的自定义数据修改Resnet50,如下所示:

代码语言:javascript
复制
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的数组,包含标签的二进制表示形式。

以下是代码:

代码语言:javascript
复制
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

我打电话是为了发挥以下作用:

代码语言:javascript
复制
clf = self.__classifyRenet(X_train, y_train)

它犯了一个错误:

代码语言:javascript
复制
ValueError: Error when checking input: expected input_24 to have 4 dimensions, but got array with shape (785, 2000)

请帮帮忙。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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和其他体系结构所做的那样。当然,您还需要更改输出层,就像您所做的那样,以便您为您的类进行预测。试一试如下:

代码语言:javascript
复制
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的一部分:

代码语言:javascript
复制
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.
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49197132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档