Пример #1
0
def get_experiment_info(checkpoint_name):
    exp_output_base_dir = core.data_dir() + '/outputs/' + checkpoint_name

    # Parse experiment config
    config_file = exp_output_base_dir + '/{}.yaml'.format(checkpoint_name)
    config = config_utils.parse_yaml_config(config_file)

    predictions_base_dir = exp_output_base_dir + '/predictions'

    return config, predictions_base_dir
Пример #2
0
def parse_yaml_config(yaml_path):
    """Parses a yaml config

    Args:
        yaml_path: path to yaml config

    Returns:
        config_obj: config converted to object
    """

    # Add check for duplicate keys in yaml
    yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
                         no_duplicates_constructor)

    with open(yaml_path, 'r') as yaml_file:
        config_dict = yaml.load(yaml_file)

    config_obj = config_dict_to_object(config_dict)
    config_obj.config_name = os.path.splitext(os.path.basename(yaml_path))[0]
    config_obj.exp_output_dir = core.data_dir(
    ) + '/outputs/' + config_obj.config_name

    # Prepend data folder to paths
    paths_config = config_obj.train_config.paths_config
    if paths_config.checkpoint_dir is None:
        checkpoint_dir = config_obj.exp_output_dir + '/checkpoints'

        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)

        paths_config.checkpoint_dir = checkpoint_dir
    else:
        paths_config.checkpoint_dir = os.path.expanduser(
            paths_config.checkpoint_dir)

    paths_config.logdir = config_obj.exp_output_dir + '/logs'
    paths_config.pred_dir = config_obj.exp_output_dir + '/predictions'

    return config_obj
def main():
    #########################################################
    # Specify Source Folders and Parameters For Frame Reader
    #########################################################
    data_split_dir = 'training'

    # Specify whether the validation or inference results need to be
    # visualized.
    #results_dir = 'validation'
    results_dir = 'testing'

    # sample_free, anchor_redundancy, black_box,  bayes_od_none,
    # bayes_od_ci_fast. bayes_od_ci,or bayes_od_ici
    uncertainty_method = 'bayes_od'

    dataset_dir = os.path.expanduser('~/Datasets/Kitti/object/')
    image_dir = os.path.join(dataset_dir, data_split_dir) + '/image_2'
    label_dir = os.path.join(dataset_dir, data_split_dir) + '/label_2'

    checkpoint_name = 'retinanet_bdd'
    checkpoint_number = '101'

    if results_dir == 'testing':
        prediction_dir = os.path.join(core.data_dir(), 'outputs',
                                      checkpoint_name, 'predictions',
                                      results_dir, 'kitti', checkpoint_number,
                                      uncertainty_method, 'data')
    else:
        prediction_dir = os.path.join(core.data_dir(), 'outputs',
                                      checkpoint_name, 'predictions',
                                      results_dir, checkpoint_number, 'data')

    frames_list = os.listdir(prediction_dir)
    index = random.randint(0, len(frames_list) - 1)
    frame_id = int(frames_list[index][0:6])

    # frame_id = 27  # Out of distribution example
    frame_id = 4079
    # frame_id = 169   # Many Cars, Hard
    # frame_id = 2290  # Many Cars, Occlusions
    # frame_id = 1941  # Many Cars, Horizontal Direction
    # frame_id = 4032  # Orientation
    # frame_id = 104   # Weird Orientation
    # frame_id = 7047  # Weird Orientation
    # frame_id = 6632 # Very hard orientations

    # frame_id = 195  # Single Pedestrian
    # frame_id = 1574  # Single Pedestrian
    # frame_id = 332  # Multiple Hard Pedestrians
    # frame_id = 1193 # Multiple Hard Pedestrians

    # frame_id = 1274 # Multiple Cyclists

    print('Showing Frame: %d' % frame_id)

    #############
    # Read Frame
    #############
    im_path = image_dir + '/{:06d}.png'.format(frame_id)
    image = cv2.imread(im_path)

    label_path = label_dir + '/{:06d}.txt'.format(frame_id)
    gt_classes_hard, gt_boxes_hard = read_labels(label_path)

    prediction_path = prediction_dir + '/{:06d}.txt'.format(frame_id)
    prediction_classes, prediction_boxes, prediction_scores = read_predictions(
        prediction_path)

    max_ious = np.zeros(prediction_boxes.shape[0])
    # Compute IOU between each prediction and the ground truth boxes
    if gt_boxes_hard.size > 0 and prediction_boxes.size > 0:
        for obj_idx in range(prediction_boxes.shape[0]):
            obj_iou_fmt = prediction_boxes[obj_idx]

            ious_2d = two_d_iou(obj_iou_fmt, gt_boxes_hard)

            max_iou = np.amax(ious_2d)
            max_ious[obj_idx] = max_iou
    #########################################################
    # Draw GT and Prediction Boxes
    #########################################################
    # Transform Predictions to left and right images
    image_out = draw_box_2d(image,
                            gt_boxes_hard,
                            gt_classes_hard,
                            line_width=2,
                            dataset='kitti',
                            is_gt=True)

    image_out = draw_box_2d(image_out,
                            prediction_boxes,
                            prediction_classes,
                            line_width=2,
                            is_gt=False,
                            dataset='kitti',
                            text_to_plot=max_ious,
                            plot_text=True)
    if results_dir == 'testing':
        cv2.imshow('Detections from ' + uncertainty_method, image_out)
    else:
        cv2.imshow('Validation Set Detections', image_out)

    cv2.waitKey()
