背景:
在 GCP 上的视觉应用程序中,我们使用 TF 服务。使用 TF Serving 的应用程序是用 Go 编写的。此应用程序将图像转换为 Tensor,并使用 gRPC 将其发送到 TF 服务。
问题:
Golang 中的预处理逻辑不像在 Python 中那样工作,使用 Keras 图像库(推理的准确性受到影响)。部分原因可能是在训练期间使用了 Python 库。
我们试过了,
Tensorflow 服务提供了一种引入可以在服务容器上运行的预处理器的方法。它似乎功能有限(无法将 Keras 库与模型打包在一起)。我们尝试了以下两个选项
什么有效是 Keras Preprocessing (Python),在客户端如下。
img = tf.keras.preprocessing.image.load_img(file_name, target_size=(HEIGHT, WIDTH))
img_array = tf.keras.preprocessing.image.img_to_array(img)
... grpc 调用 TensorflowServing ...
我们的目标是在 TFServing 空间中使用“serving_input_receiver_fn”和预处理图像,如本文所述:https://medium.com/devseed/technical-walkthrough-packaging-ml-models-for-inference-with-tf-serving-2a50f73ce6f8
但是以下作为“serving_input_receiver_fn”执行的代码没有产生正确的推断。
image = tf.image.decode_image(image_str_tensor, channels=CHANNELS dtype=tf.uint8)
image = tf.reshape(image, [HEIGHT, WIDTH, CHANNELS])
我们的目标 是在“serving_input_receiver_fn”中运行以下Keras代码(以类似的方式)(假设我们可以从“grpc”流加载图像)。
img = tf.keras.preprocessing.image.load_img(file_name, target_size=(HEIGHT, WIDTH))
img_array = tf.keras.preprocessing.image.img_to_array(img)
是否可以?这是一个大规模部署(70 个 GPU 和 2300 个 CPU),因此每一点性能都很重要。在我们的例子中,TF-Serving 机器上的图像预处理是最优化的。
最佳答案
我实际上没有答案,但也许可以为您提供一些帮助资源。我认为首先,keras.preprocessing 应该很慢,请查看 https://www.tensorflow.org/tutorials/load_data/images它建议将预处理管道构建为 tf.data.Dataset 管道
The above keras.preprocessing method is convienient, but has two downsides:
It's slow. See the performance section below. It lacks fine-grained control. It is not well integrated with the rest of TensorFlow. To load the files as a tf.data.Dataset
为什么不将预处理层作为模型图本身的一部分,以便它在 tensorflow 服务中运行?
关于keras 预处理逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58244575/