Beispiel #1
0
    def Occlusion_v5(self,
                     cls=None,
                     bbox_2d=None,
                     bbox_3d=None,
                     threshold=0.3,
                     occ_correct=None):
        '''
        cls: tensor.cuda, (N, 4),
        bbox_2d: tensor.cuda, (N, 4),
        bbox_3d: tensor.cuda, (N, 7),
        occ_correct: tensor.cuda, (7),
        threshold: float in 0~1,
        '''
        print("修改前的bbox_3d:\n", bbox_3d)

        # cls转化为类型编码,并由类型编码筛选出类型为car的数据的索引;
        typeEncode = torch.argmax(cls[:, :], 1) + 1
        # print("typeEncode:", typeEncode)
        tensor_ones = torch.ones(1).cuda().bool()
        tensor_zeros = torch.zeros(1).cuda().bool()
        idx_filter = torch.where(typeEncode == 1, tensor_ones, tensor_zeros)
        print("idx_filter:", idx_filter)

        # 由索引过滤出类型为car的数据
        bbox2d_filter = bbox_2d[idx_filter]
        bbox3d_filter = bbox_3d[idx_filter]
        bbox3d_filter = bbox3d_filter.detach()
        bbox3d_stack = torch.zeros(
            (bbox3d_filter.shape[0], bbox3d_filter.shape[0],
             bbox3d_filter.shape[1])).type(torch.DoubleTensor).cuda()
        if bbox3d_filter.shape[0] > 0:
            bbox3d_stack[0] = bbox3d_filter
        # bbox3d_filter_tmp = bbox3d_filter.clone()
        # print("bbox3d_filter修正前:\n", bbox3d_filter)
        # x,y,w,h --> x1,y1,x2,y2
        bbox2d_filter = bbXYWH2Coords(bbox2d_filter)
        # print("bbox2d_filter的type: {}".format(type(bbox2d_filter)))

        # 筛选后按深度大小排序
        idx_sort = bbox3d_filter.argsort(0)[:, 2]
        # print("id_sort:\n", idx_sort)

        print("car_num_now: {}".format(idx_sort.shape[0]))
        # self.car_num_max = idx_sort.shape[0] if idx_sort.shape[0] > self.car_num_max else self.car_num_max
        # self.car_num_min = idx_sort.shape[0] if idx_sort.shape[0] < self.car_num_min else self.car_num_min
        # print("car_num_max: {} , car_num_min: {} ".format(self.car_num_max, self.car_num_min))
        # car_num_path = os.path.join(os.getcwd(), "output", "record", "car_num数记录_{}".format(self.now))
        # with open(car_num_path, 'a') as f:
        #     f.write("{}\n".format(idx_sort.shape[0]))

        # print("occ_correct.shape:", occ_correct.shape)
        # print("occ_correct:", occ_correct)

        # 计算iou,判定遮挡关系,进行遮挡修正
        time_start = time()
        for count in range(0, idx_sort.shape[0] - 1):
            print("第 {} 次循环:".format(count))
            iouValue = torch.zeros(bbox2d_filter.shape[0]).cuda()  # 初始化全部为0;

            iouValue[count + 1:] = iou(
                bbox2d_filter[idx_sort[count:count + 1]],
                bbox2d_filter[idx_sort[count + 1:]])
            print("iouValue:", iouValue)

            idx_occlusion = torch.where(iouValue > threshold,
                                        torch.ones(1).cuda().byte(),
                                        torch.zeros(1).cuda().byte())
            print("遮挡关系:", idx_occlusion)
            print("当前值为:", bbox3d_filter[idx_sort[count]])
            # print("occ_correct:", occ_correct)
            correct_value = bbox3d_filter[
                idx_sort[count]].detach() * occ_correct
            # print("correct_value:", correct_value)
            bbox3d_stack[count + 1] = bbox3d_stack[count]
            bbox3d_stack[count + 1, idx_sort[idx_occlusion]] = bbox3d_stack[
                count, idx_sort[idx_occlusion]] + correct_value
            # print("bbox3d_filter[idx_sort[idx_occlusion]]:", bbox3d_filter[idx_sort[idx_occlusion]])
            # print("bbox3d_filter修正后:\n", bbox3d_filter)

        bbox2d_filter = bbCoords2XYWH(
            bbox2d_filter)  # 变回x,y,w,h模式,其实这一步不太需要,因为从始至终都没改变bbox_2d的值;
        # print("用了{}秒".format(time() - time_start))
        # 将修改完的值返回给原数据
        print("bbox3d_stack:\n", bbox3d_stack)
        bbox_3d[idx_filter] = bbox3d_stack[-1]

        print("修改后的bbox_3d:\n", bbox_3d)
        return bbox_3d
