Ejemplo n.º 1
0
    def __init__(self):
        with open('configs/ntusl_20cm.json', 'r') as f:
            config = json.load(f)
        device = torch.device("cuda:0")
        config['device'] = device
        voxel_generator = VoxelGenerator(config)
        anchor_assigner = AnchorAssigner(config)
        self.inference = Inference(config, anchor_assigner)
        self.infer_data = InferData(config, voxel_generator, anchor_assigner,
                                    torch.float32)
        self.net = PointPillars(config)
        self.net.cuda()
        model_path = Path(
            config['data_root']) / config['model_path'] / config['experiment']
        latest_model_path = model_path / 'latest.pth'
        checkpoint = torch.load(latest_model_path)
        self.net.load_state_dict(checkpoint['model_state_dict'])
        print('model loaded')
        self.net.eval()

        self.data_root = Path(config['data_root'])
        info_paths = config['eval_info']
        self.infos = []
        for info_path in info_paths:
            info_path = self.data_root / info_path
            with open(info_path, 'rb') as f:
                self.infos += pickle.load(f)
Ejemplo n.º 2
0
class PointPillarsNode(object):
    def __init__(self):
        print('initializing model...')
        # build model and preprocessor #
        with open('configs/ntusl_20cm.json', 'r') as f:
            config = json.load(f)

        device = torch.device("cuda:0")
        config['device'] = device
        self.voxel_generator = VoxelGenerator(config)
        self.anchor_assigner = AnchorAssigner(config)
        self.inference = Inference(config, self.anchor_assigner)
        self.infer_data = InferData(config, self.voxel_generator,
                                    self.anchor_assigner, torch.float32)
        self.net = PointPillars(config)
        self.net.cuda()
        model_path = Path(
            config['data_root']) / config['model_path'] / config['experiment']
        latest_model_path = model_path / 'latest.pth'

        checkpoint = torch.load(latest_model_path)
        self.net.load_state_dict(checkpoint['model_state_dict'])
        print('model loaded')
        self.net.eval()
        self.q_msg = queue.Queue(maxsize=2)

    def lidar_callback(self, msg):

        points = np.asarray(list(pc2.read_points(msg)))[:, :4].astype(
            np.float32)
        stamp = msg.header.stamp
        self.q_msg.put((points, stamp))

    def spin(self):
        time_elapse = 0.0
        len_infos = 0
        rospy.init_node("PointPillars", anonymous=False)
        rospy.Subscriber('/combined_lidar',
                         PointCloud2,
                         callback=self.lidar_callback,
                         queue_size=1)

        print('spinning.')

        with torch.no_grad():
            while not rospy.is_shutdown():
                points, stamp = self.q_msg.get()
                start_time = time.time()
                example = self.infer_data.get(points)
                preds_dict = self.net(example)
                annos = self.inference.infer(example, preds_dict)
                dur = time.time() - start_time
                time_elapse += dur
                len_infos += 1
                if len_infos >= 713:
                    break

            print("infor len", len_infos)
            print("average time : %.5f" % (time_elapse / len_infos))
Ejemplo n.º 3
0
    def __init__(self):
        print('initializing model...')
        # build model and preprocessor #
        with open('configs/ntusl_20cm.json', 'r') as f:
            config = json.load(f)

        device = torch.device("cuda:0")
        config['device'] = device
        self.voxel_generator = VoxelGenerator(config)
        self.anchor_assigner = AnchorAssigner(config)
        self.inference = Inference(config, self.anchor_assigner)
        self.infer_data = InferData(config, self.voxel_generator,
                                    self.anchor_assigner, torch.float32)
        self.net = PointPillars(config)
        self.net.cuda()
        model_path = Path(
            config['data_root']) / config['model_path'] / config['experiment']
        latest_model_path = model_path / 'latest.pth'

        checkpoint = torch.load(latest_model_path)
        self.net.load_state_dict(checkpoint['model_state_dict'])
        print('model loaded')
        self.net.eval()
        self.q_msg = queue.Queue(maxsize=2)
