Search code examples
python-3.xkerasdeep-learningconv-neural-networkkeras-layer

ImportError: cannot import name 'normalize_data_format'


I have read an article Here and its pretty nice enough to understand. Given its implementation on GitHub. When I am trying to train at my own using given code it gives me an Import Error in this file at line 117 like following. I am using google Colab environment. Having some search over the error i got that the following line is compatible to keras version==2.2.2. I have also installed that yet not solved with the error. Please help me to get over it. By default keras version installed in colab is 2.2.4

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-47-f8ce7e15cf87> in <module>()
      9 from keras.layers.merge import Add
     10 from keras.utils import conv_utils
---> 11 from keras.utils.conv_utils import normalize_data_format
     12 
     13 from keras.layers.core import Dropout

ImportError: cannot import name 'normalize_data_format'

---------------------------------------------------------------------------

Solution

  • https://github.com/keras-team/keras/blob/master/keras/utils/conv_utils.py

    master branch's conv_utils doesn't have normalize_data_format. some of the other branches do have it such as tf-keras branch. It is a trivial function here is its implementation:

    import keras.backend as K
    def normalize_data_format(value):
        if value is None:
            value = K.image_data_format()
        data_format = value.lower()
        if data_format not in {'channels_first', 'channels_last'}:
            raise ValueError('The `data_format` argument must be one of '
                             '"channels_first", "channels_last". Received: ' +
                             str(value))
        return data_format