gpt4 book ai didi

python - 使用特定类型的输入在 TensorFlow 2 中运行 SavedModel

转载 作者:太空宇宙 更新时间:2023-11-03 19:43:16 31 4
gpt4 key购买 nike

我正在尝试在 TensorFlow 2 中运行我以 SavedModel 格式准备的对象检测模型。

该模型采用字节编码 JPEG、PNG 或 GIF 格式作为 encoded_image_string_tensor:0 进行输入,并输出典型的 detection_scores:0 detector_boxes:0

在 TensorFlow 1.15 中,我可以执行以下操作:

with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, ["serve"], path_to_model)
cap = cv2.VideoCapture(camera_id)
ret, frame = cap.read()
ret, encoded = cv2.imencode(".jpg", frame)
inferred = sess.run(["detection_scores:0", "detection_boxes:0"], feed_dict={
"encoded_image_string_tensor:0": [encoded.tobytes(),]
})
# Use the outputs from here

我一直在尝试将此代码转换为使用 TensorFlow 2 的新 API 而不是兼容性库,但我不断遇到障碍。以下是我一直尝试在 TensorFlow 2 中使用的代码:

loaded = tf.saved_model.load(path_to_model)
infer = loaded.signatures["serving_default"]
# `encoded` is the same as the prior example,
# and TF2 is always requesting "image_bytes" and "key" as inputs
prediction = infer(image_bytes=encoded.tobytes(), key="")
"""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "venv/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1551, in __call__
return self._call_impl(args, kwargs)
File "venv/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1591, in _call_impl
return self._call_flat(args, self.captured_inputs, cancellation_manager)
File "venv/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1684, in _call_flat
"Tensor." % (self._func_graph.name, i, str(arg)))
ValueError: All inputs to `ConcreteFunction`s must be Tensors; on invocation of pruned, the 0-th input (b'...') was not a Tensor.
"""

是否存在与不使用兼容性库的 TensorFlow 1.x 代码等效的功能 TensorFlow 2.x,或者我是否以完全错误的方式处理此问题?

我一直在引用 TensorFlow 的 SavedModel 文档,可在此处获取:https://www.tensorflow.org/guide/saved_model .

最佳答案

眼前的问题似乎是:

All inputs to `ConcreteFunction`s must be Tensors...

因此,不要传入 encoded.tobytes() ,而是尝试传入 tf.convert_to_tensor(encoded.tobytes())

或者,查看 tf.io.encode_jpeg 的文档,这似乎提供了图像特定的张量转换。

关于python - 使用特定类型的输入在 TensorFlow 2 中运行 SavedModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60307567/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com