Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小毕学习代码
yolov7-obb
提交
72f1c6ff
Y
yolov7-obb
项目概览
小毕学习代码
/
yolov7-obb
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
yolov7-obb
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
72f1c6ff
编写于
2月 04, 2023
作者:
_白鹭先生_
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修复马赛克数据增强
上级
e7dcbb0a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
49 addition
and
78 deletion
+49
-78
train.py
train.py
+3
-3
utils/dataloader.py
utils/dataloader.py
+46
-75
未找到文件。
train.py
浏览文件 @
72f1c6ff
...
@@ -110,7 +110,7 @@ if __name__ == "__main__":
...
@@ -110,7 +110,7 @@ if __name__ == "__main__":
# 如果不设置model_path,pretrained = True,此时仅加载主干开始训练。
# 如果不设置model_path,pretrained = True,此时仅加载主干开始训练。
# 如果不设置model_path,pretrained = False,Freeze_Train = Fasle,此时从0开始训练,且没有冻结主干的过程。
# 如果不设置model_path,pretrained = False,Freeze_Train = Fasle,此时从0开始训练,且没有冻结主干的过程。
#----------------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------------------------------------------------#
pretrained
=
Fals
e
pretrained
=
Tru
e
#------------------------------------------------------------------#
#------------------------------------------------------------------#
# mosaic 马赛克数据增强。
# mosaic 马赛克数据增强。
# mosaic_prob 每个step有多少概率使用mosaic数据增强,默认50%。
# mosaic_prob 每个step有多少概率使用mosaic数据增强,默认50%。
...
@@ -124,7 +124,7 @@ if __name__ == "__main__":
...
@@ -124,7 +124,7 @@ if __name__ == "__main__":
# 当mosaic=True时,本代码会在special_aug_ratio范围内开启mosaic。
# 当mosaic=True时,本代码会在special_aug_ratio范围内开启mosaic。
# 默认为前70%个epoch,100个世代会开启70个世代。
# 默认为前70%个epoch,100个世代会开启70个世代。
#------------------------------------------------------------------#
#------------------------------------------------------------------#
mosaic
=
Fals
e
mosaic
=
Tru
e
mosaic_prob
=
0.5
mosaic_prob
=
0.5
mixup
=
False
mixup
=
False
mixup_prob
=
0.5
mixup_prob
=
0.5
...
@@ -186,7 +186,7 @@ if __name__ == "__main__":
...
@@ -186,7 +186,7 @@ if __name__ == "__main__":
# Freeze_Train 是否进行冻结训练
# Freeze_Train 是否进行冻结训练
# 默认先冻结主干训练后解冻训练。
# 默认先冻结主干训练后解冻训练。
#------------------------------------------------------------------#
#------------------------------------------------------------------#
Freeze_Train
=
Fals
e
Freeze_Train
=
Tru
e
#------------------------------------------------------------------#
#------------------------------------------------------------------#
# 其它训练参数:学习率、优化器、学习率下降有关
# 其它训练参数:学习率、优化器、学习率下降有关
...
...
utils/dataloader.py
浏览文件 @
72f1c6ff
...
@@ -41,18 +41,18 @@ class YoloDataset(Dataset):
...
@@ -41,18 +41,18 @@ class YoloDataset(Dataset):
# 训练时进行数据的随机增强
# 训练时进行数据的随机增强
# 验证时不进行数据的随机增强
# 验证时不进行数据的随机增强
#---------------------------------------------------#
#---------------------------------------------------#
if
self
.
mosaic
and
self
.
rand
()
<
self
.
mosaic_prob
and
self
.
epoch_now
<
self
.
epoch_length
*
self
.
special_aug_ratio
:
#
if self.mosaic and self.rand() < self.mosaic_prob and self.epoch_now < self.epoch_length * self.special_aug_ratio:
lines
=
sample
(
self
.
annotation_lines
,
3
)
lines
=
sample
(
self
.
annotation_lines
,
3
)
lines
.
append
(
self
.
annotation_lines
[
index
])
lines
.
append
(
self
.
annotation_lines
[
index
])
shuffle
(
lines
)
shuffle
(
lines
)
image
,
rbox
=
self
.
get_random_data_with_Mosaic
(
lines
,
self
.
input_shape
)
image
,
rbox
=
self
.
get_random_data_with_Mosaic
(
lines
,
self
.
input_shape
)
if
self
.
mixup
and
self
.
rand
()
<
self
.
mixup_prob
:
#
if self.mixup and self.rand() < self.mixup_prob:
lines
=
sample
(
self
.
annotation_lines
,
1
)
#
lines = sample(self.annotation_lines, 1)
image_2
,
rbox_2
=
self
.
get_random_data
(
lines
[
0
],
self
.
input_shape
,
random
=
self
.
train
)
#
image_2, rbox_2 = self.get_random_data(lines[0], self.input_shape, random = self.train)
image
,
rbox
=
self
.
get_random_data_with_MixUp
(
image
,
rbox
,
image_2
,
rbox_2
)
#
image, rbox = self.get_random_data_with_MixUp(image, rbox, image_2, rbox_2)
else
:
#
else:
image
,
rbox
=
self
.
get_random_data
(
self
.
annotation_lines
[
index
],
self
.
input_shape
,
random
=
self
.
train
)
#
image, rbox = self.get_random_data(self.annotation_lines[index], self.input_shape, random = self.train)
image
=
np
.
transpose
(
preprocess_input
(
np
.
array
(
image
,
dtype
=
np
.
float32
)),
(
2
,
0
,
1
))
image
=
np
.
transpose
(
preprocess_input
(
np
.
array
(
image
,
dtype
=
np
.
float32
)),
(
2
,
0
,
1
))
rbox
=
np
.
array
(
rbox
,
dtype
=
np
.
float32
)
rbox
=
np
.
array
(
rbox
,
dtype
=
np
.
float32
)
...
@@ -193,51 +193,20 @@ class YoloDataset(Dataset):
...
@@ -193,51 +193,20 @@ class YoloDataset(Dataset):
image
.
show
()
image
.
show
()
return
image_data
,
rbox
return
image_data
,
rbox
def
merge_bboxes
(
self
,
bboxes
,
cutx
,
cuty
):
def
merge_rboxes
(
self
,
rboxes
,
cutx
,
cuty
):
merge_bbox
=
[]
merge_rbox
=
[]
for
i
in
range
(
len
(
bboxes
)):
for
i
in
range
(
len
(
rboxes
)):
for
box
in
bboxes
[
i
]:
for
rbox
in
rboxes
[
i
]:
tmp_box
=
[]
tmp_rbox
=
[]
x1
,
y1
,
x2
,
y2
=
box
[
0
],
box
[
1
],
box
[
2
],
box
[
3
]
xc
,
yc
,
w
,
h
=
rbox
[
0
],
rbox
[
1
],
rbox
[
2
],
rbox
[
3
]
tmp_rbox
.
append
(
xc
)
if
i
==
0
:
tmp_rbox
.
append
(
yc
)
if
y1
>
cuty
or
x1
>
cutx
:
tmp_rbox
.
append
(
h
)
continue
tmp_rbox
.
append
(
w
)
if
y2
>=
cuty
and
y1
<=
cuty
:
tmp_rbox
.
append
(
rbox
[
-
1
])
y2
=
cuty
merge_rbox
.
append
(
rbox
)
if
x2
>=
cutx
and
x1
<=
cutx
:
merge_rbox
=
np
.
array
(
merge_rbox
)
x2
=
cutx
return
merge_rbox
if
i
==
1
:
if
y2
<
cuty
or
x1
>
cutx
:
continue
if
y2
>=
cuty
and
y1
<=
cuty
:
y1
=
cuty
if
x2
>=
cutx
and
x1
<=
cutx
:
x2
=
cutx
if
i
==
2
:
if
y2
<
cuty
or
x2
<
cutx
:
continue
if
y2
>=
cuty
and
y1
<=
cuty
:
y1
=
cuty
if
x2
>=
cutx
and
x1
<=
cutx
:
x1
=
cutx
if
i
==
3
:
if
y1
>
cuty
or
x2
<
cutx
:
continue
if
y2
>=
cuty
and
y1
<=
cuty
:
y2
=
cuty
if
x2
>=
cutx
and
x1
<=
cutx
:
x1
=
cutx
tmp_box
.
append
(
x1
)
tmp_box
.
append
(
y1
)
tmp_box
.
append
(
x2
)
tmp_box
.
append
(
y2
)
tmp_box
.
append
(
box
[
-
1
])
merge_bbox
.
append
(
tmp_box
)
return
merge_bbox
def
get_random_data_with_Mosaic
(
self
,
annotation_line
,
input_shape
,
jitter
=
0.3
,
hue
=
.
1
,
sat
=
0.7
,
val
=
0.4
):
def
get_random_data_with_Mosaic
(
self
,
annotation_line
,
input_shape
,
jitter
=
0.3
,
hue
=
.
1
,
sat
=
0.7
,
val
=
0.4
):
h
,
w
=
input_shape
h
,
w
=
input_shape
...
@@ -245,7 +214,7 @@ class YoloDataset(Dataset):
...
@@ -245,7 +214,7 @@ class YoloDataset(Dataset):
min_offset_y
=
self
.
rand
(
0.3
,
0.7
)
min_offset_y
=
self
.
rand
(
0.3
,
0.7
)
image_datas
=
[]
image_datas
=
[]
box_datas
=
[]
rbox_datas
=
[]
index
=
0
index
=
0
for
line
in
annotation_line
:
for
line
in
annotation_line
:
#---------------------------------#
#---------------------------------#
...
@@ -314,25 +283,21 @@ class YoloDataset(Dataset):
...
@@ -314,25 +283,21 @@ class YoloDataset(Dataset):
image_data
=
np
.
array
(
new_image
)
image_data
=
np
.
array
(
new_image
)
index
=
index
+
1
index
=
index
+
1
box_data
=
[]
rbox_data
=
[]
#---------------------------------#
#---------------------------------#
# 对box进行重新处理
# 对rbox进行重新处理
#---------------------------------#
#---------------------------------#
if
len
(
box
)
>
0
:
if
len
(
rbox
)
>
0
:
np
.
random
.
shuffle
(
box
)
np
.
random
.
shuffle
(
rbox
)
box
[:,
[
0
,
2
]]
=
box
[:,
[
0
,
2
]]
*
nw
/
iw
+
dx
rbox
[:,
0
]
=
rbox
[:,
0
]
*
nw
/
w
+
dx
/
w
box
[:,
[
1
,
3
]]
=
box
[:,
[
1
,
3
]]
*
nh
/
ih
+
dy
rbox
[:,
1
]
=
rbox
[:,
1
]
*
nh
/
h
+
dy
/
h
box
[:,
0
:
2
][
box
[:,
0
:
2
]
<
0
]
=
0
rbox
[:,
2
]
=
rbox
[:,
2
]
*
nw
/
w
box
[:,
2
][
box
[:,
2
]
>
w
]
=
w
rbox
[:,
3
]
=
rbox
[:,
3
]
*
nh
/
h
box
[:,
3
][
box
[:,
3
]
>
h
]
=
h
rbox_data
=
np
.
zeros
((
len
(
rbox
),
6
))
box_w
=
box
[:,
2
]
-
box
[:,
0
]
rbox_data
[:
len
(
rbox
)]
=
rbox
box_h
=
box
[:,
3
]
-
box
[:,
1
]
box
=
box
[
np
.
logical_and
(
box_w
>
1
,
box_h
>
1
)]
box_data
=
np
.
zeros
((
len
(
box
),
5
))
box_data
[:
len
(
box
)]
=
box
image_datas
.
append
(
image_data
)
image_datas
.
append
(
image_data
)
box_datas
.
append
(
box_data
)
rbox_datas
.
append
(
r
box_data
)
#---------------------------------#
#---------------------------------#
# 将图片分割,放在一起
# 将图片分割,放在一起
...
@@ -371,9 +336,15 @@ class YoloDataset(Dataset):
...
@@ -371,9 +336,15 @@ class YoloDataset(Dataset):
#---------------------------------#
#---------------------------------#
# 对框进行进一步的处理
# 对框进行进一步的处理
#---------------------------------#
#---------------------------------#
new_boxes
=
self
.
merge_bboxes
(
box_datas
,
cutx
,
cuty
)
new_rboxes
=
self
.
merge_rboxes
(
rbox_datas
,
cutx
,
cuty
)
# 查看旋转框是否正确
return
new_image
,
new_boxes
# newImage = Image.fromarray(new_image)
# draw = ImageDraw.Draw(newImage)
# polys = rbox2poly(new_rboxes[..., :5])*w
# for poly in polys:
# draw.polygon(xy=list(poly))
# newImage.show()
return
new_image
,
new_rboxes
def
get_random_data_with_MixUp
(
self
,
image_1
,
rbox_1
,
image_2
,
rbox_2
):
def
get_random_data_with_MixUp
(
self
,
image_1
,
rbox_1
,
image_2
,
rbox_2
):
new_image
=
np
.
array
(
image_1
,
np
.
float32
)
*
0.5
+
np
.
array
(
image_2
,
np
.
float32
)
*
0.5
new_image
=
np
.
array
(
image_1
,
np
.
float32
)
*
0.5
+
np
.
array
(
image_2
,
np
.
float32
)
*
0.5
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录