From dc124ecb46f66b5d4ab42cbbd392fd1886c5fe3f Mon Sep 17 00:00:00 2001 From: littletomatodonkey <2120160898@bit.edu.cn> Date: Tue, 15 Dec 2020 21:54:56 +0800 Subject: [PATCH] fix cpp time (#482) * fix time sta for cpp * fix time sta for cpp --- deploy/cpp_infer/include/cls.h | 2 +- deploy/cpp_infer/src/cls.cpp | 12 +++++++++++- deploy/cpp_infer/src/main.cpp | 15 ++++----------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/deploy/cpp_infer/include/cls.h b/deploy/cpp_infer/include/cls.h index f55d3952..3cc5dcc1 100644 --- a/deploy/cpp_infer/include/cls.h +++ b/deploy/cpp_infer/include/cls.h @@ -61,7 +61,7 @@ public: void LoadModel(const std::string &model_path, const std::string ¶ms_path); // Run predictor - void Run(cv::Mat &img); + double Run(cv::Mat &img); private: std::shared_ptr predictor_; diff --git a/deploy/cpp_infer/src/cls.cpp b/deploy/cpp_infer/src/cls.cpp index d304ff5e..d0fa21f7 100644 --- a/deploy/cpp_infer/src/cls.cpp +++ b/deploy/cpp_infer/src/cls.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include namespace PaddleClas { @@ -52,7 +53,7 @@ void Classifier::LoadModel(const std::string &model_path, this->predictor_ = CreatePredictor(config); } -void Classifier::Run(cv::Mat &img) { +double Classifier::Run(cv::Mat &img) { cv::Mat srcimg; cv::Mat resize_img; img.copyTo(srcimg); @@ -69,6 +70,7 @@ void Classifier::Run(cv::Mat &img) { auto input_names = this->predictor_->GetInputNames(); auto input_t = this->predictor_->GetInputHandle(input_names[0]); input_t->Reshape({1, 3, resize_img.rows, resize_img.cols}); + auto start = std::chrono::system_clock::now(); input_t->CopyFromCpu(input.data()); this->predictor_->Run(); @@ -81,6 +83,12 @@ void Classifier::Run(cv::Mat &img) { out_data.resize(out_num); output_t->CopyToCpu(out_data.data()); + auto end = std::chrono::system_clock::now(); + auto duration = + std::chrono::duration_cast(end - start); + double cost_time = double(duration.count()) * + std::chrono::microseconds::period::num / + std::chrono::microseconds::period::den; int maxPosition = max_element(out_data.begin(), out_data.end()) - out_data.begin(); @@ -88,6 +96,8 @@ void Classifier::Run(cv::Mat &img) { std::cout << "\tclass id: " << maxPosition << std::endl; std::cout << std::fixed << std::setprecision(10) << "\tscore: " << double(out_data[maxPosition]) << std::endl; + + return cost_time; } } // namespace PaddleClas diff --git a/deploy/cpp_infer/src/main.cpp b/deploy/cpp_infer/src/main.cpp index 8885cc23..ec08cde6 100644 --- a/deploy/cpp_infer/src/main.cpp +++ b/deploy/cpp_infer/src/main.cpp @@ -72,22 +72,15 @@ int main(int argc, char **argv) { cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR); cv::cvtColor(srcimg, srcimg, cv::COLOR_BGR2RGB); - auto start = std::chrono::system_clock::now(); - classifier.Run(srcimg); - auto end = std::chrono::system_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); - double curr_time = double(duration.count()) * - std::chrono::microseconds::period::num / - std::chrono::microseconds::period::den; + double run_time = classifier.Run(srcimg); if (idx >= warmup_iter) { - elapsed_time += curr_time; + elapsed_time += run_time; std::cout << "Current image path: " << img_path << std::endl; - std::cout << "Current time cost: " << curr_time << " s, " + std::cout << "Current time cost: " << run_time << " s, " << "average time cost in all: " << elapsed_time / (idx + 1 - warmup_iter) << " s." << std::endl; } else { - std::cout << "Current time cost: " << curr_time << " s." << std::endl; + std::cout << "Current time cost: " << run_time << " s." << std::endl; } } -- GitLab