data_manager = DataManager(dataset_name, split, selected_classes, with_difficult_objects) ground_truth_data = data_manager.load_data() difficult_data_flags = data_manager.parser.difficult_objects scores = [] labels = [] num_gt_boxes = 0 for image_name, gt_sample in tqdm(ground_truth_data.items()): image_path = image_prefix + image_name reference_size = get_image_size(image_path) detections = infer_from_path(image_path, model, prior_boxes) gt_sample = denormalize_boxes(gt_sample, reference_size) num_gt_boxes = num_gt_boxes + len(gt_sample) already_detected = np.zeros(shape=len(gt_sample), dtype=bool) for detection in detections: ious = calculate_intersection_over_union(detection, gt_sample) score = np.max(detection[4:]) best_iou = np.max(ious) best_iou_arg = np.argmax(ious) if best_iou > iou_threshold: if not already_detected[best_iou_arg]: labels.append(True) scores.append(best_iou) already_detected[best_iou_arg] = True else: labels.append(False) scores.append(best_iou) else: labels.append(False) scores.append(best_iou) results = compute_precision_and_recall(scores, labels, num_gt_boxes)
difficult_objects = difficult_data_flags[image_name] difficult_objects = np.asarray(difficult_objects, dtype=bool) num_ground_truth_boxes += np.sum(np.logical_not(difficult_objects)) if predicted_data is None: continue """ draw_image_boxes(predicted_data, original_image_array, class_decoder, normalized=False) draw_image_boxes(ground_truth_sample, original_image_array, class_decoder, normalized=False) """ for ground_truth_object_arg in range(len(ground_truth_sample)): ground_truth_object = ground_truth_sample[ground_truth_object_arg] ious = calculate_intersection_over_union(ground_truth_object, predicted_data) detections = np.zeros_like(ious) max_iou = np.max(ious) max_iou_arg = np.argmax(ious) difficult = difficult_objects[ground_truth_object_arg] if difficult == False: predicted_sample = predicted_data[max_iou_arg] predicted_probabilities = predicted_sample[4:] predicted_sample_arg = np.argmax(predicted_probabilities) predicted_score = np.max(predicted_probabilities) scores.append(predicted_score) if max_iou > iou_threshold: if predicted_sample_arg == ground_truth_class_arg: labels.append(True) else:
difficult_objects = np.asarray(difficult_objects, dtype=bool) num_ground_truth_boxes += np.sum(np.logical_not(difficult_objects)) if predicted_data is None: # print('Zero predictions given for image:', image_name) continue #plt.imshow(original_image_array.astype('uint8')) #plt.show() #draw_image_boxes(predicted_data, original_image_array, class_decoder, normalized=False) #print(predicted_data.shape) num_predictions = len(predicted_data) for prediction_arg in range(num_predictions): predicted_box = predicted_data[prediction_arg] predicted_class_probabilities = predicted_box[4:] predicted_class_arg = np.argmax(predicted_class_probabilities) predicted_score = np.max(predicted_class_probabilities) ious = calculate_intersection_over_union(predicted_box, ground_truth_sample) num_objects = len(ground_truth_sample) for object_arg in range(num_objects): # ground_truth_classes = ground_truth_sample[object_arg][4:] # ground_truth_class_arg = np.argmax(ground_truth_classes) difficult = difficult_objects[object_arg] if difficult: # num_ground_truth_boxes = num_ground_truth_boxes - 1 continue iou = ious[object_arg] # i think this is bad you have to take the max if iou >= iou_threshold and predicted_class_arg == ground_truth_class_arg: # print('True positive:', box_arg) scores.append(predicted_score) labels.append(True)
# print(num_images_in_class) num_images_in_class = num_images_in_class + np.sum(mask) ground_truth_classes = ground_truth_classes[mask] ground_truth_boxes = ground_truth_boxes[mask] num_ground_truth_boxes = len(ground_truth_boxes) detected_boxes = detected_box_samples[image_arg] detected_scores = detected_score_samples[image_arg] num_detected_boxes = len(detected_scores) selected_ground_truth_boxes = np.zeros(num_ground_truth_boxes, dtype=bool) for detected_box_arg in range(num_detected_boxes): detected_box = detected_boxes[detected_box_arg] detected_score = detected_scores[detected_box_arg] scores.append(detected_score) ious = calculate_intersection_over_union(detected_box, ground_truth_boxes) best_detected_box = np.argmax(ious) best_iou = np.max(ious) already_selected = selected_ground_truth_boxes[best_detected_box] if best_iou > iou_treshold: if not already_selected: selected_ground_truth_boxes[best_detected_box] = True labels.append(True) else: labels.append(False) else: labels.append(False) scores = np.asarray(scores) labels = np.asarray(labels) precision, recall = compute_precision_and_recall(scores, labels, num_images_in_class)