This repository contains the code for Building a simple Keras + deep learning REST API, published on the Keras.io blog with minor changes.
The method covered here is intended to be instructional. It is not meant to be production-level and capable of scaling under heavy load.
I assume you already have Keras (and a supported backend) and TensorFlow installed on your system. From there you need to install Flask and requests:
$ pip install flask gevent requests
*NOTES
- the original code has had all import statements of from keras updated to tensorFlow.keras
- ignore cudart64_110.dll GPU error
- may need to:
$ pip uninstall tf-nightly
and
$ pip install tensorflow --upgrade --force-reinstall
if receiving keras.utils.generic_utils module AttributeError.
Next, clone the repo:
$ git clone https://github.com/artoflearning/Image-Classification-Deep-CNN-Model-Keras-TensorFlow---Flask-.git
Below you can see the image we wish to classify, a dog, but more specifically a Shih-Tzu:
The Flask + Keras server can be started by running:
$ python run_keras_server.py
Using TensorFlow backend.
* Loading Keras model and Flask starting server...please wait until server has fully started
...
* Running on http://127.0.0.1:5000
You can now access the REST API via http://127.0.0.1:5000
.
Requests can be submitted via cURL:
$ curl -X POST -F [email protected] 'http://localhost:5000/predict'
{
"predictions": [
{
"label": "Shih-Tzu",
"probability": 0.7272
},
{
"label": "Pekinese",
"probability":
"success": true
}
Or programmatically:
$ python simple_request.py
1. Shih-Tzu: 0.7272
2. Pekinese: 0.1508
3. Lhasa: 0.0415
4. Japanese_spaniel: 0.0225
5. Sussex_spaniel: 0.0165