Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
f84a7383
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看板
未验证
提交
f84a7383
编写于
11月 09, 2022
作者:
C
chenjian
提交者:
GitHub
11月 09, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support zero dim tensor (#2116)
Co-authored-by:
N
wuzewu
<
wuzewu@baidu.com
>
上级
161f5814
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
58 addition
and
41 deletion
+58
-41
demo/autoaug/paddlehub_utils/trainer.py
demo/autoaug/paddlehub_utils/trainer.py
+29
-16
paddlehub/finetune/trainer.py
paddlehub/finetune/trainer.py
+29
-25
未找到文件。
demo/autoaug/paddlehub_utils/trainer.py
浏览文件 @
f84a7383
...
...
@@ -9,17 +9,19 @@
Authors: lvhaijun01@baidu.com
Date: 2020-11-24 20:46
"""
from
paddlehub.finetune.trainer
import
Trainer
import
os
from
collections
import
defaultdict
import
paddle
from
paddle.distributed
import
ParallelEnv
from
paddlehub.finetune.trainer
import
Trainer
from
paddlehub.utils.log
import
logger
from
paddlehub.utils.utils
import
Timer
class
CustomTrainer
(
Trainer
):
def
__init__
(
self
,
**
kwargs
)
->
None
:
super
(
CustomTrainer
,
self
).
__init__
(
**
kwargs
)
...
...
@@ -39,10 +41,15 @@ class CustomTrainer(Trainer):
place
=
paddle
.
CUDAPlace
(
ParallelEnv
().
dev_id
)
if
use_gpu
else
paddle
.
CPUPlace
()
paddle
.
disable_static
(
place
)
batch_sampler
=
paddle
.
io
.
DistributedBatchSampler
(
train_dataset
,
batch_size
=
batch_size
,
shuffle
=
True
,
drop_last
=
False
)
loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
batch_sampler
=
batch_sampler
,
places
=
place
,
num_workers
=
num_workers
,
return_list
=
True
)
batch_sampler
=
paddle
.
io
.
DistributedBatchSampler
(
train_dataset
,
batch_size
=
batch_size
,
shuffle
=
True
,
drop_last
=
False
)
loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
batch_sampler
=
batch_sampler
,
places
=
place
,
num_workers
=
num_workers
,
return_list
=
True
)
return
batch_sampler
,
loader
def
train_one_epoch
(
self
,
loader
:
paddle
.
io
.
DataLoader
,
timer
:
Timer
,
current_epoch
:
int
,
epochs
:
int
,
...
...
@@ -57,9 +64,9 @@ class CustomTrainer(Trainer):
self
.
optimizer_zero_grad
(
current_epoch
,
batch_idx
,
self
.
optimizer
)
# calculate metrics and loss
avg_loss
+=
loss
.
numpy
()[
0
]
avg_loss
+=
float
(
loss
)
for
metric
,
value
in
metrics
.
items
():
avg_metrics
[
metric
]
+=
value
.
numpy
()[
0
]
avg_metrics
[
metric
]
+=
float
(
value
)
timer
.
count
()
...
...
@@ -127,8 +134,9 @@ class CustomTrainer(Trainer):
self
.
log_writer
.
add_scalar
(
tag
=
'EVAL/loss'
,
step
=
timer
.
current_step
,
value
=
eval_loss
)
for
metric
,
value
in
eval_metrics
.
items
():
self
.
log_writer
.
add_scalar
(
tag
=
'EVAL/{}'
.
format
(
metric
),
step
=
timer
.
current_step
,
value
=
value
)
self
.
log_writer
.
add_scalar
(
tag
=
'EVAL/{}'
.
format
(
metric
),
step
=
timer
.
current_step
,
value
=
value
)
if
not
self
.
best_metrics
or
self
.
compare_metrics
(
self
.
best_metrics
,
eval_metrics
):
self
.
best_metrics
=
eval_metrics
...
...
@@ -147,11 +155,16 @@ class CustomTrainer(Trainer):
place
=
paddle
.
CUDAPlace
(
ParallelEnv
().
dev_id
)
if
use_gpu
else
paddle
.
CPUPlace
()
paddle
.
disable_static
(
place
)
batch_sampler
=
paddle
.
io
.
DistributedBatchSampler
(
eval_dataset
,
batch_size
=
batch_size
,
shuffle
=
False
,
drop_last
=
False
)
batch_sampler
=
paddle
.
io
.
DistributedBatchSampler
(
eval_dataset
,
batch_size
=
batch_size
,
shuffle
=
False
,
drop_last
=
False
)
loader
=
paddle
.
io
.
DataLoader
(
eval_dataset
,
batch_sampler
=
batch_sampler
,
places
=
place
,
num_workers
=
num_workers
,
return_list
=
True
)
loader
=
paddle
.
io
.
DataLoader
(
eval_dataset
,
batch_sampler
=
batch_sampler
,
places
=
place
,
num_workers
=
num_workers
,
return_list
=
True
)
return
loader
def
evaluate_process
(
self
,
loader
:
paddle
.
io
.
DataLoader
)
->
dict
:
...
...
@@ -168,10 +181,10 @@ class CustomTrainer(Trainer):
num_samples
+=
bs
if
loss
:
avg_loss
+=
loss
.
numpy
()[
0
]
*
bs
avg_loss
+=
float
(
loss
)
*
bs
for
metric
,
value
in
metrics
.
items
():
sum_metrics
[
metric
]
+=
value
.
numpy
()[
0
]
*
bs
sum_metrics
[
metric
]
+=
float
(
value
)
*
bs
# print avg metrics and loss
print_msg
=
'[Evaluation result]'
...
...
paddlehub/finetune/trainer.py
浏览文件 @
f84a7383
...
...
@@ -11,15 +11,17 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
pickle
import
time
from
collections
import
defaultdict
from
typing
import
Any
,
Callable
,
Generic
,
List
from
typing
import
Any
from
typing
import
Callable
from
typing
import
Generic
from
typing
import
List
import
paddle
import
numpy
as
np
import
paddle
from
visualdl
import
LogWriter
from
paddlehub.utils.log
import
logger
...
...
@@ -188,15 +190,16 @@ class Trainer(object):
if
not
hasattr
(
model
,
'validation_step'
):
raise
NotImplementedError
(
'The specified finetuning model does not support evaluation.'
)
batch_sampler
=
paddle
.
io
.
DistributedBatchSampler
(
train_dataset
,
batch_size
=
batch_size
,
shuffle
=
True
,
drop_last
=
False
)
loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
batch_sampler
=
batch_sampler
,
num_workers
=
num_workers
,
return_list
=
True
,
use_buffer_reader
=
True
,
collate_fn
=
collate_fn
)
batch_sampler
=
paddle
.
io
.
DistributedBatchSampler
(
train_dataset
,
batch_size
=
batch_size
,
shuffle
=
True
,
drop_last
=
False
)
loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
batch_sampler
=
batch_sampler
,
num_workers
=
num_workers
,
return_list
=
True
,
use_buffer_reader
=
True
,
collate_fn
=
collate_fn
)
steps_per_epoch
=
len
(
batch_sampler
)
timer
=
Timer
(
steps_per_epoch
*
epochs
)
...
...
@@ -214,7 +217,7 @@ class Trainer(object):
self
.
optimizer_zero_grad
(
self
.
current_epoch
,
batch_idx
,
self
.
optimizer
)
# calculate metrics and loss
avg_loss
+=
loss
.
numpy
()[
0
]
avg_loss
+=
float
(
loss
)
for
metric
,
value
in
metrics
.
items
():
if
isinstance
(
value
,
paddle
.
Tensor
):
value
=
value
.
numpy
()
...
...
@@ -235,8 +238,9 @@ class Trainer(object):
for
metric
,
value
in
avg_metrics
.
items
():
value
/=
log_interval
if
self
.
use_vdl
:
self
.
log_writer
.
add_scalar
(
tag
=
'TRAIN/{}'
.
format
(
metric
),
step
=
timer
.
current_step
,
value
=
value
)
self
.
log_writer
.
add_scalar
(
tag
=
'TRAIN/{}'
.
format
(
metric
),
step
=
timer
.
current_step
,
value
=
value
)
if
isinstance
(
value
,
np
.
ndarray
):
value
=
value
.
item
()
print_msg
+=
' {}={:.4f}'
.
format
(
metric
,
value
)
...
...
@@ -258,8 +262,9 @@ class Trainer(object):
self
.
log_writer
.
add_scalar
(
tag
=
'EVAL/loss'
,
step
=
timer
.
current_step
,
value
=
eval_loss
)
for
metric
,
value
in
eval_metrics
.
items
():
self
.
log_writer
.
add_scalar
(
tag
=
'EVAL/{}'
.
format
(
metric
),
step
=
timer
.
current_step
,
value
=
value
)
self
.
log_writer
.
add_scalar
(
tag
=
'EVAL/{}'
.
format
(
metric
),
step
=
timer
.
current_step
,
value
=
value
)
if
not
self
.
best_metrics
or
self
.
compare_metrics
(
self
.
best_metrics
,
eval_metrics
):
self
.
best_metrics
=
eval_metrics
...
...
@@ -293,15 +298,14 @@ class Trainer(object):
if
self
.
local_rank
==
0
:
batch_sampler
=
paddle
.
io
.
BatchSampler
(
eval_dataset
,
batch_size
=
batch_size
,
shuffle
=
False
,
drop_last
=
False
)
loader
=
paddle
.
io
.
DataLoader
(
eval_dataset
,
batch_sampler
=
batch_sampler
,
num_workers
=
num_workers
,
return_list
=
True
,
collate_fn
=
collate_fn
)
loader
=
paddle
.
io
.
DataLoader
(
eval_dataset
,
batch_sampler
=
batch_sampler
,
num_workers
=
num_workers
,
return_list
=
True
,
collate_fn
=
collate_fn
)
self
.
model
.
eval
()
avg_loss
=
num_samples
=
0
sum_metrics
=
defaultdict
(
int
)
avg_metrics
=
defaultdict
(
int
)
...
...
@@ -317,7 +321,7 @@ class Trainer(object):
num_samples
+=
bs
if
loss
:
avg_loss
+=
loss
.
numpy
()[
0
]
*
bs
avg_loss
+=
float
(
loss
)
*
bs
for
metric
,
value
in
metrics
.
items
():
sum_metrics
[
metric
]
+=
value
*
bs
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录