message: Add function to return the channel axis in convolutional layers.
diff: @@ -145,10 +145,7 @@ class Conv(Layer):
  def build(self, input_shape):
  input_shape = tensor_shape.TensorShape(input_shape)
  - if self.data_format == 'channels_first':
  - channel_axis = 1
  - else:
  - channel_axis = -1
  + channel_axis = self._get_channel_axis()
  if input_shape.dims[channel_axis].value is None:
  raise ValueError('The channel dimension of the inputs '
  'should be defined. Found `None`.')
  @@ -271,6 +268,12 @@ class Conv(Layer):
  causal_padding = [[0, 0], [0, 0], [left_pad, 0]]
  return causal_padding
  + def _get_channel_axis(self):
  + if self.data_format == 'channels_first':
  + return 1
  + else:
  + return -1
  +
  @keras_export('keras.layers.Conv1D', 'keras.layers.Convolution1D')
  class Conv1D(Conv):
  @@ -757,10 +760,7 @@ class Conv2DTranspose(Conv2D):
  if len(input_shape) != 4:
  raise ValueError('Inputs should have rank 4. Received input shape: ' +
  str(input_shape))
  - if self.data_format == 'channels_first':
  - channel_axis = 1
  - else:
  - channel_axis = -1
  + channel_axis = self._get_channel_axis()
  if input_shape.dims[channel_axis].value is None:
  raise ValueError('The channel dimension of the inputs '
  'should be defined. Found `None`.')
  @@ -1030,10 +1030,7 @@ class Conv3DTranspose(Conv3D):
  if len(input_shape) != 5:
  raise ValueError('Inputs should have rank 5, received input shape:',
  str(input_shape))
  - if self.data_format == 'channels_first':
  - channel_axis = 1
  - else:
  - channel_axis = -1
  + channel_axis = self._get_channel_axis()
  if input_shape.dims[channel_axis].value is None:
  raise ValueError('The channel dimension of the inputs '
  'should be defined, found None: ' + str(input_shape))
  @@ -1290,10 +1287,7 @@ class SeparableConv(Conv):
  def build(self, input_shape):
  input_shape = tensor_shape.TensorShape(input_shape)
  - if self.data_format == 'channels_first':
  - channel_axis = 1
  - else:
  - channel_axis = -1
  + channel_axis = self._get_channel_axis()
  if input_shape.dims[channel_axis].value is None:
  raise ValueError('The channel dimension of the inputs '
  'should be defined. Found `None`.')
  @@ -1786,10 +1780,7 @@ class DepthwiseConv2D(Conv2D):
  raise ValueError('Inputs to `DepthwiseConv2D` should have rank 4. '
  'Received input shape:', str(input_shape))
  input_shape = tensor_shape.TensorShape(input_shape)
  - if self.data_format == 'channels_first':
  - channel_axis = 1
  - else:
  - channel_axis = 3
  + channel_axis = self._get_channel_axis()
  if input_shape.dims[channel_axis].value is None:
  raise ValueError('The channel dimension of the inputs to '
  '`DepthwiseConv2D` '
  