Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
b3e2b88f
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
1 年多 前同步成功
通知
283
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看板
提交
b3e2b88f
编写于
4月 24, 2020
作者:
W
wuzewu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add dygraph demo
上级
f5dc2a65
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
138 addition
and
0 deletion
+138
-0
demo/image_classification/img_classifier_dygraph.py
demo/image_classification/img_classifier_dygraph.py
+89
-0
demo/text_classification/text_classifier_dygraph.py
demo/text_classification/text_classifier_dygraph.py
+49
-0
未找到文件。
demo/image_classification/img_classifier_dygraph.py
0 → 100644
浏览文件 @
b3e2b88f
#coding:utf-8
import
argparse
import
os
import
numpy
as
np
import
paddlehub
as
hub
import
paddle.fluid
as
fluid
from
paddle.fluid.dygraph
import
Linear
from
paddle.fluid.dygraph.base
import
to_variable
from
paddle.fluid.optimizer
import
AdamOptimizer
# yapf: disable
parser
=
argparse
.
ArgumentParser
(
__doc__
)
parser
.
add_argument
(
"--num_epoch"
,
type
=
int
,
default
=
1
,
help
=
"Number of epoches for fine-tuning."
)
parser
.
add_argument
(
"--checkpoint_dir"
,
type
=
str
,
default
=
"paddlehub_finetune_ckpt_dygraph"
,
help
=
"Path to save log data."
)
parser
.
add_argument
(
"--batch_size"
,
type
=
int
,
default
=
16
,
help
=
"Total examples' number in batch for training."
)
parser
.
add_argument
(
"--log_interval"
,
type
=
int
,
default
=
10
,
help
=
"log interval."
)
parser
.
add_argument
(
"--save_interval"
,
type
=
int
,
default
=
10
,
help
=
"save interval."
)
# yapf: enable.
class
ResNet50
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
num_classes
,
backbone
):
super
(
ResNet50
,
self
).
__init__
()
self
.
fc
=
Linear
(
input_dim
=
2048
,
output_dim
=
num_classes
)
self
.
backbone
=
backbone
def
forward
(
self
,
imgs
):
feature_map
=
self
.
backbone
(
imgs
)
feature_map
=
fluid
.
layers
.
reshape
(
feature_map
,
shape
=
[
-
1
,
2048
])
pred
=
self
.
fc
(
feature_map
)
return
fluid
.
layers
.
softmax
(
pred
)
def
finetune
(
args
):
with
fluid
.
dygraph
.
guard
():
resnet50_vd_10w
=
hub
.
Module
(
name
=
"resnet50_vd_10w"
)
dataset
=
hub
.
dataset
.
Flowers
()
resnet
=
ResNet50
(
num_classes
=
dataset
.
num_labels
,
backbone
=
resnet50_vd_10w
)
adam
=
AdamOptimizer
(
learning_rate
=
0.001
,
parameter_list
=
resnet
.
parameters
())
state_dict_path
=
os
.
path
.
join
(
args
.
checkpoint_dir
,
'dygraph_state_dict'
)
if
os
.
path
.
exists
(
state_dict_path
+
'.pdparams'
):
state_dict
,
_
=
fluid
.
load_dygraph
(
state_dict_path
)
resnet
.
load_dict
(
state_dict
)
reader
=
hub
.
reader
.
ImageClassificationReader
(
image_width
=
resnet50_vd_10w
.
get_expected_image_width
(),
image_height
=
resnet50_vd_10w
.
get_expected_image_height
(),
images_mean
=
resnet50_vd_10w
.
get_pretrained_images_mean
(),
images_std
=
resnet50_vd_10w
.
get_pretrained_images_std
(),
dataset
=
dataset
)
train_reader
=
reader
.
data_generator
(
batch_size
=
args
.
batch_size
,
phase
=
'train'
)
loss_sum
=
acc_sum
=
cnt
=
0
# 执行epoch_num次训练
for
epoch
in
range
(
args
.
num_epoch
):
# 读取训练数据进行训练
for
batch_id
,
data
in
enumerate
(
train_reader
()):
imgs
=
np
.
array
(
data
[
0
][
0
])
labels
=
np
.
array
(
data
[
0
][
1
])
pred
=
resnet
(
imgs
)
acc
=
fluid
.
layers
.
accuracy
(
pred
,
to_variable
(
labels
))
loss
=
fluid
.
layers
.
cross_entropy
(
pred
,
to_variable
(
labels
))
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
avg_loss
.
backward
()
# 参数更新
adam
.
minimize
(
avg_loss
)
loss_sum
+=
avg_loss
.
numpy
()
*
imgs
.
shape
[
0
]
acc_sum
+=
acc
.
numpy
()
*
imgs
.
shape
[
0
]
cnt
+=
imgs
.
shape
[
0
]
if
batch_id
%
args
.
log_interval
==
0
:
print
(
'epoch {}: loss {}, acc {}'
.
format
(
epoch
,
loss_sum
/
cnt
,
acc_sum
/
cnt
))
loss_sum
=
acc_sum
=
cnt
=
0
if
batch_id
%
args
.
save_interval
==
0
:
state_dict
=
resnet
.
state_dict
()
fluid
.
save_dygraph
(
state_dict
,
state_dict_path
)
if
__name__
==
"__main__"
:
args
=
parser
.
parse_args
()
finetune
(
args
)
demo/text_classification/text_classifier_dygraph.py
0 → 100644
浏览文件 @
b3e2b88f
#coding:utf-8
import
argparse
import
os
import
numpy
as
np
import
paddlehub
as
hub
import
paddle.fluid
as
fluid
from
paddle.fluid.dygraph
import
Linear
from
paddle.fluid.dygraph.base
import
to_variable
from
paddle.fluid.optimizer
import
AdamOptimizer
# yapf: disable
parser
=
argparse
.
ArgumentParser
(
__doc__
)
parser
.
add_argument
(
"--num_epoch"
,
type
=
int
,
default
=
1
,
help
=
"Number of epoches for fine-tuning."
)
parser
.
add_argument
(
"--batch_size"
,
type
=
int
,
default
=
16
,
help
=
"Total examples' number in batch for training."
)
parser
.
add_argument
(
"--log_interval"
,
type
=
int
,
default
=
10
,
help
=
"log interval."
)
parser
.
add_argument
(
"--save_interval"
,
type
=
int
,
default
=
10
,
help
=
"save interval."
)
parser
.
add_argument
(
"--checkpoint_dir"
,
type
=
str
,
default
=
"paddlehub_finetune_ckpt_dygraph"
,
help
=
"Path to save log data."
)
parser
.
add_argument
(
"--max_seq_len"
,
type
=
int
,
default
=
512
,
help
=
"Number of words of the longest seqence."
)
# yapf: enable.
def
finetune
(
args
):
with
fluid
.
dygraph
.
guard
():
ernie
=
hub
.
Module
(
name
=
"ernie"
)
dataset
=
hub
.
dataset
.
ChnSentiCorp
()
reader
=
hub
.
reader
.
ClassifyReader
(
dataset
=
dataset
,
vocab_path
=
ernie
.
get_vocab_path
(),
max_seq_len
=
args
.
max_seq_len
,
sp_model_path
=
ernie
.
get_spm_path
(),
word_dict_path
=
ernie
.
get_word_dict_path
())
train_reader
=
reader
.
data_generator
(
batch_size
=
args
.
batch_size
,
phase
=
'train'
)
for
data_id
,
data
in
enumerate
(
train_reader
()):
input_ids
=
np
.
array
(
data
[
0
][
0
]).
astype
(
np
.
int64
)
position_ids
=
np
.
array
(
data
[
0
][
1
]).
astype
(
np
.
int64
)
segment_ids
=
np
.
array
(
data
[
0
][
2
]).
astype
(
np
.
int64
)
input_mask
=
np
.
array
(
data
[
0
][
3
]).
astype
(
np
.
float32
)
labels
=
np
.
array
(
data
[
0
][
4
]).
astype
(
np
.
int64
)
pooled_output
,
sequence_output
=
ernie
(
position_ids
,
input_mask
,
input_ids
,
segment_ids
)
if
__name__
==
"__main__"
:
args
=
parser
.
parse_args
()
finetune
(
args
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录