提交 944ffc57 编写于 作者: D dongshuilong

code format for cls cpp infer

上级 4dbd85b8
...@@ -35,8 +35,8 @@ using namespace paddle_infer; ...@@ -35,8 +35,8 @@ using namespace paddle_infer;
namespace PaddleClas { namespace PaddleClas {
class Classifier { class Classifier {
public: public:
explicit Classifier(const ClsConfig &config) { explicit Classifier(const ClsConfig &config) {
this->use_gpu_ = config.use_gpu; this->use_gpu_ = config.use_gpu;
this->gpu_id_ = config.gpu_id; this->gpu_id_ = config.gpu_id;
...@@ -60,8 +60,8 @@ public: ...@@ -60,8 +60,8 @@ public:
// Run predictor // Run predictor
double Run(cv::Mat &img, std::vector<double> *times); double Run(cv::Mat &img, std::vector<double> *times);
private: private:
std::shared_ptr<Predictor> predictor_; std::shared_ptr <Predictor> predictor_;
bool use_gpu_ = false; bool use_gpu_ = false;
int gpu_id_ = 0; int gpu_id_ = 0;
...@@ -84,6 +84,6 @@ private: ...@@ -84,6 +84,6 @@ private:
Normalize normalize_op_; Normalize normalize_op_;
Permute permute_op_; Permute permute_op_;
CenterCropImg crop_op_; CenterCropImg crop_op_;
}; };
} // namespace PaddleClas } // namespace PaddleClas
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
namespace PaddleClas { namespace PaddleClas {
class ClsConfig { class ClsConfig {
public: public:
explicit ClsConfig(const std::string &path) { explicit ClsConfig(const std::string &path) {
ReadYamlConfig(path); ReadYamlConfig(path);
this->infer_imgs = this->infer_imgs =
...@@ -72,10 +72,10 @@ public: ...@@ -72,10 +72,10 @@ public:
.as<float>(); .as<float>();
this->mean = this->config_file["PreProcess"]["transform_ops"][2] this->mean = this->config_file["PreProcess"]["transform_ops"][2]
["NormalizeImage"]["mean"] ["NormalizeImage"]["mean"]
.as<std::vector<float>>(); .as < std::vector < float >> ();
this->std = this->config_file["PreProcess"]["transform_ops"][2] this->std = this->config_file["PreProcess"]["transform_ops"][2]
["NormalizeImage"]["std"] ["NormalizeImage"]["std"]
.as<std::vector<float>>(); .as < std::vector < float >> ();
if (this->config_file["Global"]["benchmark"].IsDefined()) if (this->config_file["Global"]["benchmark"].IsDefined())
this->benchmark = this->config_file["Global"]["benchmark"].as<bool>(); this->benchmark = this->config_file["Global"]["benchmark"].as<bool>();
else else
...@@ -105,7 +105,9 @@ public: ...@@ -105,7 +105,9 @@ public:
float scale = 0.00392157; float scale = 0.00392157;
std::vector<float> mean = {0.485, 0.456, 0.406}; std::vector<float> mean = {0.485, 0.456, 0.406};
std::vector<float> std = {0.229, 0.224, 0.225}; std::vector<float> std = {0.229, 0.224, 0.225};
void PrintConfigInfo(); void PrintConfigInfo();
void ReadYamlConfig(const std::string &path); void ReadYamlConfig(const std::string &path);
}; };
} // namespace PaddleClas } // namespace PaddleClas
...@@ -31,26 +31,26 @@ using namespace std; ...@@ -31,26 +31,26 @@ using namespace std;
namespace PaddleClas { namespace PaddleClas {
class Normalize { class Normalize {
public: public:
virtual void Run(cv::Mat *im, const std::vector<float> &mean, virtual void Run(cv::Mat *im, const std::vector<float> &mean,
const std::vector<float> &std, float &scale); const std::vector<float> &std, float &scale);
}; };
// RGB -> CHW // RGB -> CHW
class Permute { class Permute {
public: public:
virtual void Run(const cv::Mat *im, float *data); virtual void Run(const cv::Mat *im, float *data);
}; };
class CenterCropImg { class CenterCropImg {
public: public:
virtual void Run(cv::Mat &im, const int crop_size = 224); virtual void Run(cv::Mat &im, const int crop_size = 224);
}; };
class ResizeImg { class ResizeImg {
public: public:
virtual void Run(const cv::Mat &img, cv::Mat &resize_img, int max_size_len); virtual void Run(const cv::Mat &img, cv::Mat &resize_img, int max_size_len);
}; };
} // namespace PaddleClas } // namespace PaddleClas
...@@ -32,15 +32,15 @@ ...@@ -32,15 +32,15 @@
namespace PaddleClas { namespace PaddleClas {
class Utility { class Utility {
public: public:
static std::vector<std::string> ReadDict(const std::string &path); static std::vector <std::string> ReadDict(const std::string &path);
// template <class ForwardIterator> // template <class ForwardIterator>
// inline static size_t argmax(ForwardIterator first, ForwardIterator last) // inline static size_t argmax(ForwardIterator first, ForwardIterator last)
// { // {
// return std::distance(first, std::max_element(first, last)); // return std::distance(first, std::max_element(first, last));
// } // }
}; };
} // namespace PaddleClas } // namespace PaddleClas
\ No newline at end of file
...@@ -143,10 +143,10 @@ tar -xvf paddle_inference.tgz ...@@ -143,10 +143,10 @@ tar -xvf paddle_inference.tgz
``` ```
inference/ inference/
|--cls_infer.pdmodel |--inference.pdmodel
|--cls_infer.pdiparams |--inference.pdiparams
``` ```
**注意**:上述文件中,`cls_infer.pdmodel`文件存储了模型结构信息,`cls_infer.pdiparams`文件存储了模型参数信息。注意两个文件的路径需要与配置文件`tools/config.txt`中的`cls_model_path``cls_params_path`参数对应一致 **注意**:上述文件中,`inference.pdmodel`文件存储了模型结构信息,`inference.pdiparams`文件存储了模型参数信息。模型目录可以随意设置,但是模型名字不能修改
### 2.2 编译PaddleClas C++预测demo ### 2.2 编译PaddleClas C++预测demo
...@@ -183,6 +183,7 @@ cmake .. \ ...@@ -183,6 +183,7 @@ cmake .. \
-DCUDA_LIB=${CUDA_LIB_DIR} \ -DCUDA_LIB=${CUDA_LIB_DIR} \
make -j make -j
cd ..
``` ```
上述命令中, 上述命令中,
...@@ -200,31 +201,26 @@ make -j ...@@ -200,31 +201,26 @@ make -j
在执行上述命令,编译完成之后,会在当前路径下生成`build`文件夹,其中生成一个名为`clas_system`的可执行文件。 在执行上述命令,编译完成之后,会在当前路径下生成`build`文件夹,其中生成一个名为`clas_system`的可执行文件。
### 运行demo ### 2.3 运行demo
* 首先修改`tools/config.txt`中对应字段: #### 2.3.1 设置配置文件
* use_gpu:是否使用GPU;
* gpu_id:使用的GPU卡号;
* gpu_mem:显存;
* cpu_math_library_num_threads:底层科学计算库所用线程的数量;
* use_mkldnn:是否使用MKLDNN加速;
* use_tensorrt: 是否使用tensorRT进行加速;
* use_fp16:是否使用半精度浮点数进行计算,该选项仅在use_tensorrt为true时有效;
* cls_model_path:预测模型结构文件路径;
* cls_params_path:预测模型参数文件路径;
* resize_short_size:预处理时图像缩放大小;
* crop_size:预处理时图像裁剪后的大小。
* 然后修改`tools/run.sh` ```shell
* `./build/clas_system ./tools/config.txt ./docs/imgs/ILSVRC2012_val_00000666.JPEG` cp ../configs/inference_cls.yaml tools/
* 上述命令中分别为:编译得到的可执行文件`clas_system`;运行时的配置文件`config.txt`;待预测的图像。 ```
根据[python预测推理](../../docs/zh_CN/inference_deployment/python_deploy.md)`图像分类推理`部分修改好`tools`目录下`inference_cls.yaml`文件。`yaml`文件的参数说明详见[python预测推理](../../docs/zh_CN/inference_deployment/python_deploy.md)
请根据实际存放文件,修改好`Global.infer_imgs``Global.inference_model_dir`等参数。
* 最后执行以下命令,完成对一幅图像的分类。 #### 2.3.2 执行
```shell ```shell
sh tools/run.sh ./build/clas_system -c inference_cls.yaml
# or
./build/clas_system -config inference_cls.yaml
``` ```
* 最终屏幕上会输出结果,如下图所示。 最终屏幕上会输出结果,如下图所示。
<div align="center"> <div align="center">
<img src="./docs/imgs/cpp_infer_result.png" width="600"> <img src="./docs/imgs/cpp_infer_result.png" width="600">
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
namespace PaddleClas { namespace PaddleClas {
void Classifier::LoadModel(const std::string &model_path, void Classifier::LoadModel(const std::string &model_path,
const std::string &params_path) { const std::string &params_path) {
paddle_infer::Config config; paddle_infer::Config config;
config.SetModel(model_path, params_path); config.SetModel(model_path, params_path);
...@@ -50,9 +50,9 @@ void Classifier::LoadModel(const std::string &model_path, ...@@ -50,9 +50,9 @@ void Classifier::LoadModel(const std::string &model_path,
config.DisableGlogInfo(); config.DisableGlogInfo();
this->predictor_ = CreatePredictor(config); this->predictor_ = CreatePredictor(config);
} }
double Classifier::Run(cv::Mat &img, std::vector<double> *times) { double Classifier::Run(cv::Mat &img, std::vector<double> *times) {
cv::Mat srcimg; cv::Mat srcimg;
cv::Mat resize_img; cv::Mat resize_img;
img.copyTo(srcimg); img.copyTo(srcimg);
...@@ -107,6 +107,6 @@ double Classifier::Run(cv::Mat &img, std::vector<double> *times) { ...@@ -107,6 +107,6 @@ double Classifier::Run(cv::Mat &img, std::vector<double> *times) {
<< "\tscore: " << double(out_data[maxPosition]) << std::endl; << "\tscore: " << double(out_data[maxPosition]) << std::endl;
return inference_cost_time; return inference_cost_time;
} }
} // namespace PaddleClas } // namespace PaddleClas
...@@ -16,13 +16,13 @@ ...@@ -16,13 +16,13 @@
namespace PaddleClas { namespace PaddleClas {
void ClsConfig::PrintConfigInfo() { void ClsConfig::PrintConfigInfo() {
std::cout << "=======Paddle Class inference config======" << std::endl; std::cout << "=======Paddle Class inference config======" << std::endl;
std::cout << this->config_file << std::endl; std::cout << this->config_file << std::endl;
std::cout << "=======End of Paddle Class inference config======" << std::endl; std::cout << "=======End of Paddle Class inference config======" << std::endl;
} }
void ClsConfig::ReadYamlConfig(const std::string &path) { void ClsConfig::ReadYamlConfig(const std::string &path) {
try { try {
this->config_file = YAML::LoadFile(path); this->config_file = YAML::LoadFile(path);
...@@ -31,5 +31,5 @@ void ClsConfig::ReadYamlConfig(const std::string &path) { ...@@ -31,5 +31,5 @@ void ClsConfig::ReadYamlConfig(const std::string &path) {
<< std::endl; << std::endl;
exit(1); exit(1);
} }
} }
}; // namespace PaddleClas }; // namespace PaddleClas
...@@ -35,8 +35,10 @@ using namespace std; ...@@ -35,8 +35,10 @@ using namespace std;
using namespace cv; using namespace cv;
using namespace PaddleClas; using namespace PaddleClas;
DEFINE_string(config, "", "Path of yaml file"); DEFINE_string(config,
DEFINE_string(c, "", "Path of yaml file"); "", "Path of yaml file");
DEFINE_string(c,
"", "Path of yaml file");
int main(int argc, char **argv) { int main(int argc, char **argv) {
google::ParseCommandLineFlags(&argc, &argv, true); google::ParseCommandLineFlags(&argc, &argv, true);
...@@ -57,9 +59,9 @@ int main(int argc, char **argv) { ...@@ -57,9 +59,9 @@ int main(int argc, char **argv) {
std::string path(config.infer_imgs); std::string path(config.infer_imgs);
std::vector<std::string> img_files_list; std::vector <std::string> img_files_list;
if (cv::utils::fs::isDirectory(path)) { if (cv::utils::fs::isDirectory(path)) {
std::vector<cv::String> filenames; std::vector <cv::String> filenames;
cv::glob(path, filenames); cv::glob(path, filenames);
for (auto f : filenames) { for (auto f : filenames) {
img_files_list.push_back(f); img_files_list.push_back(f);
......
...@@ -32,16 +32,16 @@ ...@@ -32,16 +32,16 @@
namespace PaddleClas { namespace PaddleClas {
void Permute::Run(const cv::Mat *im, float *data) { void Permute::Run(const cv::Mat *im, float *data) {
int rh = im->rows; int rh = im->rows;
int rw = im->cols; int rw = im->cols;
int rc = im->channels(); int rc = im->channels();
for (int i = 0; i < rc; ++i) { for (int i = 0; i < rc; ++i) {
cv::extractChannel(*im, cv::Mat(rh, rw, CV_32FC1, data + i * rh * rw), i); cv::extractChannel(*im, cv::Mat(rh, rw, CV_32FC1, data + i * rh * rw), i);
} }
} }
void Normalize::Run(cv::Mat *im, const std::vector<float> &mean, void Normalize::Run(cv::Mat *im, const std::vector<float> &mean,
const std::vector<float> &std, float &scale) { const std::vector<float> &std, float &scale) {
if (scale) { if (scale) {
(*im).convertTo(*im, CV_32FC3, scale); (*im).convertTo(*im, CV_32FC3, scale);
...@@ -56,18 +56,18 @@ void Normalize::Run(cv::Mat *im, const std::vector<float> &mean, ...@@ -56,18 +56,18 @@ void Normalize::Run(cv::Mat *im, const std::vector<float> &mean,
(im->at<cv::Vec3f>(h, w)[2] - mean[2]) / std[2]; (im->at<cv::Vec3f>(h, w)[2] - mean[2]) / std[2];
} }
} }
} }
void CenterCropImg::Run(cv::Mat &img, const int crop_size) { void CenterCropImg::Run(cv::Mat &img, const int crop_size) {
int resize_w = img.cols; int resize_w = img.cols;
int resize_h = img.rows; int resize_h = img.rows;
int w_start = int((resize_w - crop_size) / 2); int w_start = int((resize_w - crop_size) / 2);
int h_start = int((resize_h - crop_size) / 2); int h_start = int((resize_h - crop_size) / 2);
cv::Rect rect(w_start, h_start, crop_size, crop_size); cv::Rect rect(w_start, h_start, crop_size, crop_size);
img = img(rect); img = img(rect);
} }
void ResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img, void ResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img,
int resize_short_size) { int resize_short_size) {
int w = img.cols; int w = img.cols;
int h = img.rows; int h = img.rows;
...@@ -83,6 +83,6 @@ void ResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img, ...@@ -83,6 +83,6 @@ void ResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img,
int resize_w = round(float(w) * ratio); int resize_w = round(float(w) * ratio);
cv::resize(img, resize_img, cv::Size(resize_w, resize_h)); cv::resize(img, resize_img, cv::Size(resize_w, resize_h));
} }
} // namespace PaddleClas } // namespace PaddleClas
...@@ -18,3 +18,4 @@ cmake .. \ ...@@ -18,3 +18,4 @@ cmake .. \
-DCUDA_LIB=${CUDA_LIB_DIR} \ -DCUDA_LIB=${CUDA_LIB_DIR} \
make -j make -j
cd ..
# model load config
use_gpu 0
gpu_id 0
gpu_mem 4000
cpu_threads 10
use_mkldnn 1
use_tensorrt 0
use_fp16 0
# cls config
cls_model_path /PaddleClas/inference/cls_infer.pdmodel
cls_params_path /PaddleClas/inference/cls_infer.pdiparams
resize_short_size 256
crop_size 224
# for log env info
benchmark 0
./build/clas_system ./tools/config.txt ./docs/imgs/ILSVRC2012_val_00000666.JPEG
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册