我正在将一个项目从keras 1.x迁移到2.x。
在代码中,在1.x中运行良好的keras.backend.conv2d
操作现在在2.x中崩溃。
convs = K.conv2d(a, b, padding='valid', data_format='channels_first')
输入张量 a
和 b
均为 (1024, 4, 1, 1)
,输出张量为 (1024, 1024, 1, 1)
在1.x中。
使用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后端,并在K.set_image_data_format
和conv2d
中设置channels_first
。
在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
。