Beispiel #2
0
    def Occlusion_v4(self,
                     cls=None,
                     bbox_2d=None,
                     bbox_3d=None,
                     threshold=0.3,
                     occ_correct=None):
        '''
        cls: tensor.cuda, ( N, 4),
        bbox_2d: tensor.cuda, ( N, 4),
        bbox_3d: tensor.cuda, ( N, 7),
        occ_correct: tensor.cuda, (7),
        threshold: float in 0~1,
        '''
        # print("修改前的bbox_3d:\n", bbox_3d)
        # bbox_3d_correct = torch.zeros_like(bbox_3d)
        # bbox_3d_correct = torch.tensor(bbox_3d_correct, requires_grad=False)
        # occ_correct = occ_correct.detach()

        # cls转化为类型编码,并由类型编码筛选出类型为car的数据的索引;
        typeEncode = torch.argmax(cls[:, :], 1) + 1
        # print("typeEncode:", typeEncode)
        idx_filter = torch.where(typeEncode == 1,
                                 torch.ones(1).cuda().byte(),
                                 torch.zeros(1).cuda().byte())
        idx_filter = idx_filter.bool()
        # print("idx_filter:", idx_filter)

        # 由索引过滤出类型为car的数据
        bbox2d_filter = bbox_2d[idx_filter]
        # bbox2d_filter = torch.tensor(bbox2d_filter, requires_grad=False)
        bbox3d_filter = bbox_3d[idx_filter]
        bbox3d_filter = bbox3d_filter.detach()
        # bbox3d_filter_tmp = bbox3d_filter.clone()
        # print("bbox3d_filter修正前:\n", bbox3d_filter)
        # x,y,w,h --> x1,y1,x2,y2
        bbox2d_filter = bbXYWH2Coords(bbox2d_filter)
        # print("bbox2d_filter的type: {}".format(type(bbox2d_filter)))

        # 筛选后按深度大小排序
        idx_sort = bbox3d_filter.argsort(0)[:, 2]
        # print("id_sort:\n", idx_sort)

        # 计算iou,判定遮挡关系,进行遮挡修正
        time_start = time()
        for count in range(0, idx_sort.shape[0] - 1):
            # print("第 {} 次循环:".format(count))
            iouValue = torch.zeros(bbox2d_filter.shape[0]).cuda()  # 初始化全部为0;

            iouValue[count + 1:] = iou(
                bbox2d_filter[idx_sort[count:count + 1]],
                bbox2d_filter[idx_sort[count + 1:]])
            # print("iouValue:", iouValue)
            idx_occlusion = torch.where(iouValue > threshold,
                                        torch.ones(1).cuda().byte(),
                                        torch.zeros(1).cuda().byte())
            # print("遮挡关系:", idx_occlusion)
            # print("当前值为:", bbox3d_filter[idx_sort[count]])
            # print("occ_correct:", occ_correct)
            # print("bbox3d_filter.grad_fn:", bbox3d_filter.grad_fn)
            correct_value = bbox3d_filter[
                idx_sort[count]].detach() * occ_correct
            # print("correct_value:", correct_value)
            bbox3d_filter[idx_sort[idx_occlusion]] = bbox3d_filter[idx_sort[
                idx_occlusion]] + correct_value  # 用'='表示返回的是修正值,用'+='返回的是修改后的值;
            # print("bbox3d_filter[idx_sort[idx_occlusion]]:", bbox3d_filter[idx_sort[idx_occlusion]])
            # print("bbox3d_filter修正后:\n", bbox3d_filter)

        bbox2d_filter = bbCoords2XYWH(
            bbox2d_filter)  # 变回x,y,w,h模式,其实这一步不太需要,因为从始至终都没改变bbox_2d的值;
        print("用了{}秒".format(time() - time_start))
        # 将修改完的值返回给原数据
        # bbox_3d_correct[bs, idx_filter] = bbox3d_filter - bbox3d_filter_tmp
        bbox_3d[idx_filter] = bbox3d_filter

        # bbox_3d = bbox_3d + bbox_3d_correct

        # print("修改后的bbox_3d:\n", bbox_3d)
        return bbox_3d
