Exemplo n.º 1
0
def do_detect(configs, model, bevmap, is_front):
    if not is_front:
        bevmap = torch.flip(bevmap, [1, 2])

    input_bev_maps = bevmap.unsqueeze(0).to(configs.device,
                                            non_blocking=True).float()
    t1 = time_synchronized()
    outputs = model(input_bev_maps)
    outputs['hm_cen'] = _sigmoid(outputs['hm_cen'])
    outputs['cen_offset'] = _sigmoid(outputs['cen_offset'])
    # detections size (batch_size, K, 10)
    detections = decode(outputs['hm_cen'],
                        outputs['cen_offset'],
                        outputs['direction'],
                        outputs['z_coor'],
                        outputs['dim'],
                        K=configs.K)
    detections = detections.cpu().numpy().astype(np.float32)
    detections = post_processing(detections, configs.num_classes,
                                 configs.down_ratio, configs.peak_thresh)
    t2 = time_synchronized()
    # Inference speed
    fps = 1 / (t2 - t1)

    return detections[0], bevmap, fps
Exemplo n.º 2
0
def complex_yolo(pointcloud):
    pointcloud = get_filtered_lidar(pointcloud, cnf.boundary)
    bev_maps = makeBEVMap(pointcloud, cnf.boundary)
    bev_maps = torch.from_numpy(bev_maps)
    bev_maps = torch.unsqueeze(bev_maps, 0)

    input_bev_maps = bev_maps.to(configs.device, non_blocking=True).float()
    t1 = time_synchronized()
    outputs = model(input_bev_maps)
    outputs['hm_cen'] = _sigmoid(outputs['hm_cen'])
    outputs['cen_offset'] = _sigmoid(outputs['cen_offset'])
    # detections size (batch_size, K, 10)
    detections = decode(outputs['hm_cen'], outputs['cen_offset'], outputs['direction'], outputs['z_coor'],outputs['dim'], K=configs.K)
    detections = detections.cpu().detach().numpy().astype(np.float32)
    detections = post_processing(detections, configs.num_classes, configs.down_ratio, configs.peak_thresh)
    t2 = time_synchronized()
    detections = detections[0]  # only first batch
    # Draw prediction in the image
    bev_map = (bev_maps.squeeze().permute(1, 2, 0).numpy() * 255).astype(np.uint8)    
    bev_map = cv2.resize(bev_map, (cnf.BEV_WIDTH, cnf.BEV_HEIGHT))
    bev_map = draw_predictions(bev_map, detections.copy(), configs.num_classes)
    bev_map = cv2.rotate(bev_map, cv2.ROTATE_180)

    cv2.imshow("BEV", bev_map)
    print('\tDone testing in time: {:.1f}ms, speed {:.2f}FPS'.format((t2 - t1) * 1000,1 / (t2 - t1)))
Exemplo n.º 3
0
 def post_procesiing(self, outputs):
     outputs['hm_cen'] = _sigmoid(outputs['hm_cen'])
     outputs['cen_offset'] = _sigmoid(outputs['cen_offset'])
     # detections size (batch_size, K, 10)
     detections = decode(outputs['hm_cen'], outputs['cen_offset'], outputs['direction'], outputs['z_coor'],outputs['dim'], K=self.configs.K)
     detections = detections.cpu().detach().numpy().astype(np.float32)
     detections = post_processing(detections, self.configs.num_classes, self.configs.down_ratio, self.configs.peak_thresh)
     detections = detections[0]
     detections = convert_det_to_real_values(detections)
     return detections
