def FasterRcnn_eval(dataset_path, ckpt_path, ann_file): """FasterRcnn evaluation.""" ds = create_fasterrcnn_dataset(dataset_path, batch_size=config.test_batch_size, is_training=False) net = Faster_Rcnn_Resnet50(config) param_dict = load_checkpoint(ckpt_path) load_param_into_net(net, param_dict) net.set_train(False) eval_iter = 0 total = ds.get_dataset_size() outputs = [] dataset_coco = COCO(ann_file) print("\n========================================\n") print("total images num: ", total) print("Processing, please wait a moment.") max_num = 128 for data in ds.create_dict_iterator(num_epochs=1): eval_iter = eval_iter + 1 img_data = data['image'] img_metas = data['image_shape'] gt_bboxes = data['box'] gt_labels = data['label'] gt_num = data['valid_num'] start = time.time() # run net output = net(img_data, img_metas, gt_bboxes, gt_labels, gt_num) end = time.time() print("Iter {} cost time {}".format(eval_iter, end - start)) # output all_bbox = output[0] all_label = output[1] all_mask = output[2] for j in range(config.test_batch_size): all_bbox_squee = np.squeeze(all_bbox.asnumpy()[j, :, :]) all_label_squee = np.squeeze(all_label.asnumpy()[j, :, :]) all_mask_squee = np.squeeze(all_mask.asnumpy()[j, :, :]) all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :] all_labels_tmp_mask = all_label_squee[all_mask_squee] if all_bboxes_tmp_mask.shape[0] > max_num: inds = np.argsort(-all_bboxes_tmp_mask[:, -1]) inds = inds[:max_num] all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds] all_labels_tmp_mask = all_labels_tmp_mask[inds] outputs_tmp = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes) outputs.append(outputs_tmp) eval_types = ["bbox"] result_files = results2json(dataset_coco, outputs, "./results.pkl") coco_eval(result_files, eval_types, dataset_coco, single_result=True)
def get_eval_result(ann_file, img_path): """ Get metrics result according to the annotation file and result file""" max_num = 128 result_path = "./result_Files/" outputs = [] dataset_coco = COCO(ann_file) img_ids = dataset_coco.getImgIds() for img_id in img_ids: file_id = str(img_id).zfill(12) file = img_path + "/" + file_id + ".jpg" img_size = get_img_size(file) resize_ratio = get_resize_ratio(img_size) img_metas = np.array([img_size[1], img_size[0]] + [resize_ratio, resize_ratio]) bbox_result_file = result_path + file_id + "_0.bin" label_result_file = result_path + file_id + "_1.bin" mask_result_file = result_path + file_id + "_2.bin" mask_fb_result_file = result_path + file_id + "_3.bin" all_bbox = np.fromfile(bbox_result_file, dtype=np.float16).reshape(80000, 5) all_label = np.fromfile(label_result_file, dtype=np.int32).reshape(80000, 1) all_mask = np.fromfile(mask_result_file, dtype=np.bool_).reshape(80000, 1) all_mask_fb = np.fromfile(mask_fb_result_file, dtype=np.float16).reshape(80000, 28, 28) all_bbox_squee = np.squeeze(all_bbox) all_label_squee = np.squeeze(all_label) all_mask_squee = np.squeeze(all_mask) all_mask_fb_squee = np.squeeze(all_mask_fb) all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :] all_labels_tmp_mask = all_label_squee[all_mask_squee] all_mask_fb_tmp_mask = all_mask_fb_squee[all_mask_squee, :, :] if all_bboxes_tmp_mask.shape[0] > max_num: inds = np.argsort(-all_bboxes_tmp_mask[:, -1]) inds = inds[:max_num] all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds] all_labels_tmp_mask = all_labels_tmp_mask[inds] all_mask_fb_tmp_mask = all_mask_fb_tmp_mask[inds] bbox_results = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes) segm_results = get_seg_masks(all_mask_fb_tmp_mask, all_bboxes_tmp_mask, all_labels_tmp_mask, img_metas, True, config.num_classes) outputs.append((bbox_results, segm_results)) eval_types = ["bbox", "segm"] result_files = results2json(dataset_coco, outputs, "./results.pkl") coco_eval(result_files, eval_types, dataset_coco, single_result=False)
def get_eval_result(ann_file, result_path): """ get evaluation result of faster rcnn""" max_num = 128 result_path = result_path outputs = [] dataset_coco = COCO(ann_file) img_ids = dataset_coco.getImgIds() for img_id in img_ids: file_id = str(img_id).zfill(12) bbox_result_file = os.path.join(result_path, file_id + "_0.bin") label_result_file = os.path.join(result_path, file_id + "_1.bin") mask_result_file = os.path.join(result_path, file_id + "_2.bin") all_bbox = np.fromfile(bbox_result_file, dtype=np.float16).reshape(80000, 5) all_label = np.fromfile(label_result_file, dtype=np.int32).reshape(80000, 1) all_mask = np.fromfile(mask_result_file, dtype=np.bool_).reshape(80000, 1) all_bbox_squee = np.squeeze(all_bbox) all_label_squee = np.squeeze(all_label) all_mask_squee = np.squeeze(all_mask) all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :] all_labels_tmp_mask = all_label_squee[all_mask_squee] if all_bboxes_tmp_mask.shape[0] > max_num: inds = np.argsort(-all_bboxes_tmp_mask[:, -1]) inds = inds[:max_num] all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds] all_labels_tmp_mask = all_labels_tmp_mask[inds] outputs_tmp = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes) outputs.append(outputs_tmp) eval_types = ["bbox"] result_files = results2json(dataset_coco, outputs, "./results.pkl") coco_eval(result_files, eval_types, dataset_coco, single_result=False)