1

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 ?

halfer
  • 19,824
  • 17
  • 99
  • 186
2adnielsenx xx
  • 488
  • 1
  • 5
  • 22
  • Keras-MXNet is a [different (forked) project](https://github.com/awslabs/keras-apache-mxnet), and it needs separate installation (`pip install keras-mxnet`) than Keras proper; have you installed this fork? – desertnaut Aug 03 '18 at 11:44
  • Yes I already installed keras-mxnet. I am using same code with tensorflow and all is ok. But when changed to the mxnet it give above error for vgg16 – 2adnielsenx xx Aug 03 '18 at 13:03

1 Answers1

3

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.

Sandeep
  • 341
  • 1
  • 9
  • Thanks, it looks worked for extracting features from vgg16. Is there any speed consideration to use channel last ? – 2adnielsenx xx Aug 04 '18 at 09:21
  • its worked. We were using mainly tensorflow and trying to migrate mxnet. detection speed 3 times slower than tensorflow. It may be due to from channel last or not utilizing the all cpu powers. If we use c++ it could be faster but not sure we are just. discovering the mxnet now. – 2adnielsenx xx Aug 06 '18 at 10:20