-
Notifications
You must be signed in to change notification settings - Fork 6k
add feature/infer demo/se resnext50 #11708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
22aafcb
f5d0783
2e739cf
190a871
904297e
1c0bbfd
c34da73
8b827d0
c2d9c42
c583513
c94cacc
75e5084
fc568f4
e8b0dcb
d509021
7449fe7
46285ed
5c8e323
7e7df49
33b8ea8
ba0401a
529915c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Infernce Demos | ||
|
|
||
| Input data format: | ||
|
|
||
| - Each line contains a single record | ||
| - Each record's format is | ||
|
|
||
| ``` | ||
| <space splitted floats as data>\t<space splitted ints as shape> | ||
| ``` | ||
|
|
||
| Follow the C++ codes in `vis_demo.cc`. | ||
|
|
||
| ## MobileNet | ||
|
|
||
| To execute the demo, simply run | ||
|
|
||
| ```sh | ||
| ./mobilenet_inference_demo --modeldir <model> --data <datafile> | ||
| ``` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add description for resnet
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| ## SE-ResNeXt-50 | ||
|
|
||
| To execute the demo, simply run | ||
|
|
||
| ```sh | ||
| ./se_resnext50_inference_demo --modeldir <model> --data <datafile> | ||
| ``` | ||
|
|
||
| ## OCR | ||
|
|
||
| To execute the demo, simply run | ||
|
|
||
| ```sh | ||
| ./ocr_inference_demo --modeldir <model> --data <datafile> | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "paddle/contrib/inference/paddle_inference_api.h" | ||
|
|
||
| namespace paddle { | ||
| namespace demo { | ||
|
|
||
| static void split(const std::string& str, | ||
| char sep, | ||
| std::vector<std::string>* pieces) { | ||
| pieces->clear(); | ||
| if (str.empty()) { | ||
| return; | ||
| } | ||
| size_t pos = 0; | ||
| size_t next = str.find(sep, pos); | ||
| while (next != std::string::npos) { | ||
| pieces->push_back(str.substr(pos, next - pos)); | ||
| pos = next + 1; | ||
| next = str.find(sep, pos); | ||
| } | ||
| if (!str.substr(pos).empty()) { | ||
| pieces->push_back(str.substr(pos)); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Get a summary of a PaddleTensor content. | ||
| */ | ||
| static std::string SummaryTensor(const PaddleTensor& tensor) { | ||
| std::stringstream ss; | ||
| int num_elems = tensor.data.length() / PaddleDtypeSize(tensor.dtype); | ||
|
|
||
| ss << "data[:10]\t"; | ||
| switch (tensor.dtype) { | ||
| case PaddleDType::INT64: { | ||
| for (int i = 0; i < std::min(num_elems, 10); i++) { | ||
| ss << static_cast<int64_t*>(tensor.data.data())[i] << " "; | ||
| } | ||
| break; | ||
| } | ||
| case PaddleDType::FLOAT32: | ||
| for (int i = 0; i < std::min(num_elems, 10); i++) { | ||
| ss << static_cast<float*>(tensor.data.data())[i] << " "; | ||
| } | ||
| break; | ||
| } | ||
| return ss.str(); | ||
| } | ||
|
|
||
| } // namespace demo | ||
| } // namespace paddle |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
|
|
||
| /* | ||
| * This file contains demo for mobilenet, se-resnext50 and ocr. | ||
| */ | ||
|
|
||
| #include <gflags/gflags.h> | ||
| #include <glog/logging.h> // use glog instead of PADDLE_ENFORCE to avoid importing other paddle header files. | ||
| #include <gtest/gtest.h> | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include "paddle/contrib/inference/demo/utils.h" | ||
| #include "paddle/contrib/inference/paddle_inference_api.h" | ||
|
|
||
| namespace paddle { | ||
| namespace demo { | ||
|
|
||
| DEFINE_string(modeldir, "", "Directory of the inference model."); | ||
| DEFINE_string(refer, "", "path to reference result for comparison."); | ||
| DEFINE_string( | ||
| data, | ||
| "", | ||
| "path of data; each line is a record, format is " | ||
| "'<space splitted floats as data>\t<space splitted ints as shape'"); | ||
|
|
||
| #ifdef PADDLE_WITH_CUDA | ||
| DECLARE_double(fraction_of_gpu_memory_to_use); | ||
| #endif | ||
|
|
||
| struct Record { | ||
| std::vector<float> data; | ||
| std::vector<int32_t> shape; | ||
| }; | ||
|
|
||
| void split(const std::string& str, char sep, std::vector<std::string>* pieces); | ||
|
|
||
| Record ProcessALine(const std::string& line) { | ||
| LOG(INFO) << "process a line"; | ||
| std::vector<std::string> columns; | ||
| split(line, '\t', &columns); | ||
| CHECK_EQ(columns.size(), 2UL) | ||
| << "data format error, should be <data>\t<shape>"; | ||
|
|
||
| Record record; | ||
| std::vector<std::string> data_strs; | ||
| split(columns[0], ' ', &data_strs); | ||
| for (auto& d : data_strs) { | ||
| record.data.push_back(std::stof(d)); | ||
| } | ||
|
|
||
| std::vector<std::string> shape_strs; | ||
| split(columns[1], ' ', &shape_strs); | ||
| for (auto& s : shape_strs) { | ||
| record.shape.push_back(std::stoi(s)); | ||
| } | ||
| LOG(INFO) << "data size " << record.data.size(); | ||
| LOG(INFO) << "data shape size " << record.shape.size(); | ||
| return record; | ||
| } | ||
|
|
||
| void CheckOutput(const std::string& referfile, const PaddleTensor& output) { | ||
| std::string line; | ||
| std::ifstream file(referfile); | ||
| std::getline(file, line); | ||
| auto refer = ProcessALine(line); | ||
| file.close(); | ||
|
|
||
| size_t numel = output.data.length() / PaddleDtypeSize(output.dtype); | ||
| LOG(INFO) << "predictor output numel " << numel; | ||
| LOG(INFO) << "reference output numel " << refer.data.size(); | ||
| EXPECT_EQ(numel, refer.data.size()); | ||
| switch (output.dtype) { | ||
| case PaddleDType::INT64: { | ||
| for (size_t i = 0; i < numel; ++i) { | ||
| EXPECT_EQ(static_cast<int64_t*>(output.data.data())[i], refer.data[i]); | ||
| } | ||
| break; | ||
| } | ||
| case PaddleDType::FLOAT32: | ||
| for (size_t i = 0; i < numel; ++i) { | ||
| EXPECT_NEAR( | ||
| static_cast<float*>(output.data.data())[i], refer.data[i], 1e-3); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1e-3太大了。改成1e-7吧。
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1e-7本地gpu跑了下,太小了。改为1e-6了。 |
||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Use the native fluid engine to inference the demo. | ||
| */ | ||
| void Main(bool use_gpu) { | ||
| NativeConfig config; | ||
| // config.model_dir = FLAGS_modeldir; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 78行去掉
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| config.param_file = FLAGS_modeldir + "/__params__"; | ||
| config.prog_file = FLAGS_modeldir + "/__model__"; | ||
| config.use_gpu = use_gpu; | ||
| config.fraction_of_gpu_memory = 0.1; | ||
| #ifdef PADDLE_WITH_CUDA | ||
| config.fraction_of_gpu_memory = FLAGS_fraction_of_gpu_memory_to_use; | ||
| #endif | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 82行需要留着么?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以不要的。 |
||
| config.device = 0; | ||
|
|
||
| LOG(INFO) << "init predictor"; | ||
| auto predictor = | ||
| CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config); | ||
|
|
||
| LOG(INFO) << "begin to process data"; | ||
| // Just a single batch of data. | ||
| std::string line; | ||
| std::ifstream file(FLAGS_data); | ||
| std::getline(file, line); | ||
| auto record = ProcessALine(line); | ||
| file.close(); | ||
|
|
||
| // Inference. | ||
| PaddleTensor input{ | ||
| .name = "xx", | ||
| .shape = record.shape, | ||
| .data = PaddleBuf(record.data.data(), record.data.size() * sizeof(float)), | ||
| .dtype = PaddleDType::FLOAT32}; | ||
|
|
||
| LOG(INFO) << "run executor"; | ||
| std::vector<PaddleTensor> output; | ||
| predictor->Run({input}, &output); | ||
|
|
||
| LOG(INFO) << "output.size " << output.size(); | ||
| auto& tensor = output.front(); | ||
| LOG(INFO) << "output: " << SummaryTensor(tensor); | ||
|
|
||
| // compare with reference result | ||
| CheckOutput(FLAGS_refer, tensor); | ||
| } | ||
|
|
||
| TEST(demo, vis_demo) { Main(false /*use_gpu*/); } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 需要再测下gpu的 |
||
|
|
||
| } // namespace demo | ||
| } // namespace paddle | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
能否下载到build的当前路径,不要放到thirdparty路径下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接放到build路径?还是放到别的什么路径 @Superjomn @luotao1