如何对输入为编码的\u图像\u字符串\u张量的tensorflow模型的输入进行编码
我在谷歌人工智能平台上训练了一个目标检测模型,并下载了该模型。这是一个标准保存的_model.pb文件,我想用python加载它,并提供一个用于推断的图像。 问题是该模型的输入被定义为如何对输入为编码的\u图像\u字符串\u张量的tensorflow模型的输入进行编码,tensorflow,google-cloud-platform,object-detection,Tensorflow,Google Cloud Platform,Object Detection,我在谷歌人工智能平台上训练了一个目标检测模型,并下载了该模型。这是一个标准保存的_model.pb文件,我想用python加载它,并提供一个用于推断的图像。 问题是该模型的输入被定义为编码的\图像\字符串\张量,它需要base64编码的字符串。如何在python中以这种格式对图像文件进行编码 print(model.inputs) print(model.output_dtypes) print(model.output_shapes) [<tf.Tensor 'encoded_imag
编码的\图像\字符串\张量
,它需要base64编码的字符串。如何在python中以这种格式对图像文件进行编码
print(model.inputs)
print(model.output_dtypes)
print(model.output_shapes)
[<tf.Tensor 'encoded_image_string_tensor:0' shape=(None,) dtype=string>, <tf.Tensor 'key:0' shape=(None,) dtype=string>, <tf.Tensor 'global_step:0' shape=() dtype=resource>]
{'detection_scores': tf.float32, 'detection_classes': tf.float32, 'num_detections': tf.float32, 'key': tf.string, 'detection_boxes': tf.float32}
{'detection_scores': TensorShape([None, 100]), 'detection_classes': TensorShape([None, 100]), 'num_detections': TensorShape([None]), 'key': TensorShape([None]), 'detection_boxes': TensorShape([None, 100, 4])}
当我在模型上运行此代码时,将encoded\u image\u string\u tensor
作为输入,它会产生以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
1 for i in range(1):
----> 2 show_inference(model, TEST_IMAGE_PATHS[i])
in show_inference(model, image_path)
39 # print(image_np)
40 # Actual detection.
---> 41 output_dict = run_inference_for_single_image(model, image_np)
42 # Visualization of the results of a detection.
43 print(output_dict['detection_scores'][:3])
in run_inference_for_single_image(model, image)
7
8 # Run inference
----> 9 output_dict = model(input_tensor)
10
11 # All outputs are batches tensors.
~\anaconda3\envs\tf2\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
1603 TypeError: For invalid positional/keyword argument combinations.
1604 """
-> 1605 return self._call_impl(args, kwargs)
1606
1607 def _call_impl(self, args, kwargs, cancellation_manager=None):
~\anaconda3\envs\tf2\lib\site-packages\tensorflow\python\eager\function.py in _call_impl(self, args, kwargs, cancellation_manager)
1622 "of {}), got {}. When calling a concrete function, positional "
1623 "arguments may not be bound to Tensors within nested structures."
-> 1624 ).format(self._num_positional_args, self._arg_keywords, args))
1625 args = list(args)
1626 for keyword in self._arg_keywords[len(args):]:
TypeError: Expected at most 0 positional arguments (and the rest keywords, of ['encoded_image', 'key']), got (,). When calling a concrete function, positional arguments may not be bound to Tensors within nested structures.
通过使用
tf.io.encode\u jpeg()
对图像进行编码,您可以很容易地从笔记本(您的示例中似乎使用了)中调整run\u expression\u for\u single\u image()
函数
Google AI平台的内置对象检测模型还需要一个键(任何具有批量大小维度的字符串张量)作为输入,我还将其添加到下面示例中的model()
调用中
def为单个图像(模型,图像)运行推理:
image=np.asarray(图像)
#输入必须是张量,请使用“tf.convert\u to\u tensor”将其转换。
输入\u张量=tf。将\u转换为\u张量(图像)
#将(数字)张量编码为“编码的_图像”
编码图像=tf.io.encode\u jpeg(输入张量)
#模型需要一批图像,因此添加一个带有“tf.newaxis”的轴。
编码图像=编码图像[tf.newaxis,…]
#运行推断(从AI平台下载的SavedModel也需要一个“键”作为输入。)
输出dict=模型(编码图像=编码图像,键=tf.expand\u dims(tf.convert\u to\u tensor(“test\u key”),0))
# ...
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
1 for i in range(1):
----> 2 show_inference(model, TEST_IMAGE_PATHS[i])
in show_inference(model, image_path)
39 # print(image_np)
40 # Actual detection.
---> 41 output_dict = run_inference_for_single_image(model, image_np)
42 # Visualization of the results of a detection.
43 print(output_dict['detection_scores'][:3])
in run_inference_for_single_image(model, image)
7
8 # Run inference
----> 9 output_dict = model(input_tensor)
10
11 # All outputs are batches tensors.
~\anaconda3\envs\tf2\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
1603 TypeError: For invalid positional/keyword argument combinations.
1604 """
-> 1605 return self._call_impl(args, kwargs)
1606
1607 def _call_impl(self, args, kwargs, cancellation_manager=None):
~\anaconda3\envs\tf2\lib\site-packages\tensorflow\python\eager\function.py in _call_impl(self, args, kwargs, cancellation_manager)
1622 "of {}), got {}. When calling a concrete function, positional "
1623 "arguments may not be bound to Tensors within nested structures."
-> 1624 ).format(self._num_positional_args, self._arg_keywords, args))
1625 args = list(args)
1626 for keyword in self._arg_keywords[len(args):]:
TypeError: Expected at most 0 positional arguments (and the rest keywords, of ['encoded_image', 'key']), got (,). When calling a concrete function, positional arguments may not be bound to Tensors within nested structures.