souging/TRELLIS_TextTo3D
0
1import torch2import torch.nn as nn3from .. import SparseTensor4 5 6class SparseConv3d(nn.Module):7 def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, bias=True, indice_key=None):8 super(SparseConv3d, self).__init__()9 if 'torchsparse' not in globals():10 import torchsparse11 self.conv = torchsparse.nn.Conv3d(in_channels, out_channels, kernel_size, stride, 0, dilation, bias)12 13 def forward(self, x: SparseTensor) -> SparseTensor:14 out = self.conv(x.data)15 new_shape = [x.shape[0], self.conv.out_channels]16 out = SparseTensor(out, shape=torch.Size(new_shape), layout=x.layout if all(s == 1 for s in self.conv.stride) else None)17 out._spatial_cache = x._spatial_cache18 out._scale = tuple([s * stride for s, stride in zip(x._scale, self.conv.stride)])19 return out20 21 22class SparseInverseConv3d(nn.Module):23 def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, bias=True, indice_key=None):24 super(SparseInverseConv3d, self).__init__()25 if 'torchsparse' not in globals():26 import torchsparse27 self.conv = torchsparse.nn.Conv3d(in_channels, out_channels, kernel_size, stride, 0, dilation, bias, transposed=True)28 29 def forward(self, x: SparseTensor) -> SparseTensor:30 out = self.conv(x.data) 31 new_shape = [x.shape[0], self.conv.out_channels]32 out = SparseTensor(out, shape=torch.Size(new_shape), layout=x.layout if all(s == 1 for s in self.conv.stride) else None)33 out._spatial_cache = x._spatial_cache34 out._scale = tuple([s // stride for s, stride in zip(x._scale, self.conv.stride)])35 return out36 37 38 39 