def cal_evaluation_index(path_images, path_labels, proposals, image_info): precision = 0. recall = 0. fscore = 0. images_name = image_info['image_name'] width = image_info['width'] height = image_info['height'] for index, name in enumerate(images_name): img = cv2.imread(os.path.join(path_images, name)) im_scale = [height / img.shape[0], width / img.shape[1]] filename_label = name.split('.')[0] + '.txt' with open(os.path.join(path_labels, filename_label), 'r', encoding='utf-8') as f: lines = f.readlines() gt_boxes = np.zeros((len(lines), 4), dtype=np.float32) for x, line in enumerate(lines): l = line.split('\t') gt_boxes[x][0] = float(l[0]) * im_scale[1] gt_boxes[x][1] = float(l[1]) * im_scale[0] gt_boxes[x][2] = float(l[2]) * im_scale[1] gt_boxes[x][3] = float(l[3]) * im_scale[0] inds = np.where(proposals[:, 0] == index)[0] proposal = proposals[inds, :] if (Config.USE_C): overlaps = Utils.c_cal_overlaps(proposal[:, 2:], gt_boxes) else: overlaps = Utils.cal_overlaps(proposals[:, 2:], gt_boxes) # 求每个proposal的最大overlap argmax_overlaps = overlaps.argmax(axis=1) max_overlaps = overlaps[np.arange(overlaps.shape[0]), argmax_overlaps] max_overlaps = max_overlaps[max_overlaps > 0.7] precision = precision + max_overlaps.shape[0] / overlaps.shape[0] recall = recall + max_overlaps.shape[0] / gt_boxes.shape[0] precision = precision / len(images_name) recall = recall / len(images_name) fscore = 2 * precision * recall / (precision + recall + 0.0001) return precision, recall, fscore
def generate_labels_bboxs(self, gt_boxes, feature_map_h, feature_map_w, stride): part_anchors, part_index, num_anchors = self._generate_all_anchors( feature_map_h, feature_map_w, stride) if (Config.USE_C): part_overlaps = Utils.c_cal_overlaps(part_anchors, gt_boxes) else: part_overlaps = Utils.cal_overlaps(part_anchors, gt_boxes) part_labels = self._cal_all_labels(part_overlaps) part_bbox_target = self._cal_all_box_targets(part_overlaps, part_anchors, gt_boxes) part_bbox_inside_weights = np.zeros((len(part_index), 4), dtype=np.float32) part_bbox_inside_weights[part_labels == 1, :] = np.ones((1, 4)) part_bbox_outside_weights = np.zeros((len(part_index), 4), dtype=np.float32) part_bbox_outside_weights[part_labels == 1, :] = np.ones((1, 4)) # 一开始是将超出图像范围的anchor直接丢掉的,现在在加回来 labels = Utils.unmap(part_labels, num_anchors, part_index, fill=-1) bbox_targets = Utils.unmap(part_bbox_target, num_anchors, part_index, fill=0) # 这些anchor的真值是0,也p即没有值 bbox_inside_weights = Utils.unmap(part_bbox_inside_weights, num_anchors, part_index, fill=0) # 内部权重以0填充 bbox_outside_weights = Utils.unmap(part_bbox_outside_weights, num_anchors, part_index, fill=0) # 外部权重以0填充 labels = labels.reshape(-1, 1) bbox_targets = bbox_targets.reshape(-1, 4) bbox_inside_weights = bbox_inside_weights.reshape(-1, 4) bbox_outside_weights = bbox_outside_weights.reshape(-1, 4) return labels, bbox_targets, bbox_inside_weights, bbox_outside_weights