Пример #4
0
def main():

    # set root_dir to the correct path to your dataset folder
    root_dir = core.data_dir(
    ) + '/datasets/argoverse/argoverse-tracking/sample/'

    vtk_window_size = (1280, 720)

    argoverse_loader = ArgoverseTrackingLoader(root_dir)

    print('Total number of logs:', len(argoverse_loader))
    argoverse_loader.print_all()

    for i, argoverse_data in enumerate(argoverse_loader):
        if i >= 3:
            break
        print(argoverse_data)

    argoverse_data = argoverse_loader[0]
    print(argoverse_data)

    argoverse_data = argoverse_loader.get(
        'c6911883-1843-3727-8eaa-41dc8cda8993')
    print(argoverse_data)

    log_id = 'c6911883-1843-3727-8eaa-41dc8cda8993'  # argoverse_loader.log_list[55]
    frame_idx = 150
    camera = argoverse_loader.CAMERA_LIST[0]

    argoverse_data = argoverse_loader.get(log_id)

    city_name = argoverse_data.city_name

    print('-------------------------------------------------------')
    print(f'Log: {log_id}, \n\tframe: {frame_idx}, camera: {camera}')
    print('-------------------------------------------------------\n')

    lidar_points = argoverse_data.get_lidar(frame_idx)
    img = argoverse_data.get_image_sync(frame_idx, camera=camera)
    objects = argoverse_data.get_label_object(frame_idx)
    calib = argoverse_data.get_calibration(camera)

    # TODO: Calculate point colours

    vtk_pc = VtkPointCloudGlyph()
    vtk_pc.set_points(lidar_points)
    vtk_pc.set_point_size(2)

    vtk_renderer = demo_utils.setup_vtk_renderer()
    vtk_render_window = demo_utils.setup_vtk_render_window(
        'Argoverse Demo', vtk_window_size, vtk_renderer)

    vtk_axes = vtk.vtkAxesActor()
    vtk_axes.SetTotalLength(2, 2, 2)
    vtk_renderer.AddActor(vtk_axes)
    vtk_renderer.AddActor(vtk_pc.vtk_actor)

    current_cam = vtk_renderer.GetActiveCamera()
    current_cam.SetViewUp(0, 0, 1)
    current_cam.SetPosition(-50, 0, 15)
    current_cam.SetFocalPoint(30, 0, 0)

    vtk_interactor = demo_utils.setup_vtk_interactor(vtk_render_window)
    vtk_interactor.Start()
