Deep learning API

app.helpers module

Helper funcation for preprocessing

app.helpers.base64_decode_image(a, dtype, shape)[source]

Convert the string to a NumPy array using the supplied data type and target shape

Args:
a:array
Numpy Array
dtype:np.type
np.type to convert too
shape:array
shape of output
Returns:
Reshaped and encode image
app.helpers.base64_encode_image(a)[source]

Base64 encode the input NumPy array

Args:
a:array
Numpy Array
Returns:
Numpay array iecode into uft-8

app.run_model_server module

Run deep learning model which predication the classes and update predication to redis database with id and json

app.run_model_server.classify_process()[source]

Load the pre-trained Keras model (here we are using a model pre-trained on ImageNet and provided by Keras, but you can substitute in your own networks just as easily.

Note

In this example we load Resnet50 trained on imagenet.

Images stored in redis after pre-processing, we take images in batch from redis database and predicat the class. Than result are saved to redis as key(id):predicated(json)

app.run_web_server module

Make flask server to preprocess the the image and add queue to redis database. Then check if predication is saved to redis database, once predication is saved, return the predication json.

app.run_web_server.homepage()[source]

Endpoint for homepage

app.run_web_server.predict()[source]

Predicat the image class.

Send the image as post request.

curl -X POST -F image=@jemma.png 'http://localhost/predict'
Return:

json of classes from predication

{
"predictions": [
        {
        "label": "beagle", 
        "probability": 0.9461532831192017
        }, 
        {
        "label": "bluetick", 
        "probability": 0.031958963721990585
        }, 
        {
        "label": "redbone", 
        "probability": 0.0066171870566904545
        }, 
        {
        "label": "Walker_hound", 
        "probability": 0.003387963864952326
        }, 
        {
        "label": "Greater_Swiss_Mountain_dog", 
        "probability": 0.0025766845792531967
        }
], 
"success": true
}
app.run_web_server.prepare_image(image, target)[source]

Preprocess the image for predication

Args:
images:array
Array from image
target:array
shape of image for reshape
Return:
Preprocesses image array

app.settings module

Environment variables for app

# initialize Redis connection settings
REDIS_HOST = "redis"
REDIS_PORT = 6379
REDIS_DB = 0

# initialize constants used to control image spatial dimensions and
# data type
IMAGE_WIDTH = 224
IMAGE_HEIGHT = 224
IMAGE_CHANS = 3
IMAGE_DTYPE = "float32"

# initialize constants used for server queuing
IMAGE_QUEUE = "image_queue"
BATCH_SIZE = 32
SERVER_SLEEP = 0.25
CLIENT_SLEEP = 0.25