张量流中的tensorflow.keras预处理器服务于预​​处理?

时间:2019-01-23 20:41:17

标签: python-3.x tensorflow keras normalization tensorflow-serving

我正在将tensorflow.keras模型转换为估算器,并使用estimator.export_saved_mode(serving_input_receiver_fn=serving_input_receiver_fn)使我的模型准备好进行TensorFlow服务。这是我的serving_input_receiver_fn的样子:

def image_preprocessing(image):   
    image = tf.expand_dims(image, 0)
    image = tf.image.resize_bilinear(image, [HEIGHT, WIDTH], align_corners=False)
    image = tf.squeeze(image, axis=[0])
    image = tf.cast(image, dtype=tf.uint8)
    return image

def serving_input_receiver_fn():
    def prepare_image(image_str_tensor):
        image = tf.image.decode_jpeg(image_str_tensor, channels=CHANNELS)
        return image_preprocessing(image)

    input_ph = tf.placeholder(tf.string, shape=[None], name='image_binary')
    images_tensor = tf.map_fn(prepare_image, input_ph, back_prop=False, dtype=tf.uint8)
    images_tensor = tf.image.convert_image_dtype(images_tensor, dtype=tf.float32)

    return tf.estimator.export.ServingInputReceiver(
        {model.input_names[0]: images_tensor},
        {'image_bytes': input_ph})

是否有办法继续将uint8用作输入,但将其转换为float32,然后应用tensorflow.keras预处理函数,例如tensorflow.keras.applications.xception.preprocess_input

我不确定如何根据tensorflow.keras.applications模型的均值/标准差来标准化此输入。在添加上述内容之前,我的模型接受了numpy数组的json序列化列表,我将keras在客户端进行标准化。现在它可以接受uint8 base64编码的字节字符串了,我不确定如何将标准化的keras移到该函数上。

1 个答案:

答案 0 :(得分:0)

如果您要在github上查找Xception的Keras实现,you'll notice that it uses the general imagenet preprocessing function被该存储库中的大多数其他经过预训练的模型使用。此函数应在numpy数组以及Keras / TensorFlow张量上起作用。因此,您应该能够将其导入并将其添加到您的image_preprocessing函数中。

现在还不清楚为什么要强制转换为tf.uint8。我希望预处理功能看起来像这样:

def image_preprocessing(im):
    im = tf.expand_dims(im, 0)
    im = tf.image.resize_bilinear(im, [HEIGHT, WIDTH], align_corners=False)
    im = tf.squeeze(im, axis=[0])
    im = preprocess_input(im)
    return im
相关问题