Пример #5
0
def main():
    """Interpolates the lidar point cloud using IP-Basic
    and saves a dense depth map of the scene.
    https://github.com/kujason/ip_basic
    """

    ##############################
    # Options
    ##############################

    kitti_raw_dir = os.path.expanduser(
        '/local-scratch/hadi/datasets/kitti_raw')
    drive_id = '2011_09_26_drive_0001_sync'
    # drive_id = '2011_09_26_drive_0039_sync'

    raw_data = raw_utils.get_raw_data(drive_id, kitti_raw_dir)

    # Fill algorithm ('ip_basic_{...}')
    fill_type = 'multiscale'

    save_depth_maps = True

    out_depth_map_dir = core.data_dir() + '/results/{}/depth_02_{}'.format(
        drive_id, fill_type)

    ##############################
    # End of Options
    ##############################
    os.makedirs(out_depth_map_dir, exist_ok=True)

    # Rolling average array of times for time estimation
    avg_time_arr_length = 5
    last_fill_times = np.repeat([1.0], avg_time_arr_length)
    last_total_times = np.repeat([1.0], avg_time_arr_length)

    cam_p = raw_data.calib.P_rect_20

    image_shape = raw_data.get_cam2(0).size[::-1]

    frames_to_use = raw_data.velo_files
    num_frames = len(frames_to_use)

    for frame_idx, velo_path in enumerate(frames_to_use):

        # Calculate average time with last n fill times
        avg_fill_time = np.mean(last_fill_times)
        avg_total_time = np.mean(last_total_times)

        # Print progress
        sys.stdout.write('\rProcessing {} / {}, Avg Fill Time: {:.5f}s, '
                         'Avg Time: {:.5f}s, Est Time: {:.3f}s'.format(
                             frame_idx, num_frames - 1, avg_fill_time,
                             avg_total_time,
                             avg_total_time * (num_frames - frame_idx)))
        sys.stdout.flush()

        # Start timing
        start_total_time = time.time()

        # Load point cloud
        velo_points = raw_data.get_velo(frame_idx)[:, 0:3]
        velo_pc_padded = transform_utils.pad_pc(velo_points.T)

        cam0_point_cloud = raw_data.calib.R_rect_00 @ raw_data.calib.T_cam0_velo @ velo_pc_padded

        cam0_point_cloud, _ = obj_utils.filter_pc_to_area(
            cam0_point_cloud[0:3],
            area_extents=np.asarray([[-40, 40], [-3, 5], [0, 80]]))

        # Project point cloud to create depth map
        projected_depths = depth_map_utils.project_depths(
            cam0_point_cloud, cam_p, image_shape)

        # Fill depth map
        if fill_type == 'multiscale':
            start_fill_time = time.time()
            final_depth_map, _ = ip_basic.fill_in_multiscale(projected_depths)
            end_fill_time = time.time()
        else:
            raise ValueError('Invalid fill algorithm')

        # Save depth maps
        if save_depth_maps:
            out_depth_map_path = out_depth_map_dir + '/{:010d}.png'.format(
                frame_idx)
            depth_map_utils.save_depth_map(out_depth_map_path, final_depth_map)

        # Stop timing
        end_total_time = time.time()

        # Update fill times
        last_fill_times = np.roll(last_fill_times, -1)
        last_fill_times[-1] = end_fill_time - start_fill_time

        # Update total times
        last_total_times = np.roll(last_total_times, -1)
        last_total_times[-1] = end_total_time - start_total_time
Пример #6
0
def main():
    # Load configuration
    with open("config/default.yaml") as file:
        config = yaml.full_load(file)

    # Get directories
    detections_dir = os.path.join(core.data_dir(), "detections")
    dataset_dir = os.path.join("~/Kitti/tracking")
    results_dir = os.path.join(core.top_dir(), "results")
    plot_dir = os.path.join(results_dir, "plots")
    make_dir(plot_dir)
    ylabels = [
        r"$x$ [m]", r"$y$ [m]", r"$z$ [m]", r"$\theta$ [rad]", r"$l$ [m]",
        r"$h$ [m]", r"$w$ [m]"
    ]
    for split in config["splits"]:
        for class_ in config["classes"]:
            seq_id = 15
            gt_track_id = 2
            # Load ground truth
            sequence = KittiSequence(detections_dir=detections_dir,
                                     dataset_dir=dataset_dir,
                                     seq_id=seq_id,
                                     split=split,
                                     class_=class_)
            gt_track = sequence.get_track(gt_track_id)
            gt_track = gt_track[:, 2:]

            # Load track
            track_dir = os.path.join(results_dir, "tracks", split, class_,
                                     str(seq_id).zfill(4))
            track_id = "38882006386010313624536118754.npy"
            x_file = os.path.join(track_dir, "x", track_id)
            P_file = os.path.join(track_dir, "P", track_id)
            x = np.load(x_file)
            x = x[:7, :]
            P = np.load(P_file)
            P = P[:7, :7, :]

            # Compute error and std dev
            t = np.arange(x.shape[1])
            error = x - gt_track
            error[3, :] = np.unwrap(error[3, :])
            var = np.transpose(P.diagonal())
            std_dev = np.sqrt(var)

            fig = plt.figure(figsize=(12, 7))
            plot_file = os.path.join(plot_dir, "error.png")
            for i, ylabel in enumerate(ylabels):
                ax = fig.add_subplot(241 + i)
                line1 = ax.plot(t, error[i, :])
                line2 = ax.plot(t, 3 * std_dev[i, :], 'r--', t,
                                -3 * std_dev[i, :], 'r--')[0]
                ax.set_xlabel('Frame')
                ax.set_ylabel(ylabel)

            fig.legend([line1, line2],
                       labels=['Error', 'Uncertainty Envelope'],
                       bbox_to_anchor=(0.97, 0.28),
                       loc="center right")
            plt.tight_layout()
            plt.savefig(plot_file)
            plt.close()
