简体   繁体   English

即使在图像转换后,Keras CNN图像和内核大小也不匹配

[英]Keras CNN images and kernel size mismatch even after image transformation to fit

I'm trying to run a CNN similar to the one in the Keras documantation "VGG-like convnet" but for a custom set of images and binary classification instead of a 10-class output. 我正在尝试运行类似于Keras文档“VGG-like convnet”中的CNN,但是用于自定义图像集和二进制分类而不是10级输出。

When I try to fit the CNN, i get this longwinded error that I assume is telling me my input image size is not the right size for the CNN input. 当我尝试适应CNN时,我得到了这个漫长的错误,我假设它告诉我输入的图像大小不是CNN输入的正确大小。

ValueError: GpuDnnConv images and kernel must have the same stack size

Apply node that caused the error: GpuDnnConv{algo='small', inplace=True}(GpuContiguous.0, GpuContiguous.0, GpuAllocEmpty.0, GpuDnnConvDesc{border_mode='valid', subsample=(1, 1), conv_mode='conv', precision='float32'}.0, Constant{1.0}, Constant{0.0})
Toposort index: 130
Inputs types: [CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, 4D), <theano.gof.type.CDataType object at 0x7f0eefc8d790>, Scalar(float32), Scalar(float32)]
Inputs shapes: [(32, 232, 300, 3), (300, 1, 3, 3), (32, 300, 298, 1), 'No shapes', (), ()]
Inputs strides: [(208800, 900, 3, 1), (9, 0, 3, 1), (89400, 298, 1, 0), 'No strides', (), ()]
Inputs values: ['not shown', 'not shown', 'not shown', <PyCObject object at 0x7f0efaba8e68>, 1.0, 0.0]
Inputs name: ('image', 'kernel', 'output', 'descriptor', 'alpha', 'beta')

The thing is I thought I reshaped all my images to fit. 事情是我认为我重塑了所有图像以适应。 My input is a stack of 4000 232x300 px RBG images and the output is an array of 4000 boolean values. 我的输入是4000个232x300像素RBG图像的堆栈,输出是4000个布尔值的数组。

Input : im_list.shape Out[49]: (4000, 232, 300, 3) 输入im_list.shape Out[49]: (4000, 232, 300, 3) :( im_list.shape Out[49]: (4000, 232, 300, 3)

Output : np.asarray(cls).shape Out[50]: (4000,) 输出np.asarray(cls).shape Out[50]: (4000,)

This is the function to build the CNN 这是构建CNN的功能

CNN = buildCNN(3, 232, 300, 2)
CNN.fit(im_list, cls, batch_size=32, nb_epoch=1)

    def buildCNN(depth,width,height,outputShape):
    CNN = Sequential()
        # input: 232x300 images with 3 channels -> (3, 100, 100) tensors.
        # this applies 32 convolution filters of size 3x3 each.
        CNN.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(depth,width,height)))
        CNN.add(Activation('relu'))
        CNN.add(Convolution2D(32, 3, 3))
        CNN.add(Activation('relu'))
        CNN.add(MaxPooling2D(pool_size=(2, 2)))
        CNN.add(Dropout(0.25))
        #
        CNN.add(Convolution2D(64, 3, 3, border_mode='valid'))
        CNN.add(Activation('relu'))
        CNN.add(Convolution2D(64, 3, 3))
        CNN.add(Activation('relu'))
        CNN.add(MaxPooling2D(pool_size=(2, 2)))
        CNN.add(Dropout(0.25))
        #
        CNN.add(Flatten())
        # Note: Keras does automatic shape inference.
        CNN.add(Dense(256))
        CNN.add(Activation('relu'))
        CNN.add(Dropout(0.5))
        #
        CNN.add(Dense(outputShape))
        CNN.add(Activation('softmax'))
        #
        sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
        CNN.compile(loss='categorical_crossentropy', optimizer=sgd)
        #
    return CNN

I've been banging my head against the wall with this long enough that I thought maybe someone else has had this problem. 我已经用这么长的时间撞到了墙上,以为我认为也许其他人有这个问题。 Any thoughts? 有什么想法吗? Thanks in advance. 提前致谢。

You specified input as (depth,width,height) . 您将输入指定为(depth,width,height) So the array you must must have dimensions (N,depth,width,height) , where N is the number of training examples. 因此,您必须具有尺寸(N,depth,width,height)的数组,其中N是训练示例的数量。

The input you are actually passing, (4000, 232, 300, 3) , doesn't match. 您实际传递的输入(4000, 232, 300, 3)不匹配。 It should be reshaped to be (4000, depth, width, height) . 它应重新塑造为(4000, depth, width, height) This means you'll have to resize each image, and reorder the axes. 这意味着您必须调整每个图像的大小,并重新排序轴。

上面的答案是正确的:对于后代,我的问题通过一个简单的解决:

im_list = im_list.transpose((0,3,1,2))

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

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