indoor_sample.py 2.39 KB
Newer Older
liyinhao's avatar
liyinhao committed
1
2
3
4
5
6
import numpy as np

from mmdet.datasets.registry import PIPELINES


@PIPELINES.register_module
liyinhao's avatar
liyinhao committed
7
8
class PointSample(object):
    """Point Sample.
liyinhao's avatar
liyinhao committed
9
10
11
12
13
14
15
16

    Sampling data to a certain number.

    Args:
        name (str): Name of the dataset.
        num_points (int): Number of points to be sampled.
    """

17
    def __init__(self, num_points):
liyinhao's avatar
liyinhao committed
18
19
        self.num_points = num_points

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    def points_random_sampling(self,
                               points,
                               num_samples,
                               replace=None,
                               return_choices=False,
                               seed=None):
        """Points Random Sampling.

        Sample points to a certain number.

        Args:
            points (ndarray): 3D Points.
            num_samples (int): Number of samples to be sampled.
            replace (bool): Whether the sample is with or without replacement.
            return_choices (bool): Whether return choice.

        Returns:
            points (ndarray): 3D Points.
            choices (ndarray): The generated random samples
        """
        if seed is not None:
            np.random.seed(seed)
        if replace is None:
            replace = (points.shape[0] < num_samples)
        choices = np.random.choice(
            points.shape[0], num_samples, replace=replace)
        if return_choices:
            return points[choices], choices
        else:
            return points[choices]

    def __call__(self, results, seed=None):
        point_cloud = results.get('point_cloud', None)
liyinhao's avatar
liyinhao committed
53
        pcl_color = results.get('pcl_color', None)
54
55
56
        point_cloud, choices = self.points_random_sampling(
            point_cloud, self.num_points, return_choices=True, seed=seed)
        results['point_cloud'] = point_cloud
liyinhao's avatar
liyinhao committed
57

58
        if pcl_color is not None:
liyinhao's avatar
liyinhao committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
            pcl_color = pcl_color[choices]
            instance_labels = results.get('instance_labels', None)
            semantic_labels = results.get('semantic_labels', None)
            instance_labels = instance_labels[choices]
            semantic_labels = semantic_labels[choices]
            results['instance_labels'] = instance_labels
            results['semantic_labels'] = semantic_labels
            results['pcl_color'] = pcl_color

        return results

    def __repr__(self):
        repr_str = self.__class__.__name__
        repr_str += '(num_points={})'.format(self.num_points)
        return repr_str