GithubHelp home page GithubHelp logo

smooth-sampler's Introduction

Smooth sampler

A drop-in replacement for Pytorch's grid_sample supporting smoothstep activation for interpolation weights (proposed in Instant NGP by Müller et al.) as well as double backpropagation. Currently supports 3D inputs and trilinear interpolation mode. Based on Pytorch's native grid_sampler code. Used in GO-Surf by Wang et al.

Installation

On Python3 environment with Pytorch >=1.11 CUDA installation run:

pip install git+https://github.com/tymoteuszb/smooth-sampler

Usage

The API is consistent with Pytorch's grid_sample:

import torch
from smooth_sampler import SmoothSampler

align_corners = True
padding_mode = "zeros"

input = (torch.rand([2,2,2,3,11], device="cuda")).requires_grad_(True)
grid = (torch.rand([2,2,1,5,3], device="cuda") * 2. - 1.).requires_grad_(True)

out1 = SmoothSampler.apply(input, grid, padding_mode, align_corners, False)
out2 = torch.nn.functional.grid_sample(input, grid, padding_mode=padding_mode, align_corners=align_corners)
assert torch.allclose(out1, out2)

grad1_input, grad1_grid = torch.autograd.grad(out1, [input, grid], torch.ones_like(out1), create_graph=True)
grad2_input, grad2_grid = torch.autograd.grad(out2, [input, grid], torch.ones_like(out2), create_graph=True)
assert torch.allclose(grad1_input, grad2_input)
assert torch.allclose(grad1_grid, grad2_grid)

loss1 = out1.sum() + grad1_input.sum() + grad1_grid.sum()
loss1.backward() # Works!

loss2 = out2.sum() + grad2_input.sum() + grad2_grid.sum()
loss2.backward() # RuntimeException: derivative for aten::grid_sampler_3d_backward is not implemented

Citation

If you use this code in your project, please consider adding a citation:

@article{wang2022go-surf,
  title={GO-Surf: Neural Feature Grid Optimization for Fast, High-Fidelity RGB-D Surface Reconstruction},
  author={Wang, Jingwen and Bleja, Tymoteusz and Agapito, Lourdes},
  journal={arXiv preprint},
  year={2022}
}

smooth-sampler's People

Contributors

tym-b avatar

Stargazers

Miao Ziliang avatar  avatar agithubname avatar S3D4F5 avatar  avatar  avatar Minjun Kang avatar YiChenCityU avatar zhanghe avatar  avatar  avatar Yikang Ding avatar wangm avatar  avatar  avatar Yiqun Zhao avatar lambdald avatar Di Chen avatar qqq avatar Binbin avatar sunset avatar taoranyi avatar Peterou avatar Vincent Ho avatar 爱可可-爱生活 avatar Brendan Meade avatar Jayesh K. Gupta avatar kiui avatar Jhin avatar wudlxkq avatar Jaesung Choe avatar Shitty Girl avatar Jianglong Ye avatar Robotics_Qi avatar  avatar SS avatar Xiangyue Liu avatar Jingwen Wang avatar

Watchers

James Cloos avatar  avatar Jingwen Wang avatar Jaesung Choe avatar

smooth-sampler's Issues

Fail to install

Hi, @tymoteuszb @JingwenWang95

I got the following errors by pip install git+https://github.com/tymoteuszb/smooth-sampler

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no suitable user-defined conversion from "const at::TensorBase" to "const at::Tensor" exists

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no suitable user-defined conversion from "const at::TensorBase" to "const at::Tensor" exists

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "smooth_sampler_kernel" matches the argument list
            argument types are: (int, <error-type>, <error-type>, <error-type>, at::native::detail::GridSamplerPadding, __nv_bool, __nv_bool)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "smooth_sampler_kernel" matches the argument list
            argument types are: (int64_t, <error-type>, <error-type>, <error-type>, at::native::detail::GridSamplerPadding, __nv_bool, __nv_bool)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no suitable user-defined conversion from "const at::TensorBase" to "const at::Tensor" exists

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no suitable user-defined conversion from "const at::TensorBase" to "const at::Tensor" exists

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no suitable user-defined conversion from "const at::TensorBase" to "const at::Tensor" exists

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "smooth_sampler_kernel" matches the argument list
            argument types are: (int, <error-type>, <error-type>, <error-type>, at::native::detail::GridSamplerPadding, __nv_bool, __nv_bool)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "smooth_sampler_kernel" matches the argument list
            argument types are: (int64_t, <error-type>, <error-type>, <error-type>, at::native::detail::GridSamplerPadding, __nv_bool, __nv_bool)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no suitable user-defined conversion from "const at::TensorBase" to "const at::Tensor" exists

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no suitable user-defined conversion from "const at::TensorBase" to "const at::Tensor" exists

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no suitable user-defined conversion from "const at::TensorBase" to "const at::Tensor" exists

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "smooth_sampler_kernel" matches the argument list
            argument types are: (int, <error-type>, <error-type>, <error-type>, at::native::detail::GridSamplerPadding, __nv_bool, __nv_bool)

/tmp/pip-req-build-702sk3li/smooth_sampler/csrc/smooth_sampler_kernel.cu(630): error: no instance of function template "at::cuda::detail::getTensorInfo" matches the argument list
            argument types are: (const at::TensorBase)`

some questions about implemetation about 2d sampler with double backpropagation

Hi,

Thank you for the great work.

Currently, this work only support the 3d inputs and trilinear interpolation mode. However, I want to use the 2d inputs and bilinear interpolation mode. Since something like TensoRF+volsdf also need the bilinear interpolation for the 2d inputs. A naive solution is to translate the 2d inputs to a 3d inputs with input_2d.unsqueeze() and the sample grids from [x, y] to [x, y, 0]. I wonder will this naive solution be ok with your current implementation?

If not, how should I make it work for the 2d sampler without re-writing the CUDA. I know the straight way is to implement it in CUDA, but I am not familiar with it.

Looking forward to your reply.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.