indoor_sample.py 2.16 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
    def points_random_sampling(self,
                               points,
                               num_samples,
                               replace=None,
liyinhao's avatar
liyinhao committed
24
                               return_choices=False):
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        """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 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]

48
    def __call__(self, results):
liyinhao's avatar
liyinhao committed
49
50
51
        points = results.get('points', None)
        points, choices = self.points_random_sampling(
            points, self.num_points, return_choices=True)
52
53
        pts_instance_mask = results.get('pts_instance_mask', None)
        pts_semantic_mask = results.get('pts_semantic_mask', None)
liyinhao's avatar
liyinhao committed
54
        results['points'] = points
liyinhao's avatar
liyinhao committed
55

56
57
58
59
60
        if pts_instance_mask is not None and pts_semantic_mask is not None:
            pts_instance_mask = pts_instance_mask[choices]
            pts_semantic_mask = pts_semantic_mask[choices]
            results['pts_instance_mask'] = pts_instance_mask
            results['pts_semantic_mask'] = pts_semantic_mask
liyinhao's avatar
liyinhao committed
61
62
63
64
65
66
67

        return results

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