Beispiel #3
0
    def Occlusion_v2(self,
                     cls=None,
                     bbox_2d=None,
                     bbox_3d=None,
                     threshold=0.3,
                     occ_correct=None):
        # print(cls, cls.shape)
        # print(type(cls))
        # print(type(cls))
        print("修改前的bbox_3d:\n", bbox_3d)

        for bs in range(cls.shape[0]):
            # cls转化为类型编码,并由类型编码筛选出类型为car的数据的索引;
            typeEncode = torch.argmax(cls[bs, :, :], 1) + 1
            # print("typeEncode:", typeEncode)
            # idx_zeros = torch.zeros(cls.shape[1]).cuda().byte()
            # idx_ones = torch.ones(cls.shape[1]).cuda().byte()
            # print("idx_ones", idx_ones)
            idx_filter = torch.where(typeEncode == 1,
                                     torch.ones(1).cuda().byte(),
                                     torch.zeros(1).cuda().byte())
            print("idx_filter:", idx_filter)

            # 由索引过滤出类型为car的数据
            bbox2d_filter = bbox_2d[bs, idx_filter]
            bbox3d_filter = bbox_3d[bs, idx_filter]
            # print("bbox3d_filter修正前:\n", bbox3d_filter)
            # x,y,w,h --> x1,y1,x2,y2
            bbox2d_filter = bbXYWH2Coords(bbox2d_filter)
            # print("bbox2d_filter的type: {}".format(type(bbox2d_filter)))

            # 筛选后按深度大小排序
            idx_sort = bbox3d_filter.argsort(0)[:, 2]
            # print("id_sort:\n", idx_sort)

            # 计算iou,判定遮挡关系,进行遮挡修正
            for count in range(0, idx_sort.shape[0] - 1):
                print("第 {} 次循环:".format(count))
                # threshold = 0.2         # 遮挡关系判定的阈值
                # occ_correct = np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.0])   # 这里先假定遮挡修正值
                iouValue = torch.zeros(
                    bbox2d_filter.shape[0]).cuda()  # 初始化全部为0;
                # idx_occlusion = torch.zeros(bbox2d_filter.shape[0]).cuda().byte()  # 初始化全部为False;

                # iouValue[count + 1:] = torch.from_numpy(iou(bbox2d_filter[idx_sort[count:count + 1]].cpu().detach().numpy(),
                #                            bbox2d_filter[idx_sort[count + 1:]].cpu().detach().numpy()))
                iouValue[count + 1:] = iou(
                    bbox2d_filter[idx_sort[count:count + 1]],
                    bbox2d_filter[idx_sort[count + 1:]])
                print("iouValue:", iouValue)
                idx_occlusion = torch.where(iouValue > threshold,
                                            torch.ones(1).cuda().byte(),
                                            torch.zeros(1).cuda().byte())
                # for occid in range(count + 1, idx_occlusion.shape[0]):
                #     # print(occid)
                #     idx_occlusion[occid] |= (iouValue[occid] > threshold)
                print("遮挡关系:", idx_occlusion)
                print("当前值为:", bbox3d_filter[idx_sort[count]])
                bbox3d_filter[
                    idx_sort[idx_occlusion]] += bbox3d_filter[idx_sort[
                        count]] * occ_correct  # 用'='表示返回的是修正值,用'+='返回的是修改后的值;
                # print("bbox3d_filter修正后:\n", bbox3d_filter)

            bbox2d_filter = bbCoords2XYWH(
                bbox2d_filter)  # 变回x,y,w,h模式,其实这一步不太需要,因为从始至终都没改变bbox_2d的值;

            # 将修改完的值返回给原数据
            bbox_3d[bs, idx_filter] = bbox3d_filter

        print("修改后的bbox_3d:\n", bbox_3d)

        return bbox_3d
