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

code format for cls cpp infer

上级 4dbd85b8
......@@ -35,8 +35,8 @@ using namespace paddle_infer;
namespace PaddleClas {
class Classifier {
public:
class Classifier {
public:
explicit Classifier(const ClsConfig &config) {
this->use_gpu_ = config.use_gpu;
this->gpu_id_ = config.gpu_id;
......@@ -60,8 +60,8 @@ public:
// Run predictor
double Run(cv::Mat &img, std::vector<double> *times);
private:
std::shared_ptr<Predictor> predictor_;
private:
std::shared_ptr <Predictor> predictor_;
bool use_gpu_ = false;
int gpu_id_ = 0;
......@@ -84,6 +84,6 @@ private:
Normalize normalize_op_;
Permute permute_op_;
CenterCropImg crop_op_;
};
};
} // namespace PaddleClas
......@@ -31,8 +31,8 @@
namespace PaddleClas {
class ClsConfig {
public:
class ClsConfig {
public:
explicit ClsConfig(const std::string &path) {
ReadYamlConfig(path);
this->infer_imgs =
......@@ -72,10 +72,10 @@ public:
.as<float>();
this->mean = this->config_file["PreProcess"]["transform_ops"][2]
["NormalizeImage"]["mean"]
.as<std::vector<float>>();
.as < std::vector < float >> ();
this->std = this->config_file["PreProcess"]["transform_ops"][2]
["NormalizeImage"]["std"]
.as<std::vector<float>>();
.as < std::vector < float >> ();
if (this->config_file["Global"]["benchmark"].IsDefined())
this->benchmark = this->config_file["Global"]["benchmark"].as<bool>();
else
......@@ -105,7 +105,9 @@ public:
float scale = 0.00392157;
std::vector<float> mean = {0.485, 0.456, 0.406};
std::vector<float> std = {0.229, 0.224, 0.225};
void PrintConfigInfo();
void ReadYamlConfig(const std::string &path);
};
};
} // namespace PaddleClas
......@@ -31,26 +31,26 @@ using namespace std;
namespace PaddleClas {
class Normalize {
public:
class Normalize {
public:
virtual void Run(cv::Mat *im, const std::vector<float> &mean,
const std::vector<float> &std, float &scale);
};
};
// RGB -> CHW
class Permute {
public:
class Permute {
public:
virtual void Run(const cv::Mat *im, float *data);
};
};
class CenterCropImg {
public:
class CenterCropImg {
public:
virtual void Run(cv::Mat &im, const int crop_size = 224);
};
};
class ResizeImg {
public:
class ResizeImg {
public:
virtual void Run(const cv::Mat &img, cv::Mat &resize_img, int max_size_len);
};
};
} // namespace PaddleClas
......@@ -32,15 +32,15 @@
namespace PaddleClas {
class Utility {
public:
static std::vector<std::string> ReadDict(const std::string &path);
class Utility {
public:
static std::vector <std::string> ReadDict(const std::string &path);
// template <class ForwardIterator>
// inline static size_t argmax(ForwardIterator first, ForwardIterator last)
// {
// return std::distance(first, std::max_element(first, last));
// }
};
};
} // namespace PaddleClas
\ No newline at end of file
......@@ -143,10 +143,10 @@ tar -xvf paddle_inference.tgz
```
inference/
|--cls_infer.pdmodel
|--cls_infer.pdiparams
|--inference.pdmodel
|--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
......@@ -183,6 +183,7 @@ cmake .. \
-DCUDA_LIB=${CUDA_LIB_DIR} \
make -j
cd ..
```
上述命令中,
......@@ -200,31 +201,26 @@ make -j
在执行上述命令,编译完成之后,会在当前路径下生成`build`文件夹,其中生成一个名为`clas_system`的可执行文件。
### 运行demo
* 首先修改`tools/config.txt`中对应字段:
* 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:预处理时图像裁剪后的大小。
### 2.3 运行demo
#### 2.3.1 设置配置文件
* 然后修改`tools/run.sh`
* `./build/clas_system ./tools/config.txt ./docs/imgs/ILSVRC2012_val_00000666.JPEG`
* 上述命令中分别为:编译得到的可执行文件`clas_system`;运行时的配置文件`config.txt`;待预测的图像。
```shell
cp ../configs/inference_cls.yaml tools/
```
根据[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
sh tools/run.sh
./build/clas_system -c inference_cls.yaml
# or
./build/clas_system -config inference_cls.yaml
```
* 最终屏幕上会输出结果,如下图所示。
最终屏幕上会输出结果,如下图所示。
<div align="center">
<img src="./docs/imgs/cpp_infer_result.png" width="600">
......
......@@ -16,7 +16,7 @@
namespace PaddleClas {
void Classifier::LoadModel(const std::string &model_path,
void Classifier::LoadModel(const std::string &model_path,
const std::string &params_path) {
paddle_infer::Config config;
config.SetModel(model_path, params_path);
......@@ -50,9 +50,9 @@ void Classifier::LoadModel(const std::string &model_path,
config.DisableGlogInfo();
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 resize_img;
img.copyTo(srcimg);
......@@ -107,6 +107,6 @@ double Classifier::Run(cv::Mat &img, std::vector<double> *times) {
<< "\tscore: " << double(out_data[maxPosition]) << std::endl;
return inference_cost_time;
}
}
} // namespace PaddleClas
......@@ -16,13 +16,13 @@
namespace PaddleClas {
void ClsConfig::PrintConfigInfo() {
void ClsConfig::PrintConfigInfo() {
std::cout << "=======Paddle Class inference config======" << std::endl;
std::cout << this->config_file << 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 {
this->config_file = YAML::LoadFile(path);
......@@ -31,5 +31,5 @@ void ClsConfig::ReadYamlConfig(const std::string &path) {
<< std::endl;
exit(1);
}
}
}
}; // namespace PaddleClas
......@@ -35,8 +35,10 @@ using namespace std;
using namespace cv;
using namespace PaddleClas;
DEFINE_string(config, "", "Path of yaml file");
DEFINE_string(c, "", "Path of yaml file");
DEFINE_string(config,
"", "Path of yaml file");
DEFINE_string(c,
"", "Path of yaml file");
int main(int argc, char **argv) {
google::ParseCommandLineFlags(&argc, &argv, true);
......@@ -57,9 +59,9 @@ int main(int argc, char **argv) {
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)) {
std::vector<cv::String> filenames;
std::vector <cv::String> filenames;
cv::glob(path, filenames);
for (auto f : filenames) {
img_files_list.push_back(f);
......
......@@ -32,16 +32,16 @@
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 rw = im->cols;
int rc = im->channels();
for (int i = 0; i < rc; ++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) {
if (scale) {
(*im).convertTo(*im, CV_32FC3, scale);
......@@ -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];
}
}
}
}
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_h = img.rows;
int w_start = int((resize_w - crop_size) / 2);
int h_start = int((resize_h - crop_size) / 2);
cv::Rect rect(w_start, h_start, crop_size, crop_size);
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 w = img.cols;
int h = img.rows;
......@@ -83,6 +83,6 @@ void ResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img,
int resize_w = round(float(w) * ratio);
cv::resize(img, resize_img, cv::Size(resize_w, resize_h));
}
}
} // namespace PaddleClas
......@@ -18,3 +18,4 @@ cmake .. \
-DCUDA_LIB=${CUDA_LIB_DIR} \
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.
先完成此消息的编辑!
想要评论请 注册