nesl / radar-camera-fusion-depth

Depth Estimation from Camera Image and mmWave Radar Point Cloud

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why inference result loss somthing data

YoungHsu1002 opened this issue · comments

I put nuscenes data which has 34179 for trainval, but after inference model my data is less then 34179?

I am not sure what the reason for this might be, but you can check out the train-val split here. If I remember correctly, it had 850 scenes and most scenes had 39/40 samples. Some scenes had even less samples.

You can run it without accounting for the split by modifying the following and see if it gives you the result you're looking for:

  1. Remove the max_limit:
  2. Modifying this part:

Thx for your reply.I want to know about the dataset split which use for your study is official split or custom split?

For the training and validation sets, as far as I can remember, we used the splits provided here.

[Note that in nuScenes, the scene_index and the number in scene_name are different]

Our .pkl files provided here basically map the scene_names to the corresponding scene_indices.

Feel free to verify it for yourself and let me know if you find any discrepancies between the two.

The test set is the standard / official nuScenes test set: v1.0-test

I found that the original program logic did not take into account the last detection problem. The following is my modification of the original code:

def process_scene(args):
    '''
    Processes one scene from first sample to last sample

    Arg(s):
        args : tuple(Object, Object, str, int, str, str, int, int, str, bool)
            nusc : NuScenes Object
                nuScenes object instance
            nusc_explorer : NuScenesExplorer Object
                nuScenes explorer object instance
            tag : str
                train, val
            scene_id : int
                identifier for one scene
            lidar_camera_correspondence_dict : dict
                contains correspondence between a lidar token and its closest camera token
            panoptic_seg_dir : str
                directory where all the panoptic segmentation masks are stored
            first_sample_token : str
                token to identify first sample in the scene for fetching
            last_sample_token : str
                token to identify last sample in the scene for fetching
            n_forward : int
                number of forward (future) frames to reproject
            n_backward : int
                number of backward (previous) frames to reproject
            output_dirpath : str
                root of output directory
            paths_only : bool
                if set, then only produce paths
    Returns:
        list[str] : paths to camera image
        list[str] : paths to lidar depth map
        list[str] : paths to radar depth map
        list[str] : paths to ground truth (merged lidar) depth map
        list[str] : paths to ground truth (merged lidar) interpolated depth map
    '''

    tag, \
        scene_id, \
        lidar_camera_correspondence_dict, \
        panoptic_seg_dir, \
        first_sample_token, \
        last_sample_token, \
        n_forward, \
        n_backward, \
        output_dirpath, \
        paths_only = args

    # Instantiate the first sample id
    sample_id = 0
    sample_token = first_sample_token

    camera_image_paths = []
    lidar_paths = []
    radar_points_paths = []
    radar_points_reprojected_paths = []
    ground_truth_paths = []
    ground_truth_interp_paths = []

    print('Processing scene_id={}'.format(scene_id))

    # Iterate through all samples up to the last sample
    while sample_token != last_sample_token:

        # Fetch a single sample
        current_sample = nusc.get('sample', sample_token)
        camera_token = current_sample['data']['CAM_FRONT']
        camera_sample = nusc.get('sample_data', camera_token)

        '''
        Set up paths
        '''
        camera_image_path = os.path.join(nusc.dataroot, camera_sample['filename'])

        dirpath, filename = os.path.split(camera_image_path)
        dirpath = dirpath.replace(nusc.dataroot, output_dirpath)
        filename = os.path.splitext(filename)[0]

        # Create lidar path
        lidar_dirpath = dirpath.replace(
            'samples',
            os.path.join('lidar', 'scene_{}'.format(scene_id)))
        lidar_filename = filename + '.png'

        lidar_path = os.path.join(
            lidar_dirpath,
            lidar_filename)

        # Create radar path
        radar_points_dirpath = dirpath.replace(
            'samples',
            os.path.join('radar_points', 'scene_{}'.format(scene_id)))
        radar_points_filename = filename + '.npy'

        radar_points_path = os.path.join(
            radar_points_dirpath,
            radar_points_filename)

        radar_points_reprojected_dirpath = dirpath.replace(
            'samples',
            os.path.join('radar_points_reprojected', 'scene_{}'.format(scene_id)))

        radar_points_reprojected_path = os.path.join(
            radar_points_reprojected_dirpath,
            radar_points_filename)

        # Create ground truth path
        ground_truth_dirpath = dirpath.replace(
            'samples',
            os.path.join('ground_truth', 'scene_{}'.format(scene_id)))
        ground_truth_filename = filename + '.png'

        ground_truth_path = os.path.join(
            ground_truth_dirpath,
            ground_truth_filename)

        # Create interpolated densified ground truth path
        ground_truth_interp_dirpath = dirpath.replace(
            'samples',
            os.path.join('ground_truth_interp', 'scene_{}'.format(scene_id)))
        ground_truth_interp_filename = filename + '.png'

        ground_truth_interp_path = os.path.join(
            ground_truth_interp_dirpath,
            ground_truth_interp_filename)

        # In case multiple threads create same directory
        dirpaths = [
            lidar_dirpath,
            radar_points_dirpath,
            radar_points_reprojected_dirpath,
            ground_truth_dirpath,
            ground_truth_interp_dirpath
        ]

        for dirpath in dirpaths:
            if not os.path.exists(dirpath):
                try:
                    os.makedirs(dirpath)
                except Exception:
                    pass

        '''
        Store file paths
        '''
        camera_image_paths.append(camera_image_path)
        radar_points_paths.append(radar_points_path)
        radar_points_reprojected_paths.append(radar_points_reprojected_path)
        lidar_paths.append(lidar_path)
        ground_truth_paths.append(ground_truth_path)
        ground_truth_interp_paths.append(ground_truth_interp_path)

        if not paths_only:

            '''
            Get camera data
            '''
            camera_image = data_utils.load_image(camera_image_path)

            '''
            Get lidar points projected to an image and save as PNG
            '''
            lidar_depth = lidar_depth_map_from_token(
                nusc=nusc,
                nusc_explorer=nusc_explorer,
                current_sample_token=sample_token)

            data_utils.save_depth(lidar_depth, lidar_path)

            '''
            Merge forward and backward point clouds for radar and lidar
            '''
            # Transform Lidar and Radar Points to the image coordinate
            points_radar_reprojected, depth_radar_reprojected = merge_radar_point_clouds(
                nusc=nusc,
                nusc_explorer=nusc_explorer,
                current_sample_token=sample_token,
                n_forward=n_forward,
                n_backward=n_backward)

            points_radar, depth_radar = merge_radar_point_clouds(
                nusc=nusc,
                nusc_explorer=nusc_explorer,
                current_sample_token=sample_token,
                n_forward=0,
                n_backward=0)

            # Merges n_forward and n_backward number of point clouds to frame at sample token
            points_lidar, depth_lidar = merge_lidar_point_clouds(
                nusc=nusc,
                nusc_explorer=nusc_explorer,
                current_sample_token=sample_token,
                n_forward=n_forward,
                n_backward=n_backward,
                lidar_camera_correspondence_dict=lidar_camera_correspondence_dict,
                panoptic_seg_dir=panoptic_seg_dir)

            '''
            Project point cloud onto the image plane and save as PNG
            '''
            # Merges n_forward and n_backward number of point clouds to frame at sample token
            # but in this case we need the lidar image so that we can save it
            ground_truth = points_to_depth_map(points_lidar, depth_lidar, camera_image)

            # Save depth map as PNG
            data_utils.save_depth(ground_truth, ground_truth_path)

            '''
            Interpolate missing points in ground truth point cloud and save as PNG
            '''
            validity_map = np.where(ground_truth > 0.0, 1.0, 0.0)
            ground_truth_interp = data_utils.interpolate_depth(
                ground_truth,
                validity_map)

            # Save depth map as PNG
            data_utils.save_depth(ground_truth_interp, ground_truth_interp_path)

            '''
            Save radar points as a numpy array
            '''
            radar_points_reprojected = np.stack([
                points_radar_reprojected[0, :],
                points_radar_reprojected[1, :],
                depth_radar_reprojected],
                axis=-1)

            radar_points = np.stack([
                points_radar[0, :],
                points_radar[1, :],
                depth_radar],
                axis=-1)

            np.save(radar_points_reprojected_path, radar_points_reprojected)
            np.save(radar_points_path, radar_points)

        '''
        Move to next sample in scene
        '''
        sample_id = sample_id + 1
        sample_token = current_sample['next']

    #don't forget to add the last sample
    
    current_sample = nusc.get('sample', sample_token)
    camera_token = current_sample['data']['CAM_FRONT']
    camera_sample = nusc.get('sample_data', camera_token)

    '''
    Set up paths
    '''
    camera_image_path = os.path.join(nusc.dataroot, camera_sample['filename'])

    dirpath, filename = os.path.split(camera_image_path)
    dirpath = dirpath.replace(nusc.dataroot, output_dirpath)
    filename = os.path.splitext(filename)[0]

    # Create lidar path
    lidar_dirpath = dirpath.replace(
        'samples',
        os.path.join('lidar', 'scene_{}'.format(scene_id)))
    lidar_filename = filename + '.png'

    lidar_path = os.path.join(
        lidar_dirpath,
        lidar_filename)

    # Create radar path
    radar_points_dirpath = dirpath.replace(
        'samples',
        os.path.join('radar_points', 'scene_{}'.format(scene_id)))
    radar_points_filename = filename + '.npy'

    radar_points_path = os.path.join(
        radar_points_dirpath,
        radar_points_filename)

    radar_points_reprojected_dirpath = dirpath.replace(
        'samples',
        os.path.join('radar_points_reprojected', 'scene_{}'.format(scene_id)))

    radar_points_reprojected_path = os.path.join(
        radar_points_reprojected_dirpath,
        radar_points_filename)

    # Create ground truth path
    ground_truth_dirpath = dirpath.replace(
        'samples',
        os.path.join('ground_truth', 'scene_{}'.format(scene_id)))
    ground_truth_filename = filename + '.png'

    ground_truth_path = os.path.join(
        ground_truth_dirpath,
        ground_truth_filename)

    # Create interpolated densified ground truth path
    ground_truth_interp_dirpath = dirpath.replace(
        'samples',
        os.path.join('ground_truth_interp', 'scene_{}'.format(scene_id)))
    ground_truth_interp_filename = filename + '.png'

    ground_truth_interp_path = os.path.join(
        ground_truth_interp_dirpath,
        ground_truth_interp_filename)

    # In case multiple threads create same directory
    dirpaths = [
        lidar_dirpath,
        radar_points_dirpath,
        radar_points_reprojected_dirpath,
        ground_truth_dirpath,
        ground_truth_interp_dirpath
    ]

    for dirpath in dirpaths:
        if not os.path.exists(dirpath):
            try:
                os.makedirs(dirpath)
            except Exception:
                pass

    '''
    Store file paths
    '''
    camera_image_paths.append(camera_image_path)
    radar_points_paths.append(radar_points_path)
    radar_points_reprojected_paths.append(radar_points_reprojected_path)
    lidar_paths.append(lidar_path)
    ground_truth_paths.append(ground_truth_path)
    ground_truth_interp_paths.append(ground_truth_interp_path)

    if not paths_only:

        '''
        Get camera data
        '''
        camera_image = data_utils.load_image(camera_image_path)

        '''
        Get lidar points projected to an image and save as PNG
        '''
        lidar_depth = lidar_depth_map_from_token(
            nusc=nusc,
            nusc_explorer=nusc_explorer,
            current_sample_token=sample_token)

        data_utils.save_depth(lidar_depth, lidar_path)

        '''
        Merge forward and backward point clouds for radar and lidar
        '''
        # Transform Lidar and Radar Points to the image coordinate
        points_radar_reprojected, depth_radar_reprojected = merge_radar_point_clouds(
            nusc=nusc,
            nusc_explorer=nusc_explorer,
            current_sample_token=sample_token,
            n_forward=n_forward,
            n_backward=n_backward)

        points_radar, depth_radar = merge_radar_point_clouds(
            nusc=nusc,
            nusc_explorer=nusc_explorer,
            current_sample_token=sample_token,
            n_forward=0,
            n_backward=0)

        # Merges n_forward and n_backward number of point clouds to frame at sample token
        points_lidar, depth_lidar = merge_lidar_point_clouds(
            nusc=nusc,
            nusc_explorer=nusc_explorer,
            current_sample_token=sample_token,
            n_forward=n_forward,
            n_backward=n_backward,
            lidar_camera_correspondence_dict=lidar_camera_correspondence_dict,
            panoptic_seg_dir=panoptic_seg_dir)

        '''
        Project point cloud onto the image plane and save as PNG
        '''
        # Merges n_forward and n_backward number of point clouds to frame at sample token
        # but in this case we need the lidar image so that we can save it
        ground_truth = points_to_depth_map(points_lidar, depth_lidar, camera_image)

        # Save depth map as PNG
        data_utils.save_depth(ground_truth, ground_truth_path)

        '''
        Interpolate missing points in ground truth point cloud and save as PNG
        '''
        validity_map = np.where(ground_truth > 0.0, 1.0, 0.0)
        ground_truth_interp = data_utils.interpolate_depth(
            ground_truth,
            validity_map)

        # Save depth map as PNG
        data_utils.save_depth(ground_truth_interp, ground_truth_interp_path)

        '''
        Save radar points as a numpy array
        '''
        radar_points_reprojected = np.stack([
            points_radar_reprojected[0, :],
            points_radar_reprojected[1, :],
            depth_radar_reprojected],
            axis=-1)

        radar_points = np.stack([
            points_radar[0, :],
            points_radar[1, :],
            depth_radar],
            axis=-1)

        np.save(radar_points_reprojected_path, radar_points_reprojected)
        np.save(radar_points_path, radar_points)

    '''
    Move to next sample in scene
    '''
    sample_id = sample_id + 1
    #last sample end
    #=====================================================================================================
    print('Finished {} samples in scene_id={}'.format(sample_id, scene_id))

Through this change, I successfully obtained the complete val set information !

Thank you, can you create a pull request with this so that I can review it?

Closing due to inactivity.