Ejemplo n.º 4
0
class PointPillarsNode:
    def __init__(self):
        with open('configs/ntusl_20cm.json', 'r') as f:
            config = json.load(f)
        device = torch.device("cuda:0")
        config['device'] = device
        voxel_generator = VoxelGenerator(config)
        anchor_assigner = AnchorAssigner(config)
        self.inference = Inference(config, anchor_assigner)
        self.infer_data = InferData(config, voxel_generator, anchor_assigner,
                                    torch.float32)
        self.net = PointPillars(config)
        self.net.cuda()
        model_path = Path(
            config['data_root']) / config['model_path'] / config['experiment']
        latest_model_path = model_path / 'latest.pth'
        checkpoint = torch.load(latest_model_path)
        self.net.load_state_dict(checkpoint['model_state_dict'])
        print('model loaded')
        self.net.eval()

        self.data_root = Path(config['data_root'])
        info_paths = config['eval_info']
        self.infos = []
        for info_path in info_paths:
            info_path = self.data_root / info_path
            with open(info_path, 'rb') as f:
                self.infos += pickle.load(f)

    def lidar_callback(self, msg):
        points = np.asarray(list(pc2.read_points(msg)))[:, :4]
        stamp = msg.header.stamp
        self.q_msg.put((points, stamp))
        print("puting...", stamp)

    def spin(self):
        time_elapse = 0.0
        rospy.init_node("PointPillars", anonymous=False)
        rospy.Subscriber('/combined_lidar',
                         PointCloud2,
                         callback=self.lidar_callback,
                         queue_size=1)
        print('spinning.')
        len_infos = len(self.infos)
        dt_annos = []
        for idx, info in enumerate(self.infos):
            print('\ridx %d' % idx, end='')
            v_path = self.data_root / info['velodyne_path']
            points = np.fromfile(v_path, dtype=np.float32,
                                 count=-1).reshape([-1, 4])
            start_time = time.time()
            example = self.infer_data.get(points)
            pre_time = time.time()
            with torch.no_grad():
                preds_dict = self.net(example)
            net_time = time.time()

            anno = self.inference.infer(example, preds_dict)

            post_time = time.time()

            pre_time_avg += pre_time - start_time
            net_time_avg += net_time - pre_time
            post_time_avg += post_time - net_time

            time_elapse += post_time - start_time

        print("average time : %.5f" % (time_elapse / len_infos))
        print("pre-processing time : %.5f" % (pre_time_avg / len_infos))
        print("network time : %.5f" % (net_time_avg / len_infos))
        print("post-processing time : %.5f" % (post_time_avg / len_infos))
Ejemplo n.º 5
0
def infer_trt():
    with open('configs/ntusl_20cm.json', 'r') as f:
        config = json.load(f)
    device = torch.device("cuda:0")
    config['device'] = device
    voxel_generator = VoxelGenerator(config)
    anchor_assigner = AnchorAssigner(config)
    inference = Inference(config, anchor_assigner)
    infer_data = InferData(config, voxel_generator, anchor_assigner,
                           torch.float32)
    net = PointPillars(config)

    # model_path = Path(config['model_path']) / config['experiment']
    # latest_model_path = model_path / '265000.pth'
    #
    # checkpoint = torch.load(latest_model_path, map_location=lambda storage, loc: storage)
    # net.load_state_dict(checkpoint['model_state_dict'])
    print('model loaded')
    net.to(device)
    # net.half()
    net.eval()

    data_root = Path(config['data_root'])
    info_paths = config['eval_info']
    infos = []
    for info_path in info_paths:
        info_path = data_root / info_path
        with open(info_path, 'rb') as f:
            infos += pickle.load(f)
    changeInfo(infos)
    dt_annos = []
    time_elapse, pre_time_avg, net_time_avg, post_time_avg = 0.0, 0.0, 0.0, 0.0
    len_infos = len(infos)
    for idx, info in enumerate(infos):
        print('\ridx %d' % idx, end='')
        v_path = data_root / info['velodyne_path']
        points = np.fromfile(v_path, dtype=np.float32,
                             count=-1).reshape([-1, 4])
        start_time = time.time()
        example = infer_data.get(points, toTorch=True)
        pre_time = time.time()
        with torch.no_grad():
            # inputs = (example["voxels"], example["num_points_per_voxel"], example["coordinates"], example["voxel_num"])
            # input_names = ['voxels', 'num_points_per_voxel', 'coordinates', 'voxel_num']
            # torch.onnx.export(net, inputs, "pp.onnx", verbose=True, opset_version=11, input_names=input_names)
            # return 0
            preds_dict = net(example)
            torch.cuda.synchronize()
        net_time = time.time()
        dt_annos += inference.infer_gpu(example, preds_dict)

        post_time = time.time()

        pre_time_avg += pre_time - start_time
        net_time_avg += net_time - pre_time
        post_time_avg += post_time - net_time
        time_elapse += post_time - start_time

    print("\naverage time : \t\t\t%.5f" % (time_elapse / len_infos))
    print("pre-processing time : \t%.5f" % (pre_time_avg / len_infos))
    print("network time : \t\t\t%.5f" % (net_time_avg / len_infos))

    # print("pfn_time time : \t\t\t%.5f" % (net.pfn_time / len_infos))
    # print("scatter time : \t\t\t\t%.5f" % (net.scatter_time / len_infos))
    # print("rpn time : \t\t\t\t\t%.5f" % (net.rpn_time / len_infos))
    # print("heads time : \t\t\t\t%.5f" % (net.heads_time / len_infos))

    print("post-processing time : \t%.5f" % (post_time_avg / len_infos))

    print("p1 time : \t\t\t\t\t%.5f" % (inference.p1 / len_infos))
    print("p2 time : \t\t\t\t\t%.5f" % (inference.p2 / len_infos))
    print("p3 time : \t\t\t\t\t%.5f" % (inference.p3 / len_infos))
    print("p4 time : \t\t\t\t\t%.5f" % (inference.p4 / len_infos))

    dt_path = Path(
        config['data_root']) / config['result_path'] / config['experiment']
    if not os.path.exists(dt_path):
        os.makedirs(dt_path)

    with open(dt_path / config['dt_info'], 'wb') as f:
        pickle.dump(dt_annos, f)
    gt_annos = [info["annos"] for info in infos]
    eval_classes = ["vehicle", "pedestrian",
                    "cyclist"]  # ["vehicle", "pedestrian", "cyclist"]
    for range_thresh in np.arange(80.0, 90.0, 10.0):
        APs, eval_str = get_official_eval_result(gt_annos, dt_annos,
                                                 eval_classes, range_thresh)
        print(eval_str)
