未验证 提交 dc124ecb 编写于 作者: L littletomatodonkey 提交者: GitHub

fix cpp time (#482)

* fix time sta for cpp

* fix time sta for cpp
上级 25adf371
......@@ -61,7 +61,7 @@ public:
void LoadModel(const std::string &model_path, const std::string &params_path);
// Run predictor
void Run(cv::Mat &img);
double Run(cv::Mat &img);
private:
std::shared_ptr<Predictor> predictor_;
......
......@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <chrono>
#include <include/cls.h>
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<std::chrono::microseconds>(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
......@@ -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<std::chrono::microseconds>(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;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册