Git Product home page Git Product logo

Comments (13)

edchepen avatar edchepen commented on August 12, 2024 12

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.

from simple-keras-rest-api.

paulelvers avatar paulelvers commented on August 12, 2024 3

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.

from simple-keras-rest-api.

arianyambao avatar arianyambao commented on August 12, 2024 3

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!

from simple-keras-rest-api.

ayush--jain avatar ayush--jain commented on August 12, 2024 2

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

from simple-keras-rest-api.

diptangsu avatar diptangsu commented on August 12, 2024 1

same

from simple-keras-rest-api.

jrosebr1 avatar jrosebr1 commented on August 12, 2024

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.

from simple-keras-rest-api.

chuyan0421 avatar chuyan0421 commented on August 12, 2024

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

from simple-keras-rest-api.

edchepen avatar edchepen commented on August 12, 2024

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.

from simple-keras-rest-api.

jrosebr1 avatar jrosebr1 commented on August 12, 2024

@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:

from simple-keras-rest-api.

jrosebr1 avatar jrosebr1 commented on August 12, 2024

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

from simple-keras-rest-api.

nature1995 avatar nature1995 commented on August 12, 2024

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

from simple-keras-rest-api.

polu1998 avatar polu1998 commented on August 12, 2024

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

from simple-keras-rest-api.

quantum-fusion avatar quantum-fusion commented on August 12, 2024

@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

from simple-keras-rest-api.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.