Ejemplo n.º 6
0
def trt_eval():
    with open('configs/ntusl_20cm.json', 'r') as f:
        config = json.load(f)
    device = torch.device("cuda:0")
    config['device'] = device
    voxel_generator = VoxelGenerator(config)
    anchor_assigner = AnchorAssigner(config)
    inference = Inference(config, anchor_assigner)
    infer_data = InferData(config, voxel_generator, anchor_assigner,
                           torch.float32)
    pfn_engine_path = '../deployment/pfn16.engine'
    rpn_engine_path = '../deployment/rpn16.engine'
    head_engine_path = '../deployment/head16.engine'

    net = PointPillars(config, pfn_engine_path, rpn_engine_path,
                       head_engine_path)

    data_root = Path(config['data_root'])
    info_paths = config['eval_info']
    infos = []
    for info_path in info_paths:
        info_path = data_root / info_path
        with open(info_path, 'rb') as f:
            infos += pickle.load(f)
    changeInfo(infos)
    dt_annos = []
    time_elapse, pre_time_avg, net_time_avg, post_time_avg = 0.0, 0.0, 0.0, 0.0
    len_infos = len(infos)
    for idx, info in enumerate(infos):
        print('\ridx %d' % idx, end='')
        v_path = data_root / info['velodyne_path']
        points = np.fromfile(v_path, dtype=np.float32,
                             count=-1).reshape([-1, 4])
        start_time = time.time()
        example = infer_data.get(points, toTorch=True)
        pre_time = time.time()
        with torch.no_grad():
            preds_dict = net(example)
            torch.cuda.synchronize()
        net_time = time.time()
        dt_annos += inference.infer_gpu(example, preds_dict)

        post_time = time.time()

        pre_time_avg += pre_time - start_time
        net_time_avg += net_time - pre_time
        post_time_avg += post_time - net_time
        time_elapse += post_time - start_time

    print("\naverage time : \t\t\t%.5f" % (time_elapse / len_infos))
    print("pre-processing time : \t%.5f" % (pre_time_avg / len_infos))
    print("network time : \t\t\t%.5f" % (net_time_avg / len_infos))

    # print("pfn_time time : \t\t\t%.5f" % (net.pfn_time / len_infos))
    # print("scatter time : \t\t\t\t%.5f" % (net.scatter_time / len_infos))
    # print("rpn time : \t\t\t\t\t%.5f" % (net.rpn_time / len_infos))
    # print("heads time : \t\t\t\t%.5f" % (net.heads_time / len_infos))

    print("post-processing time : \t%.5f" % (post_time_avg / len_infos))

    print("p1 time : \t\t\t\t\t%.5f" % (inference.p1 / len_infos))
    print("p2 time : \t\t\t\t\t%.5f" % (inference.p2 / len_infos))
    print("p3 time : \t\t\t\t\t%.5f" % (inference.p3 / len_infos))
    print("p4 time : \t\t\t\t\t%.5f" % (inference.p4 / len_infos))

    dt_path = Path(
        config['data_root']) / config['result_path'] / config['experiment']
    if not os.path.exists(dt_path):
        os.makedirs(dt_path)

    with open(dt_path / config['dt_info'], 'wb') as f:
        pickle.dump(dt_annos, f)
    gt_annos = [info["annos"] for info in infos]
    eval_classes = ["vehicle", "pedestrian",
                    "cyclist"]  # ["vehicle", "pedestrian", "cyclist"]
    for range_thresh in np.arange(80.0, 90.0, 10.0):
        APs, eval_str = get_official_eval_result(gt_annos, dt_annos,
                                                 eval_classes, range_thresh)
        print(eval_str)