keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 907 forks source link

Issue with decode_predictions() of inception_v3 in keras_applications #46

Closed himanshurawlani closed 5 years ago

himanshurawlani commented 6 years ago

I am using TensorFlow Serving(1.10.0-dev) and have deployed InceptionV3 model using REST API on port 9000 on my Ubuntu 18.04.1. Inorder to test the API, I have written a sample_request.py script:

import requests, argparse, json
import numpy as np
from keras.preprocessing import image
from keras_applications import inception_v3

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
                help="path of the image")
args = vars(ap.parse_args())

image_path = args['image']
img = image.img_to_array(image.load_img(image_path, target_size=(224, 224))) / 255.

# this line is added because of a bug in tf_serving < 1.11
img = img.astype('float16')

payload = {
    "instances": [{'input_image': img.tolist()}]
}

# sending post request
r = requests.post('http://localhost:9000/v1/models/ImageClassifier:predict', json=payload)
pred = json.loads(r.content.decode('utf-8'))

print(json.dumps(inception_v3.decode_predictions(np.array(pred['predictions']))[0]))

Just a month ago this script was working perfectly and now suddenly I get this error:

Using TensorFlow backend.
Traceback (most recent call last):
  File "sample_request.py", line 49, in <module>
    print(inception_v3.decode_predictions(np.array(pred['predictions']))[0])
  File "/home/ubuntu/tensorflow/lib/python3.6/site-packages/keras_applications/imagenet_utils.py", line 224, in decode_predictions
    fpath = keras_utils.get_file(
AttributeError: 'NoneType' object has no attribute 'get_file'

I did find a work around by replacing the import statement: from keras_applications import inception_v3 with from keras.applications import inception_v3

But the response format has change and it gives response like this:

$ python sample_request.py -i ./test_images/car.png
Using TensorFlow backend.
[('n04285008', 'sports_car', 0.99841400000000002), ('n04037443', 'racer', 0.00140099), ('n03459775', 'grille', 0.00016079399999999999), ('n02974003', 'car_wheel', 9.5786199999999998e-06), ('n03100240', 'convertible', 6.0158099999999999e-06)]

Those imagenet (starting with 'n0....') labels were not present earlier. Please can anyone tell me where is the issue?

taehoonlee commented 6 years ago

@himanshurawlani,

  1. The recent changes raise the error you described. If you install the latest versions of keras and keras-applications, you don't need the work around you proposed. Please pip install -U keras keras-applications.

  2. You can refer to the image labels starting with n0 in here.

taehoonlee commented 5 years ago

@himanshurawlani, I'll close the issue for now. Please feel free to open it again at any time if you have additional comments.

sumeetssaurav commented 5 years ago

I am also getting the same error File "/home/vision/.virtualenvs/dl/lib/python3.5/site-packages/keras_applications/imagenet_utils.py", line 224, in decode_predictions fpath = keras_utils.get_file( AttributeError: 'NoneType' object has no attribute 'get_file'

OanaIgnat commented 5 years ago

@sumeetssaurav Me too, with the latest versions of keras (2.2.4) and keras-applications(1.0.7).

taehoonlee commented 5 years ago

@sumeetssaurav, @OanaIgnat

Please use from keras.applications import xx NOT from keras_applications import xx.

ghost commented 3 years ago

I am getting the following error. File "serving_sample_request.py", line 33, in print(json.dumps(inception_v3.decode_predictions(np.array(pred['predictions']))[0])) KeyError: 'predictions'