Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
0dcb5a67
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
1 年多 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
0dcb5a67
编写于
4月 10, 2020
作者:
S
shippingwang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
87ada03d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
58 addition
and
72 deletion
+58
-72
docs/zh_cn/tutorials/getting_started.md
docs/zh_cn/tutorials/getting_started.md
+13
-0
tools/infer/predict.py
tools/infer/predict.py
+45
-22
tools/infer/run.sh
tools/infer/run.sh
+0
-49
tools/infer/utils.py
tools/infer/utils.py
+0
-1
未找到文件。
docs/zh_cn/tutorials/getting_started.md
浏览文件 @
0dcb5a67
...
...
@@ -62,3 +62,16 @@ python eval.py \
-o
pretrained_model
=
path_to_pretrained_models
```
您可以更改configs/eval.yaml中的architecture字段和pretrained_model字段来配置评估模型,或是通过-o参数更新配置。
## 3 模型推理
PaddleClas通过预测引擎进行预测推理
```
bash
python tools/predict.py
\
-m
model文件路径
-p
params文件路径
-i
图片路径
--use_tensorrt
True
```
更多推理方式和实验请参考
[
分类预测框架
](
../extension/paddle_inference.md
)
tools/infer/
cpp_infer
.py
→
tools/infer/
predict
.py
浏览文件 @
0dcb5a67
...
...
@@ -12,14 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
utils
import
argparse
import
numpy
as
np
import
logging
import
time
from
paddle.fluid.core
import
PaddleTensor
from
paddle.fluid.core
import
AnalysisConfig
from
paddle.fluid.core
import
create_paddle_predictor
logging
.
basicConfig
(
level
=
logging
.
INFO
)
logger
=
logging
.
getLogger
(
__name__
)
def
parse_args
():
def
str2bool
(
v
):
...
...
@@ -29,10 +32,14 @@ def parse_args():
parser
.
add_argument
(
"-i"
,
"--image_file"
,
type
=
str
)
parser
.
add_argument
(
"-m"
,
"--model_file"
,
type
=
str
)
parser
.
add_argument
(
"-p"
,
"--params_file"
,
type
=
str
)
parser
.
add_argument
(
"-b"
,
"--max_batch_size"
,
type
=
int
,
default
=
1
)
parser
.
add_argument
(
"-b"
,
"--batch_size"
,
type
=
int
,
default
=
1
)
parser
.
add_argument
(
"--use_fp16"
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
"--use_gpu"
,
type
=
str2bool
,
default
=
True
)
parser
.
add_argument
(
"--ir_optim"
,
type
=
str2bool
,
default
=
True
)
parser
.
add_argument
(
"--use_tensorrt"
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
"--gpu_mem"
,
type
=
int
,
default
=
8000
)
parser
.
add_argument
(
"--enable_benchmark"
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
"--model_name"
,
type
=
str
)
return
parser
.
parse_args
()
...
...
@@ -40,15 +47,19 @@ def parse_args():
def
create_predictor
(
args
):
config
=
AnalysisConfig
(
args
.
model_file
,
args
.
params_file
)
if
args
.
use_gpu
:
config
.
enable_use_gpu
(
1000
,
0
)
config
.
enable_use_gpu
(
args
.
gpu_mem
,
0
)
else
:
config
.
disable_gpu
()
config
.
switch_ir_optim
(
args
.
ir_optim
)
# default true
config
.
disable_glog_info
()
config
.
switch_ir_optim
(
args
.
ir_optim
)
# default true
if
args
.
use_tensorrt
:
config
.
enable_tensorrt_engine
(
precision_mode
=
AnalysisConfig
.
Precision
.
Float32
,
max_batch_size
=
args
.
max_batch_size
)
precision_mode
=
AnalysisConfig
.
Precision
.
Half
if
args
.
use_fp16
else
AnalysisConfig
.
Precision
.
Float32
,
max_batch_size
=
args
.
batch_size
)
config
.
enable_memory_optim
()
# use zero copy
config
.
switch_use_feed_fetch_ops
(
False
)
predictor
=
create_paddle_predictor
(
config
)
return
predictor
...
...
@@ -64,7 +75,7 @@ def create_operators():
resize_op
=
utils
.
ResizeImage
(
resize_short
=
256
)
crop_op
=
utils
.
CropImage
(
size
=
(
size
,
size
))
normalize_op
=
utils
.
NormalizeImage
(
scale
=
img_scale
,
mean
=
img_mean
,
std
=
img_std
)
scale
=
img_scale
,
mean
=
img_mean
,
std
=
img_std
)
totensor_op
=
utils
.
ToTensor
()
return
[
decode_op
,
resize_op
,
crop_op
,
normalize_op
,
totensor_op
]
...
...
@@ -78,25 +89,37 @@ def preprocess(fname, ops):
return
data
def
postprocess
(
outputs
,
topk
=
5
):
output
=
outputs
[
0
]
prob
=
output
.
as_ndarray
().
flatten
()
index
=
prob
.
argsort
(
axis
=
0
)[
-
topk
:][::
-
1
].
astype
(
'int32'
)
return
zip
(
index
,
prob
[
index
])
def
main
():
args
=
parse_args
()
operators
=
create_operators
()
predictor
=
create_predictor
(
args
)
data
=
preprocess
(
args
.
image_file
,
operators
)
inputs
=
[
PaddleTensor
(
data
.
copy
())]
outputs
=
predictor
.
run
(
inputs
)
probs
=
postprocess
(
outputs
)
inputs
=
preprocess
(
args
.
image_file
,
operators
)
inputs
=
np
.
expand_dims
(
inputs
,
axis
=
0
).
repeat
(
args
.
batch_size
,
axis
=
0
).
copy
()
for
idx
,
prob
in
probs
:
print
(
"class id: {:d}, probability: {:.4f}"
.
format
(
idx
,
prob
))
input_names
=
predictor
.
get_input_names
()
input_tensor
=
predictor
.
get_input_tensor
(
input_names
[
0
])
input_tensor
.
copy_from_cpu
(
inputs
)
if
not
args
.
enable_benchmark
:
predictor
.
zero_copy_run
()
else
:
for
i
in
range
(
0
,
1010
):
if
i
==
10
:
start
=
time
.
time
()
predictor
.
zero_copy_run
()
end
=
time
.
time
()
fp_message
=
"FP16"
if
args
.
use_fp16
else
"FP32"
logger
.
info
(
"{0}
\t
{1}
\t
batch size: {2}
\t
time(ms): {3}"
.
format
(
args
.
model_name
,
fp_message
,
args
.
batch_size
,
end
-
start
))
output_names
=
predictor
.
get_output_names
()
output_tensor
=
predictor
.
get_output_tensor
(
output_names
[
0
])
output
=
output_tensor
.
copy_to_cpu
()
output
=
output
.
flatten
()
cls
=
np
.
argmax
(
output
)
score
=
output
[
cls
]
logger
.
info
(
"class: {0}"
.
format
(
cls
))
logger
.
info
(
"score: {0}"
.
format
(
score
))
if
__name__
==
"__main__"
:
...
...
tools/infer/run.sh
已删除
100644 → 0
浏览文件 @
87ada03d
#!/usr/bin/env bash
python ./cpp_infer.py
\
-i
=
./test.jpeg
\
-m
=
./resnet50-vd/model
\
-p
=
./resnet50-vd/params
\
--use_gpu
=
1
python ./cpp_infer.py
\
-i
=
./test.jpeg
\
-m
=
./resnet50-vd/model
\
-p
=
./resnet50-vd/params
\
--use_gpu
=
0
python py_infer.py
\
-i
=
./test.jpeg
\
-d
./resnet50-vd/
\
-m
=
model
-p
=
params
\
--use_gpu
=
0
python py_infer.py
\
-i
=
./test.jpeg
\
-d
./resnet50-vd/
\
-m
=
model
-p
=
params
\
--use_gpu
=
1
python infer.py
\
-i
=
./test.jpeg
\
-m
ResNet50_vd
\
-p
./resnet50-vd-persistable/
\
--use_gpu
=
0
python infer.py
\
-i
=
./test.jpeg
\
-m
ResNet50_vd
\
-p
./resnet50-vd-persistable/
\
--use_gpu
=
1
python export_model.py
\
-m
ResNet50_vd
\
-p
./resnet50-vd-persistable/
\
-o
./test/
python py_infer.py
\
-i
=
./test.jpeg
\
-d
./test/
\
-m
=
model
\
-p
=
params
\
--use_gpu
=
0
tools/infer/utils.py
浏览文件 @
0dcb5a67
...
...
@@ -81,5 +81,4 @@ class ToTensor(object):
def
__call__
(
self
,
img
):
img
=
img
.
transpose
((
2
,
0
,
1
))
img
=
np
.
expand_dims
(
img
,
axis
=
0
)
return
img
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录