From bcd68837651638e77635c4b04a73f35369a719ad Mon Sep 17 00:00:00 2001 From: yohannes-taye Date: Tue, 30 Aug 2022 13:44:42 +0800 Subject: [PATCH 1/3] Initial Commit --- .gitignore | 1 + src/CMakeLists.txt | 10 +++ src/main.cpp | 202 +++++++++++++++++++++++++++++++++++++++++++++ src/run.sh | 6 ++ 4 files changed, 219 insertions(+) create mode 100644 src/CMakeLists.txt create mode 100644 src/main.cpp create mode 100755 src/run.sh diff --git a/.gitignore b/.gitignore index 534fed2..ce9849b 100644 --- a/.gitignore +++ b/.gitignore @@ -109,3 +109,4 @@ venv.bak/ *.jpeg *.pt .DS_Store +src/libtorch/ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..dba2550 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.0 FATAL_ERROR) +project(custom_ops) +# set(OpenCV_DIR /home/zjs/opt/OpenCV-3.4.0-cuda-9.0/share/OpenCV) +# include (CTest) +find_package(OpenCV REQUIRED) +include_directories(${OpenCV_INCLUDE_DIRS}) +find_package(Torch REQUIRED) +add_executable(main main.cpp) +target_link_libraries(main ${TORCH_LIBRARIES} ${OpenCV_LIBS}) +set_property(TARGET main PROPERTY CXX_STANDARD 14) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..5193863 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,202 @@ +#include // One-stop header. +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#define GPU 1 + +template +T remove_extension(T const & filename) +{ + typename T::size_type const p(filename.find_last_of('.')); + return p > 0 && p != T::npos ? filename.substr(0, p) : filename; +} +std::string base_name(std::string const & path) +{ + return path.substr(path.find_last_of("/\\") + 1); +} +int main(int argc, const char* argv[]) { + if (argc != 4) { + std::cerr << "usage: example-app \n"; + return -1; + } + + torch::jit::script::Module module; + try { + // Deserialize the ScriptModule from a file using torch::jit::load(). + module = torch::jit::load(argv[1]); + std::cout<<"Module loaded successfuly"< inputs; + // auto tensor_in = torch::ones({1, 3, 32, 96}); + // if(GPU){ + // module.to(at::kCUDA); + // tensor_in.to(at::kCUDA); + // } + // inputs.push_back(tensor_in); + // double t = (double)cv::getTickCount(); + + + std::vector fn; + glob(argv[3], fn, false); + + std::vector images; + size_t count = fn.size(); //number of png files in images folder + for (size_t i=0; i({row, col})).align_corners(false)); + std::cout<<"#############SHIT2"< Date: Tue, 30 Aug 2022 13:57:24 +0800 Subject: [PATCH 2/3] Remove run.sh helper file --- .gitignore | 1 + src/run.sh | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100755 src/run.sh diff --git a/.gitignore b/.gitignore index ce9849b..7303c64 100644 --- a/.gitignore +++ b/.gitignore @@ -110,3 +110,4 @@ venv.bak/ *.pt .DS_Store src/libtorch/ +*.sh \ No newline at end of file diff --git a/src/run.sh b/src/run.sh deleted file mode 100755 index 80e092b..0000000 --- a/src/run.sh +++ /dev/null @@ -1,6 +0,0 @@ -# cmake -DCMAKE_PREFIX_PATH=./libtorch .. -cmake --build ./build --config Release -./build/main \ - /home/tmc/project/DPT_with_onnx_export/DPT/traced_model.pt \ - /home/tmc/Documents/Data/depth_test_data/A011_04010827_C017/000206.jpg \ - /home/tmc/Documents/Data/depth_test_data/A011_04010827_C017 \ No newline at end of file From cc4d6bc4368e7fc61a4de2eb12c73579bfb8f7eb Mon Sep 17 00:00:00 2001 From: yohannes-taye Date: Tue, 30 Aug 2022 14:05:12 +0800 Subject: [PATCH 3/3] Clean up code --- .gitignore | 3 +- src/main.cpp | 253 +++++++++++++++++---------------------------------- 2 files changed, 86 insertions(+), 170 deletions(-) diff --git a/.gitignore b/.gitignore index 7303c64..4bdf96e 100644 --- a/.gitignore +++ b/.gitignore @@ -110,4 +110,5 @@ venv.bak/ *.pt .DS_Store src/libtorch/ -*.sh \ No newline at end of file +*.sh +.vscode/settings.json diff --git a/src/main.cpp b/src/main.cpp index 5193863..555ead8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,180 +23,95 @@ std::string base_name(std::string const & path) return path.substr(path.find_last_of("/\\") + 1); } int main(int argc, const char* argv[]) { - if (argc != 4) { - std::cerr << "usage: example-app \n"; - return -1; - } - - torch::jit::script::Module module; - try { - // Deserialize the ScriptModule from a file using torch::jit::load(). - module = torch::jit::load(argv[1]); - std::cout<<"Module loaded successfuly"< inputs; - // auto tensor_in = torch::ones({1, 3, 32, 96}); - // if(GPU){ - // module.to(at::kCUDA); - // tensor_in.to(at::kCUDA); - // } - // inputs.push_back(tensor_in); - // double t = (double)cv::getTickCount(); - - - std::vector fn; - glob(argv[3], fn, false); - - std::vector images; - size_t count = fn.size(); //number of png files in images folder - for (size_t i=0; i \n"; + return -1; } - // torch::Tensor mean = torch::tensor({ 0.5f, 0.5f, 0.5f }).toType(torch::kFloat16).to(torch::Device("cuda")); - - - torch::Tensor image_input = torch::from_blob((float*)ch_first.data, { 1, 3, data.rows, data.cols}); - image_input = image_input.to(at::kCUDA); - image_input = image_input.div(255); - image_input = (image_input.sub(0.5f)).div(0.5f); - image_input = image_input.toType(torch::kFloat32); - - - // image_input = image_input.to(at::kCUDA, torch::kHalf); - - // image_input.to(); - - auto net_out = module.forward({ image_input }).toTensor(); - // net_out = net_out[0]; - namespace F = torch::nn::functional; - std::cout<<"#############SHIT1"<({row, col})).align_corners(false)); - std::cout<<"#############SHIT2"< fn; + glob(argv[2], fn, false); + + std::vector images; + size_t count = fn.size(); //number of png files in images folder + for (size_t i=0; i({row, col})).align_corners(false)); + + net_out = net_out.squeeze(); + net_out = (net_out.min(net_out.max()) / (net_out.max() - net_out.min())).mul(65535.0f); + net_out = net_out.to(torch::kCPU); + int height = net_out.sizes()[0]; + int width = net_out.sizes()[1]; + int x = net_out.sizes()[2]; + + try + { + cv::Mat output_mat(cv::Size{width, height}, CV_32FC1, net_out.data_ptr()); + output_mat.convertTo(output_mat, CV_16UC1); + double fps = cv::getTickFrequency() / (cv::getTickCount() - start); + std::cout << "FPS : " << fps << std::endl; + std::string output_address = "./output/"; + + output_address.append(remove_extension(base_name(fn[i])).append(extention)); + cv::imwrite(output_address, output_mat); + + } + catch (const c10::Error& e) + { + std::cout << "an error has occured : " << e.msg() << std::endl; + } + } }