Пример #7
0
def main():
    # Load configuration
    with open("config/default.yaml") as file:
        config = yaml.full_load(file)

    # Get directories
    detections_dir = os.path.join(core.data_dir(), "detections")
    dataset_dir = os.path.join("~/Kitti/tracking")
    results_dir = os.path.join(core.top_dir(), "results")
    duration = 0
    frames = 0
    for split in config["splits"]:
        for class_ in config["classes"]:
            for seq_id in range(config["num_sequences"]):
                print("Processing: {}".format(seq_id))

                # Load detections
                sequence_detections = KittiSequence(
                    detections_dir=detections_dir,
                    dataset_dir=dataset_dir,
                    seq_id=seq_id,
                    split=split,
                    class_=class_)

                start = time.time()
                sequence_tracker = Tracker(sequence_detections)
                sequence_tracker.run()
                duration = duration + (time.time() - start)
                frames = frames + len(sequence_detections)

                # Output text predictions
                if config["output_text"]:
                    text_dir = os.path.join(results_dir, "text", split, class_)

                    # Make file if doesn't exists
                    if not os.path.exists(text_dir):
                        os.makedirs(text_dir)

                    text_file = os.path.join(text_dir,
                                             str(seq_id).zfill(4) + ".txt")

                    sequence_tracker.generate_text_output(
                        output_file=text_file)

                # Output bounding box visualization
                if config["output_vis"]:
                    vis_path = os.path.join(results_dir, "vis", split, class_,
                                            str(seq_id).zfill(4))
                    sequence_tracker.generate_visualization(
                        output_path=vis_path)

                # Output error and covariance plots
                if config["output_tracks"]:
                    track_dir = os.path.join(results_dir, "tracks", split,
                                             class_,
                                             str(seq_id).zfill(4))

                    # Make file if doesn't exists
                    if not os.path.exists(track_dir):
                        os.makedirs(track_dir)

                    sequence_tracker.generate_track_output(
                        output_path=track_dir)

    print("FPS {}".format(frames / duration))