Beispiel #4
0
    def Occlusion_v3(self,
                     cls=None,
                     bbox_2d=None,
                     bbox_3d=None,
                     threshold=0.3,
                     occ_correct=None):
        # print(cls, cls.shape)
        # print(type(cls))
        # print(type(cls))
        print("修改前的bbox_3d:\n", bbox_3d)

        bbox_3d_correct = torch.zeros_like(bbox_3d)
        bbox_3d_correct = torch.tensor(bbox_3d_correct, requires_grad=False)

        for bs in range(cls.shape[0]):
            # ======cls转化为类型编码,并由类型编码筛选出类型为car的数据的索引;
            typeEncode = torch.argmax(cls[bs, :, :], 1) + 1
            # print("typeEncode:", typeEncode)
            idx_filter = torch.where(typeEncode == 1,
                                     torch.ones(1).cuda().byte(),
                                     torch.zeros(1).cuda().byte())
            print("idx_filter:", idx_filter)

            # ======由索引过滤出类型为car的数据
            bbox2d_filter = bbox_2d[bs, idx_filter]
            bbox2d_filter = torch.tensor(
                bbox2d_filter,
                requires_grad=False)  # .type(torch.FloatTensor).cuda()
            bbox3d_filter = bbox_3d[bs, idx_filter]
            print("bbox3d_filter.grad:", bbox3d_filter.grad)
            bbox3d_filter = torch.tensor(
                bbox3d_filter,
                requires_grad=False)  # .type(torch.FloatTensor).cuda()
            print("bbox3d_filter.grad:", bbox3d_filter.grad)
            bbox3d_filter_tmp = bbox3d_filter.clone()
            # print("bbox3d_filter修正前:\n", bbox3d_filter)
            # x,y,w,h --> x1,y1,x2,y2
            bbox2d_filter = bbXYWH2Coords(bbox2d_filter)
            # print("bbox2d_filter的type: {}".format(type(bbox2d_filter)))

            # ======筛选后按深度大小排序
            idx_sort = bbox3d_filter.argsort(0)[:, 2]
            # print("id_sort:\n", idx_sort)

            # ======计算iou,判定遮挡关系,进行遮挡修正
            for count in range(0, idx_sort.shape[0] - 1):
                print("第 {} 次循环:".format(count))

                iouValue = torch.zeros(
                    bbox2d_filter.shape[0]).cuda()  # 初始化全部为0;

                iouValue[count + 1:] = iou(
                    bbox2d_filter[idx_sort[count:count + 1]],
                    bbox2d_filter[idx_sort[count + 1:]])
                print("iouValue:", iouValue)

                idx_occlusion = torch.where(iouValue > threshold,
                                            torch.ones(1).cuda().byte(),
                                            torch.zeros(1).cuda().byte())
                print("遮挡关系:", idx_occlusion)
                print("当前值为:", bbox3d_filter[idx_sort[count]])

                # 用'='表示返回的是修正值,用'+='返回的是修改后的值;
                bbox3d_filter[idx_sort[idx_occlusion]] = bbox3d_filter[
                    idx_sort[idx_occlusion]] + bbox3d_filter[
                        idx_sort[count]] * occ_correct
                # print("bbox3d_filter修正后:\n", bbox3d_filter)

            bbox2d_filter = bbCoords2XYWH(
                bbox2d_filter)  # 变回x,y,w,h模式,其实这一步可能不太需要,因为从始至终都没改变bbox_2d的值;

            bbox_3d_correct[bs, idx_filter] = bbox3d_filter - bbox3d_filter_tmp
        print("bbox_3d_correct:", bbox_3d_correct)
        # ======将修改完的值返回给原数据
        bbox_3d = bbox_3d + bbox_3d_correct
        print("修改后的bbox_3d:\n", bbox_3d)

        return bbox_3d
