Tensorflow 2.0 对象检测 API 演示错误 int() 参数必须是字符串、类字节对象或数字,而不是 'Tensor'
Tensorflow 2.0 Object Detection API Demo Error int() argument must be a string, a bytes-like object or a number, not 'Tensor'
我正在尝试在我的本地计算机上实现“object_detection_tutorial.ipynb”中的代码,以更改某些部分并进行尝试。本教程一团糟,我真的很努力地解决我遇到的任何问题,但对于这个我一无所知。所以,我来了。
我正在使用 Windows 10 和 Visual Studio 2019 Professional。任何与 Tensorflow 相关的软件包都是最新的,我还有另一个机器学习应用程序 运行 没问题。
我想指出的是,我将此代码从其原始格式 'ipynb' 转换而来。 (另存为.py)
如果您需要任何额外信息,请问我,因为我真的需要在工作代码中理解这个概念。
num_detections = int(output_dict.pop('num_detections')) 这部分报错:
错误 int() 参数必须是字符串、类字节对象或数字,而不是 'Tensor'
def run_inference_for_single_image(model, image):
image = np.asarray(image)
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
input_tensor = tf.convert_to_tensor(image)
# The model expects a batch of images, so add an axis with `tf.newaxis`.
input_tensor = input_tensor[tf.newaxis,...]
# Run inference
output_dict = model(input_tensor)
# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_detections = int(output_dict.pop('num_detections'))
output_dict = {key:value[0, :num_detections].numpy()
for key,value in output_dict.items()}
output_dict['num_detections'] = num_detections
# detection_classes should be ints.
output_dict['detection_classes'] =
output_dict['detection_classes'].astype(np.int64)
# Handle models with masks:
if 'detection_masks' in output_dict:
# Reframe the the bbox mask to the image size.
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
output_dict['detection_masks'], output_dict['detection_boxes'],
image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
tf.uint8)
output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()
return output_dict
当我打印一些与 output_dict 相关的变量时,我明白了;
输入张量
Tensor("strided_slice:0", shape=(1, 636, 1024, 3), dtype=uint8)
型号(input_tensor)
{'detection_scores':
< tf.Tensor 'StatefulPartitionedCall_1:2' shape=(?, 100) dtype=float32 >,
'detection_classes':
< tf.Tensor 'StatefulPartitionedCall_1:1' shape=(?, 100) dtype=float32 >,
'num_detections':
< tf.Tensor 'StatefulPartitionedCall_1:3' shape=(?,) dtype=float32 >,
'detection_boxes':
< tf.Tensor 'StatefulPartitionedCall_1:0' shape=(?, 100, 4) dtype=float32 >
}
output_dict
{'detection_scores':
< tf.Tensor 'StatefulPartitionedCall:2' shape=(?, 100) dtype=float32 >,
'detection_classes':
< tf.Tensor 'StatefulPartitionedCall:1' shape=(?, 100) dtype=float32 >,
'num_detections':
< tf.Tensor 'StatefulPartitionedCall:3' shape=(?,) dtype=float32 >,
'detection_boxes':
< tf.Tensor 'StatefulPartitionedCall:0' shape=(?, 100, 4) dtype=float32 >
}
output_dict.pop
Tensor("StatefulPartitionedCall:3", shape=(?,), dtype=float32)
WARNING:tensorflow:Tensor._shape is private, use Tensor.shape instead.
Tensor._shape will eventually be removed.
伙计们,我已经解决了这个问题。显然,我的 Tensorflow 安装有问题。所以,我删除了所有相关安装并重新安装了所有内容。
问题应该与此有关,因为TF v2.0已经有了Tensor到int的转换。
我正在尝试在我的本地计算机上实现“object_detection_tutorial.ipynb”中的代码,以更改某些部分并进行尝试。本教程一团糟,我真的很努力地解决我遇到的任何问题,但对于这个我一无所知。所以,我来了。
我正在使用 Windows 10 和 Visual Studio 2019 Professional。任何与 Tensorflow 相关的软件包都是最新的,我还有另一个机器学习应用程序 运行 没问题。
我想指出的是,我将此代码从其原始格式 'ipynb' 转换而来。 (另存为.py)
如果您需要任何额外信息,请问我,因为我真的需要在工作代码中理解这个概念。
num_detections = int(output_dict.pop('num_detections')) 这部分报错:
错误 int() 参数必须是字符串、类字节对象或数字,而不是 'Tensor'
def run_inference_for_single_image(model, image):
image = np.asarray(image)
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
input_tensor = tf.convert_to_tensor(image)
# The model expects a batch of images, so add an axis with `tf.newaxis`.
input_tensor = input_tensor[tf.newaxis,...]
# Run inference
output_dict = model(input_tensor)
# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_detections = int(output_dict.pop('num_detections'))
output_dict = {key:value[0, :num_detections].numpy()
for key,value in output_dict.items()}
output_dict['num_detections'] = num_detections
# detection_classes should be ints.
output_dict['detection_classes'] =
output_dict['detection_classes'].astype(np.int64)
# Handle models with masks:
if 'detection_masks' in output_dict:
# Reframe the the bbox mask to the image size.
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
output_dict['detection_masks'], output_dict['detection_boxes'],
image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
tf.uint8)
output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()
return output_dict
当我打印一些与 output_dict 相关的变量时,我明白了;
输入张量
Tensor("strided_slice:0", shape=(1, 636, 1024, 3), dtype=uint8)
型号(input_tensor)
{'detection_scores':
< tf.Tensor 'StatefulPartitionedCall_1:2' shape=(?, 100) dtype=float32 >,
'detection_classes':
< tf.Tensor 'StatefulPartitionedCall_1:1' shape=(?, 100) dtype=float32 >,
'num_detections':
< tf.Tensor 'StatefulPartitionedCall_1:3' shape=(?,) dtype=float32 >,
'detection_boxes':
< tf.Tensor 'StatefulPartitionedCall_1:0' shape=(?, 100, 4) dtype=float32 >
}
output_dict
{'detection_scores':
< tf.Tensor 'StatefulPartitionedCall:2' shape=(?, 100) dtype=float32 >,
'detection_classes':
< tf.Tensor 'StatefulPartitionedCall:1' shape=(?, 100) dtype=float32 >,
'num_detections':
< tf.Tensor 'StatefulPartitionedCall:3' shape=(?,) dtype=float32 >,
'detection_boxes':
< tf.Tensor 'StatefulPartitionedCall:0' shape=(?, 100, 4) dtype=float32 >
}
output_dict.pop
Tensor("StatefulPartitionedCall:3", shape=(?,), dtype=float32)
WARNING:tensorflow:Tensor._shape is private, use Tensor.shape instead.
Tensor._shape will eventually be removed.
伙计们,我已经解决了这个问题。显然,我的 Tensorflow 安装有问题。所以,我删除了所有相关安装并重新安装了所有内容。
问题应该与此有关,因为TF v2.0已经有了Tensor到int的转换。