import torch def output_process(output, image_size, s, b, conf_th, iou_th): """ param: output [batch_size, 4, 14, 14, 204] :return output: list[], bbox: xmin, ymin, xmax, ymax, obj_conf, classes_conf, classes """ batch_size = output.size(0) grid_size = s num_classes = 20 # 假设类别数为 20 # 将输出转换为 [batch_size, 4, s^2, 4, 51] 的形状 output = output.view(batch_size, 4, grid_size ** 2, 4, 51) # 提取每个点的置信度、类别、位置和链接信息 point_conf = output[:, :, :, :, 50] # 点存在概率 classes_conf = output[:, :, :, :, 2:2 + num_classes] # 类别置信度 x = output[:, :, :, :, 0] # x 偏移量 y = output[:, :, :, :, 1] # y 偏移量 link_x = output[:, :, :, :, 22:36] # 链接向量 Lx link_y = output[:, :, :, :, 36:50] # 链接向量 Ly bboxes = [] for i in range(batch_size): for branch in range(4): # 遍历 4 个分支 for grid_idx in range(grid_size ** 2): # 遍历每个网格 for point_idx in range(4): # 遍历每个点 # 提取当前点的信息 point_x = x[i, branch, grid_idx, point_idx] point_y = y[i, branch, grid_idx, point_idx] point_confidence = point_conf[i, branch, grid_idx, point_idx] if point_idx < 2: # 前两个点为 center center_x = (grid_idx % grid_size + point_x) * (image_size / grid_size) center_y = (grid_idx // grid_size + point_y) * (image_size / grid_size) else: # 后两个点为 corner corner_x = (grid_idx % grid_size + point_x) * (image_size / grid_size) corner_y = (grid_idx // grid_size + point_y) * (image_size / grid_size) for classes in range(num_classes): point_class_conf = classes_conf[i, branch, grid_idx, point_idx,classes] for grid_idx_pair in range(grid_size ** 2): if point_idx < 2: # 前两个点为 center point_idx_pair = point_idx +2 point_x_pair = x[i, branch, grid_idx_pair, point_idx_pair] point_y_pair = y[i, branch, grid_idx_pair, point_idx_pair] corner_x = (grid_idx_pair % grid_size + point_x_pair) * (image_size / grid_size) corner_y = (grid_idx_pair // grid_size + point_y_pair) * (image_size / grid_size) else: # 后两个点为 corner point_idx_pair = point_idx-2 point_x_pair = x[i, branch, grid_idx_pair, point_idx_pair] point_y_pair = y[i, branch, grid_idx_pair, point_idx_pair] center_x = (grid_idx_pair % grid_size + point_x_pair) * (image_size / grid_size) center_y = (grid_idx_pair // grid_size + point_y_pair) * (image_size / grid_size) point_confidence_pair = point_conf[i, branch, grid_idx_pair, point_idx_pair] point_class_conf_pair = classes_conf[i, branch, grid_idx_pair, point_idx_pair,classes] L_x = link_x[i, branch,grid_idx,point_idx,grid_idx_pair % grid_size] L_y = link_y[i, branch,grid_idx,point_idx,grid_idx_pair // grid_size] L_x_pair = link_x[i, branch,grid_idx,point_idx,grid_idx % grid_size] L_y_pair = link_y[i, branch,grid_idx,point_idx,grid_idx // grid_size] obj_conf = point_confidence*point_confidence_pair*point_class_conf*point_class_conf_pair*((L_x*L_y+L_x_pair*L_y_pair)/2) half_w = abs(corner_x-center_x) half_h = abs(corner_y-center_y) x_min = center_x - half_w y_min = center_y - half_h x_max = center_x + half_w y_max = center_y + half_h bboxes.append([x_min, y_min, x_max, y_max, obj_conf, classes]) # 将边界框列表转换为张量 bboxes = torch.tensor(bboxes) # 应用 NMS filtered_bboxes = nms(bboxes, conf_th, iou_th) return filtered_bboxes def nms(bboxes, conf_th, iou_th): """ 非极大值抑制 (NMS) :param bboxes: [N, 6],其中每一行是 [xmin, ymin, xmax, ymax, obj_conf, classes] :param conf_th: 置信度阈值 :param iou_th: IOU 阈值 :return: 过滤后的边界框 """ # 过滤掉置信度低于阈值的边界框 bboxes = bboxes[bboxes[:, 4] > conf_th] # 按置信度排序 indices = torch.argsort(bboxes[:, 4], descending=True) bboxes = bboxes[indices] keep = [] while bboxes.size(0) > 0: # 选择置信度最高的边界框 keep.append(bboxes[0]) ious = get_iou(bboxes[0].unsqueeze(0)[:, :4], bboxes[1:, :4]) # 过滤掉与当前边界框 IOU 大于阈值的边界框 bboxes = bboxes[1:][ious < iou_th] return torch.stack(keep) def get_iou(bbox1, bbox2): """ 计算两个边界框的交并比 (IOU) :param bbox1: [N, 4],其中每一行是 [xmin, ymin, xmax, ymax] :param bbox2: [M, 4],其中每一行是 [xmin, ymin, xmax, ymax] :return: IOU 矩阵 [N, M] """ # 计算交集的坐标 xmin = torch.maximum(bbox1[:, 0].unsqueeze(1), bbox2[:, 0].unsqueeze(0)) ymin = torch.maximum(bbox1[:, 1].unsqueeze(1), bbox2[:, 1].unsqueeze(0)) xmax = torch.minimum(bbox1[:, 2].unsqueeze(1), bbox2[:, 2].unsqueeze(0)) ymax = torch.minimum(bbox1[:, 3].unsqueeze(1), bbox2[:, 3].unsqueeze(0)) # 计算交集的面积 intersection = torch.clamp(xmax - xmin, min=0) * torch.clamp(ymax - ymin, min=0) # 计算两个边界框的面积 area1 = (bbox1[:, 2] - bbox1[:, 0]) * (bbox1[:, 3] - bbox1[:, 1]) area2 = (bbox2[:, 2] - bbox2[:, 0]) * (bbox2[:, 3] - bbox2[:, 1]) # 计算并集的面积 union = area1.unsqueeze(1) + area2.unsqueeze(0) - intersection # 计算 IOU iou = intersection / union return iou # 示例输入 batch_size = 1 image_size = 448 s = 14 b = 2 conf_th = 0.5 iou_th = 0.5 output = torch.randn(batch_size, 4, 14, 14, 204) # 调用函数 result = output_process(output, image_size, s, b, conf_th, iou_th) print(result)