simple-keras-rest-api icon indicating copy to clipboard operation
simple-keras-rest-api copied to clipboard

JSONDecodeError

Open bbloks opened this issue 6 years ago • 13 comments

Thanks for the interersting guide and repo. I have cloned it and setup a virtualenv with required packages. I am able to run the flask server, however not able to make requests.

When using curl request: curl -X POST -F [email protected] 'http://localhost:5000/predict', I get the following error:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

And subsequently when running the python simple_request.py I get the following error as an output:

Traceback (most recent call last):
  File "simple_request.py", line 17, in <module>
    r = requests.post(KERAS_REST_API_URL, files=payload).json()
  File "/home/bloks/Projects/Sentriq/simple-keras-rest-api/skra_venv/lib/python3.5/site-packages/requests/models.py", line 896, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

As far as I could find, it may have something to do with a version conflict of some of the packages.

Maybe include a requirements.txt file solves the problem?

bbloks avatar Sep 26 '18 12:09 bbloks

It's not an issue with requirements.txt, it's a problem with your request itself, perhaps being an invalid path to the input image. Double-check your image path and try again.

jrosebr1 avatar Sep 27 '18 05:09 jrosebr1

I'm facing the same issue, and the image path is also valid.

ayush--jain avatar Oct 01 '18 18:10 ayush--jain

same

diptangsu avatar Nov 10 '18 19:11 diptangsu

reference to the closed issue 'Error particular to a case when I tunnel SSH to server on a remote server #1',may be helped

chuyan0421 avatar Dec 06 '18 01:12 chuyan0421

Same issue here and getting same error. I simply cloned the repo (no changes of my own) and I cannot get this to work. Ran code in debugger to make sure that path is correct, tried a different image. Getting the same error as @bbloks and others. Has anybody sorted this out? I may be making a mistake, but not sure where. I really would like to get this working. @jrosebr1 are there other examples in your blog or your books (I own two of them) that would help or provide guidance? Thanks for any help.

edchepen avatar Jan 08 '19 23:01 edchepen

@edchepen Double and tripe-check your input image path. It could be the case that your input image path is incorrect.

Secondly, I do have two more tutorials on the PyImageSearch blog related to building a REST API:

  • https://www.pyimagesearch.com/2018/01/29/scalable-keras-deep-learning-rest-api/
  • https://www.pyimagesearch.com/2018/02/05/deep-learning-production-keras-redis-flask-apache/

jrosebr1 avatar Jan 09 '19 14:01 jrosebr1

I have found a solution to the problem. @jrosebr1 as you suggested I double/triple-checked the image path and made certain that it was correct. My debugger confirmed the path was right and the image was being loaded correctly. However as it turns out the problem was not in simple_request.py. The problem (at least for me) was in the run_keras_server.py file. The following things fixed it and it now works quite well:

I added the fix discussed elsewhere in this thread that requires you to load graph after loading the model:

model = ResNet50(weights="imagenet") graph = tf.get_default_graph()

replaced the original line original press = model.predict(image) with

with graph.as_default(): preds = model.predict(image)

I initialized the graph variable at the beginning of the program, next to where you initialized model

graph = [] model = None

both of the above variables needed to be declared global in all functions that assign to them, in this case inside def load_model()

global graph global model model = ResNet50(weights="imagenet") graph = tf.get_default_graph()

One you do that, I got everything to work (Python file and using curl):

(ml3) Ed-MacBook-Pro:simple-keras-rest-api edm$ python simple_request.py 1. beagle: 0.9901 2. Walker_hound: 0.0024 3. pot: 0.0014 4. Brittany_spaniel: 0.0013 5. bluetick: 0.0011

Not sure if this is right (I am not an expert - I know math, but still learning Python), but as I said this got it working for me and i was able to move forward.

Many, many thanks for the help and for taking the time to reply.

edchepen avatar Jan 09 '19 21:01 edchepen

Congrats on resolving the issue, @edchepen! And thanks so much for providing your fix, I'm sure it will help other readers as well 😄

jrosebr1 avatar Jan 10 '19 13:01 jrosebr1

I follow @edchepen method, it really solve the problem. Thx!

nature1995 avatar Jan 17 '19 20:01 nature1995

I did also follow @edchepen on this one and everything works fine. Thanks for that. However, you should also add

import tensorflow as tf

to the run_keras_server.py module.

paulelvers avatar Feb 07 '19 16:02 paulelvers

Just in case you're new and currently using a latest version of Keras and it is not still working after following @edchepen and @paulelvers , downgrade you Keras version to at least 2.2.5:

pip uninstall keras
pip install keras==2.2.5

then execture the run_keras_server.py module

Special thanks to the guys I have mentioned!

arianyambao avatar Sep 25 '19 05:09 arianyambao

used tf.compat.v1.get_default_graph() instead of tf.get_default()

polu1998 avatar May 27 '20 12:05 polu1998

@edchepen What are the dependencies you are using with pip list? I tried your fix, but it did not work.

