将keras.backend.conv2d从Keras 1.x迁移到2.x

问题描述:

我正在将项目从Keras 1.x迁移到2.x.将keras.backend.conv2d从Keras 1.x迁移到2.x

在代码中,在1.x中正常运行的keras.backend.conv2d操作现在在2.x中崩溃。

convs = K.conv2d(a, b, padding='valid', data_format='channels_first') 

输入张量形状ab均为(1024, 4, 1, 1)和输出张量形状1.x中是(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后端,并在K.set_image_data_formatconv2d设置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

+0

'K.permute_dimensions(x,(3,2,1,0))'完成了这项工作。谢谢 ! – Overdrivr