Skip to content

Commit 7c5e6d2

Browse files
authored
Merge pull request #12160 from luotao1/vis_demo
add independent vis_demo for inference
2 parents 0b9abcb + 6313bdc commit 7c5e6d2

File tree

8 files changed

+104
-129
lines changed

8 files changed

+104
-129
lines changed

paddle/contrib/inference/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,3 @@ if (WITH_ANAKIN) # only needed in CI
104104
target_compile_options(inference_anakin_test BEFORE PUBLIC ${ANAKIN_COMPILE_EXTRA_FLAGS})
105105
endif(WITH_TESTING)
106106
endif()
107-
108-
if(WITH_TESTING)
109-
add_subdirectory(demo)
110-
endif()

paddle/contrib/inference/demo/CMakeLists.txt

Lines changed: 0 additions & 59 deletions
This file was deleted.

paddle/contrib/inference/demo/README.md

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Inference Demos
2+
3+
There are several demos:
4+
5+
- simple_on_word2vec:
6+
- Follow the C++ codes is in `simple_on_word2vec.cc`.
7+
- It is suitable for word2vec model.
8+
- vis_demo:
9+
- Follow the C++ codes is in `vis_demo.cc`.
10+
- It is suitable for mobilenet, se_resnext50 and ocr three models.
11+
- Input data format:
12+
- Each line contains a single record
13+
- Each record's format is
14+
```
15+
<space splitted floats as data>\t<space splitted ints as shape>
16+
```
17+
18+
To build and execute the demos, simply run
19+
```
20+
./run.sh $PADDLE_ROOT $TURN_ON_MKL $TEST_GPU_CPU
21+
```
22+
- It will build and execute the demos in both static and shared library.
23+
- `$PADDLE_ROOT`: paddle library path
24+
- `$TURN_ON_MKL`: use MKL or Openblas
25+
- `$TEST_GPU_CPU`: test both GPU/CPU mode or only CPU mode
26+
- NOTE: for simple_on_word2vec, must run `ctest -R test_word2vec -R` to obtain word2vec model at first.

paddle/contrib/inference/demo_ci/run.sh

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,30 @@ else
1313
use_gpu_list='false'
1414
fi
1515

16+
# download vis_demo data
17+
function download() {
18+
dir_name=$1
19+
mkdir -p $dir_name
20+
cd $dir_name
21+
wget -q ${URL_ROOT}$dir_name.tar.gz
22+
tar xzf *.tar.gz
23+
cd ..
24+
}
25+
URL_ROOT=http://paddlemodels.bj.bcebos.com/inference-vis-demos%2F
26+
mkdir -p data
27+
cd data
28+
vis_demo_list='se_resnext50 ocr mobilenet'
29+
for vis_demo_name in $vis_demo_list; do
30+
download $vis_demo_name
31+
done
32+
cd ..
33+
34+
# compile and test the demo
1635
mkdir -p build
1736
cd build
1837

1938
for WITH_STATIC_LIB in ON OFF; do
39+
# -----simple_on_word2vec-----
2040
rm -rf *
2141
cmake .. -DPADDLE_LIB=${PADDLE_ROOT}/build/fluid_install_dir/ \
2242
-DWITH_MKL=$TURN_ON_MKL \
@@ -29,9 +49,30 @@ for WITH_STATIC_LIB in ON OFF; do
2949
--dirname=${PADDLE_ROOT}/build/python/paddle/fluid/tests/book/word2vec.inference.model \
3050
--use_gpu=$use_gpu
3151
if [ $? -ne 0 ]; then
32-
echo "inference demo runs fail."
52+
echo "simple_on_word2vec demo runs fail."
3353
exit 1
3454
fi
3555
done
56+
# ---------vis_demo---------
57+
rm -rf *
58+
cmake .. -DPADDLE_LIB=${PADDLE_ROOT}/build/fluid_install_dir/ \
59+
-DWITH_MKL=$TURN_ON_MKL \
60+
-DDEMO_NAME=vis_demo \
61+
-DWITH_GPU=$TEST_GPU_CPU \
62+
-DWITH_STATIC_LIB=$WITH_STATIC_LIB
63+
make -j
64+
for use_gpu in false; do
65+
for vis_demo_name in $vis_demo_list; do
66+
./vis_demo \
67+
--modeldir=../data/$vis_demo_name/model \
68+
--data=../data/$vis_demo_name/data.txt \
69+
--refer=../data/$vis_demo_name/result.txt \
70+
--use_gpu=$use_gpu
71+
if [ $? -ne 0 ]; then
72+
echo "vis demo $vis_demo_name runs fail."
73+
exit 1
74+
fi
75+
done
76+
done
3677
done
3778
set +x

paddle/contrib/inference/demo/utils.h renamed to paddle/contrib/inference/demo_ci/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <string>
1717
#include <vector>
1818

19-
#include "paddle/contrib/inference/paddle_inference_api.h"
19+
#include "contrib/inference/paddle_inference_api.h"
2020

