def run(generator, args): # display images, one at a time for i in range(generator.size()): # load the data image = generator.load_image(i) annotations = generator.load_annotations(i) # apply random transformations if args.random_transform: image, annotations = generator.random_transform_group_entry( image, annotations) # resize the image and annotations if args.resize: image, image_scale = generator.resize_image(image) annotations[:, :4] *= image_scale # draw anchors on the image if args.anchors: labels, _, anchors = generator.compute_anchor_targets( image.shape, annotations, generator.num_classes(), ratios=[1, 1.25, 1.5], scales=[0, 0.33333333, 0.66666667], sizes=[16, 32, 64, 128, 256], strides=[8, 16, 32, 64, 128]) draw_boxes(image, anchors[np.max(labels, axis=1) == 1], (255, 255, 0), thickness=1) a = anchors[np.max(labels, axis=1) == 1] b = [abs((k[3] - k[1]) / (k[2] - k[0])) for k in a] print(len(a)) # print(min(b), max(b)) # draw annotations on the image if args.annotations: # draw annotations in red draw_annotations(image, annotations, color=(0, 0, 255), label_to_name=generator.label_to_name) # draw regressed anchors in green to override most red annotations # result is that annotations without anchors are red, with anchors are green labels, boxes, _ = generator.compute_anchor_targets( image.shape, annotations, generator.num_classes()) draw_boxes(image, boxes[np.max(labels, axis=1) == 1], (0, 255, 0)) cv2.imshow('Image', image) if cv2.waitKey() == ord('q'): return False return True
def run(generator, args): """ Main loop in which data is provided by the generator and then displayed Args: generator: The generator to debug. args: parseargs args object. """ while True: # display images, one at a time for i in range(generator.size()): # load the data image = generator.load_image(i) annotations = generator.load_annotations(i) mask = generator.load_mask(i) camera_matrix = generator.load_camera_matrix(i) if len(annotations['labels']) > 0: # apply random transformations image, annotations = generator.random_transform_group_entry( image, annotations, mask, camera_matrix) anchors = anchors_for_shape(image.shape, anchor_params=None) positive_indices, _, max_indices = compute_gt_annotations( anchors[0], annotations['bboxes']) #switch image RGB to BGR again image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # draw anchors on the image if args.anchors: draw_boxes(image, anchors[0][positive_indices], (255, 255, 0), thickness=1) # draw annotations on the image if args.annotations: draw_annotations( image, annotations, class_to_bbox_3D=generator.get_bbox_3d_dict(), camera_matrix=camera_matrix, label_to_name=generator.label_to_name, draw_bbox_2d=args.draw_2d_bboxes, draw_name=args.draw_class_names) print("Generator idx: {}".format(i)) cv2.imshow('Image', image) if cv2.waitKey() == ord('q'): cv2.destroyAllWindows() return
def run(generator, args, anchor_params): """!@brief Main loop. @param generator : The generator to debug. @param args : Parseargs args object. """ # display images, one at a time for i in range(generator.size()): # load the data image = generator.load_image(i) annotations = generator.load_annotations(i) # Apply random transformations # if args.random_transform or args.random_deformable or args.random_photometric:# or args.random_psf_blur: if True: image, annotations = generator.random_transform_group_entry(image, annotations) # resize the image and annotations if args.resize: image, image_scale = generator.resize_image(image) annotations['bboxes'] *= image_scale anchors = anchors_for_shape(image.shape, anchor_params=anchor_params) positive_indices, _, max_indices = compute_gt_annotations(anchors, annotations['bboxes']) # draw anchors on the image if args.anchors: draw_boxes(image, anchors[positive_indices], (255, 255, 0), thickness=1) # draw annotations on the image if args.annotations: # draw annotations in red draw_annotations(image, annotations, color=(0, 0, 255), label_to_name=generator.label_to_name) # draw regressed anchors in green to override most red annotations # result is that annotations without anchors are red, with anchors are green draw_boxes(image, annotations['bboxes'][max_indices[positive_indices], :], (0, 255, 0)) cv2.imshow('Image', image) if cv2.waitKey() == ord('q'): return False return True
def _get_detections(generator, model, score_threshold=0.05, max_detections=100, visualize=False): """ Get the detections from the model using the generator. The result is a list of lists such that the size is: all_detections[num_images][num_classes] = detections[num_class_detections, 5] Args: generator: The generator used to run images through the model. model: The model to run on the images. score_threshold: The score confidence threshold to use. max_detections: The maximum number of detections to use per image. save_path: The path to save the images with visualized detections to. Returns: A list of lists containing the detections for each image in the generator. """ all_detections = [[ None for i in range(generator.num_classes()) if generator.has_label(i) ] for j in range(generator.size())] for i in progressbar.progressbar(range(generator.size()), prefix='Running network: '): image = generator.load_image(i) src_image = image.copy() h, w = image.shape[:2] anchors = generator.anchors image, scale, offset_h, offset_w = generator.preprocess_image(image) # run network boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0), np.expand_dims(anchors, axis=0)]) boxes[..., [0, 2]] = boxes[..., [0, 2]] - offset_w boxes[..., [1, 3]] = boxes[..., [1, 3]] - offset_h boxes /= scale boxes[:, :, 0] = np.clip(boxes[:, :, 0], 0, w - 1) boxes[:, :, 1] = np.clip(boxes[:, :, 1], 0, h - 1) boxes[:, :, 2] = np.clip(boxes[:, :, 2], 0, w - 1) boxes[:, :, 3] = np.clip(boxes[:, :, 3], 0, h - 1) # select indices which have a score above the threshold indices = np.where(scores[0, :] > score_threshold)[0] # select those scores scores = scores[0][indices] # find the order with which to sort the scores scores_sort = np.argsort(-scores)[:max_detections] # select detections # (n, 4) image_boxes = boxes[0, indices[scores_sort], :] # (n, ) image_scores = scores[scores_sort] # (n, ) image_labels = labels[0, indices[scores_sort]] # (n, 6) detections = np.concatenate([ image_boxes, np.expand_dims(image_scores, axis=1), np.expand_dims(image_labels, axis=1) ], axis=1) if visualize: draw_annotations(src_image, generator.load_annotations(i), label_to_name=generator.label_to_name) draw_detections(src_image, detections[:5, :4], detections[:5, 4], detections[:5, 5].astype(np.int32), label_to_name=generator.label_to_name, score_threshold=score_threshold) # cv2.imwrite(os.path.join(save_path, '{}.png'.format(i)), raw_image) cv2.namedWindow('{}'.format(i), cv2.WINDOW_NORMAL) cv2.imshow('{}'.format(i), src_image) cv2.waitKey(0) # copy detections to all_detections for class_id in range(generator.num_classes()): all_detections[i][class_id] = detections[detections[:, -1] == class_id, :-1] return all_detections
def _get_detections(generator, model, score_threshold=0.05, max_detections=100, save_path=None): """ Get the detections from the model using the generator. The result is a list of lists such that the size is: all_detections[num_images][num_classes] = detections[num_detections, 4 + num_classes] # Arguments generator : The generator used to run images through the model. model : The model to run on the images. score_threshold : The score confidence threshold to use. max_detections : The maximum number of detections to use per image. save_path : The path to save the images with visualized detections to. # Returns A list of lists containing the detections for each image in the generator. """ all_detections = [[ None for i in range(generator.num_classes()) if generator.has_label(i) ] for j in range(generator.size())] ## added by me image_names = [] detection_list = [] scores_list = [] labels_list = [] for i in range(generator.size() ): #progressbar.progressbar(, prefix='Running network: '): raw_image = generator.load_image(i) ## i added the names part image_name = generator.image_path(i) image_names.append(image_name) image = generator.preprocess_image(raw_image.copy()) image, scale = generator.resize_image(image) if keras.backend.image_data_format() == 'channels_first': image = image.transpose((2, 0, 1)) # run network boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0))[:3] # correct boxes for image scale boxes /= scale # select indices which have a score above the threshold indices = np.where(scores[0, :] > score_threshold)[0] # select those scores scores = scores[0][indices] # find the order with which to sort the scores scores_sort = np.argsort(-scores)[:max_detections] # select detections image_boxes = boxes[0, indices[scores_sort], :] ## annotations for drawing: detection_list.append(image_boxes) image_scores = scores[scores_sort] scores_list.append(image_scores) image_labels = labels[0, indices[scores_sort]] labels_list.append(image_labels) image_detections = np.concatenate([ image_boxes, np.expand_dims(image_scores, axis=1), np.expand_dims(image_labels, axis=1) ], axis=1) if save_path is not None: ## both annotations and detections are drawn an "raw_image" draw_annotations(raw_image, generator.load_annotations(i), label_to_name=generator.label_to_name) draw_detections(raw_image, image_boxes, image_scores, image_labels, label_to_name=generator.label_to_name) cv2.imwrite(os.path.join(save_path, '{}.png'.format(i)), raw_image) # copy detections to all_detections for label in range(generator.num_classes()): if not generator.has_label(label): continue all_detections[i][label] = image_detections[ image_detections[:, -1] == label, :-1] #print("scores_list: ",scores_list) #print("labels_list: ",labels_list) return all_detections, image_names, detection_list, scores_list, labels_list
# std_ = [0.2, 0.2, 0.2, 0.2] # # width = boxes[:, 2] - boxes[:, 0] # height = boxes[:, 3] - boxes[:, 1] # x1 = boxes[:, 0] + (deltas[:, 0] * std_[0] + mean_[0]) * width # y1 = boxes[:, 1] + (deltas[:, 1] * std_[1] + mean_[1]) * height # x2 = boxes[:, 2] + (deltas[:, 2] * std_[2] + mean_[2]) * width # y2 = boxes[:, 3] + (deltas[:, 3] * std_[3] + mean_[3]) * height # src_img = image.copy() # for x1_, y1_, x2_, y2_, class_id in zip(x1, y1, x2, y2, ids): # if train_generator.has_label(class_id): # x1_, y1_, x2_, y2_ = int(x1_), int(y1_), int(x2_), int(y2_) # cv2.rectangle(image, (x1_, y1_), (x2_, y2_), (0, 255, 0), 2) # class_name = train_generator.labels[class_id] # label = class_name # ret, baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.3, 1) # cv2.rectangle(image, (x1_, y2_ - ret[1] - baseline), (x1_ + ret[0], y2_), (255, 255, 255), -1) # cv2.putText(image, label, (x1_, y2_ - baseline), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1) # cv2.imshow('image', cv2.hconcat([src_img.astype(np.uint8)[..., ::-1],image.astype(np.uint8)[..., ::-1]])) # cv2.waitKey(0) for group in train_generator.groups: imgs, anns = train_generator.get_augmented_data(group) for img, ann in zip(imgs, anns): from utils.visualization import draw_annotations draw_annotations(img, ann) cv2.imshow("img", img) key = cv2.waitKey(0) if key == 27: break cv2.destroyAllWindows()
def _get_detections(generator, model, score_threshold=0.05, max_detections=100, save_path=None): """ Get the detections from the model using the generator. The result is a list of lists such that the size is: all_detections[num_images][num_classes] = detections[num_class_detections, 5] Args: generator: The generator used to run images through the model. model: The model to run on the images. score_threshold: The score confidence threshold to use. max_detections: The maximum number of detections to use per image. save_path: The path to save the images with visualized detections to. Returns: A list of lists containing the detections for each image in the generator. """ all_detections = [[ None for i in range(generator.num_classes()) if generator.has_label(i) ] for j in range(generator.size())] for i in progressbar.progressbar(range(generator.size()), prefix='Running network: '): raw_image = generator.load_image(i) image = generator.preprocess_image(raw_image.copy()) image, scale = generator.resize_image(image) # run network boxes, scores, labels = model.predict_on_batch( np.expand_dims(image, axis=0))[:3] # correct boxes for image scale boxes /= scale # select indices which have a score above the threshold indices = np.where(scores[0, :] > score_threshold)[0] # select those scores scores = scores[0][indices] # find the order with which to sort the scores scores_sort = np.argsort(-scores)[:max_detections] # select detections # (n, 4) image_boxes = boxes[0, indices[scores_sort], :] # (n, ) image_scores = scores[scores_sort] # (n, ) image_labels = labels[0, indices[scores_sort]] # (n, 6) image_detections = np.concatenate([ image_boxes, np.expand_dims(image_scores, axis=1), np.expand_dims(image_labels, axis=1) ], axis=1) if save_path is not None: draw_annotations(raw_image, generator.load_annotations(i), label_to_name=generator.label_to_name) draw_detections(raw_image, image_boxes, image_scores, image_labels, label_to_name=generator.label_to_name, score_threshold=score_threshold) cv2.imwrite(os.path.join(save_path, '{}.png'.format(i)), raw_image) # copy detections to all_detections for label in range(generator.num_classes()): if not generator.has_label(label): continue all_detections[i][label] = image_detections[ image_detections[:, -1] == label, :-1] return all_detections
def _get_detections(generator, model, score_threshold=0.05, max_detections=100, visualize=False): """ Get the detections from the model using the generator. The result is a list of lists such that the size is: all_detections[num_images][num_classes] = detections[num_class_detections, 5] Args: generator: The generator used to run images through the model. model: The model to run on the images. score_threshold: The score confidence threshold to use. max_detections: The maximum number of detections to use per image. save_path: The path to save the images with visualized detections to. Returns: A list of lists containing the detections for each image in the generator. """ all_detections = [[None for i in range(generator.num_classes()) if generator.has_label(i)] for j in range(generator.size())] for i in progressbar.progressbar(range(generator.size()), prefix='Running network: '): image = generator.load_image(i) src_image = image.copy() image_shape = image.shape[:2] image_shape = np.array(image_shape) image = generator.preprocess_image(image)[0] # run network detections = model.predict_on_batch([np.expand_dims(image, axis=0), np.expand_dims(image_shape, axis=0)])[0] detections_copy = detections.copy() detections = np.zeros_like(detections_copy, dtype=np.float64) # x1 detections[:, 0] = detections_copy[:, 1] # y1 detections[:, 1] = detections_copy[:, 0] # x2 detections[:, 2] = detections_copy[:, 3] # y2 detections[:, 3] = detections_copy[:, 2] detections[:, 4:] = detections_copy[:, 4:] scores = detections[:, 4] # select indices which have a score above the threshold indices = np.where(scores > score_threshold)[0] # select those detections detections = detections[indices] if visualize: draw_annotations(src_image, generator.load_annotations(i), label_to_name=generator.label_to_name) draw_detections(src_image, detections[:5, :4], detections[:5, 4], detections[:5, 5].astype(np.int32), label_to_name=generator.label_to_name, score_threshold=score_threshold) # cv2.imwrite(os.path.join(save_path, '{}.png'.format(i)), raw_image) cv2.namedWindow('{}'.format(i), cv2.WINDOW_NORMAL) cv2.imshow('{}'.format(i), src_image) cv2.waitKey(0) # copy detections to all_detections for class_id in range(generator.num_classes()): all_detections[i][class_id] = detections[detections[:, -1] == class_id, :-1] return all_detections
def _get_detections(generator, model, score_threshold=0.05, max_detections=100, save_path=None): """ Get the detections from the model using the generator. The result is a list of lists such that the size is: all_detections[num_images][num_classes] = (boxes+classes = detections[num_detections, 4 + num_classes], rotations = detections[num_detections, num_rotation_parameters], translations = detections[num_detections, num_translation_parameters) # Arguments generator : The generator used to run images through the model. model : The model to run on the images. score_threshold : The score confidence threshold to use. max_detections : The maximum number of detections to use per image. save_path : The path to save the images with visualized detections to. # Returns A list of lists containing the detections for each image in the generator. """ all_detections = [[ None for i in range(generator.num_classes()) if generator.has_label(i) ] for j in range(generator.size())] for i in progressbar.progressbar(range(generator.size()), prefix='Running network: '): raw_image = generator.load_image(i) image, scale = generator.preprocess_image(raw_image.copy()) # image, scale = generator.resize_image(image) camera_matrix = generator.load_camera_matrix(i) camera_input = generator.get_camera_parameter_input( camera_matrix, scale, generator.translation_scale_norm) # if keras.backend.image_data_format() == 'channels_first': # image = image.transpose((2, 0, 1)) # run network boxes, scores, labels, rotations, translations = model.predict_on_batch( [ np.expand_dims(image, axis=0), np.expand_dims(camera_input, axis=0) ])[:5] if tf.version.VERSION >= '2.0.0': boxes = boxes.numpy() scores = scores.numpy() labels = labels.numpy() rotations = rotations.numpy() translations = translations.numpy() # correct boxes for image scale boxes /= scale #rescale rotations and translations rotations *= math.pi height, width, _ = raw_image.shape # select indices which have a score above the threshold indices = np.where(scores[0, :] > score_threshold)[0] # select those scores scores = scores[0][indices] # find the order with which to sort the scores scores_sort = np.argsort(-scores)[:max_detections] # select detections image_boxes = boxes[0, indices[scores_sort], :] image_rotations = rotations[0, indices[scores_sort], :] image_translations = translations[0, indices[scores_sort], :] image_scores = scores[scores_sort] image_labels = labels[0, indices[scores_sort]] image_detections = np.concatenate([ image_boxes, np.expand_dims(image_scores, axis=1), np.expand_dims(image_labels, axis=1) ], axis=1) if save_path is not None: raw_image = cv2.cvtColor(raw_image, cv2.COLOR_RGB2BGR) draw_annotations(raw_image, generator.load_annotations(i), class_to_bbox_3D=generator.get_bbox_3d_dict(), camera_matrix=generator.load_camera_matrix(i), label_to_name=generator.label_to_name) draw_detections(raw_image, image_boxes, image_scores, image_labels, image_rotations, image_translations, class_to_bbox_3D=generator.get_bbox_3d_dict(), camera_matrix=generator.load_camera_matrix(i), label_to_name=generator.label_to_name) cv2.imwrite(os.path.join(save_path, '{}.png'.format(i)), raw_image) # copy detections to all_detections for label in range(generator.num_classes()): if not generator.has_label(label): continue all_detections[i][label] = ( image_detections[image_detections[:, -1] == label, :-1], image_rotations[image_detections[:, -1] == label, :], image_translations[image_detections[:, -1] == label, :]) return all_detections