Is there a bug with imagenet_utils.preprocess_input method in keras 2.2.4 or is it just me?

Viewed 1288

Trying to use the imagenet_utils.preprocess_input(x) method available from keras and an error is thrown:

File "C:\Dev\workspace\venvs\venv36\lib\site-packages\keras_applications\imagenet_utils.py", line 186, in preprocess_input
data_format = backend.image_data_format()
AttributeError: 'NoneType' object has no attribute 'image_data_format'

I have found this post (Keras : Create MobileNet_V2 model "AttributeError"), is there any chance there is a link between both ?

I am running this script with Python 3.6, Keras 2.2.4 and Tensorflow backend 1.12

I am trying to use the already pretrained models, particularly the VGG16 one, and copied sample of code found on several blogs. For example here: https://blog.keras.io/building-a-simple-keras-deep-learning-rest-api.html

Code throws an exception from imagenet_utils.preprocess_input method because this underlying code:

backend, _, _, _ = get_submodules_from_kwargs(kwargs)

returns backend = None so the code cannot go further...

That is why I am printing the backend before calling the method to see if it is None or not. It seems something somewhere that replace it to None ?

import keras
import numpy as np
from keras.applications import VGG16
from keras_applications import imagenet_utils
from keras_preprocessing.image import load_img, img_to_array


if __name__ == '__main__':
    model = VGG16(weights="imagenet")
    print("backend: {}".format(keras.backend.image_data_format()))

    img = load_img('./images/whatever.jpg', target_size=(224, 224))
    x = img_to_array(img)
    x = np.expand_dims(x, axis=0)

    print("backend: {}".format(keras.backend.image_data_format()))

    # Seems there is this bug to solve in 2.2.4
    x = imagenet_utils.preprocess_input(x)  # Will throw an error 'AttributeError: 'NoneType' object has no attribute 'image_data_format''
    predictions = model.predict(x)
    top_preds = imagenet_utils.decode_predictions(predictions)

    print(top_preds)

Should I open an issue to Keras team ? Am I missing something ?

1 Answers

If some people encounter the same issue, I finally found where the "error" was. After reading this github issue wich is highly similar to mine, I changed my imports:

  • from keras_applications import VGG16 now becomes from keras.applications import VGG16

  • from keras_applications import imagenet_utils now becomes from keras.applications import imagenet_utils

Related