예제 #1
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)))
예제 #2
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
예제 #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
예제 #4
0
def evaluate_one_epoch(val_loader, model, epoch, configs, logger):
    batch_time = AverageMeter('Time', ':6.3f')
    data_time = AverageMeter('Data', ':6.3f')

    conf_thresh = 0.5
    nms_thresh = 0.5
    iou_threshold = 0.5

    progress = ProgressMeter(len(val_loader), [batch_time, data_time],
                             prefix="Evaluate - Epoch: [{}/{}]".format(
                                 epoch, configs.num_epochs))
    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)):
            data_time.update(time.time() - start_time)
            _, imgs, targets = batch_data
            # Extract labels
            labels += targets[:, 1].tolist()
            # Rescale target
            targets[:, 2:] *= configs.img_size
            imgs = imgs.to(configs.device, non_blocking=True)

            outputs = model(imgs)
            outputs = post_processing(outputs,
                                      conf_thresh=conf_thresh,
                                      nms_thresh=nms_thresh)

            sample_metrics += get_batch_statistics_rotated_bbox(
                outputs, targets, iou_threshold=iou_threshold)

            # 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
예제 #5
0
        'cpu' if configs.no_cuda else 'cuda:{}'.format(configs.gpu_idx))
    model = model.to(device=configs.device)

    out_cap = None

    model.eval()

    test_dataloader = create_test_dataloader(configs)
    with torch.no_grad():
        for batch_idx, (img_paths, imgs_bev) in enumerate(test_dataloader):
            input_imgs = imgs_bev.to(device=configs.device).float()
            t1 = time_synchronized()
            outputs = model(input_imgs)
            t2 = time_synchronized()
            detections = post_processing(outputs,
                                         conf_thresh=configs.conf_thresh,
                                         nms_thresh=configs.nms_thresh)

            img_detections = []  # Stores detections for each image index
            img_detections.extend(detections)

            img_bev = imgs_bev.squeeze() * 255
            img_bev = img_bev.permute(1, 2, 0).numpy().astype(np.uint8)
            img_bev = cv2.resize(img_bev, (configs.img_size, configs.img_size))
            for detections in img_detections:
                if detections is None:
                    continue
                # Rescale boxes to original image
                detections = rescale_boxes(detections, configs.img_size,
                                           img_bev.shape[:2])
                for x, y, w, l, im, re, cls_conf, cls_pred in detections:
예제 #6
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)
예제 #7
0
    with torch.no_grad():
        for batch_idx, batch_data in enumerate(test_dataloader):
            sample_ids, batch_size, lidarData, voxels_features, voxels_coors = batch_data
            voxels_features = voxels_features.to(configs.device, non_blocking=True)
            t1 = time_synchronized()
            outputs = model(voxels_features, voxels_coors, batch_size)
            t2 = time_synchronized()
            outputs['hm_cen'] = _sigmoid(outputs['hm_cen'])
            outputs['hm_conners'] = _sigmoid(outputs['hm_conners'])
            outputs['cen_offset'] = _sigmoid(outputs['cen_offset'])
            outputs['direction'] = _sigmoid(outputs['direction'])
            # detections size (batch_size, K, 10)
            detections = centernet3d_decode(outputs['hm_cen'], outputs['hm_conners'], outputs['cen_offset'],
                                            outputs['direction'], outputs['z_coor'], outputs['dim'], K=configs.K)
            detections = detections.cpu().numpy()
            detections = post_processing(detections, configs.num_classes, configs.down_ratio)
            detections = get_final_pred(detections[0], configs.num_classes, configs.peak_thresh)
            car_detections = detections[0]
            # calib = kitti_data_utils.Calibration(img_paths[0].replace(".png", ".txt").replace("image_2", "calib"))
            if len(car_detections) > 0:
                # Draw prediction in the image
                pred_boxes3d = []
                for car_det in car_detections[:, 1:]:
                    pred_boxes3d.append(box3d_center_to_conners(car_det))

                fig = draw_lidar(lidarData[0], is_grid=False, is_top_region=True)
                cls_ids = np.zeros((len(car_detections)), dtype=np.int)
                draw_gt_boxes3d(gt_boxes3d=pred_boxes3d, fig=fig, cls_ids=cls_ids)
                # mlab.savefig(filename='test.png')
                mlab.show()
            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)

            # Rotate the bev_map
            bev_map = cv2.rotate(bev_map, cv2.ROTATE_180)

            img_path = metadatas['img_path'][0]
예제 #9
0
    model = create_model(configs)
    model.load_state_dict(torch.load(configs.pretrained_path))

    configs.device = torch.device('cpu' if configs.no_cuda else 'cuda:{}'.format(configs.gpu_idx))
    model.to(device=configs.device)

    model.eval()

    test_dataloader = create_test_dataloader(configs)
    with torch.no_grad():
        for batch_idx, (img_paths, bev_maps) in enumerate(test_dataloader):
            input_imgs = bev_maps.to(device=configs.device).float()
            t1 = time_synchronized()
            detections = model(input_imgs)
            detections = post_processing(detections, conf_thresh=0.95, nms_thresh=0.4)
            t2 = time_synchronized()

            img_detections = []  # Stores detections for each image index
            img_detections.extend(detections)
            bev_maps = torch.squeeze(bev_maps).numpy()
            RGB_Map = np.zeros((cnf.BEV_WIDTH, cnf.BEV_WIDTH, 3))
            RGB_Map[:, :, 2] = bev_maps[0, :, :]  # r_map
            RGB_Map[:, :, 1] = bev_maps[1, :, :]  # g_map
            RGB_Map[:, :, 0] = bev_maps[2, :, :]  # b_map

            RGB_Map = (255 * RGB_Map).astype(np.uint8)
            for detections in img_detections:
                if detections is None:
                    continue
                # Rescale boxes to original image
예제 #10
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)):