def forward(self, scores): output = scn.SparseConvNetTensor() output.metadata = scores.metadata output.spatial_size = scores.spatial_size output.features = scores.features.new().resize_(1).expand_as(scores.features).fill_(1.0) output.features = output.features * (self.softmax(scores.features)[:, 1] > self.threshold).float()[:, None] return output
def forward(self, x, y): output = scn.SparseConvNetTensor() output.metadata = x.metadata output.spatial_size = x.spatial_size attention = y if torch.is_tensor(y) else y.features[:, 1][:, None] output.features = x.features * attention return output
def forward(self, x, y): output = scn.SparseConvNetTensor() output.metadata = x.metadata output.spatial_size = x.spatial_size output.features = x.features.new().resize_(1).expand_as(x.features).fill_(0.0) output.features = x.features * y.features[:, 1][:, None] return output
def forward(self, a, b): # assert input.features.shape[-1] == vecter.shape[-1] point_idx_a = a.get_spatial_locations()[:, -1] point_idx_b = b.get_spatial_locations()[:, -1] max_idx = torch.max(point_idx_a).max().item() coordinate_a = a.get_spatial_locations()[:, :-1] coordinate_b = b.get_spatial_locations()[:, :-1] output = scn.SparseConvNetTensor() output.metadata = a.metadata output.spatial_size = a.spatial_size tmp = [] for i in range(max_idx + 1): if self.topk > coordinate_b[point_idx_b == i].size()[0]: topk = coordinate_b[point_idx_b == i].size()[0] else: topk = self.topk dist_c = self.pairwise_distances_l2( coordinate_a[point_idx_a == i].type(torch.cuda.FloatTensor), coordinate_b[point_idx_b == i].type(torch.cuda.FloatTensor), ) dist = dist_c tmp_f = [] # dist_w, idx_pick = torch.topk(dist, topk)# idx_pick = torch.argsort(dist, 1)[:,:topk] dist_w = torch.gather(dist_c,1,idx_pick) dist_w = (self.r - torch.clamp(dist_w, 0., self.r)) tmp_b = b.features[point_idx_b == i][idx_pick.reshape(-1)].reshape(-1,topk,b.features.size()[-1]) *dist_w.view(-1,topk,1) tmp_b = torch.sum(tmp_b,1) tmp.append(tmp_b) tmp = torch.cat(tmp, 0) output.features = torch.cat([a.features, tmp.cuda()], 1) return output
def forward(self, input, vecter): assert input.features.shape[-1] == vecter.shape[-1] point_idx = input.get_spatial_locations()[:, -1] output = scn.SparseConvNetTensor() output.metadata = input.metadata output.spatial_size = input.spatial_size output.features = input.features * torch.sigmoid(vecter[point_idx]) * 2 return output
def forward(self, ghost_mask, coords, feature_map, factor=0.0): """ ghost_mask = 1 for points to be kept (nonghost) ghost_mask = 0 for ghost points output has same shape/locations as feature_map """ #print(ghost_mask.shape, coords.shape, feature_map.features.size()) #print((ghost_mask==0).sum(), float(len(ghost_mask)), (ghost_mask==1).sum()/float(len(ghost_mask))) output = scn.SparseConvNetTensor() output.metadata = feature_map.metadata output.spatial_size = feature_map.spatial_size # First downsample the ghost mask # scale_coords, unique_indices = np.unique(np.concatenate([np.floor(coords[:, :-1]/2**factor), coords[:, -1:]], axis=1), axis=0, return_index=True) # perm2 = np.lexsort((scale_coords[:, 0], scale_coords[:, 1], scale_coords[:, 2], scale_coords[:, 3])) # scale_ghost_mask = ghost_mask[unique_indices][perm2] # Append to each coordinate its value in ghost mask coords = np.concatenate([coords, ghost_mask[:, None].cpu().numpy()], axis=1) # Downsample the spatial coordinates, preserving batch id and ghost mask value scale_coords, unique_indices = np.unique(np.concatenate([ np.floor(coords[:, :self._data_dim] / 2**factor), coords[:, self._data_dim:] ], axis=1), axis=0, return_index=True) # Re-order: put nonghost points first keep = np.concatenate([ np.where(scale_coords[:, -1] == 1)[0], np.where(scale_coords[:, -1] == 0)[0] ], axis=0) # Eliminate duplicates. This will keep the first occurrence of each position. # Hence if it contains at least one nonghost point, it is kept. scale_coords2, unique_indices2 = np.unique( scale_coords[keep][:, :self._data_dim + 1], axis=0, return_index=True) # Now do lexsort to match with ppn1_coords below. perm2 = np.lexsort((scale_coords2[:, 0], scale_coords2[:, 1], scale_coords2[:, 2], scale_coords2[:, 3])) # Combine everything for a new ghost mask. scale_ghost_mask = ghost_mask[unique_indices][keep][unique_indices2][ perm2] # Now order the feature map and multiply with ghost mask ppn1_coords = feature_map.get_spatial_locations() perm = np.lexsort((ppn1_coords[:, 0], ppn1_coords[:, 1], ppn1_coords[:, 2], ppn1_coords[:, 3])) # Reverse permutation inv_perm = np.argsort(perm) new_ghost_mask = scale_ghost_mask[:, None][inv_perm].float() output.features = feature_map.features * new_ghost_mask return output, new_ghost_mask
def create_sparse_tensor(spatial_size, coords, features): dimension = spatial_size.ndim metadata = scn.Metadata(dimension) sparse_features = scn.ioLayers.InputLayerFunction.apply( dimension, metadata, spatial_size, coords, features, 0, 4) return scn.SparseConvNetTensor(features=sparse_features, metadata=metadata, spatial_size=spatial_size)
def forward(self, input, vecter): assert input.features.shape[-1] == vecter.shape[-1] point_idx = input.get_spatial_locations()[:, -1] output = scn.SparseConvNetTensor() output.metadata = input.metadata output.spatial_size = input.spatial_size vecter = torch.softmax(vecter, 1) output.features = input.features * vecter[ point_idx] #torch.softmax(vecter[point_idx],1) return output
def combine(new_coords, new_features, spatial_size, batch_size=0): ndim = len(spatial_size) metadata = scn.Metadata(ndim) features = scn.ioLayers.InputLayerFunction.apply( ndim, metadata, spatial_size, new_coords, new_features, batch_size, 4) return scn.SparseConvNetTensor(features=features, metadata=metadata, spatial_size=spatial_size)
def forward(self, attention, label): output = scn.SparseConvNetTensor() output.metadata = attention.metadata output.spatial_size = attention.spatial_size output.features = attention.features.new().resize_(1).expand_as(attention.features).fill_(1.0) output.features = output.features * attention.features positions = attention.get_spatial_locations().cuda() # print(positions.max(), label.max()) for l in label: index = (positions == l).all(dim=1) output.features[index] = 1.0 return output
def forward(self, x): in0 = self.input0(x) # coors = coors.int()[:,[3,2,1,0]] # ordering is [batch, z, y, z] coors = in0.get_spatial_locations().int()[:, [3, 2, 1, 0]] coord.cuda() ret = spconv.SparseConvTensor(in0.features, coors, dense_shape, data.batch_size) x = self.sparseModel(ret) temp0 = scn.SparseConvNetTensor(x.features, in0.metadata) x = self.out0(temp0) x = self.linear(x) return x
def forward(self, input): a = input[0] b = input[1] output = scn.SparseConvNetTensor() output.metadata = a.metadata output.spatial_size = a.spatial_size axyz = a.get_spatial_locations() y = axyz[:,0] x = axyz[:,1] z = axyz[:,2] output.features = a.features + b[z,:,y,x] return output
def forward(self, coords, features, spatial_size, batch_size=0): spatial_size = torch.as_tensor(spatial_size, dtype=torch.long) dimension = len(spatial_size) metadata = scn.Metadata(dimension) if len(coords): sparse_features = scn.ioLayers.InputLayerFunction.apply( dimension, metadata, spatial_size, coords.long(), features, batch_size, self.mode) return scn.SparseConvNetTensor(metadata=metadata, spatial_size=spatial_size, features=sparse_features) else: return None
def forward(self, input): output = scn.SparseConvNetTensor() i = input.features.data if self.training: m = i.new().resize_(1).expand(1, i.shape[1]).fill_(1 - self.p) output.features = Variable( i * torch.bernoulli(m), requires_grad=input.features.requires_grad) else: output.features = Variable( i * (1 - self.p), requires_grad=input.features.requires_grad) output.metadata = input.metadata output.spatial_size = input.spatial_size return output