def test_single_pbox_set_covar(self): # Create detection.json file det_data = {} det_data['classes'] = self.defaul_det_classes det_data['img_names'] = self.default_img_names det_data['detections'] = [[self.default_det_dict]] det_file_name = os.path.join(self.det_files_root, 'det_file.json') with open(det_file_name, 'w') as f: json.dump(det_data, f) detections = read_files.read_pbox_json(det_file_name, override_cov=200) self.assertEqual(len(detections), 1) self.assertIsInstance(detections, read_files.BoxLoader) img_iterator = iter(detections) img_dets = next(img_iterator) self.assertEqual(len(img_dets), 1) self.assertIsInstance(img_dets[0], PBoxDetInst) self.assertTrue( np.allclose(img_dets[0].covs, [[[200, 0], [0, 200]], [[200, 0], [0, 200]]])) self.assertTrue( np.allclose(img_dets[0].box, self.default_det_dict['bbox'])) self.assertListEqual(list(img_dets[0].class_list), self.default_det_dict['label_probs'])
def load_gt_and_det_data(gt_loc, det_json, data_type): """ Function for loading ground-truth and detection instances for a single folder/sequence :param gt_loc: location of folder/file where ground-truth information can be extracted :param det_json: detection json file for the given folder/sequence :param data_type: string defining if we are analysing 'coco' or 'rvc1' data as this will effect what, and how, data is loaded given the specific filtering process of rvc1. :return: gt_instances, det_instances, and class list """ if data_type == 'coco': # output is a generator of lists of GroundTruthInstance objects and a map of gt_class_ids gt_instances, gt_class_ids_map = read_files.read_COCO_gt(gt_loc, ret_classes=True) # output is a generator of lists of DetectionInstance objects (BBox or PBox depending) det_instances = read_files.read_pbox_json(det_json, gt_class_ids_map, override_cov=args.set_cov) class_idxs = [gt_class_ids_map[key] for key in sorted(gt_class_ids_map.keys())] class_names = list(sorted(gt_class_ids_map.keys())) class_list = [class_names[idx] for idx in np.argsort(class_idxs)] elif data_type == 'rvc1': gt_instances = rvc1_gt_loader.SequenceGTLoader(gt_loc) det_instances = rvc1_submission_loader.DetSequenceLoader(det_json) class_list = rvc1_class_list.CLASSES else: sys.exit("ERROR! Invalid data type provided") # check gt_instances and det_instances are the same size if len(gt_instances) != len(det_instances): sys.exit("ERROR! gt and det not same length. gt: {0}, det: {1}".format(len(gt_instances), len(det_instances))) return gt_instances, det_instances, class_list
def test_single_pbox_w_non_gt_class(self): # Create detection.json file det_data = {} det_data['classes'] = self.defaul_det_classes det_data['img_names'] = self.default_img_names self.default_det_dict.update( {'covars': [self.default_covar, self.default_covar]}) self.default_det_dict.update({'label_probs': [0, 0.7, 0.3]}) det_data['detections'] = [[self.default_det_dict]] det_file_name = os.path.join(self.det_files_root, 'det_file.json') with open(det_file_name, 'w') as f: json.dump(det_data, f) gt_class_ids_dict = {'none': 0, 'diamond': 1, 'square': 2} detections = read_files.read_pbox_json(det_file_name, gt_class_ids_dict) self.assertEqual(len(detections), 1) self.assertIsInstance(detections, read_files.BoxLoader) img_iterator = iter(detections) img_dets = next(img_iterator) self.assertEqual(len(img_dets), 1) self.assertIsInstance(img_dets[0], PBoxDetInst) self.assertTrue( np.allclose(img_dets[0].covs, [self.default_covar, self.default_covar])) self.assertTrue( np.allclose(img_dets[0].box, self.default_det_dict['bbox'])) expected_label_probs = np.array([0.0, 0.0, 0.7]) self.assertTrue( np.allclose(img_dets[0].class_list, expected_label_probs))
def test_single_pbox_w_extra_gt_label(self): # Create detection.json file det_data = {} det_data['classes'] = self.defaul_det_classes det_data['img_names'] = self.default_img_names self.default_det_dict.update( {'covars': [self.default_covar, self.default_covar]}) det_data['detections'] = [[self.default_det_dict]] det_file_name = os.path.join(self.det_files_root, 'det_file.json') with open(det_file_name, 'w') as f: json.dump(det_data, f) detections = read_files.read_pbox_json(det_file_name, self.default_gt_class_ids_dict) self.assertEqual(len(detections), 1) self.assertIsInstance(detections, read_files.BoxLoader) img_iterator = iter(detections) img_dets = next(img_iterator) self.assertEqual(len(img_dets), 1) self.assertIsInstance(img_dets[0], PBoxDetInst) self.assertTrue( np.allclose(img_dets[0].covs, [self.default_covar, self.default_covar])) self.assertTrue( np.allclose(img_dets[0].box, self.default_det_dict['bbox'])) expected_label_probs = [0, 1, 0, 0] self.assertListEqual(list(img_dets[0].class_list), expected_label_probs)
def main(): # Load all relevant information for this sequence of information det_instances, class_list = read_files.read_pbox_json( args.det_json, override_cov=args.set_cov, get_class_names=True) img_data_sequence = zip( sorted( glob.glob(os.path.join(args.gt_img_folder, '*.' + args.img_type))), det_instances) # Go over each image and draw appropriate for img_name, img_dets in tqdm(img_data_sequence, total=len(det_instances), desc='image drawing'): if args.img_set is None or os.path.basename(img_name) in args.img_set: save_detection_img(img_name, img_dets, class_list, args.save_folder, args.corner_mode)
def test_one_pbox_one_bbox_diff_imgs(self): # Create detection.json file det_data = {} det_data['classes'] = self.defaul_det_classes det_data['img_names'] = self.default_img_names # Create one bbox detection and one pbox detection box_det_dict = self.default_det_dict.copy() self.default_det_dict.update( {'covars': [self.default_covar, self.default_covar]}) det_data['detections'] = [[self.default_det_dict], [box_det_dict]] det_file_name = os.path.join(self.det_files_root, 'det_file.json') with open(det_file_name, 'w') as f: json.dump(det_data, f) detections = read_files.read_pbox_json(det_file_name) self.assertEqual(len(detections), 2) self.assertIsInstance(detections, read_files.BoxLoader) # Image 1 img_iterator = iter(detections) img_dets = next(img_iterator) self.assertEqual(len(img_dets), 1) self.assertIsInstance(img_dets[0], PBoxDetInst) self.assertTrue( np.allclose(img_dets[0].covs, [self.default_covar, self.default_covar])) self.assertTrue( np.allclose(img_dets[0].box, self.default_det_dict['bbox'])) self.assertListEqual(list(img_dets[0].class_list), self.default_det_dict['label_probs']) # Image 2 img_dets = next(img_iterator) self.assertEqual(len(img_dets), 1) self.assertIsInstance(img_dets[0], BBoxDetInst) self.assertTrue( np.allclose(img_dets[0].box, self.default_det_dict['bbox'])) self.assertListEqual(list(img_dets[0].class_list), self.default_det_dict['label_probs'])
def gen_param_sequence(): """ Function for generating the parameter sequence to be used in evaluation procedure. Parameter sequence holds all GroundTruthInstances, DetectionInstances, and ground-truth filter flags across all sequences. :return: param_sequences: ParamSequenceHolder containing all GroundTruthInstances, DetectionInstances, and ground-truth filter flags across all sequences being evaluated. len_sequences: list of sequence lengths for all sequences being evaluated. """ # Load GTs and Detections as appropriate for different data sets (multiple sequences or one folder) if args.test_set == 'coco': # output is a generator of lists of GTInstance objects and a map of gt_class_ids gt_instances, gt_class_ids = read_files.read_COCO_gt( coco_gt_file, ret_classes=True, bbox_gt=args.bbox_gt) det_filename = args.det_loc # output is a generator of lists of DetectionInstance objects (BBox or PBox depending on detection) det_instances = read_files.read_pbox_json(det_filename, gt_class_ids, override_cov=args.set_cov, prob_seg=args.prob_seg) all_gt_instances = [gt_instances] all_det_instances = [det_instances] elif args.test_set == 'rvc1': # output is a list of generator of generators of GTInstance objects all_gt_instances = rvc1_gt_loader.read_ground_truth( rvc1_gt_folder, bbox_gt=args.bbox_gt) # Currently set this version to assume there is a .json file for all sequences in ground-truth all_det_instances = rvc1_submission_loader.read_submission( args.det_loc, ["{0:06d}".format(idx) for idx in range(len(all_gt_instances))], override_cov=args.set_cov) else: sys.exit( "ERROR! Invalid test_set parameter (must be 'coco' or 'rvc1')") param_sequence = ParamSequenceHolder(all_gt_instances, all_det_instances) len_sequences = [ len(all_gt_instances[idx]) for idx in range(len(all_gt_instances)) ] return param_sequence, len_sequences
def main(): # Load all relevant information for this sequence of information det_instances, class_list = read_files.read_pbox_json( args.det_json, override_cov=args.set_cov, get_class_names=True) all_images = sorted( glob.glob(os.path.join(args.gt_img_folder, '*.' + args.img_type))) if len(all_images) != len(det_instances): sys.exit( "ERROR! Ground truth images (--gt_img_folder) and det_instances are not the same length." "\ngt_img_folder: {0}, det_instances: {1}".format( len(all_images), len(det_instances))) img_data_sequence = zip(all_images, det_instances) # Go over each image and draw appropriate for img_name, img_dets in tqdm(img_data_sequence, total=len(det_instances), desc='image drawing'): if args.img_set is None or os.path.basename(img_name) in args.img_set: save_detection_img(img_name, img_dets, class_list, args.save_folder, args.corner_mode)
def gen_param_sequence(): """ Function for generating the parameter sequence to be used in evaluation procedure. Parameter sequence holds all GroundTruthInstances, DetectionInstances, and ground-truth filter flags across all sequences. :return: param_sequences: ParamSequenceHolder containing all GroundTruthInstances, DetectionInstances, and ground-truth filter flags across all sequences being evaluated. len_sequences: list of sequence lengths for all sequences being evaluated. """ # Load GTs and Detections as appropriate for different data sets (multiple sequences or one folder) #if args.test_set == 'coco': # output is a generator of lists of GTInstance objects and a map of gt_class_ids coco_gt_file = 'gt.json' det_loc = 'pred.json' gt_instances, gt_class_ids = read_files.read_COCO_gt(coco_gt_file, ret_classes=True, bbox_gt=True) det_filename = det_loc # output is a generator of lists of DetectionInstance objects (BBox or PBox depending on detection) det_instances = read_files.read_pbox_json(det_filename, gt_class_ids, prob_seg=False) all_gt_instances = [gt_instances] all_det_instances = [det_instances] #print("all_gt = ", all_gt_instances[0], all_det_instances) param_sequence = ParamSequenceHolder(all_gt_instances, all_det_instances) len_sequences = [ len(all_gt_instances[idx]) for idx in range(len(all_gt_instances)) ] return param_sequence, len_sequences