def main():
    """Comparison of ground truth and ORBSLAM2 poses
    https://github.com/raulmur/ORB_SLAM2
    """

    ####################
    # Options
    ####################

    odom_dataset_dir = os.path.expanduser('~/Kitti/odometry/dataset')

    # sequence = '02'
    # sequence = '03'
    # sequence = '08'
    sequence = '09'
    # sequence = '10'
    # sequence = '11'
    # sequence = '12'

    # vtk_window_size = (1280, 720)
    vtk_window_size = (960, 540)
    # vtk_window_size = (400, 300)

    point_cloud_source = 'lidar'
    # point_cloud_source = 'fast'
    # point_cloud_source = 'multiscale'

    # first_frame_idx = None
    # first_frame_pose = None

    # Setup odometry dataset handler
    odom_dataset = pykitti.odometry(odom_dataset_dir, sequence)

    # # Check that velo length matches timestamps?
    # if len(odom_dataset.velo_files) != len(odom_dataset.timestamps):
    #     raise ValueError('velo files and timestamps have different length!')

    frame_range = (0, len(odom_dataset.timestamps))
    # frame_range = (0, 100)

    # camera_viewpoint = 'front'
    camera_viewpoint = 'elevated'
    # camera_viewpoint = 'bev'

    buffer_size = 50
    buffer_update = 10

    save_screenshots = False

    ##############################

    vtk_renderer = demo_utils.setup_vtk_renderer()

    vtk_render_window = demo_utils.setup_vtk_render_window(
        'Overlaid Point Cloud', vtk_window_size, vtk_renderer)

    if save_screenshots:
        vtk_win_to_img_filter, vtk_png_writer = vtk_utils.setup_screenshots(
            vtk_render_window)

    vtk_interactor = vtk.vtkRenderWindowInteractor()
    vtk_interactor.SetRenderWindow(vtk_render_window)
    vtk_interactor.SetInteractorStyle(
        vtk_utils.ToggleActorsInteractorStyle(None, vtk_renderer))
    vtk_interactor.Initialize()

    cam_p2 = odom_dataset.calib.P_rect_20

    # Load poses
    cam0_ref_poses_orbslam = np.loadtxt(
        odom_dataset_dir + '/poses_orbslam2/{}.txt'.format(sequence))
    cam0_ref_poses_orbslam = np.pad(cam0_ref_poses_orbslam, ((0, 0), (0, 4)),
                                    mode='constant')
    cam0_ref_poses_orbslam[:, -1] = 1
    cam0_ref_poses_orbslam = cam0_ref_poses_orbslam.reshape((-1, 4, 4))

    cam0_ref_poses_gt = np.asarray(odom_dataset.poses, np.float32)

    # Setup camera
    if camera_viewpoint == 'front':
        cam0_curr_vtk_cam_pos = [0.0, 0.0, 0.0, 1.0]
        cam0_curr_vtk_focal_point = [0.0, 0.0, 20.0, 1.0]
    elif camera_viewpoint == 'elevated':
        cam0_curr_vtk_cam_pos = [0.0, -5.0, -15.0, 1.0]
        cam0_curr_vtk_focal_point = [0.0, 0.0, 20.0, 1.0]
    elif camera_viewpoint == 'bev':
        # camera_zoom = 1.0
        cam0_curr_vtk_cam_pos = [0.0, -50.0, 10.0, 1.0]
        # camera_pos = (0.0, 0.0, 0.0)
        cam0_curr_vtk_focal_point = [0.0, 0.0, 15.0, 1.0]
    else:
        raise ValueError('Invalid camera_pos', camera_viewpoint)

    # Create VtkAxes
    vtk_axes = vtk.vtkAxesActor()
    vtk_axes.SetTotalLength(2, 2, 2)
    vtk_renderer.AddActor(vtk_axes)

    vtk_pc_poses_orbslam = VtkPointCloudGlyph()
    vtk_pc_poses_gt = VtkPointCloudGlyph()

    vtk_pc_poses_orbslam.vtk_actor.GetProperty().SetPointSize(5)
    vtk_pc_poses_gt.vtk_actor.GetProperty().SetPointSize(5)

    actor_buffer = []

    for frame_idx in range(*frame_range):

        print('{} / {}'.format(frame_idx, len(odom_dataset.timestamps) - 1))

        # Load next frame data
        load_start_time = time.time()

        rgb_load_start = time.time()
        bgr_image = cv2.imread(odom_dataset.cam2_files[frame_idx])
        print('rgb_load', time.time() - rgb_load_start)

        if point_cloud_source == 'lidar':

            velo_curr_points_padded = get_velo_points(odom_dataset, frame_idx)

            # Velo in cam0_curr
            cam0_curr_pc_all_padded = odom_dataset.calib.T_cam0_velo @ velo_curr_points_padded.T

            pass
            # cam0_curr_points = cam0_curr_pc.T

        elif point_cloud_source in ['fast', 'multiscale']:

            # depth_map_path = depth_map_dir + '/{:010d}.png'.format(frame_idx)
            # depth_map = depth_map_utils.read_depth_map(depth_map_path)
            # points_cam2 = depth_map_utils.get_depth_point_cloud(depth_map, cam_p).T
            raise NotImplementedError()

        else:
            raise ValueError('Invalid point cloud source')
        print('load\t\t', time.time() - load_start_time)

        # Project velodyne points
        projection_start_time = time.time()

        if point_cloud_source == 'lidar':

            # Project into image2
            points_in_img2 = calib_utils.project_pc_to_image2(
                cam0_curr_pc_all_padded, cam_p2)
            points_in_img2_int = np.round(points_in_img2).astype(np.int32)

            image_filter = obj_utils.points_in_img_filter(
                points_in_img2_int, bgr_image.shape)

            cam0_curr_pc_padded = cam0_curr_pc_all_padded[:, image_filter]

            # points = points_cam2[image_mask]
            points_in_img_int_valid = points_in_img2_int[:, image_filter]
            point_colours = bgr_image[points_in_img_int_valid[1],
                                      points_in_img_int_valid[0]]
        else:
            raise ValueError('Invalid point_cloud_source', point_cloud_source)

        print('projection\t', time.time() - projection_start_time)

        # Get pose
        cam0_ref_pose_orbslam = cam0_ref_poses_orbslam[frame_idx]
        cam0_ref_pose_gt = cam0_ref_poses_gt[frame_idx]

        tf_cam0_ref_cam0_curr = cam0_ref_pose_orbslam
        cam0_ref_pc_padded = tf_cam0_ref_cam0_curr @ cam0_curr_pc_padded

        # VtkPointCloud
        vtk_pc = VtkPointCloudGlyph()
        vtk_pc.vtk_actor.GetProperty().SetPointSize(2)

        vtk_pc_start_time = time.time()
        vtk_pc.set_points(cam0_ref_pc_padded[0:3].T, point_colours)
        print('vtk_pc\t\t', time.time() - vtk_pc_start_time)

        # Display orbslam pose
        vtk_pc_poses_orbslam.set_points(
            cam0_ref_poses_orbslam[max(0, frame_idx - buffer_size):frame_idx +
                                   1, 0:3, 3],
            np.tile([255, 0, 0], [buffer_size, 1]))

        # Display gt pose
        vtk_pc_poses_gt.vtk_actor.GetProperty().SetPointSize(5)
        vtk_pc_poses_gt.set_points(
            cam0_ref_poses_gt[max(0, frame_idx - buffer_size):frame_idx + 1,
                              0:3, 3], np.tile([0, 255, 0], [buffer_size, 1]))

        # Add vtk actors
        vtk_renderer.AddActor(vtk_pc.vtk_actor)
        vtk_renderer.AddActor(vtk_pc_poses_orbslam.vtk_actor)
        vtk_renderer.AddActor(vtk_pc_poses_gt.vtk_actor)

        cam0_ref_vtk_cam_pos = tf_cam0_ref_cam0_curr.dot(cam0_curr_vtk_cam_pos)
        cam0_ref_vtk_focal_point = tf_cam0_ref_cam0_curr.dot(
            cam0_curr_vtk_focal_point)

        current_cam = vtk_renderer.GetActiveCamera()
        vtk_renderer.ResetCamera()
        current_cam.SetViewUp(0, -1, 0)
        current_cam.SetPosition(cam0_ref_vtk_cam_pos[0:3])
        current_cam.SetFocalPoint(*cam0_ref_vtk_focal_point[0:3])
        current_cam.Zoom(0.5)

        vtk_renderer.ResetCameraClippingRange()

        # Render
        render_start_time = time.time()
        vtk_render_window.Render()
        print('render\t\t', time.time() - render_start_time)

        actor_buffer.append(vtk_pc.vtk_actor)
        if len(actor_buffer) > buffer_size:
            if frame_idx % buffer_update != 0:
                actor_buffer[frame_idx - buffer_size].SetVisibility(0)

        if save_screenshots:
            screenshot_start_time = time.time()
            screenshot_path = core.data_dir() + '/temp/{:010d}.png'.format(
                frame_idx)
            vtk_utils.save_screenshot(screenshot_path, vtk_win_to_img_filter,
                                      vtk_png_writer)
            print('screenshot\t', time.time() - screenshot_start_time)

        print('---')

    for vtk_actor in actor_buffer:
        vtk_actor.SetVisibility(1)

    print('Done')

    # Keep window open
    vtk_interactor.Start()
