我正在将一个项目从Keras 1.x迁移到2.x。
在代码中,在1.x中运行良好的keras.backend.conv2d
操作现在在2.x中崩溃。
convs = K.conv2d(a, b, padding='valid', data_format='channels_first')
在1.x中,输入张量形状
a
和b
均为(1024, 4, 1, 1)
,输出张量形状为(1024, 1024, 1, 1)
。使用2.x,出现以下错误:
ValueError: CorrMM: impossible output shape
bottom shape: 1024 x 4 x 1 x 1
weights shape: 1 x 1 x 1024 x 4
top shape: 1024 x 1 x -1022 x -2
Apply node that caused the error: CorrMM{valid, (1, 1), (1, 1), 1 False}(Print{message='a', attrs=('__str__',), global_fn=<function DEBUG_printTensorShape at 0x00000272EF1FAD08>}.0, Subtensor{::, ::, ::int64, ::int64}.0)
Toposort index: 30
Inputs types: [TensorType(float32, (False, False, True, True)), TensorType(float32, (True, True, False, False))]
Inputs shapes: [(1024, 4, 1, 1), (1, 1, 1024, 4)]
我正在使用Theano后端,并在
channels_first
和K.set_image_data_format
中都设置了conv2d
。 最佳答案
在conv2D
方法中,a
是实际图像,而b
是内核。a
的预期形状为(带有“ channels_first”):
(batchSize, channels, side1, side2)
因此,您的输入具有:
1024张图片
4通道
图片1 x 1
但是,尽管使用
'channels_last'
,但b
的预期形状为:(side1,side2, inputChannels,outputChannels)
这似乎有点误导,因为在过滤器中,它仍然是最后一个通道。 (已在我的keras 2.0.4版上测试)
因此,如果您的输出为
(1024,1024,1,1)
,则假定b
应该具有1024个输出过滤器,因此其形状应为:(1,1,4,1024)
您可能应该使用某种方法来排列尺寸,而不仅仅是重塑形状。 Numpy的
swapaxes
和keras的K.permute_dimensions
。