我正在尝试为“校园建筑探测器”运行我的代码,并且我正在使用Tensorflow的对象检测api和faster_rcnn_inception_v2
作为模型。
我已经对该网络进行了7000次训练(耗时12小时),并且由于迭代次数更多(我有900000)而中止,现在当我尝试运行代码时,出现以下错误:
无法为形状为((?,?,?,3)'的张量'image_tensor:0'输入形状(480,640,3)的值
我正在使用anaconda,Jupiter Notebook,Python v3.6.8,Tensorflow v1.13.1
代码:
import cv2
cap = cv2.VideoCapture(0)
try:
with detection_graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
while True:
ret, image_np = cap.read()
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8)
cv2.imshow('object_detection', cv2.resize(image_np, (800, 600)))
if cv2.waitKey(25) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
except Exception as e:
print(e)
cap.release()
谢谢。
答案 0 :(得分:1)
函数run_inference_for_single_image
期望成批输入图像(四个维度),因此下面的行尝试将三个维度的图像扩展为四个,
image_np_expanded = np.expand_dims(image_np, axis=0)
您只需要更改行
output_dict = run_inference_for_single_image(image_np, detection_graph)
进入
output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
那将解决问题。