问题
I'm trying to implement the code from 'object_detection_tutorial.ipynb' on my local machine to change some parts and play around. This tutorial is a huge mess and I'm trying really hard to fix any problem I came across but for this one I had no clue. So, here I am.
I'm using Windows 10 and Visual Studio 2019 Professional. Any package related to Tensorflow is up to date and I have another Machine Learning application running with no problems.
I'd like to point out that, I converted this code from its original format which is 'ipynb'. (save as .py)
If you need any extra information please ask me because I really need to understand this concept on a working code.
num_detections = int(output_dict.pop('num_detections')) this part gives the error:
Error int() argument must be a string, a bytes-like object or a number, not '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
When I print few variables related to output_dict, I see;
input tensor
Tensor("strided_slice:0", shape=(1, 636, 1024, 3), dtype=uint8)
model(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.
回答1:
Guys I've fixed the problem. Apparently, I had a problem with my Tensorflow installation. So, I've deleted all the related installation and re-installed everything.
The problem should be related to this because TF v2.0 has Tensor to int conversion already.
来源:https://stackoverflow.com/questions/58689097/tensorflow-2-0-object-detection-api-demo-error-int-argument-must-be-a-string