Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
b2853121
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看板
未验证
提交
b2853121
编写于
11月 04, 2022
作者:
jm_12138
提交者:
GitHub
11月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update porn_detection_cnn (#2102)
上级
96baa2b8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
34 addition
and
98 deletion
+34
-98
modules/text/text_review/porn_detection_cnn/README.md
modules/text/text_review/porn_detection_cnn/README.md
+17
-15
modules/text/text_review/porn_detection_cnn/module.py
modules/text/text_review/porn_detection_cnn/module.py
+16
-82
modules/text/text_review/porn_detection_cnn/processor.py
modules/text/text_review/porn_detection_cnn/processor.py
+1
-1
未找到文件。
modules/text/text_review/porn_detection_cnn/README.md
浏览文件 @
b2853121
...
...
@@ -22,7 +22,7 @@
-
### 1、环境依赖
-
paddlepaddle >= 1.6.2
-
paddlehub >= 1.6.0 |
[
如何安装PaddleHub
](
../../../../docs/docs_ch/get_start/installation.rst
)
-
### 2、安装
...
...
@@ -41,45 +41,45 @@
-
```shell
$ hub run porn_detection_cnn --input_text "黄片下载"
```
-
或者
-
```shell
$ hub run porn_detection_cnn --input_file test.txt
```
- 其中test.txt存放待审查文本,每行仅放置一段待审核文本
-
通过命令行方式实现hub模型的调用,更多请见
[
PaddleHub命令行指令
](
../../../../docs/docs_ch/tutorial/cmd_usage.rst
)
-
### 2、预测代码示例
-
```python
import paddlehub as hub
porn_detection_cnn = hub.Module(name="porn_detection_cnn")
test_text = ["黄片下载", "打击黄牛党"]
results = porn_detection_cnn.detection(texts=test_text, use_gpu=True, batch_size=1)
for index, text in enumerate(test_text):
results[index]["text"] = text
for index, result in enumerate(results):
print(results[index])
# 输出结果如下:
# {'text': '黄片下载', 'porn_detection_label': 1, 'porn_detection_key': 'porn', 'porn_probs': 0.9324, 'not_porn_probs': 0.0676}
# {'text': '打击黄牛党', 'porn_detection_label': 0, 'porn_detection_key': 'not_porn', 'porn_probs': 0.0004, 'not_porn_probs': 0.9996}
```
-
### 3、API
-
```python
def detection(texts=[], data={}, use_gpu=False, batch_size=1)
```
- porn_detection_cnn预测接口,鉴定输入句子是否包含色情文案
- **参数**
...
...
@@ -145,7 +145,7 @@
-
```python
import requests
import json
# 待预测数据
text = ["黄片下载", "打击黄牛党"]
...
...
@@ -177,8 +177,10 @@
大幅提升预测性能,同时简化接口使用
*
1.2.0
移除 fluid api
-
```shell
$ hub install porn_detection_cnn==1.
1
.0
$ hub install porn_detection_cnn==1.
2
.0
```
modules/text/text_review/porn_detection_cnn/module.py
浏览文件 @
b2853121
# -*- coding:utf-8 -*-
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
json
import
math
import
os
import
six
import
paddle.fluid
as
fluid
import
paddlehub
as
hub
from
paddlehub.common.paddle_helper
import
get_variable_info
from
paddlehub.module.module
import
moduleinfo
,
serving
from
paddlehub.reader
import
tokenization
from
p
orn_detection_cnn.processor
import
load_vocab
,
preprocess
,
postprocess
@
moduleinfo
(
name
=
"porn_detection_cnn"
,
version
=
"1.1
.0"
,
summary
=
"Baidu's open-source Porn Detection Model."
,
author
=
"baidu-nlp"
,
author_email
=
""
,
type
=
"nlp/sentiment_analysis"
)
from
.processor
import
load_vocab
from
.processor
import
postprocess
from
.processor
import
preprocess
from
paddlehub.compat.task
import
tokenization
from
p
addlehub.module.module
import
moduleinfo
from
paddlehub.module.module
import
serving
@
moduleinfo
(
name
=
"porn_detection_cnn"
,
version
=
"1.2
.0"
,
summary
=
"Baidu's open-source Porn Detection Model."
,
author
=
"baidu-nlp"
,
author_email
=
""
,
type
=
"nlp/sentiment_analysis"
)
class
PornDetectionCNN
(
hub
.
NLPPredictionModule
):
def
_initialize
(
self
):
def
__init__
(
self
):
"""
initialize with the necessary elements
"""
...
...
@@ -42,42 +35,6 @@ class PornDetectionCNN(hub.NLPPredictionModule):
self
.
_set_config
()
def
context
(
self
,
trainable
=
False
):
"""
Get the input ,output and program of the pretrained porn_detection_cnn
Args:
trainable(bool): whether fine-tune the pretrained parameters of porn_detection_cnn or not
Returns:
inputs(dict): the input variables of porn_detection_cnn (words)
outputs(dict): the output variables of porn_detection_cnn (the sentiment prediction results)
main_program(Program): the main_program of porn_detection_cnn with pretrained prameters
"""
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
program
,
feed_target_names
,
fetch_targets
=
fluid
.
io
.
load_inference_model
(
dirname
=
self
.
pretrained_model_path
,
executor
=
exe
)
with
open
(
self
.
param_file
,
'r'
)
as
file
:
params_list
=
file
.
readlines
()
for
param
in
params_list
:
param
=
param
.
strip
()
var
=
program
.
global_block
().
var
(
param
)
var_info
=
get_variable_info
(
var
)
program
.
global_block
().
create_parameter
(
shape
=
var_info
[
'shape'
],
dtype
=
var_info
[
'dtype'
],
name
=
var_info
[
'name'
])
for
param
in
program
.
global_block
().
iter_parameters
():
param
.
trainable
=
trainable
for
name
,
var
in
program
.
global_block
().
vars
.
items
():
if
name
==
feed_target_names
[
0
]:
inputs
=
{
"words"
:
var
}
# output of sencond layer from the end prediction layer (fc-softmax)
if
name
==
"@HUB_porn_detection_cnn@layer_norm_1.tmp_2"
:
outputs
=
{
"class_probs"
:
fetch_targets
[
0
],
"sentence_feature"
:
var
}
return
inputs
,
outputs
,
program
@
serving
def
detection
(
self
,
texts
=
[],
data
=
{},
use_gpu
=
False
,
batch_size
=
1
):
"""
...
...
@@ -135,26 +92,3 @@ class PornDetectionCNN(hub.NLPPredictionModule):
"""
self
.
labels
=
{
"porn"
:
1
,
"not_porn"
:
0
}
return
self
.
labels
if
__name__
==
"__main__"
:
porn_detection_cnn
=
PornDetectionCNN
()
test_text
=
[
"黄片下载"
,
"打击黄牛党"
]
results
=
porn_detection_cnn
.
detection
(
texts
=
test_text
,
batch_size
=
9
)
for
index
,
text
in
enumerate
(
test_text
):
results
[
index
][
"text"
]
=
text
for
index
,
result
in
enumerate
(
results
):
if
six
.
PY2
:
print
(
json
.
dumps
(
results
[
index
],
encoding
=
"utf8"
,
ensure_ascii
=
False
))
else
:
print
(
results
[
index
])
input_dict
=
{
"text"
:
test_text
}
results
=
porn_detection_cnn
.
detection
(
data
=
input_dict
)
for
index
,
text
in
enumerate
(
test_text
):
results
[
index
][
"text"
]
=
text
for
index
,
result
in
enumerate
(
results
):
if
six
.
PY2
:
print
(
json
.
dumps
(
results
[
index
],
encoding
=
"utf8"
,
ensure_ascii
=
False
))
else
:
print
(
results
[
index
])
modules/text/text_review/porn_detection_cnn/processor.py
浏览文件 @
b2853121
# -*- coding: utf-8 -*-
import
io
import
numpy
as
np
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录