These were the results when executed: python simple_request.py Traceback (most recent call last): File "simple_request.py", line 17, in r = requests.post(KERAS_REST_API_URL, files=payload).json() File "/Users/hottelet/Library/Python/2.7/lib/python/site-packages/requests/models.py", line 898, in json return complexjson.loads(self.text, **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 339, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded MacBook-Pro:~/Desktop/simple-keras-rest-api-master quantum-fusion$ python simple_request.py Traceback (most recent call last): File "simple_request.py", line 17, in r = requests.post(KERAS_REST_API_URL, files=payload).json() File "/Users/hottelet/Library/Python/2.7/lib/python/site-packages/requests/models.py", line 898, in json return complexjson.loads(self.text, **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 339, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded MacBook-Pro:~/Desktop/simple-keras-rest-api-master quantum-fusion$ curl -X POST -F [email protected] 'http://localhost:5000/predict'

See the attached program in compressed format.

Package Version


absl-py 0.9.0 altgraph 0.10.2 ansible 2.6.2 asn1crypto 0.24.0 astor 0.8.1 awscli 1.14.11 backports.weakref 1.0.post1 bcrypt 3.1.4 bdist-mpkg 0.5.0 bonjour-py 0.3 botocore 1.8.15 cachetools 3.1.1 ccm 2.0 certifi 2020.6.20 cffi 1.11.5 chardet 3.0.4 click 7.1.2 colorama 0.3.7 cryptography 2.3.1 Django 1.11.29 docutils 0.14 enum34 1.1.6 Flask 1.1.2 funcsigs 1.0.2 functools32 3.2.3.post2 future 0.17.1 futures 3.3.0 gast 0.2.2 gevent 21.1.2 google-auth 1.18.0 google-auth-oauthlib 0.4.1 google-pasta 0.2.0 graphviz 0.8.4 greenlet 1.0.0 grpcio 1.30.0 h5py 2.10.0 idna 2.7 image 1.5.33 ipaddress 1.0.22 itsdangerous 1.1.0 Jinja2 2.11.3 jmespath 0.9.3 Keras 2.2.5 Keras-Applications 1.0.8 Keras-Preprocessing 1.1.2 macholib 1.5.1 Markdown 3.1.1 MarkupSafe 1.0 matplotlib 1.3.1 mock 3.0.5 modulegraph 0.10.4 mxnet 1.6.0 np 1.0.2 numpy 1.16.6 oauthlib 3.1.0 onnx 1.7.0 opt-einsum 2.3.2 paramiko 2.4.1 Pillow 6.2.2 pip 20.3.4 protobuf 3.12.2 psutil 2.1.3 py2app 0.7.3 pyasn1 0.4.8 pyasn1-modules 0.2.8 pycparser 2.18 PyNaCl 1.2.1 pyobjc-core 2.5.1 pyobjc-framework-Accounts 2.5.1 pyobjc-framework-AddressBook 2.5.1 pyobjc-framework-AppleScriptKit 2.5.1 pyobjc-framework-AppleScriptObjC 2.5.1 pyobjc-framework-Automator 2.5.1 pyobjc-framework-CFNetwork 2.5.1 pyobjc-framework-Cocoa 2.5.1 pyobjc-framework-Collaboration 2.5.1 pyobjc-framework-CoreData 2.5.1 pyobjc-framework-CoreLocation 2.5.1 pyobjc-framework-CoreText 2.5.1 pyobjc-framework-DictionaryServices 2.5.1 pyobjc-framework-EventKit 2.5.1 pyobjc-framework-ExceptionHandling 2.5.1 pyobjc-framework-FSEvents 2.5.1 pyobjc-framework-InputMethodKit 2.5.1 pyobjc-framework-InstallerPlugins 2.5.1 pyobjc-framework-InstantMessage 2.5.1 pyobjc-framework-LatentSemanticMapping 2.5.1 pyobjc-framework-LaunchServices 2.5.1 pyobjc-framework-Message 2.5.1 pyobjc-framework-OpenDirectory 2.5.1 pyobjc-framework-PreferencePanes 2.5.1 pyobjc-framework-PubSub 2.5.1 pyobjc-framework-QTKit 2.5.1 pyobjc-framework-Quartz 2.5.1 pyobjc-framework-ScreenSaver 2.5.1 pyobjc-framework-ScriptingBridge 2.5.1 pyobjc-framework-SearchKit 2.5.1 pyobjc-framework-ServiceManagement 2.5.1 pyobjc-framework-Social 2.5.1 pyobjc-framework-SyncServices 2.5.1 pyobjc-framework-SystemConfiguration 2.5.1 pyobjc-framework-WebKit 2.5.1 pyOpenSSL 19.0.0 pyparsing 2.0.1 python-dateutil 2.6.1 pytz 2013.7 PyYAML 3.12 requests 2.24.0 requests-oauthlib 1.3.0 rsa 3.4.2 s3transfer 0.1.12 scikit-learn 0.20.4 scipy 1.2.2 setuptools 41.0.1 six 1.12.0 sklearn 0.0 tailer 0.3 tensorboard 1.14.0 tensorflow 1.14.0 tensorflow-estimator 1.14.0 termcolor 1.1.0 typing 3.7.4.3 typing-extensions 3.7.4.2 urllib3 1.25.9 Werkzeug 1.0.1 wheel 0.30.0 wrapt 1.12.1 xattr 0.6.4 zope.event 4.5.0 zope.interface 5.2.0

run_keras_server.py.zip

quantum-fusion avatar Mar 06 '21 11:03 quantum-fusion