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

mar*_*rko 6 python rgb theano conv-neural-network keras

我正在尝试运行类似于Keras文档"VGG-like convnet"中的CNN,但用于自定义图像集和二进制分类而不是10级输出.

当我尝试适应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')
Run Code Online (Sandbox Code Playgroud)

事情是我认为我重塑了所有图像以适应.我的输入是4000个232x300像素RBG图像的堆栈,输出是4000个布尔值的数组.

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

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

这是构建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
Run Code Online (Sandbox Code Playgroud)

我已经用这么长的时间撞到了墙上,以为我认为也许其他人有这个问题.有什么想法吗?提前致谢.

yhe*_*non 6

您将输入指定为(depth,width,height).因此,您必须具有维度的数组(N,depth,width,height),其中N是训练示例的数量.

您实际传递的输入(4000, 232, 300, 3)不匹配.它应该被重塑为(4000, depth, width, height).这意味着您必须调整每个图像的大小,并重新排序轴.


mar*_*rko 6

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

im_list = im_list.transpose((0,3,1,2))
Run Code Online (Sandbox Code Playgroud)