forked from pytorch/audio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
torch_script_custom_ops restructure (pytorch#1057)
Signed-off-by: Edward Z. Yang <[email protected]>
- Loading branch information
Showing
3 changed files
with
80 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
cmake_minimum_required(VERSION 3.1 FATAL_ERROR) | ||
project(warp_perspective) | ||
|
||
find_package(Torch REQUIRED) | ||
find_package(OpenCV REQUIRED) | ||
|
||
# Define our library target | ||
add_library(warp_perspective SHARED op.cpp) | ||
# Enable C++14 | ||
target_compile_features(warp_perspective PRIVATE cxx_std_14) | ||
# Link against LibTorch | ||
target_link_libraries(warp_perspective "${TORCH_LIBRARIES}") | ||
# Link against OpenCV | ||
target_link_libraries(warp_perspective opencv_core opencv_imgproc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <opencv2/opencv.hpp> | ||
#include <torch/script.h> | ||
|
||
// BEGIN warp_perspective | ||
torch::Tensor warp_perspective(torch::Tensor image, torch::Tensor warp) { | ||
// BEGIN image_mat | ||
cv::Mat image_mat(/*rows=*/image.size(0), | ||
/*cols=*/image.size(1), | ||
/*type=*/CV_32FC1, | ||
/*data=*/image.data_ptr<float>()); | ||
// END image_mat | ||
|
||
// BEGIN warp_mat | ||
cv::Mat warp_mat(/*rows=*/warp.size(0), | ||
/*cols=*/warp.size(1), | ||
/*type=*/CV_32FC1, | ||
/*data=*/warp.data_ptr<float>()); | ||
// END warp_mat | ||
|
||
// BEGIN output_mat | ||
cv::Mat output_mat; | ||
cv::warpPerspective(image_mat, output_mat, warp_mat, /*dsize=*/{8, 8}); | ||
// END output_mat | ||
|
||
// BEGIN output_tensor | ||
torch::Tensor output = torch::from_blob(output_mat.ptr<float>(), /*sizes=*/{8, 8}); | ||
return output.clone(); | ||
// END output_tensor | ||
} | ||
// END warp_perspective | ||
|
||
// BEGIN registry | ||
static auto registry = | ||
torch::RegisterOperators("my_ops::warp_perspective", &warp_perspective); | ||
// END registry |