Search code examples
pythontensorflowkerasmxnet

How to convert keras tensorflow to keras mxnet


I have a working python code in keras with tensorflow backend. I am utilizing transfer learning from VGG16. All is good.

I want to use mxnet backend but had some issues:

from keras.preprocessing.image import ImageDataGenerator
from keras import applications
from helper import target_size, batch_size
from math import ceil
import numpy as np

datagen = ImageDataGenerator(rescale=1./255)

loading vgg16 model, excluding the top fully connected layers

model = applications.VGG16(include_top=False, weights='imagenet' , input_shape=(224, 224 , 3))

above code (shape(224,224,3)) gives :

ValueError: The input must have 3 channels; got input_shape=(224, 224, 3)

If I use : shape(3,224,24)

'Redefinition of variable %s' % self.name AssertionError: Redefinition of variable block1_conv1/kernel1

How can I properly use mxnet instead of tensorflow backend in working code?

Note: keras.json:

{
"epsilon": 1e-07, 
"floatx": "float32", 
"image_data_format": "channels_first", 
"backend": "mxnet"
}

Edit 1

When I change the backend to mxnet from tensorflow is keras need to re-download vgg16 model for mxnet ?


Solution

  • Solution:

    Set data_format to 'channels_last'.

    Details:

    VGG16 imagenet weights are in 'channels_last' format. You should set the keras config to 'channels_last' to get it working with MXNet backend.

    We have a Github issue and working on allowing MXNet backend to load other backend trained weights in different data_format. i.e., Say you have a TF backend trained model that was trained with channels_last format. If you try to load this in MXNet backend with data_format set to 'channels_first' then automatic conversion from channels_last to channels_first doest not happen.

    Reason for issue:

    MXNet backend transposes the Conv layer input and kernels when layer is called with channels_last format to speed up. And, this will cause issues with pre-trained other backend model weights that is not transposed. We are working on fixing it and enabling the feature.