Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
last_winter
yolov4-pytorch
提交
c1c7677c
Y
yolov4-pytorch
项目概览
last_winter
/
yolov4-pytorch
与 Fork 源项目一致
从无法访问的项目Fork
通知
7
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
yolov4-pytorch
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
c1c7677c
编写于
7月 29, 2020
作者:
B
Bubbliiiing
提交者:
GitHub
7月 29, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update train_with_tensorboard.py
上级
8c04cbd6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
69 addition
and
53 deletion
+69
-53
train_with_tensorboard.py
train_with_tensorboard.py
+69
-53
未找到文件。
train_with_tensorboard.py
浏览文件 @
c1c7677c
...
...
@@ -15,6 +15,7 @@ from utils.dataloader import yolo_dataset_collate, YoloDataset
from
nets.yolo_training
import
YOLOLoss
,
Generator
from
nets.yolo4
import
YoloBody
from
tensorboardX
import
SummaryWriter
from
tqdm
import
tqdm
#---------------------------------------------------#
# 获得类和先验框
...
...
@@ -33,76 +34,91 @@ def get_anchors(anchors_path):
anchors
=
[
float
(
x
)
for
x
in
anchors
.
split
(
','
)]
return
np
.
array
(
anchors
).
reshape
([
-
1
,
3
,
2
])[::
-
1
,:,:]
def
get_lr
(
optimizer
):
for
param_group
in
optimizer
.
param_groups
:
return
param_group
[
'lr'
]
def
fit_ont_epoch
(
net
,
yolo_losses
,
epoch
,
epoch_size
,
epoch_size_val
,
gen
,
genval
,
Epoch
,
cuda
,
writer
):
total_loss
=
0
val_loss
=
0
start_time
=
time
.
time
()
for
iteration
,
batch
in
enumerate
(
gen
):
if
iteration
>=
epoch_size
:
break
images
,
targets
=
batch
[
0
],
batch
[
1
]
with
torch
.
no_grad
():
if
cuda
:
images
=
Variable
(
torch
.
from_numpy
(
images
).
type
(
torch
.
FloatTensor
)).
cuda
()
targets
=
[
Variable
(
torch
.
from_numpy
(
ann
).
type
(
torch
.
FloatTensor
))
for
ann
in
targets
]
else
:
images
=
Variable
(
torch
.
from_numpy
(
images
).
type
(
torch
.
FloatTensor
))
targets
=
[
Variable
(
torch
.
from_numpy
(
ann
).
type
(
torch
.
FloatTensor
))
for
ann
in
targets
]
optimizer
.
zero_grad
()
outputs
=
net
(
images
)
losses
=
[]
for
i
in
range
(
3
):
loss_item
=
yolo_losses
[
i
](
outputs
[
i
],
targets
)
losses
.
append
(
loss_item
[
0
])
loss
=
sum
(
losses
)
loss
.
backward
()
optimizer
.
step
()
# 将loss写入tensorboard,每一步都写
writer
.
add_scalar
(
'Train_loss'
,
loss
,
(
epoch
*
epoch_size
+
iteration
))
total_loss
+=
loss
waste_time
=
time
.
time
()
-
start_time
print
(
'
\n
Epoch:'
+
str
(
epoch
+
1
)
+
'/'
+
str
(
Epoch
))
print
(
'iter:'
+
str
(
iteration
)
+
'/'
+
str
(
epoch_size
)
+
' || Total Loss: %.4f || %.4fs/step'
%
(
total_loss
/
(
iteration
+
1
),
waste_time
))
start_time
=
time
.
time
()
# 将loss写入tensorboard,下面注释的是每个世代保存一次
# writer.add_scalar('Train_loss', total_loss/(iteration+1), epoch)
print
(
'Start Validation'
)
for
iteration
,
batch
in
enumerate
(
genval
):
if
iteration
>=
epoch_size_val
:
break
images_val
,
targets_val
=
batch
[
0
],
batch
[
1
]
with
torch
.
no_grad
():
if
cuda
:
images_val
=
Variable
(
torch
.
from_numpy
(
images_val
).
type
(
torch
.
FloatTensor
)).
cuda
()
targets_val
=
[
Variable
(
torch
.
from_numpy
(
ann
).
type
(
torch
.
FloatTensor
))
for
ann
in
targets_val
]
else
:
images_val
=
Variable
(
torch
.
from_numpy
(
images_val
).
type
(
torch
.
FloatTensor
))
targets_val
=
[
Variable
(
torch
.
from_numpy
(
ann
).
type
(
torch
.
FloatTensor
))
for
ann
in
targets_val
]
with
tqdm
(
total
=
epoch_size
,
desc
=
f
'Epoch
{
epoch
+
1
}
/
{
Epoch
}
'
,
postfix
=
dict
,
mininterval
=
0.3
)
as
pbar
:
for
iteration
,
batch
in
enumerate
(
gen
):
if
iteration
>=
epoch_size
:
break
images
,
targets
=
batch
[
0
],
batch
[
1
]
with
torch
.
no_grad
():
if
cuda
:
images
=
Variable
(
torch
.
from_numpy
(
images
).
type
(
torch
.
FloatTensor
)).
cuda
()
targets
=
[
Variable
(
torch
.
from_numpy
(
ann
).
type
(
torch
.
FloatTensor
))
for
ann
in
targets
]
else
:
images
=
Variable
(
torch
.
from_numpy
(
images
).
type
(
torch
.
FloatTensor
))
targets
=
[
Variable
(
torch
.
from_numpy
(
ann
).
type
(
torch
.
FloatTensor
))
for
ann
in
targets
]
optimizer
.
zero_grad
()
outputs
=
net
(
images
_val
)
outputs
=
net
(
images
)
losses
=
[]
for
i
in
range
(
3
):
loss_item
=
yolo_losses
[
i
](
outputs
[
i
],
targets
_val
)
loss_item
=
yolo_losses
[
i
](
outputs
[
i
],
targets
)
losses
.
append
(
loss_item
[
0
])
loss
=
sum
(
losses
)
val_loss
+=
loss
# 将loss写入tensorboard, 下面注释的是每一步都写
# writer.add_scalar('Val_loss',val_loss/(epoch_size_val+1), (epoch*epoch_size_val + iteration))
loss
.
backward
()
optimizer
.
step
()
# 将loss写入tensorboard,每一步都写
writer
.
add_scalar
(
'Train_loss'
,
loss
,
(
epoch
*
epoch_size
+
iteration
))
total_loss
+=
loss
waste_time
=
time
.
time
()
-
start_time
pbar
.
set_postfix
(
**
{
'total_loss'
:
total_loss
.
item
()
/
(
iteration
+
1
),
'lr'
:
get_lr
(
optimizer
),
'step/s'
:
waste_time
})
pbar
.
update
(
1
)
start_time
=
time
.
time
()
# 将loss写入tensorboard,下面注释的是每个世代保存一次
# writer.add_scalar('Train_loss', total_loss/(iteration+1), epoch)
print
(
'Start Validation'
)
with
tqdm
(
total
=
epoch_size_val
,
desc
=
f
'Epoch
{
epoch
+
1
}
/
{
Epoch
}
'
,
postfix
=
dict
,
mininterval
=
0.3
)
as
pbar
:
for
iteration
,
batch
in
enumerate
(
genval
):
if
iteration
>=
epoch_size_val
:
break
images_val
,
targets_val
=
batch
[
0
],
batch
[
1
]
with
torch
.
no_grad
():
if
cuda
:
images_val
=
Variable
(
torch
.
from_numpy
(
images_val
).
type
(
torch
.
FloatTensor
)).
cuda
()
targets_val
=
[
Variable
(
torch
.
from_numpy
(
ann
).
type
(
torch
.
FloatTensor
))
for
ann
in
targets_val
]
else
:
images_val
=
Variable
(
torch
.
from_numpy
(
images_val
).
type
(
torch
.
FloatTensor
))
targets_val
=
[
Variable
(
torch
.
from_numpy
(
ann
).
type
(
torch
.
FloatTensor
))
for
ann
in
targets_val
]
optimizer
.
zero_grad
()
outputs
=
net
(
images_val
)
losses
=
[]
for
i
in
range
(
3
):
loss_item
=
yolo_losses
[
i
](
outputs
[
i
],
targets_val
)
losses
.
append
(
loss_item
[
0
])
loss
=
sum
(
losses
)
val_loss
+=
loss
# 将loss写入tensorboard, 下面注释的是每一步都写
# writer.add_scalar('Val_loss',val_loss/(epoch_size_val+1), (epoch*epoch_size_val + iteration))
pbar
.
set_postfix
(
**
{
'total_loss'
:
val_loss
.
item
()
/
(
iteration
+
1
)})
pbar
.
update
(
1
)
# 将loss写入tensorboard,每个世代保存一次
writer
.
add_scalar
(
'Val_loss'
,
val_loss
/
(
epoch_size_val
+
1
),
epoch
)
print
(
'Finish Validation'
)
print
(
'
\n
Epoch:'
+
str
(
epoch
+
1
)
+
'/'
+
str
(
Epoch
))
print
(
'Epoch:'
+
str
(
epoch
+
1
)
+
'/'
+
str
(
Epoch
))
print
(
'Total Loss: %.4f || Val Loss: %.4f '
%
(
total_loss
/
(
epoch_size
+
1
),
val_loss
/
(
epoch_size_val
+
1
)))
print
(
'Saving state, iter:'
,
str
(
epoch
+
1
))
torch
.
save
(
model
.
state_dict
(),
'logs/Epoch%d-Total_Loss%.4f-Val_Loss%.4f.pth'
%
((
epoch
+
1
),
total_loss
/
(
epoch_size
+
1
),
val_loss
/
(
epoch_size_val
+
1
)))
if
__name__
==
"__main__"
:
#-------------------------------#
# 输入的shape大小
...
...
@@ -248,4 +264,4 @@ if __name__ == "__main__":
for
epoch
in
range
(
Freeze_Epoch
,
Unfreeze_Epoch
):
fit_ont_epoch
(
net
,
yolo_losses
,
epoch
,
epoch_size
,
epoch_size_val
,
gen
,
gen_val
,
Unfreeze_Epoch
,
Cuda
,
writer
)
lr_scheduler
.
step
()
\ No newline at end of file
lr_scheduler
.
step
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录