Tensorflow Serving不支持py_func

本文探讨了在TensorFlow Serving中使用tf.py_func的局限性及其导致的问题,并提供了几种替代方案,包括自定义servable、修改TensorFlow源码添加自定义OP及采用Python服务端框架。

如题,在一些情况下,我们可能希望编写一些自己的python代码,不完全使用tensorflow已提供的计算图来做,在一般情况下,可以使用tf.py_func。
但是如果想让该代码部署到tensorflow serving上,那么恭喜,tensorflow serving是不支持的,会报如下错误:

Loading servable: {name: 模块名 version: 版本号} failed: Not found: Op type not registered 'PyFunc' in binary running on 容器ID 

截止目前(2017.11.29)为止,官方并没有说要支持这个特性,有人让你去使用tf.transform,解决办法暂时只有C++:
1、自定义servable: https://www.tensorflow.org/serving/custom_servable
2、改tensorflow,自添加一个tensorflow op。

当然,可以像我一样,特殊情况下选择一些Python的服务端框架(如,tornado)自编写,然后静等tensorflow serving发展。

可以自己参考下tensorflow serving在git的相关issue

E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert.py:854: UserWarning: Statistics for quantized inputs were expected, but not specified; continuing anyway. warnings.warn( WARNING: All log messages before absl::InitializeLog() is called are written to STDERR W0000 00:00:1750592469.236287 1300 tf_tfl_flatbuffer_helpers.cc:365] Ignored output_format. W0000 00:00:1750592469.236390 1300 tf_tfl_flatbuffer_helpers.cc:368] Ignored drop_control_dependency. 2025-06-22 19:41:09.237337: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: C:\Users\27731\AppData\Local\Temp\tmpou2lior2 2025-06-22 19:41:09.249839: I tensorflow/cc/saved_model/reader.cc:52] Reading meta graph with tags { serve } 2025-06-22 19:41:09.249859: I tensorflow/cc/saved_model/reader.cc:147] Reading SavedModel debug info (if present) from: C:\Users\27731\AppData\Local\Temp\tmpou2lior2 I0000 00:00:1750592469.381787 1300 mlir_graph_optimization_pass.cc:425] MLIR V1 optimization pass is not enabled 2025-06-22 19:41:09.404910: I tensorflow/cc/saved_model/loader.cc:236] Restoring SavedModel bundle. 2025-06-22 19:41:10.235403: I tensorflow/cc/saved_model/loader.cc:220] Running initialization op on SavedModel bundle at path: C:\Users\27731\AppData\Local\Temp\tmpou2lior2 2025-06-22 19:41:10.478873: I tensorflow/cc/saved_model/loader.cc:471] SavedModel load for tags { serve }; Status: success: OK. Took 1241546 microseconds. 2025-06-22 19:41:10.657810: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:269] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable. Traceback (most recent call last): File "E:\pycharm\study\计算机视觉\test2.py", line 302, in <module> deployer.convert_to_tflite_with_metadata(tflite_path, quantize=True) File "E:\pycharm\study\计算机视觉\test2.py", line 255, in convert_to_tflite_with_metadata tflite_model = converter.convert() File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1250, in wrapper return self._convert_and_export_metrics(convert_func, *args, **kwargs) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1202, in _convert_and_export_metrics result = convert_func(self, *args, **kwargs) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1768, in convert saved_model_convert_result = self._convert_as_saved_model() File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1749, in _convert_as_saved_model return super(TFLiteKerasModelConverterV2, self).convert( File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1494, in convert return self._optimize_tflite_model( File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 215, in wrapper raise error from None # Re-throws the exception. File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 205, in wrapper return func(*args, **kwargs) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1146, in _optimize_tflite_model model = self._quantize( File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 760, in _quantize calibrated = calibrate_quantize.calibrate( File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 215, in wrapper raise error from None # Re-throws the exception. File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 205, in wrapper return func(*args, **kwargs) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\optimize\calibrator.py", line 258, in calibrate self._feed_tensors(dataset_gen, resize_input=True) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\optimize\calibrator.py", line 152, in _feed_tensors self._calibrator.FeedTensor(input_array) ValueError: Cannot set tensor: Got value of type UINT8 but expected type FLOAT32 for input 0, name: serving_default_input_layer_1:0
06-23
在转换模型的时候报错 TensorFlow SavedModel: export failure ❌ 5595.6s: generic_type: cannot initialize type "StatusCode": an object with that name is already defined Traceback (most recent call last): File "/home/z02/code/yolov8s/Deploy.py", line 14, in <module> model.export(format="tflite",int8=True,) # creates 'yolo11n_int8.tflite' File "/home/z02/code/yolov8s/ultralytics/engine/model.py", line 328, in export return Exporter(overrides=args, _callbacks=self.callbacks)(model=self.model) File "/home/z02/anaconda3/envs/yolo/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context return func(*args, **kwargs) File "/home/z02/code/yolov8s/ultralytics/engine/exporter.py", line 263, in __call__ f[5], keras_model = self.export_saved_model() File "/home/z02/code/yolov8s/ultralytics/engine/exporter.py", line 122, in outer_func raise e File "/home/z02/code/yolov8s/ultralytics/engine/exporter.py", line 117, in outer_func f, model = inner_func(*args, **kwargs) File "/home/z02/code/yolov8s/ultralytics/engine/exporter.py", line 722, in export_saved_model f.unlink() if 'quant_with_int16_act.tflite' in str(f) else self._add_tflite_metadata(file) File "/home/z02/code/yolov8s/ultralytics/engine/exporter.py", line 830, in _add_tflite_metadata from tflite_support import flatbuffers # noqa File "/home/z02/anaconda3/envs/yolo/lib/python3.10/site-packages/tflite_support/__init__.py", line 53, in <module> from tflite_support import task File "/home/z02/anaconda3/envs/yolo/lib/python3.10/site-packages/tflite_support/task/__init__.py", line 32, in <module> from . import vision File "/home/z02/anaconda3/envs/yolo/lib/python3.10/site-packages/tflite_support/task/vision/__init__.py", line 20, in <module> from tensorflow_lite_support.python.task.vision import image_classifier File "/home/z02/anaconda3/envs/yolo/lib/python3.10/site-packages/tensorflow_lite_support/python/task/vision/image_classifier.py", line 23, in <module> from tensorflow_lite_support.python.task.vision.core import tensor_image File "/home/z02/anaconda3/envs/yolo/lib/python3.10/site-packages/tensorflow_lite_support/python/task/vision/core/tensor_image.py", line 19, in <module> from tensorflow_lite_support.python.task.vision.core.pybinds import image_utils ImportError: generic_type: cannot initialize type "StatusCode": an object with that name is already defined
最新发布
07-20
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值