2

I have a pre-trained Keras model (in h5 format) where all the layers operate on channel first data format. I want to convert this model to operate on the channel's last data format (the default data format). Does TensorFlow support this out of the box? Any help or pointers on how to do this is much appreciated.

For some clarity, the current model summary looks like this:

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input0 (InputLayer)             [(None, 3, 240, 320) 0
__________________________________________________________________________________________________
259_pad (ZeroPadding2D)         (None, 3, 242, 322)  0           input0[0][0]
__________________________________________________________________________________________________
259 (Conv2D)                    (None, 16, 120, 160) 432         259_pad[0][0]
__________________________________________________________________________________________________
260 (BatchNormalization)        (None, 16, 120, 160) 64          259[0][0]
__________________________________________________________________________________________________
261 (Activation)                (None, 16, 120, 160) 0           260[0][0]
__________________________________________________________________________________________________
262_pad (ZeroPadding2D)         (None, 16, 122, 162) 0           261[0][0]
__________________________________________________________________________________________________
262 (DepthwiseConv2D)           (None, 16, 120, 160) 144         262_pad[0][0]

As you can see, it's in the channel first format. I want to convert each layer in the model to operate on channel last format. So the ideal model summary will be as follows:

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input0 (InputLayer)             [(None, 240, 320, 3) 0
__________________________________________________________________________________________________
259_pad (ZeroPadding2D)         (None, 242, 322, 3)  0           input0[0][0]
__________________________________________________________________________________________________
259 (Conv2D)                    (None, 120, 160, 16) 432         259_pad[0][0]
__________________________________________________________________________________________________
260 (BatchNormalization)        (None, 120, 160, 16) 64          259[0][0]
__________________________________________________________________________________________________
261 (Activation)                (None, 120, 160, 16) 0           260[0][0]
__________________________________________________________________________________________________
262_pad (ZeroPadding2D)         (None, 122, 162, 16) 0           261[0][0]
__________________________________________________________________________________________________
262 (DepthwiseConv2D)           (None, 120, 160, 16) 144         262_pad[0][0]

Thanks.

Related:

Innat
  • 16,113
  • 6
  • 53
  • 101
mrtpk
  • 1,398
  • 4
  • 18
  • 38
  • 1
    Have you treid this `tf.keras.backend.set_image_data_format('channels_last')` at the beginning? – Innat May 14 '21 at 10:18
  • Yes, it didn't work. The model is pretrained and is in channel first format. So, setting `tf.keras.backend.set_image_data_format('channels_first') ` makes the default ordering to be `channels_first` and doesn't affect the loaded model. – mrtpk May 14 '21 at 10:23
  • Related: https://github.com/tensorflow/models/issues/3167#issuecomment-371410527 – mrtpk May 14 '21 at 18:01
  • So, it seems like in order to make it happen, you need to run the model by specifying the `channel_last` at the beginning? Is that correct? Have you tested? – Innat May 14 '21 at 18:10
  • Nope. If we put the `channel_last` while loading the model or while doing inference, it doesn't change the channel order. I have tested this. – mrtpk May 14 '21 at 18:37
  • 1
    https://github.com/keras-team/keras-io/issues/467 – mrtpk May 14 '21 at 20:39

0 Answers0