def main():
    #########################################################
    # Specify Source Folders and Parameters For Frame Reader
    #########################################################
    data_split_dir = 'val'

    # Specify whether the validation or inference results need to be
    # visualized.
    # results_dir = 'validation'  # Or testing
    results_dir = 'testing'

    # sample_free, anchor_redundancy, black_box,  bayes_od_none,
    # bayes_od_ci_fast. bayes_od_ci,or bayes_od_ici
    uncertainty_method = 'bayes_od_ci_fast'

    dataset_dir = os.path.expanduser('~/Datasets/bdd100k')
    image_dir = os.path.join(dataset_dir, 'images', '100k', data_split_dir)
    label_file_name = os.path.join(dataset_dir, 'labels',
                                   data_split_dir) + '.json'

    checkpoint_name = 'retinanet_bdd'
    checkpoint_number = '101'

    if results_dir == 'testing':
        prediction_dir = os.path.join(core.data_dir(), 'outputs',
                                      checkpoint_name, 'predictions',
                                      results_dir, 'bdd', checkpoint_number,
                                      uncertainty_method, 'data')
    else:
        prediction_dir = os.path.join(core.data_dir(), 'outputs',
                                      checkpoint_name, 'predictions',
                                      results_dir, checkpoint_number, 'data')
    prediction_file_name = os.path.join(prediction_dir, 'predictions.json')

    frames_list = os.listdir(image_dir)
    index = random.randint(0, len(frames_list))
    frame_id = frames_list[index]

    print('Showing Frame ID:' + frame_id)

    #############
    # Read Frame
    #############
    im_path = image_dir + '/' + frame_id
    image = cv2.imread(im_path)

    all_labels = json.load(open(label_file_name, 'r'))
    category_gt, boxes_2d_gt, _ = read_bdd_format(
        frame_id,
        all_labels,
        categories=['car', 'truck', 'bus', 'person', 'rider', 'bike', 'motor'])

    all_predictions = json.load(open(prediction_file_name, 'r'))
    category_pred, boxes_2d_pred, _ = read_bdd_format(
        frame_id,
        all_predictions,
        categories=['car', 'truck', 'bus', 'person', 'rider', 'bike', 'motor'])

    max_ious = np.zeros(boxes_2d_pred.shape[0])
    # Compute IOU between each prediction and the ground truth boxes
    if boxes_2d_gt.size > 0 and boxes_2d_pred.size > 0:
        for obj_idx in range(boxes_2d_pred.shape[0]):
            obj_iou_fmt = boxes_2d_pred[obj_idx]

            ious_2d = two_d_iou(obj_iou_fmt, boxes_2d_gt)

            max_iou = np.amax(ious_2d)
            max_ious[obj_idx] = max_iou

    #########################################################
    # Draw GT and Prediction Boxes
    #########################################################
    # Transform Predictions to left and right images
    image_out = draw_box_2d(image,
                            boxes_2d_gt,
                            category_gt,
                            line_width=2,
                            dataset='bdd',
                            is_gt=True)

    image_out = draw_box_2d(image_out,
                            boxes_2d_pred,
                            category_pred,
                            line_width=2,
                            is_gt=False,
                            dataset='bdd',
                            text_to_plot=max_ious,
                            plot_text=True)

    if results_dir == 'testing':
        cv2.imshow('Detections from ' + uncertainty_method, image_out)
    else:
        cv2.imshow('Validation Set Detections', image_out)

    cv2.waitKey()
