Migrating keras.backend.conv2d from Keras 1.x to 2.x



我正在将一个项目从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),输出张量为 (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_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

相关内容

最新更新