Skip to content

Fix transposed convolutions with unknown batch or spatial sizes, add 3D - #41

Open
Shaisolaris wants to merge 3 commits into
JesperDramsch:mainfrom
Shaisolaris:fix-transposed-conv-dynamic-shapes
Open

Shaisolaris wants to merge 3 commits into
JesperDramsch:mainfrom
Shaisolaris:fix-transposed-conv-dynamic-shapes

Conversation

@Shaisolaris

Copy link
Copy Markdown

Fixes #39.

The transposed convolution helpers built output_shape from the static input shape. During fit and predict the batch size is None, so ComplexConv1D(..., transposed=True) failed with a ConcatV2 type error, and any input with an unknown spatial size failed the same way in 1D and 2D. ComplexConv3D with transposed=True had no implementation and raised KeyError: 3.

Changes:

  • One shared helper builds the output shape from tf.shape(inputs), using the runtime batch size and the runtime value of any spatial size that is unknown at trace time. Static sizes still go through conv_utils.deconv_output_length.
  • conv1d_transpose, conv2d_transpose and the new conv3d_transpose swap the kernel's input and output channel axes, then call tf.nn.conv_transpose. 3D uses the (0, 1, 2, 4, 3) permutation.
  • README: removed transposed ComplexConv1D from the list of things that don't work.

Tests: the new TestTransposedConvTraining class in tests/test_conv.py compiles each model, calls predict and fits it for one epoch:

  • 1D with a fixed length;
  • 1D with variable lengths 8, 13 and 11;
  • 2D valid with variable sizes 6x6 and 5x9;
  • 3D.

On main all 4 fail. With this change they pass, and the full suite gives 85 passed, 1 xfailed. I ran this on CPU only, with TensorFlow 2.21 and Keras 3.15, and didn't test the channels_first path.

Build the transposed convolution output shape from tf.shape(inputs) so
the batch size and any spatial size that is unknown at trace time come
from the runtime input. ComplexConv1D and ComplexConv2D with
transposed=True now train and accept variable-size inputs, and
ComplexConv3D with transposed=True is implemented with
tf.nn.conv3d_transpose.

Fixes JesperDramsch#39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ComplexConv1D with transposed=True fails when the batch size is not known in advance

1 participant