|
| 1 | +import torch |
| 2 | +import re |
| 3 | +import logging |
| 4 | +import torch.nn.functional as F |
| 5 | +from torch_scatter import scatter_mean, scatter_add |
| 6 | +from torch_geometric.nn.pool.consecutive import consecutive_cluster |
| 7 | +from torch_geometric.nn import voxel_grid |
| 8 | +from torch_cluster import grid_cluster |
| 9 | + |
| 10 | +log = logging.getLogger(__name__) |
| 11 | + |
| 12 | +class AddOnes(object): |
| 13 | + """ |
| 14 | + Add ones tensor to data |
| 15 | + """ |
| 16 | + |
| 17 | + def __call__(self, data): |
| 18 | + num_nodes = data.pos.shape[0] |
| 19 | + data.ones = torch.ones((num_nodes, 1)).float() |
| 20 | + return data |
| 21 | + |
| 22 | + def __repr__(self): |
| 23 | + return "{}()".format(self.__class__.__name__) |
| 24 | + |
| 25 | + |
| 26 | +# Label will be the majority label in each voxel |
| 27 | +_INTEGER_LABEL_KEYS = ["y", "instance_labels"] |
| 28 | + |
| 29 | +def shuffle_data(data): |
| 30 | + num_points = data.pos.shape[0] |
| 31 | + shuffle_idx = torch.randperm(num_points) |
| 32 | + for key in set(data.keys): |
| 33 | + item = data[key] |
| 34 | + if torch.is_tensor(item) and num_points == item.shape[0]: |
| 35 | + data[key] = item[shuffle_idx] |
| 36 | + return data |
| 37 | + |
| 38 | + |
| 39 | +def group_data(data, cluster=None, unique_pos_indices=None, mode="last", skip_keys=[]): |
| 40 | + """ Group data based on indices in cluster. |
| 41 | + The option ``mode`` controls how data gets agregated within each cluster. |
| 42 | +
|
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + data : Data |
| 46 | + [description] |
| 47 | + cluster : torch.Tensor |
| 48 | + Tensor of the same size as the number of points in data. Each element is the cluster index of that point. |
| 49 | + unique_pos_indices : torch.tensor |
| 50 | + Tensor containing one index per cluster, this index will be used to select features and labels |
| 51 | + mode : str |
| 52 | + Option to select how the features and labels for each voxel is computed. Can be ``last`` or ``mean``. |
| 53 | + ``last`` selects the last point falling in a voxel as the representent, ``mean`` takes the average. |
| 54 | + skip_keys: list |
| 55 | + Keys of attributes to skip in the grouping |
| 56 | + """ |
| 57 | + |
| 58 | + assert mode in ["mean", "last"] |
| 59 | + if mode == "mean" and cluster is None: |
| 60 | + raise ValueError("In mean mode the cluster argument needs to be specified") |
| 61 | + if mode == "last" and unique_pos_indices is None: |
| 62 | + raise ValueError("In last mode the unique_pos_indices argument needs to be specified") |
| 63 | + |
| 64 | + num_nodes = data.num_nodes |
| 65 | + for key, item in data: |
| 66 | + if bool(re.search("edge", key)): |
| 67 | + raise ValueError("Edges not supported. Wrong data type.") |
| 68 | + if key in skip_keys: |
| 69 | + continue |
| 70 | + |
| 71 | + if torch.is_tensor(item) and item.size(0) == num_nodes: |
| 72 | + if mode == "last" or key == "batch": #or key == SaveOriginalPosId.KEY: |
| 73 | + data[key] = item[unique_pos_indices] |
| 74 | + elif mode == "mean": |
| 75 | + is_item_bool = item.dtype == torch.bool |
| 76 | + if is_item_bool: |
| 77 | + item = item.int() |
| 78 | + if key in _INTEGER_LABEL_KEYS: |
| 79 | + item_min = item.min() |
| 80 | + item = F.one_hot(item - item_min) |
| 81 | + item = scatter_add(item, cluster, dim=0) |
| 82 | + data[key] = item.argmax(dim=-1) + item_min |
| 83 | + else: |
| 84 | + data[key] = scatter_mean(item, cluster, dim=0) |
| 85 | + if is_item_bool: |
| 86 | + data[key] = data[key].bool() |
| 87 | + return data |
| 88 | + |
| 89 | +# todo: replace these with minkowski/torchsparse impl? |
| 90 | +class GridSampling3D: |
| 91 | + """ Clusters points into voxels with size :attr:`size`. |
| 92 | + Parameters |
| 93 | + ---------- |
| 94 | + size: float |
| 95 | + Size of a voxel (in each dimension). |
| 96 | + quantize_coords: bool |
| 97 | + If True, it will convert the points into their associated sparse coordinates within the grid and store |
| 98 | + the value into a new `coords` attribute |
| 99 | + mode: string: |
| 100 | + The mode can be either `last` or `mean`. |
| 101 | + If mode is `mean`, all the points and their features within a cell will be averaged |
| 102 | + If mode is `last`, one random points per cell will be selected with its associated features |
| 103 | + """ |
| 104 | + |
| 105 | + def __init__(self, size, quantize_coords=False, mode="mean", verbose=False): |
| 106 | + self._grid_size = size |
| 107 | + self._quantize_coords = quantize_coords |
| 108 | + self._mode = mode |
| 109 | + if verbose: |
| 110 | + log.warning( |
| 111 | + "If you need to keep track of the position of your points, use SaveOriginalPosId transform before using GridSampling3D" |
| 112 | + ) |
| 113 | + |
| 114 | + if self._mode == "last": |
| 115 | + log.warning( |
| 116 | + "The tensors within data will be shuffled each time this transform is applied. Be careful that if an attribute doesn't have the size of num_points, it won't be shuffled" |
| 117 | + ) |
| 118 | + |
| 119 | + def _process(self, data): |
| 120 | + if self._mode == "last": |
| 121 | + data = shuffle_data(data) |
| 122 | + |
| 123 | + coords = torch.round((data.pos) / self._grid_size) |
| 124 | + if "batch" not in data: |
| 125 | + cluster = grid_cluster(coords, torch.tensor([1, 1, 1])) |
| 126 | + else: |
| 127 | + cluster = voxel_grid(coords, data.batch, 1) |
| 128 | + cluster, unique_pos_indices = consecutive_cluster(cluster) |
| 129 | + |
| 130 | + data = group_data(data, cluster, unique_pos_indices, mode=self._mode) |
| 131 | + if self._quantize_coords: |
| 132 | + data.coords = coords[unique_pos_indices].int() |
| 133 | + |
| 134 | + data.grid_size = torch.tensor([self._grid_size]) |
| 135 | + return data |
| 136 | + |
| 137 | + def __call__(self, data): |
| 138 | + if isinstance(data, list): |
| 139 | + data = [self._process(d) for d in data] |
| 140 | + else: |
| 141 | + data = self._process(data) |
| 142 | + return data |
| 143 | + |
| 144 | + def __repr__(self): |
| 145 | + return "{}(grid_size={}, quantize_coords={}, mode={})".format( |
| 146 | + self.__class__.__name__, self._grid_size, self._quantize_coords, self._mode |
| 147 | + ) |
0 commit comments