deep_hough_cuda.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <torch/extension.h>
  2. #include <ATen/ATen.h>
  3. #include <vector>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <iostream>
  7. // CUDA forward declarations
  8. std::vector<torch::Tensor> line_accum_cuda_forward(
  9. const torch::Tensor feat,
  10. const float* tabCos,
  11. const float* tabSin,
  12. torch::Tensor output,
  13. const int numangle,
  14. const int numrho);
  15. std::vector<torch::Tensor> line_accum_cuda_backward(
  16. torch::Tensor grad_outputs,
  17. torch::Tensor grad_in,
  18. torch::Tensor feat,
  19. const float* tabCos,
  20. const float* tabSin,
  21. const int numangle,
  22. const int numrho);
  23. // C++ interface
  24. #define CHECK_CUDA(x) AT_ASSERT(x.type().is_cuda()) //, #x " must be a CUDA tensor")
  25. #define CHECK_CONTIGUOUS(x) AT_ASSERT(x.is_contiguous()) //, #x " must be contiguous")
  26. #define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x)
  27. #define PI 3.14159265358979323846
  28. void initTab(float* tabSin, float* tabCos, const int numangle, const int numrho, const int H, const int W)
  29. {
  30. float irho = int(std::sqrt(H*H + W*W) + 1) / float((numrho - 1));
  31. float itheta = PI / numangle;
  32. float angle = 0;
  33. for(int i = 0; i < numangle; ++i)
  34. {
  35. tabCos[i] = std::cos(angle) / irho;
  36. tabSin[i] = std::sin(angle) / irho;
  37. angle += itheta;
  38. }
  39. }
  40. std::vector<at::Tensor> line_accum_forward(
  41. const at::Tensor feat,
  42. at::Tensor output,
  43. const int numangle,
  44. const int numrho) {
  45. CHECK_INPUT(feat);
  46. CHECK_INPUT(output);
  47. float tabSin[numangle], tabCos[numangle];
  48. const int H = feat.size(2);
  49. const int W = feat.size(3);
  50. initTab(tabSin, tabCos, numangle, numrho, H, W);
  51. const int batch_size = feat.size(0);
  52. const int channels_size = feat.size(1);
  53. // torch::set_requires_grad(output, true);
  54. auto out = line_accum_cuda_forward(feat, tabCos, tabSin, output, numangle, numrho);
  55. // std::cout << out[0].sum() << std::endl;
  56. CHECK_CONTIGUOUS(out[0]);
  57. return out;
  58. }
  59. std::vector<torch::Tensor> line_accum_backward(
  60. torch::Tensor grad_outputs,
  61. torch::Tensor grad_inputs,
  62. torch::Tensor feat,
  63. const int numangle,
  64. const int numrho) {
  65. CHECK_INPUT(grad_outputs);
  66. CHECK_INPUT(grad_inputs);
  67. CHECK_INPUT(feat);
  68. float tabSin[numangle], tabCos[numangle];
  69. const int H = feat.size(2);
  70. const int W = feat.size(3);
  71. initTab(tabSin, tabCos, numangle, numrho, H, W);
  72. const int batch_size = feat.size(0);
  73. const int channels_size = feat.size(1);
  74. const int imH = feat.size(2);
  75. const int imW = feat.size(3);
  76. return line_accum_cuda_backward(
  77. grad_outputs,
  78. grad_inputs,
  79. feat,
  80. tabCos,
  81. tabSin,
  82. numangle,
  83. numrho);
  84. }
  85. PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  86. m.def("forward", &line_accum_forward, "line features accumulating forward (CUDA)");
  87. m.def("backward", &line_accum_backward, "line features accumulating backward (CUDA)");
  88. }