|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +import torch.nn.functional as F |
| 4 | +from open3d.ml.torch.layers import SparseConv, SparseConvTranspose |
| 5 | + |
| 6 | +class SparseConvFunc(torch.autograd.Function): |
| 7 | + @staticmethod |
| 8 | + def symbolic(g, cls, feat, in_pos, out_pos, voxel_size): |
| 9 | + kernel = cls.state_dict()["kernel"] |
| 10 | + offset = cls.state_dict()["offset"] |
| 11 | + kernel = g.op("Constant", value_t=kernel) |
| 12 | + offset = g.op("Constant", value_t=offset) |
| 13 | + return g.op("org.open3d::SparseConv", feat, in_pos, out_pos, kernel, offset) |
| 14 | + |
| 15 | + @staticmethod |
| 16 | + def forward(self, cls, feat, in_pos, out_pos, voxel_size): |
| 17 | + return cls.origin_forward(feat, in_pos, out_pos, voxel_size) |
| 18 | + |
| 19 | + |
| 20 | +class SparseConvONNX(SparseConv): |
| 21 | + """ |
| 22 | + This is a support class which helps export network with SparseConv in ONNX format. |
| 23 | + """ |
| 24 | + def __init__(self, *args, **kwargs): |
| 25 | + super().__init__(*args, **kwargs) |
| 26 | + self.origin_forward = super().forward |
| 27 | + |
| 28 | + def forward(self, feat, in_pos, out_pos, voxel_size): |
| 29 | + return SparseConvFunc.apply(self, feat, in_pos, out_pos, voxel_size) |
| 30 | + |
| 31 | + |
| 32 | +class SparseConvTransposeFunc(torch.autograd.Function): |
| 33 | + @staticmethod |
| 34 | + def symbolic(g, cls, feat, in_pos, out_pos, voxel_size): |
| 35 | + kernel = cls.state_dict()["kernel"] |
| 36 | + offset = cls.state_dict()["offset"] |
| 37 | + kernel = g.op("Constant", value_t=kernel) |
| 38 | + offset = g.op("Constant", value_t=offset) |
| 39 | + return g.op("org.open3d::SparseConvTranspose", feat, in_pos, out_pos, kernel, offset) |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def forward(self, cls, feat, in_pos, out_pos, voxel_size): |
| 43 | + return cls.origin_forward(feat, in_pos, out_pos, voxel_size) |
| 44 | + |
| 45 | + |
| 46 | +class SparseConvTransposeONNX(SparseConvTranspose): |
| 47 | + """ |
| 48 | + This is a support class which helps export network with SparseConvTranspose in ONNX format. |
| 49 | + """ |
| 50 | + def __init__(self, *args, **kwargs): |
| 51 | + super().__init__(*args, **kwargs) |
| 52 | + self.origin_forward = super().forward |
| 53 | + |
| 54 | + def forward(self, feat, in_pos, out_pos, voxel_size): |
| 55 | + return SparseConvTransposeFunc.apply(self, feat, in_pos, out_pos, voxel_size) |
0 commit comments