Пример #1
0
def bbox_loss(all_trackers, all_observed_boxes):
    '''
    Compute the bbox loss using IOU between predicted boxes and observed boxes
    '''
    try:
        all_observed_track_id = all_observed_boxes[:, 0]
    except:
        # if no boxes observed, bbox anomaly score is 0
        return 0
    L_bbox = 0
    # for each observed car, find the previous prediction of the car's box at the current time
    # e.g., X_{t}{t-1}, X_{t}{t-2}, X_{t}{t-3},...
    for track_id in all_observed_track_id:
        if track_id not in all_trackers.trackers.keys():
            # skip if the track id has already been forgotten
            continue
        # # use average prediction as the compare metrics
        pred_boxes_t = all_trackers.trackers[track_id].pred_boxes_t
        if len(pred_boxes_t) == 0:
            continue
        pred_box_t = torch.mean(pred_boxes_t, dim=0)  #.view(1, 4)
        observed_box_t = all_trackers.trackers[track_id].bbox.squeeze()
        iou = compute_IOU(pred_box_t, observed_box_t)
        # if iou == 0:
        #     print("Warning: observed object location is very anomalous!")
        L_bbox += iou
    L_bbox /= len(all_observed_track_id)
    return 1 - float(L_bbox)
Пример #2
0
def iou_metrics(all_pred_boxes_t,
                all_observed_boxes,
                multi_box='average',
                iou_type='average'):
    '''
    given the boxes of observed objects, 
    compute the average iou between each box and its several previous predictions
    Params:
        all_pred_boxes_t: a dictionary saves all pred_box_t
        all_observed_boxes: a dictionary saves all bbox
        multi_box: The method to combine the five predictions: 'average', 'union' or 'intersection'
        iou_type: The method to compute across all objects: 'average', 'min'
    '''
    try:
        all_observed_track_id = all_observed_boxes.keys()  #[:,0]
    except:
        # if no boxes observed, bbox anomaly score is 0
        return 0
    L_bbox = []
    # for each observed car, find the previous prediction of the car's box at the current time
    # e.g., X_{t}{t-1}, X_{t}{t-2}, X_{t}{t-3},...
    for track_id in all_observed_track_id:
        if track_id not in all_pred_boxes_t.keys():
            # skip if the track id has already been forgotten
            continue
        # # use average prediction as the compare metrics
        pred_boxes_t = all_pred_boxes_t[track_id]
        if len(pred_boxes_t) == 0:
            continue

        if multi_box == 'average':
            pred_box_t = np.mean(pred_boxes_t, axis=0)  #.view(1, 4)
        elif multi_box == 'union':
            _, pred_box_t = boxes_union(pred_boxes_t)
        elif multi_box == 'intersection':
            _, pred_box_t = boxes_intersection(pred_boxes_t)
        else:
            raise NameError("Multi_box type: " + multi_box + " is unknown!")
        observed_box_t = all_observed_boxes[track_id].squeeze()
        iou = compute_IOU(pred_box_t, observed_box_t)
        L_bbox.append(iou)
    if len(L_bbox) <= 0:
        return 0
    if iou_type == 'average':
        L_bbox = np.mean(L_bbox)

    elif iou_type == 'min':
        L_bbox = np.min(L_bbox)
    else:
        raise NameError("IOU measure type: " + iou_type + " is unknown!")

    return 1 - float(L_bbox)
def test_fol(fol_model, test_gen):
    '''
    Validate future vehicle localization module 
    Params:
        fol_model: The fol model as nn.Module
        test_gen: test data generator
    Returns:
        
    '''
    fol_model.eval() # Sets the module in training mode.


    fol_loss = 0

    loader = tqdm(test_gen, total=len(test_gen))

    FDE = 0
    ADE = 0
    FIOU = 0
    with torch.set_grad_enabled(False):
        for batch_idx, data in enumerate(loader):
            input_bbox, input_flow, input_ego_motion, target_bbox, target_ego_motion = data

            # run forward
            fol_predictions,_ = fol_model(input_bbox, input_flow)
            
            # convert to numpy array, use [0] since batchsize if 1 for test
            fol_predictions = fol_predictions.to('cpu').numpy()[0] 
            input_bbox = input_bbox.to('cpu').numpy()[0]
            target_bbox = target_bbox.to('cpu').numpy()[0]

            # compute FDE, ADE and FIOU metrics used in FVL2019ICRA paper
            input_bbox = np.expand_dims(input_bbox, axis=1)

            input_bbox = bbox_denormalize(input_bbox, W=1280, H=640)
            fol_predictions = bbox_denormalize(fol_predictions, W=1280, H=640)
            target_bbox = bbox_denormalize(target_bbox, W=1280, H=640)
            
            fol_predictions_xyxy = cxcywh_to_x1y1x2y2(fol_predictions)
            target_bbox_xyxy = cxcywh_to_x1y1x2y2(target_bbox)

            ADE += np.mean(np.sqrt(np.sum((target_bbox_xyxy[:,:,:2] - fol_predictions_xyxy[:,:,:2]) ** 2, axis=-1)))
            FDE += np.mean(np.sqrt(np.sum((target_bbox_xyxy[:,-1,:2] - fol_predictions_xyxy[:,-1,:2]) ** 2, axis=-1)))
            tmp_FIOU = []
            for i in range(target_bbox_xyxy.shape[0]):
                tmp_FIOU.append(compute_IOU(target_bbox_xyxy[i,-1,:], fol_predictions_xyxy[i,-1,:], format='x1y1x2y2'))
            FIOU += np.mean(tmp_FIOU)
    print("FDE: %4f;    ADE: %4f;   FIOU: %4f" % (FDE, ADE, FIOU))
    ADE /= len(test_gen.dataset)
    FDE /= len(test_gen.dataset)
    FIOU /= len(test_gen.dataset)
    print("FDE: %4f;    ADE: %4f;   FIOU: %4f" % (FDE, ADE, FIOU))