Beispiel #5
0
    def Occlusion_v1(self,
                     cls=None,
                     bbox_2d=None,
                     bbox_3d=None,
                     threshold=0.3,
                     occ_correct=None):
        # print(cls, cls.shape)
        # print(type(cls))
        cls = cls.cpu().detach().numpy()
        bbox_2d = bbox_2d.cpu().detach().numpy()
        bbox_3d = bbox_3d.cpu().detach().numpy()
        occ_correct = occ_correct.cpu().detach().numpy()
        # print(type(cls))
        # print("修改前的bbox_3d:\n", bbox_3d)

        for bs in range(cls.shape[0]):
            # cls转化为类型编码,并由类型编码筛选出类型为car的数据的索引;
            typeEncode = np.argmax(cls[bs, :, :], axis=1) + 1
            idx_filter = np.zeros([cls.shape[1]], dtype=bool)
            for i in range(cls.shape[1]):
                idx_filter |= typeEncode == 1

            # 由索引过滤出类型为car的数据
            bbox2d_filter = bbox_2d[bs, idx_filter]
            bbox3d_filter = bbox_3d[bs, idx_filter]
            # print("bbox3d_filter修正前:\n", bbox3d_filter)
            # x,y,w,h --> x1,y1,x2,y2
            bbox2d_filter = bbXYWH2Coords(bbox2d_filter)

            # 筛选后按深度大小排序
            idx_sort = bbox3d_filter.argsort(axis=0)[:, 2]
            print("id_sort:\n", idx_sort)

            # 计算iou,判定遮挡关系,进行遮挡修正
            for count in range(0, idx_sort.shape[0] - 1):
                # print(count)
                # threshold = 0.2         # 遮挡关系判定的阈值
                # occ_correct = np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.0])   # 这里先假定遮挡修正值
                iouValue = np.zeros(bbox2d_filter.shape[0])  # 初始化全部为0;
                idx_occlusion = np.zeros(bbox2d_filter.shape[0],
                                         dtype=bool)  # 初始化全部为False;

                iouValue[count + 1:] = iou(
                    bbox2d_filter[idx_sort[count:count + 1]],
                    bbox2d_filter[idx_sort[count + 1:]])
                # print("iouValue:", iouValue)
                for occid in range(count + 1, idx_occlusion.shape[0]):
                    # print(occid)
                    idx_occlusion[occid] |= (iouValue[occid] > threshold)
                # print("遮挡关系:", idx_occlusion)
                # print("当前值为:", bbox3d_filter[idx_sort[count]])
                bbox3d_filter[
                    idx_sort[idx_occlusion]] += bbox3d_filter[idx_sort[
                        count]] * occ_correct  # 用'='表示返回的是修正值,用'+='返回的是修改后的值;
                # print("bbox3d_filter修正后:\n", bbox3d_filter)

            bbox2d_filter = bbCoords2XYWH(
                bbox2d_filter)  # 变回x,y,w,h模式,其实这一步不太需要,因为从始至终都没改变bbox_2d的值;

            # 将修改完的值返回给原数据
            bbox_3d[bs, idx_filter] = bbox3d_filter

        # print("修改后的bbox_3d:\n", bbox_3d)

        bbox_3d = torch.from_numpy(bbox_3d).cuda()
        return bbox_3d