2121
namespace paddle {
2222
namespace demo {

paddle/contrib/inference/demo/vis_demo.cc renamed to paddle/contrib/inference/demo_ci/vis_demo.cc

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,25 @@ limitations under the License. */
1818

1919
#include <gflags/gflags.h>
2020
#include <glog/logging.h> // use glog instead of PADDLE_ENFORCE to avoid importing other paddle header files.
21-
#include <gtest/gtest.h>
2221
#include <fstream>
2322
#include <iostream>
24-
#include "paddle/contrib/inference/demo/utils.h"
25-
#include "paddle/contrib/inference/paddle_inference_api.h"
23+
#include "paddle/fluid/platform/enforce.h"
24+
#include "utils.h"
2625

2726
#ifdef PADDLE_WITH_CUDA
2827
DECLARE_double(fraction_of_gpu_memory_to_use);
2928
#endif
30-
31-
namespace paddle {
32-
namespace demo {
33-
3429
DEFINE_string(modeldir, "", "Directory of the inference model.");
3530
DEFINE_string(refer, "", "path to reference result for comparison.");
3631
DEFINE_string(
3732
data,
3833
"",
3934
"path of data; each line is a record, format is "
4035
"'<space splitted floats as data>\t<space splitted ints as shape'");
36+
DEFINE_bool(use_gpu, false, "Whether use gpu.");
37+
38+
namespace paddle {
39+
namespace demo {
4140

4241
struct Record {
4342
std::vector<float> data;
@@ -47,7 +46,7 @@ struct Record {
4746
void split(const std::string& str, char sep, std::vector<std::string>* pieces);
4847

4948
Record ProcessALine(const std::string& line) {
50-
LOG(INFO) << "process a line";
49+
VLOG(3) << "process a line";
5150
std::vector<std::string> columns;
5251
split(line, '\t', &columns);
5352
CHECK_EQ(columns.size(), 2UL)
@@ -65,8 +64,8 @@ Record ProcessALine(const std::string& line) {
6564
for (auto& s : shape_strs) {
6665
record.shape.push_back(std::stoi(s));
6766
}
68-
LOG(INFO) << "data size " << record.data.size();
69-
LOG(INFO) << "data shape size " << record.shape.size();
67+
VLOG(3) << "data size " << record.data.size();
68+
VLOG(3) << "data shape size " << record.shape.size();
7069
return record;
7170
}
7271

@@ -78,20 +77,22 @@ void CheckOutput(const std::string& referfile, const PaddleTensor& output) {
7877
file.close();
7978

8079
size_t numel = output.data.length() / PaddleDtypeSize(output.dtype);
81-
LOG(INFO) << "predictor output numel " << numel;
82-
LOG(INFO) << "reference output numel " << refer.data.size();
83-
EXPECT_EQ(numel, refer.data.size());
80+
VLOG(3) << "predictor output numel " << numel;
81+
VLOG(3) << "reference output numel " << refer.data.size();
82+
PADDLE_ENFORCE_EQ(numel, refer.data.size());
8483
switch (output.dtype) {
8584
case PaddleDType::INT64: {
8685
for (size_t i = 0; i < numel; ++i) {
87-
EXPECT_EQ(static_cast<int64_t*>(output.data.data())[i], refer.data[i]);
86+
PADDLE_ENFORCE_EQ(static_cast<int64_t*>(output.data.data())[i],
87+
refer.data[i]);
8888
}
8989
break;
9090
}
9191
case PaddleDType::FLOAT32:
9292
for (size_t i = 0; i < numel; ++i) {
93-
EXPECT_NEAR(
94-
static_cast<float*>(output.data.data())[i], refer.data[i], 1e-5);
93+
PADDLE_ENFORCE_LT(
94+
fabs(static_cast<float*>(output.data.data())[i] - refer.data[i]),
95+
1e-5);
9596
}
9697
break;
9798
}
@@ -106,15 +107,15 @@ void Main(bool use_gpu) {
106107
config.prog_file = FLAGS_modeldir + "/__model__";
107108
config.use_gpu = use_gpu;
108109
config.device = 0;
109-
#ifdef PADDLE_WITH_CUDA
110-
config.fraction_of_gpu_memory = FLAGS_fraction_of_gpu_memory_to_use;
111-
#endif
110+
if (FLAGS_use_gpu) {
111+
config.fraction_of_gpu_memory = 0.1; // set by yourself
112+
}
112113

113-
LOG(INFO) << "init predictor";
114+
VLOG(3) << "init predictor";
114115
auto predictor =
115116
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);
116117

117-
LOG(INFO) << "begin to process data";
118+
VLOG(3) << "begin to process data";
118119
// Just a single batch of data.
119120
std::string line;
120121
std::ifstream file(FLAGS_data);
@@ -129,21 +130,26 @@ void Main(bool use_gpu) {
129130
.data = PaddleBuf(record.data.data(), record.data.size() * sizeof(float)),
130131
.dtype = PaddleDType::FLOAT32};
131132

132-
LOG(INFO) << "run executor";
133+
VLOG(3) << "run executor";
133134
std::vector<PaddleTensor> output;
134135
predictor->Run({input}, &output);
135136

136-
LOG(INFO) << "output.size " << output.size();
137+
VLOG(3) << "output.size " << output.size();
137138
auto& tensor = output.front();
138-
LOG(INFO) << "output: " << SummaryTensor(tensor);
139+
VLOG(3) << "output: " << SummaryTensor(tensor);
139140

140141
// compare with reference result
141142
CheckOutput(FLAGS_refer, tensor);
142143
}
143144

144-
TEST(demo, vis_demo_cpu) { Main(false /*use_gpu*/); }
145-
#ifdef PADDLE_WITH_CUDA
146-
TEST(demo, vis_demo_gpu) { Main(true /*use_gpu*/); }
147-
#endif
148145
} // namespace demo
149146
} // namespace paddle
147+
148+
int main(int argc, char** argv) {
149+
google::ParseCommandLineFlags(&argc, &argv, true);
150+
paddle::demo::Main(false /* use_gpu*/);
151+
if (FLAGS_use_gpu) {
152+
paddle::demo::Main(true /*use_gpu*/);
153+
}
154+
return 0;
155+
}

0 commit comments

Comments
 (0)