def main():
    ##############################
    # Options
    ##############################
    """Note: Run scripts/depth_completion/save_depth_maps_raw.py first.
    Demo to show depth completed point clouds
    """

    raw_dir = os.path.expanduser('~/Kitti/raw')
    drive_id = '2011_09_26_drive_0023_sync'

    vtk_window_size = (1280, 720)
    max_fps = 30.0

    # point_cloud_source = 'lidar'
    # fill_type = None

    point_cloud_source = 'depth'
    fill_type = 'multiscale'

    # Load raw data
    drive_date = drive_id[0:10]
    drive_num_str = drive_id[17:21]
    raw_data = pykitti.raw(raw_dir, drive_date, drive_num_str)

    # Check that velo length matches timestamps?
    if len(raw_data.velo_files) != len(raw_data.timestamps):
        raise ValueError('velo files and timestamps have different length!')

    frame_range = (0, len(raw_data.timestamps))

    ##############################

    min_loop_time = 1.0 / max_fps

    vtk_renderer = demo_utils.setup_vtk_renderer()
    vtk_render_window = demo_utils.setup_vtk_render_window(
        'Overlaid Point Cloud', vtk_window_size, vtk_renderer)

    # Setup camera
    current_cam = vtk_renderer.GetActiveCamera()
    current_cam.SetViewUp(0, 0, 1)
    current_cam.SetPosition(0.0, -8.0, -15.0)
    current_cam.SetFocalPoint(0.0, 0.0, 20.0)
    current_cam.Zoom(0.7)

    # Create VtkAxes
    vtk_axes = vtk.vtkAxesActor()
    vtk_axes.SetTotalLength(2, 2, 2)

    # Setup interactor
    vtk_interactor = vtk.vtkRenderWindowInteractor()
    vtk_interactor.SetRenderWindow(vtk_render_window)
    vtk_interactor.SetInteractorStyle(
        vtk_utils.ToggleActorsInteractorStyle(None, vtk_renderer, current_cam,
                                              vtk_axes))
    vtk_interactor.Initialize()

    if point_cloud_source not in ['lidar', 'depth']:
        raise ValueError(
            'Invalid point cloud source {}'.format(point_cloud_source))

    cam_p = raw_data.calib.P_rect_20

    # Point cloud
    vtk_pc = VtkPointCloudGlyph()

    # Add actors
    vtk_renderer.AddActor(vtk_axes)
    vtk_renderer.AddActor(vtk_pc.vtk_actor)

    for frame_idx in range(*frame_range):

        loop_start_time = time.time()

        print('{} / {}'.format(frame_idx, len(raw_data.timestamps) - 1))

        # Load next frame data
        load_start_time = time.time()

        rgb_image = np.asarray(raw_data.get_cam2(frame_idx))
        bgr_image = rgb_image[..., ::-1]

        if point_cloud_source == 'lidar':
            velo_points = get_velo_points(raw_data, frame_idx)

            # Transform point cloud to cam_0 frame
            velo_curr_points_padded = np.pad(velo_points, [[0, 0], [0, 1]],
                                             constant_values=1.0,
                                             mode='constant')

            cam0_curr_pc_all_padded = raw_data.calib.T_cam0_velo @ velo_curr_points_padded.T

            # Project velodyne points
            projection_start_time = time.time()
            points_in_img = calib_utils.project_pc_to_image(
                cam0_curr_pc_all_padded[0:3], cam_p)
            points_in_img_int = np.round(points_in_img).astype(np.int32)
            print('projection\t', time.time() - projection_start_time)

            image_filter = obj_utils.points_in_img_filter(
                points_in_img_int, bgr_image.shape)

            cam0_curr_pc = cam0_curr_pc_all_padded[0:3, image_filter]

            points_in_img_int_valid = points_in_img_int[:, image_filter]
            point_colours = bgr_image[points_in_img_int_valid[1],
                                      points_in_img_int_valid[0]]

        elif point_cloud_source == 'depth':
            depth_maps_dir = core.data_dir() + \
                '/depth_completion/raw/{}/depth_02_{}'.format(drive_id, fill_type)
            depth_map_path = depth_maps_dir + '/{:010d}.png'.format(frame_idx)
            depth_map = depth_map_utils.read_depth_map(depth_map_path)
            cam0_curr_pc = depth_map_utils.get_depth_point_cloud(
                depth_map, cam_p)

            point_colours = bgr_image.reshape(-1, 3)

            # Mask to valid points
            valid_mask = (cam0_curr_pc[2] != 0)
            cam0_curr_pc = cam0_curr_pc[:, valid_mask]
            point_colours = point_colours[valid_mask]
        else:
            raise ValueError('Invalid point cloud source')

        print('load\t\t', time.time() - load_start_time)

        # VtkPointCloud
        vtk_pc_start_time = time.time()
        vtk_pc.set_points(cam0_curr_pc.T, point_colours)
        print('vtk_pc\t\t', time.time() - vtk_pc_start_time)

        # Reset the clipping range to show all points
        vtk_renderer.ResetCameraClippingRange()

        # Render
        render_start_time = time.time()
        vtk_render_window.Render()
        print('render\t\t', time.time() - render_start_time)

        # Pause to keep frame rate under max
        loop_run_time = time.time() - loop_start_time
        print('loop\t\t', loop_run_time)
        if loop_run_time < min_loop_time:
            time.sleep(min_loop_time - loop_run_time)

        print('---')

    print('Done')

    # Keep window open
    vtk_interactor.Start()
