Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. def run_inference(graph):
  2. results = {}
  3. with graph.as_default():
  4. with tf.Session() as sess:
  5. # Get handles to input and output tensors
  6. ops = tf.get_default_graph().get_operations()
  7. all_tensor_names = {output.name for op in ops for output in op.outputs}
  8. for i, image_path in enumerate(os.listdir(PATH_TO_TEST_IMAGES)):
  9. image = Image.open(os.path.join(PATH_TO_TEST_IMAGES, image_path))
  10. # the array based representation of the image will be used later in order to prepare the
  11. # result image with boxes and labels on it.
  12. image_np = load_image_into_numpy_array(image)
  13. # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  14. image_np_expanded = np.expand_dims(image_np, axis=0)
  15. tensor_dict = {}
  16. for key in [
  17. 'num_detections', 'detection_boxes', 'detection_scores',
  18. 'detection_classes', 'detection_masks'
  19. ]:
  20. tensor_name = key + ':0'
  21. if tensor_name in all_tensor_names:
  22. tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
  23. tensor_name)
  24.  
  25. if 'detection_masks' in tensor_dict:
  26. # The following processing is only for single image
  27. detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
  28. detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
  29. # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
  30. real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
  31. detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
  32. detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
  33. detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
  34. detection_masks, detection_boxes, image_np.shape[0], image_np.shape[1])
  35. detection_masks_reframed = tf.cast(
  36. tf.greater(detection_masks_reframed, 0.5), tf.uint8)
  37. # Follow the convention by adding back the batch dimension
  38. tensor_dict['detection_masks'] = tf.expand_dims(
  39. detection_masks_reframed, 0)
  40. image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
  41.  
  42. # Run inference
  43. output_dict = sess.run(tensor_dict,
  44. feed_dict={image_tensor: np.expand_dims(image, 0)})
  45. # all outputs are float32 numpy arrays, so convert types as appropriate
  46. output_dict['num_detections'] = int(output_dict['num_detections'][0])
  47. output_dict['detection_classes'] = output_dict[
  48. 'detection_classes'][0].astype(np.uint8)
  49. output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
  50. output_dict['detection_scores'] = output_dict['detection_scores'][0]
  51. if 'detection_masks' in output_dict:
  52. output_dict['detection_masks'] = output_dict['detection_masks'][0]
  53. visualize_masks_and_labels_on_image_array(
  54. image_np,
  55. output_dict['detection_boxes'],
  56. output_dict['detection_classes'],
  57. output_dict['detection_scores'],
  58. category_index,
  59. instance_masks=output_dict.get('detection_masks'),
  60. use_normalized_coordinates=True,
  61. line_thickness=0)
  62. plt.figure(figsize=IMAGE_SIZE)
  63. plt.imshow(image_np)
  64. plt.imsave("{i}.png".format(i=i), image_np, format="png")
  65. results[i] = output_dict
  66. return results
  67.  
  68. def visualize_masks_and_labels_on_image_array(
  69. image,
  70. boxes,
  71. classes,
  72. scores,
  73. category_index,
  74. instance_masks=None,
  75. instance_boundaries=None,
  76. keypoints=None,
  77. track_ids=None,
  78. use_normalized_coordinates=False,
  79. max_boxes_to_draw=20,
  80. min_score_thresh=.5,
  81. agnostic_mode=False,
  82. line_thickness=4,
  83. groundtruth_box_visualization_color='black',
  84. skip_scores=False,
  85. skip_labels=False,
  86. skip_track_ids=False):
  87. box_to_display_str_map = collections.defaultdict(list)
  88. box_to_color_map = collections.defaultdict(str)
  89. box_to_instance_masks_map = {}
  90. box_to_instance_boundaries_map = {}
  91. box_to_keypoints_map = collections.defaultdict(list)
  92. box_to_track_ids_map = {}
  93. if not max_boxes_to_draw:
  94. max_boxes_to_draw = boxes.shape[0]
  95. for i in range(min(max_boxes_to_draw, boxes.shape[0])):
  96. if scores is None or scores[i] > min_score_thresh:
  97. box = tuple(boxes[i].tolist())
  98. if instance_masks is not None:
  99. box_to_instance_masks_map[box] = instance_masks[i]
  100. if instance_boundaries is not None:
  101. box_to_instance_boundaries_map[box] = instance_boundaries[i]
  102. if keypoints is not None:
  103. box_to_keypoints_map[box].extend(keypoints[i])
  104. if track_ids is not None:
  105. box_to_track_ids_map[box] = track_ids[i]
  106. if scores is None:
  107. box_to_color_map[box] = groundtruth_box_visualization_color
  108. else:
  109. display_str = ''
  110. if not skip_labels:
  111. if not agnostic_mode:
  112. if classes[i] in category_index.keys():
  113. class_name = category_index[classes[i]]['name']
  114. else:
  115. class_name = 'N/A'
  116. display_str = str(class_name)
  117. if not skip_scores:
  118. if not display_str:
  119. display_str = '{}%'.format(int(100*scores[i]))
  120. else:
  121. display_str = '{}: {}%'.format(display_str, int(100*scores[i]))
  122. if not skip_track_ids and track_ids is not None:
  123. if not display_str:
  124. display_str = 'ID {}'.format(track_ids[i])
  125. else:
  126. display_str = '{}: ID {}'.format(display_str, track_ids[i])
  127. box_to_display_str_map[box].append(display_str)
  128. if agnostic_mode:
  129. box_to_color_map[box] = 'DarkOrange'
  130. elif track_ids is not None:
  131. prime_multipler = vis_util._get_multiplier_for_color_randomness()
  132. box_to_color_map[box] = vis_util.STANDARD_COLORS[
  133. (prime_multipler * track_ids[i]) % len(vis_util.STANDARD_COLORS)]
  134. else:
  135. box_to_color_map[box] = vis_util.STANDARD_COLORS[
  136. classes[i] % len(vis_util.STANDARD_COLORS)]
  137.  
  138. # Draw all boxes onto image.
  139. for box, color in box_to_color_map.items():
  140. ymin, xmin, ymax, xmax = box
  141. if instance_masks is not None:
  142. vis_util.draw_mask_on_image_array(
  143. image,
  144. box_to_instance_masks_map[box],
  145. color=color
  146. )
  147. if instance_boundaries is not None:
  148. vis_util.draw_mask_on_image_array(
  149. image,
  150. box_to_instance_boundaries_map[box],
  151. color='red',
  152. alpha=1.0
  153. )
  154.  
  155. if keypoints is not None:
  156. vis_util.draw_keypoints_on_image_array(
  157. image,
  158. box_to_keypoints_map[box],
  159. color=color,
  160. radius=line_thickness / 2,
  161. use_normalized_coordinates=use_normalized_coordinates)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement