Reputation: 1
I'm trying to run my code for a "Campus Building Detector" and I'm using Tensorflow's object detection api with faster_rcnn_inception_v2
as model.
I have trained the network for 7000 times (took 12Hrs) and aborted as it has more number of iterations (I have 900000) and now when I'm trying to run the code, I get the following error:
Cannot feed value of shape (480, 640, 3) for Tensor 'image_tensor:0', which has shape '(?, ?, ?, 3)'
I'm using anaconda, Jupiter notebook, Python v3.6.8, Tensorflow v1.13.1
CODE:
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()
Thanks in advance.
Upvotes: 0
Views: 1541
Reputation: 4071
The function run_inference_for_single_image
expects input of images as in batches (four dimensions), so the line below is trying to expand the image in three dimensions into four,
image_np_expanded = np.expand_dims(image_np, axis=0)
You just need to change the line
output_dict = run_inference_for_single_image(image_np, detection_graph)
into
output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
That will solve the problem.
Upvotes: 1