From 4b1fc50cae96a9716d1a84e83686121f70ed1406 Mon Sep 17 00:00:00 2001 From: wangxiaomeng Date: Mon, 4 Nov 2024 13:59:01 +0800 Subject: [PATCH] format code --- .pre-commit-config.yaml | 22 +++--- README.md | 4 ++ docs/contributing.md | 69 +++++++++++++++++++ scripts/utils/eval_ate.py | 2 +- scripts/utils/viz_utils.py | 1 + slam/algorithms/base_algorithm.py | 1 + slam/algorithms/coslam.py | 1 + slam/algorithms/dpvo.py | 8 +-- slam/algorithms/neural_recon.py | 1 + slam/algorithms/voxfusion.py | 5 +- slam/common/datasets.py | 16 +++-- slam/common/frame.py | 1 + slam/common/mesher.py | 1 + slam/configs/base_config.py | 1 + slam/configs/input_config.py | 7 +- slam/engine/optimizers.py | 1 + slam/engine/schedulers.py | 4 +- slam/model_components/blocks_dpvo.py | 9 +++ slam/model_components/decoder_coslam.py | 4 ++ slam/model_components/decoder_nice.py | 11 ++- slam/model_components/decoder_pointslam.py | 18 +++-- slam/model_components/decoder_voxfusion.py | 9 ++- slam/model_components/extractor_dpvo.py | 4 ++ slam/model_components/feature_grid_nice.py | 1 + .../gaussian_cloud_splatam.py | 1 + slam/model_components/neural_point_cloud.py | 1 + .../neural_recon_components/mesh_renderer.py | 1 + .../models/backbone.py | 1 + .../models/gru_fusion.py | 7 +- .../neural_recon_components/models/modules.py | 6 ++ .../models/neucon_network.py | 15 ++-- .../models/neuralrecon.py | 1 + slam/model_components/projective_ops_dpvo.py | 8 +-- .../model_components/slam_external_splatam.py | 18 ++--- slam/model_components/utils.py | 6 +- slam/model_components/utils_dpvo.py | 9 +-- slam/model_components/vonet_dpvo.py | 8 ++- .../voxel_helpers_voxfusion.py | 6 ++ slam/models/conv_onet.py | 3 +- slam/models/conv_onet_pointslam.py | 3 +- slam/models/sparse_voxel.py | 2 +- slam/pipeline/mapper.py | 1 + slam/pipeline/tracker.py | 1 + slam/pipeline/visualizer.py | 1 + slam/pipeline/xrdslam.py | 1 + slam/utils/opt_pose.py | 1 + 46 files changed, 237 insertions(+), 65 deletions(-) create mode 100644 docs/contributing.md diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 165aa02..9a64b4d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ exclude: '^slam/model_components/slam_helpers_splatam\.py$|^scripts/download_dat repos: - repo: https://github.com/pycqa/flake8.git - rev: 3.8.3 + rev: 7.1.1 hooks: - id: flake8 @@ -10,12 +10,15 @@ repos: rev: 5.10.1 hooks: - id: isort + - repo: https://github.com/pre-commit/mirrors-yapf - rev: v0.30.0 + rev: v0.32.0 hooks: - id: yapf + additional_dependencies: [toml] + - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.1.0 + rev: v5.0.0 hooks: - id: trailing-whitespace - id: check-yaml @@ -27,13 +30,16 @@ repos: args: ["--remove"] - id: mixed-line-ending args: ["--fix=lf"] - - repo: https://github.com/myint/docformatter - rev: v1.3.1 + + - repo: https://github.com/PyCQA/docformatter.git + rev: v1.7.5 hooks: - id: docformatter - args: ["--in-place", "--wrap-descriptions", "79"] + additional_dependencies: [tomli] + args: ["--in-place", "--config", "./pyproject.toml"] + - repo: https://github.com/codespell-project/codespell - rev: v2.1.0 + rev: v2.3.0 hooks: - id: codespell - args: ["--skip", "*.ipynb,tools/data/hvu/label_map.json", "-L", "te,nd,thre,Gool,gool"] + args: ["--skip", "*.ipynb,tools/data/hvu/label_map.json", "-L", "te,nd,thre,Gool,gool,CoFusion,cofusion"] diff --git a/README.md b/README.md index 696959f..11b3475 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,10 @@ Note: The default configuration in the algorithm is suitable for Replica. If you The license for our codebase is under the [Apache-2.0](LICENSE). Please note that this license only applies to the code in our library, the dependencies of which are separate and individually licensed. In the source code files, we have made specific licensing declarations for the third-party code being used. We would like to pay tribute to open-source implementations to which we rely on. Please be aware that utilizing both external dependencies and the fundamental code from their original sources might impact our codebase's licensing. +## Contributing + +We appreciate all contributions to improve XRDSLAM. Please refer to [contributing.md](docs/contributing.md) for the contributing guideline. + ## Acknowledgement In addition to the implemented algorithm ([NICE-SLAM](https://github.com/cvg/nice-slam),[Co-SLAM](https://github.com/HengyiWang/Co-SLAM),[Vox-Fusion](https://github.com/zju3dv/Vox-Fusion),[Point-SLAM](https://github.com/eriksandstroem/Point-SLAM),[SplaTAM](https://github.com/spla-tam/SplaTAM), [DPVO](https://github.com/princeton-vl/DPVO), [NeuralRecon](https://github.com/zju3dv/NeuralRecon)), our code also adapt some codes from [Nerfstudio](https://github.com/nerfstudio-project/nerfstudio/), [Sdfstudio](https://autonomousvision.github.io/sdfstudio/). Thanks for making the code available. diff --git a/docs/contributing.md b/docs/contributing.md new file mode 100644 index 0000000..c41a7a8 --- /dev/null +++ b/docs/contributing.md @@ -0,0 +1,69 @@ +# Contributing to XRDSLAM + +All kinds of contributions are welcome, including but not limited to the following. + +- Fixes (typo, bugs) +- New features and components + +## Workflow + +1. Fork and pull the latest xrdslam +1. Checkout a new branch with a meaningful name (do not use master branch for PRs) +1. Commit your changes +1. Create a PR + +```{note} +- If you plan to add some new features that involve large changes, it is encouraged to open an issue for discussion first. +- If you are the author of some papers and would like to include your method to xrnerf, please contact us. We will much appreciate your contribution. +``` + +## Code style + +### Python + +We adopt [PEP8](https://www.python.org/dev/peps/pep-0008/) as the preferred code style. + +We use the following tools for linting and formatting: + +- [flake8](http://flake8.pycqa.org/en/latest/): linter +- [yapf](https://github.com/google/yapf): formatter +- [isort](https://github.com/timothycrosley/isort): sort imports + +Style configurations of yapf and isort can be found in [setup.cfg](../setup.cfg). + +We use [pre-commit hook](https://pre-commit.com/) that checks and formats for `flake8`, `yapf`, `isort`, `trailing whitespaces`, +fixes `end-of-files`, sorts `requirements.txt` automatically on every commit. +The config for a pre-commit hook is stored in [.pre-commit-config](../.pre-commit-config.yaml). + +After you clone the repository, you will need to install initialize pre-commit hook. + +``` +pip install pre-commit==3.8.0 +``` + +From the repository folder + +``` +pre-commit install +``` + +If you are facing an issue when installing markdown lint, you may install ruby for markdown lint by +referring to [this repo](https://github.com/innerlee/setup) by following the usage and taking [`zzruby.sh`](https://github.com/innerlee/setup/blob/master/zzruby.sh) + + or by the following steps + + ```shell +# install rvm +curl -L https://get.rvm.io | bash -s -- --autolibs=read-fail +rvm autolibs disable + # install ruby +rvm install 2.7.1 + ``` + +After this on every commit check code linters and formatter will be enforced. + +> Before you create a PR, make sure that your code lints and is formatted by yapf. + +### C++ and CUDA + +We follow the [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html). diff --git a/scripts/utils/eval_ate.py b/scripts/utils/eval_ate.py index e9d416c..a5484fa 100644 --- a/scripts/utils/eval_ate.py +++ b/scripts/utils/eval_ate.py @@ -11,7 +11,7 @@ def get_tensor_from_camera(RT, Tquad=False): """Convert transformation matrix to quaternion and translation.""" gpu_id = -1 - if type(RT) == torch.Tensor: + if isinstance(RT, torch.Tensor): if RT.get_device() != -1: RT = RT.detach().cpu() gpu_id = RT.get_device() diff --git a/scripts/utils/viz_utils.py b/scripts/utils/viz_utils.py index 3136170..6c353fd 100644 --- a/scripts/utils/viz_utils.py +++ b/scripts/utils/viz_utils.py @@ -186,6 +186,7 @@ def animation_callback(vis): class SLAMFrontend: + def __init__(self, output, init_pose, diff --git a/slam/algorithms/base_algorithm.py b/slam/algorithms/base_algorithm.py index 37f3806..0b95b9e 100644 --- a/slam/algorithms/base_algorithm.py +++ b/slam/algorithms/base_algorithm.py @@ -42,6 +42,7 @@ class AlgorithmConfig(InstantiateConfig): class Algorithm(): + def __init__(self, config: AlgorithmConfig, camera: Camera, device: str) -> None: self.config = config diff --git a/slam/algorithms/coslam.py b/slam/algorithms/coslam.py index 8c4a47b..4ee9595 100644 --- a/slam/algorithms/coslam.py +++ b/slam/algorithms/coslam.py @@ -39,6 +39,7 @@ class CoSLAMConfig(AlgorithmConfig): class CoSLAM(Algorithm): + def __init__(self, config: CoSLAMConfig, camera: Camera, device: str) -> None: super().__init__(config, camera, device) diff --git a/slam/algorithms/dpvo.py b/slam/algorithms/dpvo.py index 54f41f1..bb91745 100644 --- a/slam/algorithms/dpvo.py +++ b/slam/algorithms/dpvo.py @@ -212,7 +212,7 @@ def gmap(self): def corr(self, coords, indices=None): """This function is from DPVO, licensed under the MIT License.""" - """local correlation volume.""" + """Local correlation volume.""" ii, jj = indices if indices is not None else (self.kk, self.jj) ii1 = ii % (self.M * self.mem) jj1 = jj % (self.mem) @@ -224,7 +224,7 @@ def corr(self, coords, indices=None): def reproject(self, indices=None): """This function is from DPVO, licensed under the MIT License.""" - """reproject patch k from i -> j.""" + """Reproject patch k from i -> j.""" (ii, jj, kk) = indices if indices is not None else (self.ii, self.jj, self.kk) coords = pops.transform(SE3(self.poses), self.patches, self.intrinsics, @@ -249,7 +249,7 @@ def remove_factors(self, m): def motion_probe(self): """This function is from DPVO, licensed under the MIT License.""" - """kinda hacky way to ensure enough motion for initialization.""" + """Kinda hacky way to ensure enough motion for initialization.""" kk = torch.arange(self.m - self.M, self.m, device='cuda') jj = self.n * torch.ones_like(kk) ii = self.ix[kk] @@ -383,7 +383,7 @@ def get_pose(self, t): def get_all_poses(self): """This function is from DPVO, licensed under the MIT License.""" - """interpolate missing poses.""" + """Interpolate missing poses.""" self.traj = {} for i in range(self.n): self.traj[self.tstamps_[i].item()] = self.poses_[i] diff --git a/slam/algorithms/neural_recon.py b/slam/algorithms/neural_recon.py index d7f6598..995a652 100644 --- a/slam/algorithms/neural_recon.py +++ b/slam/algorithms/neural_recon.py @@ -36,6 +36,7 @@ class NeuralReconConfig(AlgorithmConfig): class NeuralRecon(Algorithm): + def __init__(self, config: NeuralReconConfig, camera: Camera, device: str) -> None: super().__init__(config, camera, device) diff --git a/slam/algorithms/voxfusion.py b/slam/algorithms/voxfusion.py index 675bfc5..30f74fc 100644 --- a/slam/algorithms/voxfusion.py +++ b/slam/algorithms/voxfusion.py @@ -28,6 +28,7 @@ class VoxFusionConfig(AlgorithmConfig): class VoxFusion(Algorithm): + def __init__(self, config: VoxFusionConfig, camera: Camera, device: str) -> None: super().__init__(config, camera, device) @@ -202,9 +203,7 @@ def extract_mesh(self, res=8, clean_mesh=False, require_color=False): verts, faces = self.marching_cubes( voxel_centres, sdf_grid) # [N_verts, 3], [N_face, 3 (3 ids of verts)] - ''' - # TODO: clean mesh need depth and pose, use key frame info - ''' + """# TODO: clean mesh need depth and pose, use key frame info""" colors = None if require_color: verts_torch = torch.from_numpy(verts).float().cuda() diff --git a/slam/common/datasets.py b/slam/common/datasets.py index b340fcf..1bb4a54 100644 --- a/slam/common/datasets.py +++ b/slam/common/datasets.py @@ -58,6 +58,7 @@ def get_dataset(data_path, data_type, device='cuda:0'): # RGBD dataset class BaseDataset(Dataset): + def __init__(self, data_path, device='cuda:0'): super(BaseDataset, self).__init__() self.data_format = 'RGBD' @@ -138,6 +139,7 @@ def get_camera(self): class Replica(BaseDataset): + def __init__(self, data_path, device='cuda:0'): super(Replica, self).__init__(data_path, device) self.color_paths = sorted( @@ -168,6 +170,7 @@ def load_poses(self, path): # mono_imu dataset class Euroc(Dataset): + def __init__(self, data_path, device='cuda:0'): self.data_format = 'MonoImu' @@ -334,6 +337,7 @@ def load_poses(self, path): class Azure(BaseDataset): + def __init__(self, data_path, device='cuda:0'): super(Azure, self).__init__(data_path, device) self.color_paths = sorted( @@ -371,6 +375,7 @@ def load_poses(self, path): class ScanNet(BaseDataset): + def __init__(self, data_path, device='cuda:0'): super(ScanNet, self).__init__(data_path, device) self.input_folder = os.path.join(self.input_folder, 'frames') @@ -402,6 +407,7 @@ def load_poses(self, path): class Scenes7(BaseDataset): + def __init__(self, data_path, device='cuda:0'): super(Scenes7, self).__init__(data_path, device) self.color_paths = sorted( @@ -436,6 +442,7 @@ def load_poses(self, path): class CoFusion(BaseDataset): + def __init__(self, data_path, device='cuda:0'): super(CoFusion, self).__init__(data_path, device) self.input_folder = os.path.join(data_path) @@ -459,6 +466,7 @@ def load_poses(self, path): class TUM_RGBD(BaseDataset): + def __init__(self, data_path, device='cuda:0'): super(TUM_RGBD, self).__init__(data_path, device) self.color_paths, self.depth_paths, self.poses = self.loadtum( @@ -466,7 +474,7 @@ def __init__(self, data_path, device='cuda:0'): self.n_img = len(self.color_paths) def parse_list(self, filepath, skiprows=0): - """read list data.""" + """Read list data.""" data = np.loadtxt(filepath, delimiter=' ', dtype=np.unicode_, @@ -478,7 +486,7 @@ def associate_frames(self, tstamp_depth, tstamp_pose, max_dt=0.08): - """pair images, depths, and poses.""" + """Pair images, depths, and poses.""" associations = [] for i, t in enumerate(tstamp_image): if tstamp_pose is None: @@ -497,7 +505,7 @@ def associate_frames(self, return associations def loadtum(self, datapath, frame_rate=-1): - """read video data in tum-rgbd format.""" + """Read video data in tum-rgbd format.""" if os.path.isfile(os.path.join(datapath, 'groundtruth.txt')): pose_list = os.path.join(datapath, 'groundtruth.txt') elif os.path.isfile(os.path.join(datapath, 'pose.txt')): @@ -544,7 +552,7 @@ def loadtum(self, datapath, frame_rate=-1): return images, depths, poses def pose_matrix_from_quaternion(self, pvec): - """convert 4x4 pose matrix to (t, q)""" + """Convert 4x4 pose matrix to (t, q)""" from scipy.spatial.transform import Rotation pose = np.eye(4) diff --git a/slam/common/frame.py b/slam/common/frame.py index 9989505..7b18172 100644 --- a/slam/common/frame.py +++ b/slam/common/frame.py @@ -8,6 +8,7 @@ class Frame(nn.Module): + def __init__(self, fid, rgb, diff --git a/slam/common/mesher.py b/slam/common/mesher.py index f22f296..e9fda16 100644 --- a/slam/common/mesher.py +++ b/slam/common/mesher.py @@ -30,6 +30,7 @@ class MesherConfig(InstantiateConfig): class Mesher(): + def __init__(self, config: MesherConfig, camera: Camera, bounding_box, marching_cubes_bound) -> None: self.config = config diff --git a/slam/configs/base_config.py b/slam/configs/base_config.py index 5d3c0e9..19404db 100644 --- a/slam/configs/base_config.py +++ b/slam/configs/base_config.py @@ -11,6 +11,7 @@ # Pretty printing class class PrintableConfig: # pylint: disable=too-few-public-methods """Printable Config defining str function.""" + def __str__(self): lines = [self.__class__.__name__ + ':'] for key, val in vars(self).items(): diff --git a/slam/configs/input_config.py b/slam/configs/input_config.py index 5df7568..3360bab 100644 --- a/slam/configs/input_config.py +++ b/slam/configs/input_config.py @@ -497,5 +497,8 @@ tyro.conf.FlagConversionOff[tyro.extras.subcommand_type_from_defaults( defaults=algorithm_configs, descriptions=descriptions)]] """Union[] type over config types, annotated with default instances for use -with tyro.cli(). Allows the user to pick between one of several base -configurations, and then override values in it.""" +with tyro.cli(). + +Allows the user to pick between one of several base configurations, and +then override values in it. +""" diff --git a/slam/engine/optimizers.py b/slam/engine/optimizers.py index 06f0de3..8261abf 100644 --- a/slam/engine/optimizers.py +++ b/slam/engine/optimizers.py @@ -67,6 +67,7 @@ class Optimizers: config: The optimizer configuration object. param_groups: A dictionary of parameter groups to optimize. """ + def __init__(self, config: Dict[str, Any] = None, param_groups: Dict[str, List[Parameter]] = None, diff --git a/slam/engine/schedulers.py b/slam/engine/schedulers.py index 1d8baf5..5719c4f 100644 --- a/slam/engine/schedulers.py +++ b/slam/engine/schedulers.py @@ -20,7 +20,7 @@ class SchedulerConfig(InstantiateConfig): """Basic scheduler config.""" _target: Type = field(default_factory=lambda: Scheduler) - """target class to instantiate""" + """Target class to instantiate.""" class Scheduler: @@ -70,6 +70,7 @@ class NiceSLAMScheduler(Scheduler): def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> LRScheduler: + def func(step): if self.config.coarse: learning_factor = self.config.stage_lr.coarse @@ -101,6 +102,7 @@ class PointSLAMScheduler(Scheduler): def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> LRScheduler: + def func(step): if step <= self.config.max_steps * self.config.geo_iter_ratio: learning_factor = self.config.start_lr diff --git a/slam/model_components/blocks_dpvo.py b/slam/model_components/blocks_dpvo.py index e4e66c3..7f880d2 100644 --- a/slam/model_components/blocks_dpvo.py +++ b/slam/model_components/blocks_dpvo.py @@ -7,6 +7,7 @@ class LayerNorm1D(nn.Module): + def __init__(self, dim): super(LayerNorm1D, self).__init__() self.norm = nn.LayerNorm(dim, eps=1e-4) @@ -16,6 +17,7 @@ def forward(self, x): class GatedResidual(nn.Module): + def __init__(self, dim): super().__init__() @@ -29,6 +31,7 @@ def forward(self, x): class SoftAgg(nn.Module): + def __init__(self, dim=512, expand=True): super(SoftAgg, self).__init__() self.dim = dim @@ -50,6 +53,7 @@ def forward(self, x, ix): class SoftAggBasic(nn.Module): + def __init__(self, dim=512, expand=True): super(SoftAggBasic, self).__init__() self.dim = dim @@ -75,6 +79,7 @@ def forward(self, x, ix): class GradClip(torch.autograd.Function): + @staticmethod def forward(ctx, x): return x @@ -87,6 +92,7 @@ def backward(ctx, grad_x): class GradientClip(nn.Module): + def __init__(self): super(GradientClip, self).__init__() @@ -95,6 +101,7 @@ def forward(self, x): class GradZero(torch.autograd.Function): + @staticmethod def forward(ctx, x): return x @@ -109,6 +116,7 @@ def backward(ctx, grad_x): class GradientZero(nn.Module): + def __init__(self): super(GradientZero, self).__init__() @@ -117,6 +125,7 @@ def forward(self, x): class GradMag(torch.autograd.Function): + @staticmethod def forward(ctx, x): return x diff --git a/slam/model_components/decoder_coslam.py b/slam/model_components/decoder_coslam.py index ee36ab7..bd4f857 100644 --- a/slam/model_components/decoder_coslam.py +++ b/slam/model_components/decoder_coslam.py @@ -7,6 +7,7 @@ class ColorNet(nn.Module): + def __init__(self, config, input_ch=4): super(ColorNet, self).__init__() self.input_ch = input_ch @@ -57,6 +58,7 @@ def get_model(self, tcnn_network=False): class SDFNet(nn.Module): + def __init__(self, config, input_ch=3): super(SDFNet, self).__init__() self.input_ch = input_ch @@ -113,6 +115,7 @@ def get_model(self, tcnn_network=False): class ColorSDFNet(nn.Module): """Color grid + SDF grid.""" + def __init__(self, config, input_ch=3, input_ch_pos=12): super(ColorSDFNet, self).__init__() self.color_net = ColorNet(config, input_ch=input_ch + input_ch_pos) @@ -138,6 +141,7 @@ def forward(self, embed, embed_pos, embed_color): class ColorSDFNet_v2(nn.Module): """No color grid.""" + def __init__(self, config, input_ch=3, input_ch_pos=12): super(ColorSDFNet_v2, self).__init__() self.color_net = ColorNet(config, input_ch=input_ch_pos) diff --git a/slam/model_components/decoder_nice.py b/slam/model_components/decoder_nice.py index ac49476..8077bc0 100755 --- a/slam/model_components/decoder_nice.py +++ b/slam/model_components/decoder_nice.py @@ -14,9 +14,10 @@ class GaussianFourierFeatureTransform(torch.nn.Module): "Fourier Features Let Networks Learn High Frequency Functions in Low Dimensional Domains": - https://arxiv.org/abs/2006.10739 - https://people.eecs.berkeley.edu/~bmild/fourfeat/index.html + https://arxiv.org/abs/2006.10739 + https://people.eecs.berkeley.edu/~bmild/fourfeat/index.html """ + def __init__(self, num_input_channels, mapping_size=93, @@ -40,6 +41,7 @@ def forward(self, x): class Nerf_positional_embedding(torch.nn.Module): """Nerf positional embedding.""" + def __init__(self, multires, log_sampling=True): super().__init__() self.log_sampling = log_sampling @@ -74,6 +76,7 @@ def forward(self, x): class DenseLayer(nn.Linear): + def __init__(self, in_dim: int, out_dim: int, @@ -92,6 +95,7 @@ def reset_parameters(self) -> None: class Same(nn.Module): + def __init__(self): super().__init__() @@ -119,6 +123,7 @@ class MLP(nn.Module): concat_feature (bool): whether to get feature from middle level and concat to the current feature. """ + def __init__(self, name='', dim=3, @@ -250,6 +255,7 @@ class MLP_no_xyz(nn.Module): skips (list): list of layers to have skip connection. grid_len (float): voxel length of its corresponding feature grid. """ + def __init__(self, name='', dim=3, @@ -334,6 +340,7 @@ class NICE(nn.Module): coarse (bool): whether or not to use coarse level. pos_embedding_method (str): positional embedding method. """ + def __init__(self, dim=3, c_dim=32, diff --git a/slam/model_components/decoder_pointslam.py b/slam/model_components/decoder_pointslam.py index 2f204da..61c375c 100644 --- a/slam/model_components/decoder_pointslam.py +++ b/slam/model_components/decoder_pointslam.py @@ -13,11 +13,12 @@ class GaussianFourierFeatureTransform(torch.nn.Module): """Modified based on the implementation of Gaussian Fourier feature mapping. - "Fourier Features Let Networks Learn High Frequency Functions - in Low Dimensional Domains": - https://arxiv.org/abs/2006.10739 - https://people.eecs.berkeley.edu/~bmild/fourfeat/index.html + "Fourier Features Let Networks Learn High Frequency Functions in Low + Dimensional Domains": + https://arxiv.org/abs/2006.10739 + https://people.eecs.berkeley.edu/~bmild/fourfeat/index.html """ + def __init__(self, num_input_channels, mapping_size=93, @@ -47,6 +48,7 @@ def forward(self, x): class DenseLayer(nn.Linear): + def __init__(self, in_dim: int, out_dim: int, @@ -65,6 +67,7 @@ def reset_parameters(self) -> None: class Same(nn.Module): + def __init__(self, mapping_size=3): super().__init__() self.mapping_size = mapping_size @@ -91,6 +94,7 @@ class MLP_geometry(nn.Module): middle level and concat to the current feature. use_view_direction (bool): whether to use view direction or not. """ + def __init__(self, use_dynamic_radius, pointcloud_nn_weighting, @@ -215,7 +219,7 @@ def forward(self, is_tracker=False, pts_views_d=None, dynamic_r_query=None): - """forward method of geometric decoder. + """Forward method of geometric decoder. Args: p (tensor): sampling locations, N*3 @@ -326,6 +330,7 @@ class MLP_color(nn.Module): level and concat to the current feature. use_view_direction (bool): whether to use view direction. """ + def __init__(self, use_dynamic_radius, pointcloud_nn_weighting, @@ -473,7 +478,7 @@ def forward(self, pts_views_d=None, dynamic_r_query=None, exposure_feat=None): - """forward method of decoder. + """Forward method of decoder. Args: p (tensor): sampling locations, N*3 @@ -551,6 +556,7 @@ class POINT(nn.Module): pos_embedding_method (str): positional embedding method. use_view_direction (bool): use view direction or not. """ + def __init__(self, use_dynamic_radius, pointcloud_nn_weighting, diff --git a/slam/model_components/decoder_voxfusion.py b/slam/model_components/decoder_voxfusion.py index 8915ab3..4698c2f 100644 --- a/slam/model_components/decoder_voxfusion.py +++ b/slam/model_components/decoder_voxfusion.py @@ -12,9 +12,10 @@ class GaussianFourierFeatureTransform(torch.nn.Module): "Fourier Features Let Networks Learn High Frequency Functions in Low Dimensional Domains": - https://arxiv.org/abs/2006.10739 - https://people.eecs.berkeley.edu/~bmild/fourfeat/index.html + https://arxiv.org/abs/2006.10739 + https://people.eecs.berkeley.edu/~bmild/fourfeat/index.html """ + def __init__(self, num_input_channels, mapping_size=93, @@ -39,6 +40,7 @@ def forward(self, x): class Nerf_positional_embedding(torch.nn.Module): """Nerf positional embedding.""" + def __init__(self, in_dim, multires, log_sampling=True): super().__init__() self.log_sampling = log_sampling @@ -74,6 +76,7 @@ def forward(self, x): class Same(nn.Module): + def __init__(self, in_dim): super().__init__() self.embedding_size = in_dim @@ -83,6 +86,7 @@ def forward(self, x): class Decoder(nn.Module): + def __init__(self, depth=8, width=256, @@ -150,6 +154,7 @@ def forward(self, inputs): class Embeddings(nn.Module): + def __init__(self, num_embeddings, embed_dim): super().__init__() diff --git a/slam/model_components/extractor_dpvo.py b/slam/model_components/extractor_dpvo.py index ca7876f..2c40796 100644 --- a/slam/model_components/extractor_dpvo.py +++ b/slam/model_components/extractor_dpvo.py @@ -5,6 +5,7 @@ class ResidualBlock(nn.Module): + def __init__(self, in_planes, planes, norm_fn='group', stride=1): super(ResidualBlock, self).__init__() @@ -65,6 +66,7 @@ def forward(self, x): class BottleneckBlock(nn.Module): + def __init__(self, in_planes, planes, norm_fn='group', stride=1): super(BottleneckBlock, self).__init__() @@ -138,6 +140,7 @@ def forward(self, x): class BasicEncoder(nn.Module): + def __init__(self, output_dim=128, norm_fn='batch', @@ -231,6 +234,7 @@ def forward(self, x): class BasicEncoder4(nn.Module): + def __init__(self, output_dim=128, norm_fn='batch', diff --git a/slam/model_components/feature_grid_nice.py b/slam/model_components/feature_grid_nice.py index bb29058..f46bf39 100644 --- a/slam/model_components/feature_grid_nice.py +++ b/slam/model_components/feature_grid_nice.py @@ -2,6 +2,7 @@ class FeatureGrid(): + def __init__(self, xyz_len, grid_len, c_dim, std=0.01): super(FeatureGrid, self).__init__() diff --git a/slam/model_components/gaussian_cloud_splatam.py b/slam/model_components/gaussian_cloud_splatam.py index 9db45c0..476c61c 100644 --- a/slam/model_components/gaussian_cloud_splatam.py +++ b/slam/model_components/gaussian_cloud_splatam.py @@ -19,6 +19,7 @@ def inverse_sigmoid(x): class GaussianCloud(nn.Module): + def __init__(self, init_rgb, init_depth, w2c, camera, prune_dict, densify_dict): super(GaussianCloud, self).__init__() diff --git a/slam/model_components/neural_point_cloud.py b/slam/model_components/neural_point_cloud.py index 4b30bfe..9e3e6ba 100644 --- a/slam/model_components/neural_point_cloud.py +++ b/slam/model_components/neural_point_cloud.py @@ -10,6 +10,7 @@ class NeuralPointCloud(nn.Module): + def __init__(self, c_dim, nn_num, radius_add, cuda_id, radius_min, radius_query, fix_interval_when_add_along_ray, use_dynamic_radius, N_surface, N_add, near_end_surface, diff --git a/slam/model_components/neural_recon_components/mesh_renderer.py b/slam/model_components/neural_recon_components/mesh_renderer.py index 005f154..78372d2 100644 --- a/slam/model_components/neural_recon_components/mesh_renderer.py +++ b/slam/model_components/neural_recon_components/mesh_renderer.py @@ -14,6 +14,7 @@ class Renderer(): Used to render depthmaps from a mesh for 2d evaluation """ + def __init__(self, height=480, width=640): self.renderer = pyrender.OffscreenRenderer(width, height) self.scene = pyrender.Scene() diff --git a/slam/model_components/neural_recon_components/models/backbone.py b/slam/model_components/neural_recon_components/models/backbone.py index ffa6909..3686211 100644 --- a/slam/model_components/neural_recon_components/models/backbone.py +++ b/slam/model_components/neural_recon_components/models/backbone.py @@ -26,6 +26,7 @@ def _get_depths(alpha): class MnasMulti(nn.Module): + def __init__(self, alpha=1.0): super(MnasMulti, self).__init__() depths = _get_depths(alpha) diff --git a/slam/model_components/neural_recon_components/models/gru_fusion.py b/slam/model_components/neural_recon_components/models/gru_fusion.py index 73d12fb..f800476 100644 --- a/slam/model_components/neural_recon_components/models/gru_fusion.py +++ b/slam/model_components/neural_recon_components/models/gru_fusion.py @@ -18,6 +18,7 @@ class GRUFusion(nn.Module): Update hidden state features with ConvGRU. 2. Substitute TSDF in the global volume when direct_substitute = True. """ + def __init__(self, cfg, ch_in=None, direct_substitute=False): super(GRUFusion, self).__init__() self.cfg = cfg @@ -165,9 +166,11 @@ def update_map(self, value, coords, target_volume, valid, valid_target, :param value: (Tensor) fused feature (N, C) :param coords: (Tensor) updated coords (N, 3) - :param target_volume: (Tensor) tsdf volume (DIM_X, DIM_Y, DIM_Z, 1) + :param target_volume: (Tensor) tsdf volume (DIM_X, DIM_Y, DIM_Z, + 1) :param valid: (Tensor) mask: 1 represent in current FBV (N,) - :param valid_target: (Tensor) gt mask: 1 represent in current FBV (N,) + :param valid_target: (Tensor) gt mask: 1 represent in current + FBV (N,) :param relative_origin: (Tensor), origin in global volume, (3,) :param scale: :return: diff --git a/slam/model_components/neural_recon_components/models/modules.py b/slam/model_components/neural_recon_components/models/modules.py index a299332..5442754 100644 --- a/slam/model_components/neural_recon_components/models/modules.py +++ b/slam/model_components/neural_recon_components/models/modules.py @@ -16,6 +16,7 @@ class BasicConvolutionBlock(nn.Module): + def __init__(self, inc, outc, ks=3, stride=1, dilation=1): super().__init__() self.net = nn.Sequential( @@ -31,6 +32,7 @@ def forward(self, x): class BasicDeconvolutionBlock(nn.Module): + def __init__(self, inc, outc, ks=3, stride=1): super().__init__() self.net = nn.Sequential( @@ -46,6 +48,7 @@ def forward(self, x): class ResidualBlock(nn.Module): + def __init__(self, inc, outc, ks=3, stride=1, dilation=1): super().__init__() self.net = nn.Sequential( @@ -76,6 +79,7 @@ def forward(self, x): class SPVCNN(nn.Module): + def __init__(self, **kwargs): super().__init__() @@ -178,6 +182,7 @@ def forward(self, z): class SConv3d(nn.Module): + def __init__(self, inc, outc, press, vres, ks=3, stride=1, dilation=1): super().__init__() self.net = spnn.Conv3d(inc, @@ -198,6 +203,7 @@ def forward(self, z): class ConvGRU(nn.Module): + def __init__(self, hidden_dim=128, input_dim=192 + 128, press=1, vres=1): super(ConvGRU, self).__init__() self.convz = SConv3d(hidden_dim + input_dim, hidden_dim, press, vres, diff --git a/slam/model_components/neural_recon_components/models/neucon_network.py b/slam/model_components/neural_recon_components/models/neucon_network.py index f432330..44b8409 100644 --- a/slam/model_components/neural_recon_components/models/neucon_network.py +++ b/slam/model_components/neural_recon_components/models/neucon_network.py @@ -19,6 +19,7 @@ class NeuConNet(nn.Module): """Coarse-to-fine network.""" + def __init__(self, cfg): super(NeuConNet, self).__init__() self.cfg = cfg @@ -53,15 +54,15 @@ def __init__(self, cfg): def get_target(self, coords, inputs, scale): """Won't be used when 'fusion_on' flag is turned on. - :param coords: (Tensor), coordinates of voxels, (N, 4) - (4 : Batch ind, x, y, z) - :param inputs: (List), inputs['tsdf_list' / 'occ_list']: ground truth - volume list, [(B, DIM_X, DIM_Y, DIM_Z)] + :param coords: (Tensor), coordinates of voxels, (N, 4) (4 : + Batch ind, x, y, z) + :param inputs: (List), inputs['tsdf_list' / 'occ_list']: ground + truth volume list, [(B, DIM_X, DIM_Y, DIM_Z)] :param scale: - :return: tsdf_target: (Tensor), tsdf ground truth for - each predicted voxels, (N,) + :return: tsdf_target: (Tensor), tsdf ground truth for each + predicted voxels, (N,) :return: occ_target: (Tensor), occupancy ground truth for each - predicted voxels, (N,) + predicted voxels, (N,) """ with torch.no_grad(): tsdf_target = inputs['tsdf_list'][scale] diff --git a/slam/model_components/neural_recon_components/models/neuralrecon.py b/slam/model_components/neural_recon_components/models/neuralrecon.py index a2df90a..ffda47e 100644 --- a/slam/model_components/neural_recon_components/models/neuralrecon.py +++ b/slam/model_components/neural_recon_components/models/neuralrecon.py @@ -13,6 +13,7 @@ class NeuralRecon(nn.Module): """NeuralRecon main class.""" + def __init__(self, cfg): super(NeuralRecon, self).__init__() self.cfg = cfg.MODEL diff --git a/slam/model_components/projective_ops_dpvo.py b/slam/model_components/projective_ops_dpvo.py index 6c8762b..3a0d854 100644 --- a/slam/model_components/projective_ops_dpvo.py +++ b/slam/model_components/projective_ops_dpvo.py @@ -19,7 +19,7 @@ def coords_grid(ht, wd, **kwargs): def iproj(patches, intrinsics): - """inverse projection.""" + """Inverse projection.""" x, y, d = patches.unbind(dim=2) fx, fy, cx, cy = intrinsics[..., None, None].unbind(dim=2) i = torch.ones_like(d) @@ -60,7 +60,7 @@ def transform(poses, valid=False, jacobian=False, tonly=False): - """projective transform.""" + """Projective transform.""" X0 = iproj(patches[:, kk], intrinsics[:, ii]) @@ -139,12 +139,12 @@ def transform(poses, def point_cloud(poses, patches, intrinsics, ix): - """generate point cloud from patches.""" + """Generate point cloud from patches.""" return poses[:, ix, None, None].inv() * iproj(patches, intrinsics[:, ix]) def flow_mag(poses, patches, intrinsics, ii, jj, kk, beta=0.3): - """projective transform.""" + """Projective transform.""" coords0 = transform(poses, patches, intrinsics, ii, ii, kk) coords1 = transform(poses, patches, intrinsics, ii, jj, kk, tonly=False) diff --git a/slam/model_components/slam_external_splatam.py b/slam/model_components/slam_external_splatam.py index ce6abd8..ba2a489 100644 --- a/slam/model_components/slam_external_splatam.py +++ b/slam/model_components/slam_external_splatam.py @@ -119,17 +119,19 @@ def get_expon_lr_func(lr_init, max_steps=1000000): """Copied from Plenoxels. - Continuous learning rate decay function. Adapted from JaxNeRF - The returned rate is lr_init when step=0 and lr_final when step=max_steps, - is log-linearly interpolated elsewhere (equivalent to exponential decay). - If lr_delay_steps>0 then the learning rate will be scaled by some smooth - function of lr_delay_mult, such that the initial learning rate is - lr_init*lr_delay_mult at the beginning of optimization but will be eased - back to the normal learning rate when steps>lr_delay_steps. + Continuous learning rate decay function. Adapted from JaxNeRF The + returned rate is lr_init when step=0 and lr_final when + step=max_steps, is log-linearly interpolated elsewhere (equivalent + to exponential decay). If lr_delay_steps>0 then the learning rate + will be scaled by some smooth function of lr_delay_mult, such that + the initial learning rate is lr_init*lr_delay_mult at the beginning + of optimization but will be eased back to the normal learning rate + when steps>lr_delay_steps. :param conf: config subtree 'lr' or similar :param max_steps: int, the number of steps during optimization. - :return HoF which takes step as input + :return HoF which takes step as input """ + def helper(step): if step < 0 or (lr_init == 0.0 and lr_final == 0.0): # Disable this parameter diff --git a/slam/model_components/utils.py b/slam/model_components/utils.py index 0056580..400a481 100644 --- a/slam/model_components/utils.py +++ b/slam/model_components/utils.py @@ -212,6 +212,7 @@ def raw2outputs_nerf_color(raw, weights (tensor, N_rays*N_samples): weights assigned to each sampled color. """ + def raw2alpha(raw, dists, act_fn=F.relu): return 1. - torch.exp(-act_fn(raw) * dists) @@ -448,9 +449,10 @@ def sparse_to_dense_channel(locs, values, dim, c, default_val, device): def make_recursive_func(func): """Code from NeuralRecon, licensed under the Apache License, Version 2.0. - convert a function into recursive style to handle nested dict/list/tuple - variables + convert a function into recursive style to handle nested + dict/list/tuple variables """ + def wrapper(vars): if isinstance(vars, list): return [wrapper(x) for x in vars] diff --git a/slam/model_components/utils_dpvo.py b/slam/model_components/utils_dpvo.py index 2dfe369..0f461ea 100644 --- a/slam/model_components/utils_dpvo.py +++ b/slam/model_components/utils_dpvo.py @@ -8,6 +8,7 @@ class Timer: + def __init__(self, name, enabled=True): self.name = name self.enabled = enabled @@ -32,7 +33,7 @@ def __exit__(self, type, value, traceback): def coords_grid(b, n, h, w, **kwargs): - """coordinate grid.""" + """Coordinate grid.""" x = torch.arange(0, w, dtype=torch.float, **kwargs) y = torch.arange(0, h, dtype=torch.float, **kwargs) coords = torch.stack(torch.meshgrid(y, x, indexing='ij')) @@ -40,7 +41,7 @@ def coords_grid(b, n, h, w, **kwargs): def coords_grid_with_index(d, **kwargs): - """coordinate grid with frame index.""" + """Coordinate grid with frame index.""" b, n, h, w = d.shape x = torch.arange(0, w, dtype=torch.float, **kwargs) y = torch.arange(0, h, dtype=torch.float, **kwargs) @@ -57,7 +58,7 @@ def coords_grid_with_index(d, **kwargs): def patchify(x, patch_size=3): - """extract patches from video.""" + """Extract patches from video.""" b, n, c, h, w = x.shape x = x.view(b * n, c, h, w) y = F.unfold(x, patch_size) @@ -66,7 +67,7 @@ def patchify(x, patch_size=3): def pyramidify(fmap, lvls=[1]): - """turn fmap into a pyramid.""" + """Turn fmap into a pyramid.""" b, n, c, h, w = fmap.shape pyramid = [] diff --git a/slam/model_components/vonet_dpvo.py b/slam/model_components/vonet_dpvo.py index dd2d7ea..874d7e1 100644 --- a/slam/model_components/vonet_dpvo.py +++ b/slam/model_components/vonet_dpvo.py @@ -16,6 +16,7 @@ class Update(nn.Module): + def __init__(self, p): super(Update, self).__init__() @@ -53,7 +54,7 @@ def __init__(self, p): GradientClip(), nn.Sigmoid()) def forward(self, net, inp, corr, flow, ii, jj, kk): - """update operator.""" + """Update operator.""" net = net + inp + self.corr(corr) net = self.norm(net) ix, jx = fastba.neighbors(kk, jj) @@ -70,6 +71,7 @@ def forward(self, net, inp, corr, flow, ii, jj, kk): class Patchifier(nn.Module): + def __init__(self, patch_size=3): super(Patchifier, self).__init__() self.patch_size = patch_size @@ -90,7 +92,7 @@ def forward(self, disps=None, gradient_bias=False, return_color=False): - """extract patches from input images.""" + """Extract patches from input images.""" fmap = self.fnet(images) / 4.0 imap = self.inet(images) / 4.0 @@ -152,6 +154,7 @@ def forward(self, class CorrBlock: + def __init__(self, fmap, gmap, radius=3, dropout=0.2, levels=[1, 4]): self.dropout = dropout self.radius = radius @@ -166,6 +169,7 @@ def __call__(self, ii, jj, coords): class VONet(nn.Module): + def __init__(self, use_viewer=False): super(VONet, self).__init__() self.P = 3 diff --git a/slam/model_components/voxel_helpers_voxfusion.py b/slam/model_components/voxel_helpers_voxfusion.py index 52655d9..d4ece27 100644 --- a/slam/model_components/voxel_helpers_voxfusion.py +++ b/slam/model_components/voxel_helpers_voxfusion.py @@ -167,6 +167,7 @@ def offset_points(point_xyz, quarter_voxel=1, offset_only=False, bits=2): class BallRayIntersect(Function): + @staticmethod def forward(ctx, radius, n_max, points, ray_start, ray_dir): inds, min_depth, max_depth = _ext.ball_intersect( @@ -188,6 +189,7 @@ def backward(ctx, a, b, c): class AABBRayIntersect(Function): + @staticmethod def forward(ctx, voxelsize, n_max, points, ray_start, ray_dir): # HACK: speed-up ray-voxel intersection by batching... @@ -231,6 +233,7 @@ def backward(ctx, a, b, c): class SparseVoxelOctreeRayIntersect(Function): + @staticmethod def forward(ctx, voxelsize, n_max, points, children, ray_start, ray_dir): # HACK: avoid out-of-memory @@ -279,6 +282,7 @@ def backward(ctx, a, b, c): class TriangleRayIntersect(Function): + @staticmethod def forward(ctx, cagesize, blur_ratio, n_max, points, faces, ray_start, ray_dir): @@ -329,6 +333,7 @@ def backward(ctx, a, b, c): class UniformRaySampling(Function): + @staticmethod def forward( ctx, @@ -397,6 +402,7 @@ def backward(ctx, a, b, c): class InverseCDFRaySampling(Function): + @staticmethod def forward( ctx, diff --git a/slam/models/conv_onet.py b/slam/models/conv_onet.py index fef8663..cdb9464 100644 --- a/slam/models/conv_onet.py +++ b/slam/models/conv_onet.py @@ -294,7 +294,8 @@ def load_pretrain(self): """This function is modified from nice-slam, licensed under the Apache License, Version 2.0. - Load parameters of pretrained ConvOnet checkpoints to the decoders. + Load parameters of pretrained ConvOnet checkpoints to the + decoders. """ if self.config.coarse: ckpt = torch.load(self.config.pretrained_decoders_coarse, diff --git a/slam/models/conv_onet_pointslam.py b/slam/models/conv_onet_pointslam.py index ea3d276..5014c1f 100644 --- a/slam/models/conv_onet_pointslam.py +++ b/slam/models/conv_onet_pointslam.py @@ -229,7 +229,8 @@ def load_pretrain(self): """This function is modified from point-slam, licensed under the Apache License, Version 2.0. - Load parameters of pretrained ConvOnet checkpoints to the decoders. + Load parameters of pretrained ConvOnet checkpoints to the + decoders. """ ckpt = torch.load(self.config.pretrained_decoders_middle_fine, map_location=self.device) diff --git a/slam/models/sparse_voxel.py b/slam/models/sparse_voxel.py index 3696c5f..775ba90 100644 --- a/slam/models/sparse_voxel.py +++ b/slam/models/sparse_voxel.py @@ -105,7 +105,7 @@ def get_loss_dict(self, inputs, is_mapping, stage=None) -> Dict[str, torch.Tensor]: - """color and depth are filled, use 'ray_mask' to select valid rays.""" + """Color and depth are filled, use 'ray_mask' to select valid rays.""" loss_dict = {} # select by svo intersection diff --git a/slam/pipeline/mapper.py b/slam/pipeline/mapper.py index b657ba2..fe3eae3 100644 --- a/slam/pipeline/mapper.py +++ b/slam/pipeline/mapper.py @@ -14,6 +14,7 @@ class MapperConfig(InstantiateConfig): class Mapper(): + def __init__(self, config: MapperConfig) -> None: self.config = config diff --git a/slam/pipeline/tracker.py b/slam/pipeline/tracker.py index f50f812..a8f7d85 100644 --- a/slam/pipeline/tracker.py +++ b/slam/pipeline/tracker.py @@ -31,6 +31,7 @@ class TrackerConfig(InstantiateConfig): class Tracker(): + def __init__(self, config: TrackerConfig, dataset: BaseDataset, diff --git a/slam/pipeline/visualizer.py b/slam/pipeline/visualizer.py index d37ef15..4aa8ee0 100644 --- a/slam/pipeline/visualizer.py +++ b/slam/pipeline/visualizer.py @@ -31,6 +31,7 @@ class VisualizerConfig(InstantiateConfig): class Visualizer(): + def __init__(self, config: VisualizerConfig, camera: Camera, diff --git a/slam/pipeline/xrdslam.py b/slam/pipeline/xrdslam.py index 11e3453..4ac37f9 100644 --- a/slam/pipeline/xrdslam.py +++ b/slam/pipeline/xrdslam.py @@ -37,6 +37,7 @@ class XRDSLAMConfig(InstantiateConfig): class XRDSLAM(): + def __init__(self, config: XRDSLAMConfig) -> None: self.config = config # get dataset diff --git a/slam/utils/opt_pose.py b/slam/utils/opt_pose.py index 50cd4c6..73b1630 100644 --- a/slam/utils/opt_pose.py +++ b/slam/utils/opt_pose.py @@ -11,6 +11,7 @@ class OptimizablePose(nn.Module): + def __init__(self, init_pose, separate_LR=True, rot_rep='axis_angle'): super().__init__() # init_pose=[tx,ty,tz, qx,qy,qz,qw], init_pose=[tx,ty,tz, rx,ry,rz]