Exemplo n.º 4
0
    def callback(self, data):
        rospy.loginfo("detection")
        with torch.no_grad():
            gen = point_cloud2.read_points(data)
            #print(type(gen))
            cloudata = []
            for idx, p in enumerate(gen):
                data = np.array([p[0], p[1], p[2], p[3]])
                data = data.reshape((1, 4))
                cloudata.append(data)

            lidarData = np.concatenate([x for x in cloudata], axis=0)

            lidarData = get_filtered_lidar(lidarData, cnf.boundary)
            res = self.voxel_generator.generate(lidarData, 20000)

            coorinput = np.pad(res["coordinates"], ((0, 0), (1, 0)),
                               mode='constant',
                               constant_values=0)
            voxelinput = res["voxels"]
            numinput = res["num_points_per_voxel"]

            dtype = torch.float32
            voxelinputr = torch.tensor(voxelinput,
                                       dtype=torch.float32,
                                       device=configs.device).to(dtype)

            coorinputr = torch.tensor(coorinput,
                                      dtype=torch.int32,
                                      device=configs.device)

            numinputr = torch.tensor(numinput,
                                     dtype=torch.int32,
                                     device=configs.device)

            outputs = self.model(voxelinputr, coorinputr, numinputr)
            outputs = outputs._asdict()
            outputs['hm_cen'] = _sigmoid(outputs['hm_cen'])
            outputs['cen_offset'] = _sigmoid(outputs['cen_offset'])
            # detections size (batch_size, K, 10)
            detections = decode(outputs['hm_cen'],
                                outputs['cen_offset'],
                                outputs['direction'],
                                outputs['z_coor'],
                                outputs['dim'],
                                K=configs.K)
            detections = detections.cpu().numpy().astype(np.float32)
            detections = post_processing(detections, configs.num_classes,
                                         configs.down_ratio,
                                         configs.peak_thresh)

            detections = detections[0]

            bev_map = np.ones((432, 432, 3), dtype=np.uint8)
            bev_map = bev_map * 0.5

            bev_map = makeBEVMap(lidarData, cnf.boundary)
            bev_map = bev_map.transpose((1, 2, 0)) * 255

            #bev_map = (bev_maps.squeeze().permute(1, 2, 0).numpy() * 255).astype(np.uint8)
            bev_map = cv2.resize(bev_map, (cnf.BEV_WIDTH, cnf.BEV_HEIGHT))
            bev_map = draw_predictions(bev_map, detections.copy(),
                                       configs.num_classes)

            # Rotate the bev_map
            bev_map = cv2.rotate(bev_map, cv2.ROTATE_180)
            out_img = bev_map
            cv2.imshow('test-img', out_img)
            cv2.waitKey(1)
    model.eval()

    test_dataloader = create_test_dataloader(configs)
    with torch.no_grad():
        for batch_idx, batch_data in enumerate(test_dataloader):
            metadatas, bev_maps, img_rgbs = batch_data
            input_bev_maps = bev_maps.to(configs.device,
                                         non_blocking=True).float()
            t1 = time_synchronized()
            outputs = model(input_bev_maps)
            outputs['hm_cen'] = _sigmoid(outputs['hm_cen'])
            outputs['cen_offset'] = _sigmoid(outputs['cen_offset'])
            # detections size (batch_size, K, 10)
            detections = decode(outputs['hm_cen'],
                                outputs['cen_offset'],
                                outputs['direction'],
                                outputs['z_coor'],
                                outputs['dim'],
                                K=configs.K)
            detections = detections.cpu().numpy().astype(np.float32)
            detections = post_processing(detections, configs.num_classes,
                                         configs.down_ratio,
                                         configs.peak_thresh)
            t2 = time_synchronized()

            detections = detections[0]  # only first batch
            # Draw prediction in the image
            bev_map = (bev_maps.squeeze().permute(1, 2, 0).numpy() *
                       255).astype(np.uint8)
            bev_map = cv2.resize(bev_map, (cnf.BEV_WIDTH, cnf.BEV_HEIGHT))
            bev_map = draw_predictions(bev_map, detections.copy(),
                                       configs.num_classes)
Exemplo n.º 6
0
def evaluate_mAP(val_loader, model, configs, logger):
    batch_time = AverageMeter('Time', ':6.3f')
    data_time = AverageMeter('Data', ':6.3f')

    progress = ProgressMeter(len(val_loader), [batch_time, data_time],
                             prefix="Evaluation phase...")
    labels = []
    sample_metrics = []  # List of tuples (TP, confs, pred)
    # switch to evaluate mode
    model.eval()
    with torch.no_grad():
        start_time = time.time()
        for batch_idx, batch_data in enumerate(tqdm(val_loader)):
            metadatas, targets = batch_data

            batch_size = len(metadatas['img_path'])

            voxelinput = metadatas['voxels']
            coorinput = metadatas['coors']
            numinput = metadatas['num_points']

            dtype = torch.float32
            voxelinputr = torch.tensor(voxelinput,
                                       dtype=torch.float32,
                                       device=configs.device).to(dtype)

            coorinputr = torch.tensor(coorinput,
                                      dtype=torch.int32,
                                      device=configs.device)

            numinputr = torch.tensor(numinput,
                                     dtype=torch.int32,
                                     device=configs.device)
            t1 = time_synchronized()
            outputs = model(voxelinputr, coorinputr, numinputr)
            outputs = outputs._asdict()

            outputs['hm_cen'] = _sigmoid(outputs['hm_cen'])
            outputs['cen_offset'] = _sigmoid(outputs['cen_offset'])
            # detections size (batch_size, K, 10)

            detections = decode(outputs['hm_cen'],
                                outputs['cen_offset'],
                                outputs['direction'],
                                outputs['z_coor'],
                                outputs['dim'],
                                K=configs.K)
            detections = detections.cpu().numpy().astype(np.float32)
            detections = post_processingv2(detections, configs.num_classes,
                                           configs.down_ratio,
                                           configs.peak_thresh)

            for sample_i in range(len(detections)):
                # print(output.shape)
                num = targets['count'][sample_i]
                # print(targets['batch'][sample_i][:num].shape)
                target = targets['batch'][sample_i][:num]
                #print(target[:, 8].tolist())
                labels += target[:, 8].tolist()

            sample_metrics += get_batch_statistics_rotated_bbox(
                detections, targets, iou_threshold=configs.iou_thresh)

            t2 = time_synchronized()

            # measure elapsed time
            # torch.cuda.synchronize()
            batch_time.update(time.time() - start_time)

            # Log message
            if logger is not None:
                if ((batch_idx + 1) % configs.print_freq) == 0:
                    logger.info(progress.get_message(batch_idx))

            start_time = time.time()

        # Concatenate sample statistics
        true_positives, pred_scores, pred_labels = [
            np.concatenate(x, 0) for x in list(zip(*sample_metrics))
        ]
        precision, recall, AP, f1, ap_class = ap_per_class(
            true_positives, pred_scores, pred_labels, labels)

    return precision, recall, AP, f1, ap_class
