【问题标题】:How do deploy a keras model with tensorflow serve, while using base64 encodede images?如何在使用 base64 编码图像的同时使用 tensorflow 部署 keras 模型?
【发布时间】:2019-11-23 15:44:14
【问题描述】:

将 keras 模型用于生产时,tensorflow 服务通常用作 REST API。它有一些缺点,因为图像数据需要与网络输入层相同的输入格式,例如json 中形状为 (300,300,3) 的数组。使这项工作的唯一方法似乎是将 tensorflow 服务 API 包装到另一个服务中。

如何让 tensorflow 提供一个接受 base64 编码图像的 keras 模型,而不将其包装在另一个 API 中?

【问题讨论】:

    标签: python tensorflow keras tensorflow-serving


    【解决方案1】:

    我找到了解决方案,here 是更详细的解释:

    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)
    

    【讨论】:

      猜你喜欢
      • 2020-01-31
      • 2020-01-17
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 2017-12-07
      相关资源
      最近更新 更多