1

I'm migrating a project from Keras 1.x to 2.x.

In the code, a keras.backend.conv2d operation that was running fine in 1.x now crashes in 2.x.

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

Input tensors shapes a and b are both (1024, 4, 1, 1) and output tensor shape was (1024, 1024, 1, 1) in 1.x.

With 2.x I'm getting the following error:

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)]

I'm using Theano backend, and set channels_first both in K.set_image_data_format and conv2d.

Overdrivr
  • 6,296
  • 5
  • 44
  • 70

1 Answers1

1

In the conv2D method, a is the actual image, and b is the kernel.


The expected shape for a is (with "channels_first"):

(batchSize, channels, side1, side2)

So, your input has:

  • 1024 images
  • 4 channels
  • image 1 x 1

But although using 'channels_last', the expected shape for b is:

(side1,side2, inputChannels,outputChannels)

This seems a little misleading, because in the filters, it is still channels last. (Tested on my keras, version 2.0.4)

So, if your output was (1024,1024,1,1), I assume b should have 1024 output filters, so it should be shaped as:

(1,1,4,1024)

You should probably use some method to permute the dimensions, not just reshape. Numpy has swapaxes and keras has K.permute_dimensions.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214