Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
3a7f6e80
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 1 年 前同步成功
通知
282
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
3a7f6e80
编写于
3月 23, 2021
作者:
七
七年期限
提交者:
GitHub
3月 23, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Upload New Modules about Food (#1330)
* add food classification module
上级
e4d3bbce
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
235 addition
and
0 deletion
+235
-0
docs/imgs/Readme_Related/Image_Classification_apple_pie.png
docs/imgs/Readme_Related/Image_Classification_apple_pie.png
+0
-0
modules/thirdparty/image/classification/food_classification/README.md
...dparty/image/classification/food_classification/README.md
+84
-0
modules/thirdparty/image/classification/food_classification/__init__.py
...arty/image/classification/food_classification/__init__.py
+1
-0
modules/thirdparty/image/classification/food_classification/module.py
...dparty/image/classification/food_classification/module.py
+147
-0
modules/thirdparty/image/classification/food_classification/requirements.txt
...image/classification/food_classification/requirements.txt
+3
-0
未找到文件。
docs/imgs/Readme_Related/Image_Classification_apple_pie.png
0 → 100644
浏览文件 @
3a7f6e80
54.6 KB
modules/thirdparty/image/classification/food_classification/README.md
0 → 100644
浏览文件 @
3a7f6e80
# food_classification
类别 图像 - 图像分类
网络 ResNet50_vd_ssld
> 模型概述
美食分类(food_classification),该模型可识别苹果派,小排骨,烤面包,牛肉馅饼,牛肉鞑靼。该PaddleHub Module支持API预测及命令行预测。
> 选择模型版本进行安装
```
shell
$
hub
install
food_classification
==
1.0.0
```
> Module API说明
```
python
def
predict
(
self
,
images
=
None
,
paths
=
None
,
batch_size
=
1
,
use_gpu
=
False
,
**
kwargs
):
```
美食分类预测接口,输入一张图像,输出该图像上食物的类别
参数
*
images (list[numpy.ndarray]): 图片数据,ndarray.shape 为 [H, W, C],BGR格式;
*
paths (list[str]): 图片的路径;
*
batch_size (int): batch 的大小;
*
use_gpu (bool): 是否使用 GPU;
返回
*
res (list[dict]): 识别结果的列表,列表中每一个元素为 dict,各字段为:
*
category_id (int): 类别的id;
*
category(str): 类别;
*
score(float): 准确率;
## 代码示例
### API调用
```
python
import
cv2
import
paddlehub
as
hub
module
=
hub
.
Module
(
name
=
"food_classification"
)
images
=
[
cv2
.
imread
(
'PATH/TO/IMAGE'
)]
# execute predict and print the result
results
=
module
.
predict
(
images
=
images
)
for
result
in
results
:
print
(
result
)
```
### 命令行调用
```
shell
$
hub run food_classification
--input_path
/PATH/TO/IMAGE
--use_gpu
True
```
## 效果展示
### 原图
<img
src=
"/docs/imgs/Readme_Related/Image_Classification_apple_pie.png"
>
### 输出结果
```
python
[{
'category_id'
:
0
,
'category'
:
'apple_pie'
,
'score'
:
0.9985085
}]
```
## 贡献者
彭兆帅、郑博培
## 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
paddlex >= 1.3.7
modules/thirdparty/image/classification/food_classification/__init__.py
0 → 100644
浏览文件 @
3a7f6e80
modules/thirdparty/image/classification/food_classification/module.py
0 → 100644
浏览文件 @
3a7f6e80
from
__future__
import
absolute_import
from
__future__
import
division
import
os
import
cv2
import
argparse
import
base64
import
paddlex
as
pdx
import
numpy
as
np
import
paddlehub
as
hub
from
paddlehub.module.module
import
moduleinfo
,
runnable
,
serving
def
base64_to_cv2
(
b64str
):
data
=
base64
.
b64decode
(
b64str
.
encode
(
'utf8'
))
data
=
np
.
fromstring
(
data
,
np
.
uint8
)
data
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
return
data
def
cv2_to_base64
(
image
):
# return base64.b64encode(image)
data
=
cv2
.
imencode
(
'.jpg'
,
image
)[
1
]
return
base64
.
b64encode
(
data
.
tostring
()).
decode
(
'utf8'
)
def
read_images
(
paths
):
images
=
[]
for
path
in
paths
:
images
.
append
(
cv2
.
imread
(
path
))
return
images
@
moduleinfo
(
name
=
'food_classification'
,
type
=
'cv/classification'
,
author
=
'郑博培、彭兆帅'
,
author_email
=
'2733821739@qq.com, 1084667371@qq.com'
,
summary
=
'Food classification'
,
version
=
'1.0.0'
)
class
MODULE
(
hub
.
Module
):
def
_initialize
(
self
,
**
kwargs
):
self
.
default_pretrained_model_path
=
os
.
path
.
join
(
self
.
directory
,
'assets'
)
self
.
model
=
pdx
.
deploy
.
Predictor
(
self
.
default_pretrained_model_path
,
**
kwargs
)
def
predict
(
self
,
images
=
None
,
paths
=
None
,
data
=
None
,
batch_size
=
1
,
use_gpu
=
False
,
**
kwargs
):
all_data
=
images
if
images
is
not
None
else
read_images
(
paths
)
total_num
=
len
(
all_data
)
loop_num
=
int
(
np
.
ceil
(
total_num
/
batch_size
))
res
=
[]
for
iter_id
in
range
(
loop_num
):
batch_data
=
list
()
handle_id
=
iter_id
*
batch_size
for
image_id
in
range
(
batch_size
):
try
:
batch_data
.
append
(
all_data
[
handle_id
+
image_id
])
except
IndexError
:
break
out
=
self
.
model
.
batch_predict
(
batch_data
,
**
kwargs
)
res
.
extend
(
out
)
return
res
@
serving
def
serving_method
(
self
,
images
,
**
kwargs
):
"""
Run as a service.
"""
images_decode
=
[
base64_to_cv2
(
image
)
for
image
in
images
]
results
=
self
.
predict
(
images_decode
,
**
kwargs
)
res
=
[]
for
result
in
results
:
if
isinstance
(
result
,
dict
):
# result_new = dict()
for
key
,
value
in
result
.
items
():
if
isinstance
(
value
,
np
.
ndarray
):
result
[
key
]
=
cv2_to_base64
(
value
)
elif
isinstance
(
value
,
np
.
generic
):
result
[
key
]
=
np
.
asscalar
(
value
)
elif
isinstance
(
result
,
list
):
for
index
in
range
(
len
(
result
)):
for
key
,
value
in
result
[
index
].
items
():
if
isinstance
(
value
,
np
.
ndarray
):
result
[
index
][
key
]
=
cv2_to_base64
(
value
)
elif
isinstance
(
value
,
np
.
generic
):
result
[
index
][
key
]
=
np
.
asscalar
(
value
)
else
:
raise
RuntimeError
(
'The result cannot be used in serving.'
)
res
.
append
(
result
)
return
res
@
runnable
def
run_cmd
(
self
,
argvs
):
"""
Run as a command.
"""
self
.
parser
=
argparse
.
ArgumentParser
(
description
=
"Run the {} module."
.
format
(
self
.
name
),
prog
=
'hub run {}'
.
format
(
self
.
name
),
usage
=
'%(prog)s'
,
add_help
=
True
)
self
.
arg_input_group
=
self
.
parser
.
add_argument_group
(
title
=
"Input options"
,
description
=
"Input data. Required"
)
self
.
arg_config_group
=
self
.
parser
.
add_argument_group
(
title
=
"Config options"
,
description
=
"Run configuration for controlling module behavior, not required."
)
self
.
add_module_config_arg
()
self
.
add_module_input_arg
()
args
=
self
.
parser
.
parse_args
(
argvs
)
results
=
self
.
predict
(
paths
=
[
args
.
input_path
],
use_gpu
=
args
.
use_gpu
)
return
results
def
add_module_config_arg
(
self
):
"""
Add the command config options.
"""
self
.
arg_config_group
.
add_argument
(
'--use_gpu'
,
type
=
bool
,
default
=
False
,
help
=
"whether use GPU or not"
)
def
add_module_input_arg
(
self
):
"""
Add the command input options.
"""
self
.
arg_input_group
.
add_argument
(
'--input_path'
,
type
=
str
,
help
=
"path to image."
)
if
__name__
==
'__main__'
:
module
=
MODULE
(
directory
=
'./new_model'
)
images
=
[
cv2
.
imread
(
'./cat.jpg'
),
cv2
.
imread
(
'./cat.jpg'
),
cv2
.
imread
(
'./cat.jpg'
)]
res
=
module
.
predict
(
images
=
images
)
modules/thirdparty/image/classification/food_classification/requirements.txt
0 → 100644
浏览文件 @
3a7f6e80
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
paddlex >= 1.3.7
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录