Exemplo n.º 7
0
def evaluate_mAP(val_loader, model, configs, logger):
    batch_time = AverageMeter('Time', ':6.3f')
    data_time = AverageMeter('Data', ':6.3f')

    progress = ProgressMeter(len(val_loader), [batch_time, data_time],
                             prefix="Evaluation phase...")
    labels = []
    sample_metrics = []  # List of tuples (TP, confs, pred)
    # switch to evaluate mode
    model.eval()

    class_id = {0:'Car', 1:'Pedestrian', 2:'Cyclist'}

    with torch.no_grad():
        start_time = time.time()
        for batch_idx, batch_data in enumerate(tqdm(val_loader)):
            metadatas, targets= batch_data

            batch_size = len(metadatas['img_path'])

            voxelinput = metadatas['voxels']
            coorinput = metadatas['coors']
            numinput = metadatas['num_points']

            dtype = torch.float32
            voxelinputr = torch.tensor(
                voxelinput, dtype=torch.float32, device=configs.device).to(dtype)

            coorinputr = torch.tensor(
                coorinput, dtype=torch.int32, device=configs.device)

            numinputr = torch.tensor(
                numinput, dtype=torch.int32, device=configs.device)
            t1 = time_synchronized()
            outputs =  model(voxelinputr, coorinputr, numinputr)
            outputs = outputs._asdict()

            outputs['hm_cen'] = _sigmoid(outputs['hm_cen'])
            outputs['cen_offset'] = _sigmoid(outputs['cen_offset'])
            # detections size (batch_size, K, 10)
            img_path = metadatas['img_path'][0]
            #print(img_path)
            calib = Calibration(img_path.replace(".png", ".txt").replace("image_2", "calib"))

            detections = decode(outputs['hm_cen'], outputs['cen_offset'], outputs['direction'], outputs['z_coor'],
                                outputs['dim'], K=configs.K)
            detections = detections.cpu().numpy().astype(np.float32)
            detections = post_processing(detections, configs.num_classes, configs.down_ratio, configs.peak_thresh)

            for i in range(configs.batch_size):
                detections[i] = convert_det_to_real_values(detections[i])
                img_path = metadatas['img_path'][i]
                #rint(img_path)
                datap = str.split(img_path,'/')
                filename = str.split(datap[7],'.')
                file_write_obj = open('../result/' + filename[0] + '.txt', 'w')
                lidar_path = '/' + datap[1] + '/' + datap[2] + '/' + datap[3] + '/' + \
                             datap[4] + '/' + datap[5] + '/' + 'velodyne' + '/' + filename[0] + '.bin'
                #print(lidar_path)
                #show3dlidar(lidar_path, detections[i], calib.V2C, calib.R0, calib.P2)
                dets = detections[i]
                if len(dets) >0 :
                    dets[:, 1:] = lidar_to_camera_box(dets[:, 1:], calib.V2C, calib.R0, calib.P2)
                    for box_idx, label in enumerate(dets):
                        location, dim, ry = label[1:4], label[4:7], label[7]
                        if ry < -np.pi:
                            ry = 2*np.pi + ry
                        if ry > np.pi:
                            ry = -2*np.pi + ry
                        corners_3d = compute_box_3d(dim, location, ry)
                        corners_2d = project_to_image(corners_3d, calib.P2)
                        minxy = np.min(corners_2d, axis=0)
                        maxxy = np.max(corners_2d, axis=0)
                        bbox = np.concatenate([minxy, maxxy], axis=0)
                        if bbox[0] < 0 or bbox[2]<0:
                            continue
                        if bbox[1] > 1272 or bbox[3] > 375:
                            continue
                        oblist = ['Car',' ','0.0', ' ', '0', ' ', '-10', ' ','%.2f'%bbox[0], ' ', \
                              '%.2f' %bbox[1], ' ','%.2f'%bbox[2], ' ','%.2f'%bbox[3], ' ','%.2f'%dim[0], ' ','%.2f'%dim[1], ' ','%.2f'%dim[2], ' ', \
                              '%.2f' %location[0], ' ','%.2f'%location[1], ' ','%.2f'%location[2], ' ', '%.2f'%ry, '\n']
                        file_write_obj.writelines(oblist)
                file_write_obj.close()

            '''for sample_i in range(len(detections)):