Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

When bringing a keras model to production tensorflow serve is often used as a REST API. It has some drawbacks, as the image data is expecting the same Input format as the network input layer e.g. a array with shape (300,300,3) in json. The only way to make this working seems to be wraping the tensorflow serve API into another service.

How is it possible to make tensorflow serve delivering a keras model, which accepts base64 encoded images, without wrapping it in another API?

1 Answer

0 votes
by (17.6k points)

Here is a solution for your question:

import tensorflow as tf

sess = tf.Session() # get the tensorflow session to reuse it in keras

from keras import backend as K

from keras.models import load_model

K.set_session(sess)

K.set_learning_phase(0) # make sure we disable dropout and other training specific layers

string_inp = tf.placeholder(tf.string, shape=(None,)) #string input for the base64 encoded image

imgs_map = tf.map_fn(

    tf.image.decode_image,

    string_inp,

    dtype=tf.uint8

) # decode jpeg

imgs_map.set_shape((None, None, None, 3))

imgs = tf.image.resize_images(imgs_map, [300, 300]) # resize images

imgs = tf.reshape(imgs, (-1, 300, 300, 3)) # reshape them 

img_float = tf.cast(imgs, dtype=tf.float32) / 255 - 0.5 # and make them to floats

model = load_model('keras.h5', compile=False) # load the model

output = model(img_float) # use the image tensor as input for keras

# ...(save to savedModel format and load in tensorflow serve)

Browse Categories

...