Пример #11
0
def setup_config(args, random_seed=None, is_testing=False):
    """
    Sets up config node with probabilistic detectron elements. Also sets up a fixed random seed for all scientific
    computing libraries, and sets up all supported datasets as instances of coco.

    Args:
        args (Namespace): args from argument parser
        random_seed (int): set a fixed random seed throughout torch, numpy, and python
        is_testing (bool): set to true if inference. If true function will return an error if checkpoint directory not
        already existing.
    Returns:
        (CfgNode) detectron2 config object
    """
    # Get default detectron config file
    cfg = get_cfg()
    add_detr_config(cfg)
    add_probabilistic_config(cfg)

    # Update default config file with custom config file
    configs_dir = core.configs_dir()
    args.config_file = os.path.join(configs_dir, args.config_file)
    cfg.merge_from_file(args.config_file)

    # Add dropout rate for faster RCNN box head
    cfg.MODEL.ROI_BOX_HEAD.DROPOUT_RATE = cfg.MODEL.PROBABILISTIC_MODELING.DROPOUT_RATE

    # Update config with inference configurations. Only applicable for when in
    # probabilistic inference mode.
    if args.inference_config != "":
        args.inference_config = os.path.join(configs_dir,
                                             args.inference_config)
        cfg.merge_from_file(args.inference_config)

    # Create output directory
    model_name = os.path.split(os.path.split(args.config_file)[0])[-1]
    dataset_name = os.path.split(
        os.path.split(os.path.split(args.config_file)[0])[0])[-1]

    cfg['OUTPUT_DIR'] = os.path.join(core.data_dir(), dataset_name, model_name,
                                     os.path.split(args.config_file)[-1][:-5],
                                     'random_seed_' + str(random_seed))
    if is_testing:
        if not os.path.isdir(cfg['OUTPUT_DIR']):
            raise NotADirectoryError(
                "Checkpoint directory {} does not exist.".format(
                    cfg['OUTPUT_DIR']))

    os.makedirs(cfg['OUTPUT_DIR'], exist_ok=True)

    # copy config file to output directory
    copyfile(
        args.config_file,
        os.path.join(cfg['OUTPUT_DIR'],
                     os.path.split(args.config_file)[-1]))

    # Freeze config file
    cfg['SEED'] = random_seed
    cfg.freeze()

    # Initiate default setup
    default_setup(cfg, args)

    # Setup logger for probabilistic detectron module
    setup_logger(output=cfg.OUTPUT_DIR,
                 distributed_rank=comm.get_rank(),
                 name="Probabilistic Detectron")

    # Set a fixed random seed for all numerical libraries
    if random_seed is not None:
        torch.manual_seed(random_seed)
        np.random.seed(random_seed)
        random.seed(random_seed)

    # Setup datasets
    if args.image_corruption_level != 0:
        image_root_corruption_prefix = '_' + str(args.image_corruption_level)
    else:
        image_root_corruption_prefix = None

    dataset_dir = os.path.expanduser(args.dataset_dir)

    # Handle cases when this function has been called multiple times. In that case skip fully.
    # Todo this is very bad practice, should fix.
    try:
        setup_all_datasets(
            dataset_dir,
            image_root_corruption_prefix=image_root_corruption_prefix)
        return cfg
    except AssertionError:
        return cfg