Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Oneflow-Inc
oneflow_yolov3
提交
ab3718cf
O
oneflow_yolov3
项目概览
Oneflow-Inc
/
oneflow_yolov3
9 个月 前同步成功
通知
4
Star
6
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
oneflow_yolov3
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
ab3718cf
编写于
7月 20, 2020
作者:
L
Lyon
提交者:
GitHub
7月 20, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #11 from Oneflow-Inc/optimize_autopep8
style: Format the code
上级
c48b9cf8
0825c452
变更
12
展开全部
隐藏空白更改
内联
并排
Showing
12 changed file
with
1236 addition
and
665 deletion
+1236
-665
oneflow_yolov3/model/data_preprocess.py
oneflow_yolov3/model/data_preprocess.py
+132
-79
oneflow_yolov3/model/data_preprocess_multiprocess.py
oneflow_yolov3/model/data_preprocess_multiprocess.py
+78
-47
oneflow_yolov3/model/predict_with_print_box.py
oneflow_yolov3/model/predict_with_print_box.py
+73
-21
oneflow_yolov3/model/python_load_data_predict_with_print_box.py
...w_yolov3/model/python_load_data_predict_with_print_box.py
+94
-31
oneflow_yolov3/model/python_load_data_predict_with_print_box_multiprocessing.py
...ython_load_data_predict_with_print_box_multiprocessing.py
+144
-51
oneflow_yolov3/model/utils.py
oneflow_yolov3/model/utils.py
+109
-46
oneflow_yolov3/model/yolo_net.py
oneflow_yolov3/model/yolo_net.py
+310
-238
oneflow_yolov3/model/yolo_test_python.py
oneflow_yolov3/model/yolo_test_python.py
+80
-36
oneflow_yolov3/model/yolo_train.py
oneflow_yolov3/model/yolo_train.py
+125
-30
oneflow_yolov3/ops/upsample_nearest.py
oneflow_yolov3/ops/upsample_nearest.py
+1
-3
oneflow_yolov3/ops/yolo_decode.py
oneflow_yolov3/ops/yolo_decode.py
+26
-24
oneflow_yolov3/ops/yolo_detect.py
oneflow_yolov3/ops/yolo_detect.py
+64
-59
未找到文件。
oneflow_yolov3/model/data_preprocess.py
浏览文件 @
ab3718cf
import
sys
import
cv2
import
os
import
numpy
as
np
import
time
import
cv2
import
numpy
as
np
def
image_preprocess
(
img_path
,
image_height
,
image_width
):
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
w
=
image_width
w
=
image_width
h
=
image_height
origin_h
=
img
.
shape
[
0
]
origin_w
=
img
.
shape
[
1
]
new_w
=
origin_w
new_h
=
origin_h
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
new_h
=
origin_h
*
w
//
origin_w
else
:
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
resize_img
=
cv2
.
resize
(
img
,(
int
(
new_w
),
int
(
new_h
)),
interpolation
=
cv2
.
INTER_CUBIC
)
resize_img
=
cv2
.
resize
(
img
,
(
int
(
new_w
),
int
(
new_h
)),
interpolation
=
cv2
.
INTER_CUBIC
)
resize_img
=
resize_img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
resize_img
=
resize_img
/
255
resize_img
[[
0
,
1
,
2
],
:,
:]
=
resize_img
[[
2
,
1
,
0
],
:,
:]
resize_img
[[
0
,
1
,
2
],
:,
:]
=
resize_img
[[
2
,
1
,
0
],
:,
:]
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
padh_before
=
int
(
dh
)
padh_after
=
int
(
h
-
new_h
-
padh_before
)
padw_before
=
int
(
dw
)
padw_after
=
int
(
w
-
new_w
-
padw_before
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
((
0
,
0
),(
padh_before
,
padh_after
),(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
(
(
0
,
0
),
(
padh_before
,
padh_after
),
(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
expand_dims
(
result
,
axis
=
0
)
origin_image_info
=
np
.
zeros
((
1
,
2
),
dtype
=
np
.
int32
)
origin_image_info
[
0
,
0
]
=
origin_h
origin_image_info
[
0
,
1
]
=
origin_w
origin_image_info
=
np
.
zeros
((
1
,
2
),
dtype
=
np
.
int32
)
origin_image_info
[
0
,
0
]
=
origin_h
origin_image_info
[
0
,
1
]
=
origin_w
return
result
,
origin_image_info
...
...
@@ -44,34 +55,45 @@ def batch_image_preprocess(img_path_list, image_height, image_width):
result_list
=
[]
origin_info_list
=
[]
for
img_path
in
img_path_list
:
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
w
=
image_width
w
=
image_width
h
=
image_height
origin_h
=
img
.
shape
[
0
]
origin_w
=
img
.
shape
[
1
]
new_w
=
origin_w
new_h
=
origin_h
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
new_h
=
origin_h
*
w
//
origin_w
else
:
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
resize_img
=
cv2
.
resize
(
img
,(
int
(
new_w
),
int
(
new_h
)),
interpolation
=
cv2
.
INTER_CUBIC
)
resize_img
=
cv2
.
resize
(
img
,
(
int
(
new_w
),
int
(
new_h
)),
interpolation
=
cv2
.
INTER_CUBIC
)
resize_img
=
resize_img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
resize_img
=
resize_img
/
255
resize_img
[[
0
,
1
,
2
],
:,
:]
=
resize_img
[[
2
,
1
,
0
],
:,
:]
resize_img
[[
0
,
1
,
2
],
:,
:]
=
resize_img
[[
2
,
1
,
0
],
:,
:]
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
padh_before
=
int
(
dh
)
padh_after
=
int
(
h
-
new_h
-
padh_before
)
padw_before
=
int
(
dw
)
padw_after
=
int
(
w
-
new_w
-
padw_before
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
((
0
,
0
),(
padh_before
,
padh_after
),(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
(
(
0
,
0
),
(
padh_before
,
padh_after
),
(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
origin_image_info
=
[
origin_h
,
origin_w
]
result_list
.
append
(
result
)
origin_info_list
.
append
(
origin_image_info
)
...
...
@@ -83,98 +105,118 @@ def batch_image_preprocess(img_path_list, image_height, image_width):
def
resize_image
(
img
,
origin_h
,
origin_w
,
image_height
,
image_width
):
w
=
image_width
h
=
image_height
resized
=
np
.
zeros
((
3
,
image_height
,
image_width
),
dtype
=
np
.
float32
)
part
=
np
.
zeros
((
3
,
origin_h
,
image_width
),
dtype
=
np
.
float32
)
resized
=
np
.
zeros
((
3
,
image_height
,
image_width
),
dtype
=
np
.
float32
)
part
=
np
.
zeros
((
3
,
origin_h
,
image_width
),
dtype
=
np
.
float32
)
w_scale
=
(
float
)(
origin_w
-
1
)
/
(
w
-
1
)
h_scale
=
(
float
)(
origin_h
-
1
)
/
(
h
-
1
)
for
c
in
range
(
w
):
if
c
==
w
-
1
or
origin_w
==
1
:
val
=
img
[:,
:,
origin_w
-
1
]
if
c
==
w
-
1
or
origin_w
==
1
:
val
=
img
[:,
:,
origin_w
-
1
]
else
:
sx
=
c
*
w_scale
ix
=
int
(
sx
)
dx
=
sx
-
ix
val
=
(
1
-
dx
)
*
img
[:,
:,
ix
]
+
dx
*
img
[:,
:,
ix
+
1
]
dx
=
sx
-
ix
val
=
(
1
-
dx
)
*
img
[:,
:,
ix
]
+
dx
*
img
[:,
:,
ix
+
1
]
part
[:,
:,
c
]
=
val
for
r
in
range
(
h
):
sy
=
r
*
h_scale
iy
=
int
(
sy
)
dy
=
sy
-
iy
val
=
(
1
-
dy
)
*
part
[:,
iy
,
:]
resized
[:,
r
,
:]
=
val
if
r
==
h
-
1
or
origin_h
==
1
:
val
=
(
1
-
dy
)
*
part
[:,
iy
,
:]
resized
[:,
r
,
:]
=
val
if
r
==
h
-
1
or
origin_h
==
1
:
continue
resized
[:,
r
,
:]
=
resized
[:,
r
,
:]
+
dy
*
part
[:,
iy
+
1
,
:]
resized
[:,
r
,
:]
=
resized
[:,
r
,
:]
+
dy
*
part
[:,
iy
+
1
,
:]
return
resized
def
image_preprocess_v2
(
img_path
,
image_height
,
image_width
):
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
img
/
255
# /255
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
w
=
image_width
w
=
image_width
h
=
image_height
origin_h
=
img
.
shape
[
1
]
origin_w
=
img
.
shape
[
2
]
new_w
=
origin_w
new_h
=
origin_h
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
new_h
=
origin_h
*
w
//
origin_w
else
:
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
resize_img
=
resize_image
(
img
,
origin_h
,
origin_w
,
new_h
,
new_w
)
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
padh_before
=
int
(
dh
)
padh_after
=
int
(
h
-
new_h
-
padh_before
)
padw_before
=
int
(
dw
)
padw_after
=
int
(
w
-
new_w
-
padw_before
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
((
0
,
0
),(
padh_before
,
padh_after
),(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
(
(
0
,
0
),
(
padh_before
,
padh_after
),
(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
expand_dims
(
result
,
axis
=
0
)
origin_image_info
=
np
.
zeros
((
1
,
2
),
dtype
=
np
.
int32
)
origin_image_info
[
0
,
0
]
=
origin_h
origin_image_info
[
0
,
1
]
=
origin_w
origin_image_info
=
np
.
zeros
((
1
,
2
),
dtype
=
np
.
int32
)
origin_image_info
[
0
,
0
]
=
origin_h
origin_image_info
[
0
,
1
]
=
origin_w
return
result
.
astype
(
np
.
float32
),
origin_image_info
def
batch_image_preprocess_v2
(
img_path_list
,
image_height
,
image_width
):
result_list
=
[]
origin_info_list
=
[]
for
i
,
img_path
in
enumerate
(
img_path_list
):
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
img
/
255
# /255
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
w
=
image_width
w
=
image_width
h
=
image_height
origin_h
=
img
.
shape
[
1
]
origin_w
=
img
.
shape
[
2
]
new_w
=
origin_w
new_h
=
origin_h
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
new_h
=
origin_h
*
w
//
origin_w
else
:
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
resize_img
=
resize_image
(
img
,
origin_h
,
origin_w
,
new_h
,
new_w
)
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
padh_before
=
int
(
dh
)
padh_after
=
int
(
h
-
new_h
-
padh_before
)
padw_before
=
int
(
dw
)
padw_after
=
int
(
w
-
new_w
-
padw_before
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
((
0
,
0
),(
padh_before
,
padh_after
),(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
(
(
0
,
0
),
(
padh_before
,
padh_after
),
(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
origin_image_info
=
[
origin_h
,
origin_w
]
result_list
.
append
(
result
)
origin_info_list
.
append
(
origin_image_info
)
...
...
@@ -183,38 +225,49 @@ def batch_image_preprocess_v2(img_path_list, image_height, image_width):
return
results
,
origin_image_infos
def
batch_image_preprocess_with_label
(
img_path_list
,
image_height
,
image_width
):
def
batch_image_preprocess_with_label
(
img_path_list
,
image_height
,
image_width
):
result_list
=
[]
origin_info_list
=
[]
labels
=
np
.
empty
((
0
,
6
),
np
.
float32
)
for
i
,
img_path
in
enumerate
(
img_path_list
):
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
img
/
255
# /255
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
w
=
image_width
w
=
image_width
h
=
image_height
origin_h
=
img
.
shape
[
1
]
origin_w
=
img
.
shape
[
2
]
new_w
=
origin_w
new_h
=
origin_h
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
new_h
=
origin_h
*
w
//
origin_w
else
:
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
resize_img
=
resize_image
(
img
,
origin_h
,
origin_w
,
new_h
,
new_w
)
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
padh_before
=
int
(
dh
)
padh_after
=
int
(
h
-
new_h
-
padh_before
)
padw_before
=
int
(
dw
)
padw_after
=
int
(
w
-
new_w
-
padw_before
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
((
0
,
0
),(
padh_before
,
padh_after
),(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
(
(
0
,
0
),
(
padh_before
,
padh_after
),
(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
origin_image_info
=
[
origin_h
,
origin_w
]
result_list
.
append
(
result
)
origin_info_list
.
append
(
origin_image_info
)
...
...
@@ -222,7 +275,8 @@ def batch_image_preprocess_with_label(img_path_list, image_height, image_width):
label
=
[]
if
os
.
path
.
isfile
(
label_path
):
with
open
(
label_path
,
'r'
)
as
f
:
x
=
np
.
array
([
x
.
split
()
for
x
in
f
.
read
().
splitlines
()],
dtype
=
np
.
float32
)
x
=
np
.
array
([
x
.
split
()
for
x
in
f
.
read
().
splitlines
()],
dtype
=
np
.
float32
)
if
x
.
size
>
0
:
# Normalized xywh to pixel xyxy format
label
=
x
.
copy
()
...
...
@@ -237,7 +291,6 @@ def batch_image_preprocess_with_label(img_path_list, image_height, image_width):
label
=
np
.
append
(
ind
,
label
,
axis
=
1
)
labels
=
np
.
append
(
labels
,
label
,
axis
=
0
)
results
=
np
.
asarray
(
result_list
).
astype
(
np
.
float32
)
origin_image_infos
=
np
.
asarray
(
origin_info_list
).
astype
(
np
.
int32
)
return
results
,
origin_image_infos
,
labels
oneflow_yolov3/model/data_preprocess_multiprocess.py
浏览文件 @
ab3718cf
from
multiprocessing
import
Process
,
Lock
,
Queue
from
multiprocessing
import
Value
,
Array
,
RawArray
from
ctypes
import
Structure
,
c_double
,
memmove
import
cv2
import
numpy
as
np
import
time
from
multiprocessing
import
Process
,
Queue
from
multiprocessing
import
Value
,
RawArray
import
cv2
import
numpy
as
np
def
resize_image
(
img
,
origin_h
,
origin_w
,
image_height
,
image_width
):
w
=
image_width
h
=
image_height
resized
=
np
.
zeros
((
3
,
image_height
,
image_width
),
dtype
=
np
.
float32
)
part
=
np
.
zeros
((
3
,
origin_h
,
image_width
),
dtype
=
np
.
float32
)
resized
=
np
.
zeros
((
3
,
image_height
,
image_width
),
dtype
=
np
.
float32
)
part
=
np
.
zeros
((
3
,
origin_h
,
image_width
),
dtype
=
np
.
float32
)
w_scale
=
(
float
)(
origin_w
-
1
)
/
(
w
-
1
)
h_scale
=
(
float
)(
origin_h
-
1
)
/
(
h
-
1
)
for
c
in
range
(
w
):
if
c
==
w
-
1
or
origin_w
==
1
:
val
=
img
[:,
:,
origin_w
-
1
]
if
c
==
w
-
1
or
origin_w
==
1
:
val
=
img
[:,
:,
origin_w
-
1
]
else
:
sx
=
c
*
w_scale
ix
=
int
(
sx
)
dx
=
sx
-
ix
val
=
(
1
-
dx
)
*
img
[:,
:,
ix
]
+
dx
*
img
[:,
:,
ix
+
1
]
dx
=
sx
-
ix
val
=
(
1
-
dx
)
*
img
[:,
:,
ix
]
+
dx
*
img
[:,
:,
ix
+
1
]
part
[:,
:,
c
]
=
val
for
r
in
range
(
h
):
sy
=
r
*
h_scale
iy
=
int
(
sy
)
dy
=
sy
-
iy
val
=
(
1
-
dy
)
*
part
[:,
iy
,
:]
resized
[:,
r
,
:]
=
val
if
r
==
h
-
1
or
origin_h
==
1
:
val
=
(
1
-
dy
)
*
part
[:,
iy
,
:]
resized
[:,
r
,
:]
=
val
if
r
==
h
-
1
or
origin_h
==
1
:
continue
resized
[:,
r
,
:]
=
resized
[:,
r
,
:]
+
dy
*
part
[:,
iy
+
1
,
:]
resized
[:,
r
,
:]
=
resized
[:,
r
,
:]
+
dy
*
part
[:,
iy
+
1
,
:]
return
resized
def
image_preprocess_v2
(
img_path
,
image_height
,
image_width
):
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
img
/
255
# /255
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
w
=
image_width
w
=
image_width
h
=
image_height
origin_h
=
img
.
shape
[
1
]
origin_w
=
img
.
shape
[
2
]
new_w
=
origin_w
new_h
=
origin_h
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
new_h
=
origin_h
*
w
//
origin_w
else
:
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
resize_img
=
resize_image
(
img
,
origin_h
,
origin_w
,
new_h
,
new_w
)
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
padh_before
=
int
(
dh
)
padh_after
=
int
(
h
-
new_h
-
padh_before
)
padw_before
=
int
(
dw
)
padw_after
=
int
(
w
-
new_w
-
padw_before
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
((
0
,
0
),(
padh_before
,
padh_after
),(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
(
(
0
,
0
),
(
padh_before
,
padh_after
),
(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
expand_dims
(
result
,
axis
=
0
)
origin_image_info
=
np
.
zeros
((
1
,
2
),
dtype
=
np
.
int32
)
origin_image_info
[
0
,
0
]
=
origin_h
origin_image_info
[
0
,
1
]
=
origin_w
origin_image_info
=
np
.
zeros
((
1
,
2
),
dtype
=
np
.
int32
)
origin_image_info
[
0
,
0
]
=
origin_h
origin_image_info
[
0
,
1
]
=
origin_w
return
result
.
astype
(
np
.
float32
),
origin_image_info
...
...
@@ -72,33 +84,43 @@ def batch_image_preprocess_v2(img_path_list, image_height, image_width):
result_list
=
[]
origin_info_list
=
[]
for
img_path
in
img_path_list
:
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
cv2
.
imread
(
img_path
,
cv2
.
IMREAD_COLOR
)
img
=
img
.
transpose
(
2
,
0
,
1
).
astype
(
np
.
float32
)
# hwc->chw
img
=
img
/
255
# /255
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
img
[[
0
,
1
,
2
],
:,
:]
=
img
[[
2
,
1
,
0
],
:,
:]
#
bgr2rgb
w
=
image_width
w
=
image_width
h
=
image_height
origin_h
=
img
.
shape
[
1
]
origin_w
=
img
.
shape
[
2
]
new_w
=
origin_w
new_h
=
origin_h
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
if
w
/
origin_w
<
h
/
origin_h
:
new_w
=
w
new_h
=
origin_h
*
w
//
origin_w
else
:
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
new_h
=
h
new_w
=
origin_w
*
h
//
origin_h
resize_img
=
resize_image
(
img
,
origin_h
,
origin_w
,
new_h
,
new_w
)
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
dw
=
(
w
-
new_w
)
//
2
dh
=
(
h
-
new_h
)
//
2
padh_before
=
int
(
dh
)
padh_after
=
int
(
h
-
new_h
-
padh_before
)
padw_before
=
int
(
dw
)
padw_after
=
int
(
w
-
new_w
-
padw_before
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
((
0
,
0
),(
padh_before
,
padh_after
),(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
result
=
np
.
pad
(
resize_img
,
pad_width
=
(
(
0
,
0
),
(
padh_before
,
padh_after
),
(
padw_before
,
padw_after
)),
mode
=
'constant'
,
constant_values
=
0.5
)
origin_image_info
=
[
origin_h
,
origin_w
]
result_list
.
append
(
result
)
origin_info_list
.
append
(
origin_image_info
)
...
...
@@ -107,7 +129,7 @@ def batch_image_preprocess_v2(img_path_list, image_height, image_width):
return
results
,
origin_image_infos
img_path_list
=
[]
img_path_list
=
[]
image_height
=
608
image_width
=
608
batch_size
=
16
...
...
@@ -117,6 +139,7 @@ queue_size = batch_size // num_worker
size
=
queue_size
*
3
*
image_height
*
image_width
origin_info_size
=
queue_size
*
2
def
worker
(
idx
,
flag
,
buffer
,
origin_info_buffer
,
queue
):
while
True
:
time1
=
time
.
time
()
...
...
@@ -124,7 +147,8 @@ def worker(idx, flag, buffer, origin_info_buffer, queue):
for
i
in
range
(
queue_size
):
path
=
queue
.
get
()
img_path_list
.
append
(
path
)
images
,
origin_image_info
=
batch_image_preprocess_v2
(
img_path_list
,
image_width
,
image_width
)
images
,
origin_image_info
=
batch_image_preprocess_v2
(
img_path_list
,
image_width
,
image_width
)
while
flag
.
value
!=
0
:
time
.
sleep
(
0.01
)
arr
=
np
.
ctypeslib
.
as_array
(
buffer
)
...
...
@@ -135,15 +159,23 @@ def worker(idx, flag, buffer, origin_info_buffer, queue):
flag
.
value
=
1
#print("make data time", time.time()-time1)
def
make_work
(
idx
):
flag
=
Value
(
'i'
,
0
,
lock
=
True
)
buffer
=
RawArray
(
'f'
,
size
)
origin_info_buffer
=
RawArray
(
'i'
,
origin_info_size
)
queue
=
Queue
(
queue_size
)
proc
=
Process
(
target
=
worker
,
args
=
(
idx
,
flag
,
buffer
,
origin_info_buffer
,
queue
))
proc
=
Process
(
target
=
worker
,
args
=
(
idx
,
flag
,
buffer
,
origin_info_buffer
,
queue
))
return
proc
,
flag
,
queue
,
buffer
,
origin_info_buffer
#if __name__ == '__main__':
#
if __name__ == '__main__':
# workers = []
# for i in range(num_worker):
# workers.append(make_work(i))
...
...
@@ -153,12 +185,12 @@ def make_work(idx):
# ss = time.time()
#
# for it in range(1, 5):
# path=['../../data/images/00000'+str(it)+'.jpg'] * 16
# path=['../../data/images/00000'+str(it)+'.jpg'] * 16
# for i in range(num_worker):
# _, _, w_q, _, _ = workers[i]
# for j in range(queue_size):
# w_q.put(path[i * queue_size + j])
#
#
# s = time.time()
# batch_image = np.zeros((num_worker, queue_size*3*image_height*image_width))
# batch_origin_info = np.zeros((num_worker, queue_size*2))
...
...
@@ -180,4 +212,3 @@ def make_work(idx):
#
# for w in workers:
# w[0].join()
oneflow_yolov3/model/predict_with_print_box.py
浏览文件 @
ab3718cf
import
oneflow
as
flow
import
time
import
argparse
import
os
import
time
import
oneflow
as
flow
import
utils
from
yolo_net
import
YoloPredictNet
import
oneflow_yolov3
from
oneflow_yolov3.ops.yolo_decode
import
yolo_predict_decoder
import
utils
import
numpy
as
np
parser
=
argparse
.
ArgumentParser
(
description
=
"flags for predict"
)
parser
.
add_argument
(
"-g"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-g"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-load"
,
"--pretrained_model"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-label_path"
,
"--label_path"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-loss_print_steps"
,
"--loss_print_steps"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-use_tensorrt"
,
"--use_tensorrt"
,
type
=
int
,
default
=
0
,
required
=
False
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-loss_print_steps"
,
"--loss_print_steps"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-use_tensorrt"
,
"--use_tensorrt"
,
type
=
int
,
default
=
0
,
required
=
False
)
parser
.
add_argument
(
"-input_dir"
,
"--input_dir"
,
type
=
str
,
required
=
False
)
parser
.
add_argument
(
"-output_dir"
,
"--output_dir"
,
type
=
str
,
default
=
'data/result'
,
required
=
False
)
parser
.
add_argument
(
"-image_paths"
,
"--image_paths"
,
type
=
str
,
nargs
=
'+'
,
required
=
False
)
parser
.
add_argument
(
"-output_dir"
,
"--output_dir"
,
type
=
str
,
default
=
'data/result'
,
required
=
False
)
parser
.
add_argument
(
"-image_paths"
,
"--image_paths"
,
type
=
str
,
nargs
=
'+'
,
required
=
False
)
args
=
parser
.
parse_args
()
...
...
@@ -36,9 +77,10 @@ if args.use_tensorrt != 0:
@
flow
.
global_function
(
func_config
)
def
yolo_user_op_eval_job
():
images
,
origin_image_info
=
yolo_predict_decoder
(
args
.
batch_size
,
args
.
image_height
,
args
.
image_width
,
args
.
image_paths
,
"yolo"
)
yolo_pos_result
,
yolo_prob_result
=
YoloPredictNet
(
images
,
origin_image_info
,
trainable
=
False
)
images
,
origin_image_info
=
yolo_predict_decoder
(
args
.
batch_size
,
args
.
image_height
,
args
.
image_width
,
args
.
image_paths
,
"yolo"
)
yolo_pos_result
,
yolo_prob_result
=
YoloPredictNet
(
images
,
origin_image_info
,
trainable
=
False
)
return
yolo_pos_result
,
yolo_prob_result
,
origin_image_info
...
...
@@ -48,7 +90,11 @@ if __name__ == "__main__":
assert
os
.
path
.
exists
(
args
.
label_path
)
if
args
.
input_dir
and
os
.
path
.
exists
(
args
.
input_dir
):
args
.
image_paths
=
[
args
.
input_dir
+
os
.
sep
+
path
for
path
in
os
.
listdir
(
args
.
input_dir
)]
args
.
image_paths
=
[
args
.
input_dir
+
os
.
sep
+
path
for
path
in
os
.
listdir
(
args
.
input_dir
)]
flow
.
config
.
gpu_device_num
(
args
.
gpu_num_per_node
)
# set gpu num
check_point
=
flow
.
train
.
CheckPoint
()
...
...
@@ -62,11 +108,17 @@ if __name__ == "__main__":
yolo_pos
,
yolo_prob
,
origin_image_info
=
yolo_user_op_eval_job
().
get
()
print
(
'cost: %.4f ms'
%
(
1000
*
(
time
.
time
()
-
start
)))
if
python_nms
:
bboxes
=
utils
.
postprocess_boxes_new
(
yolo_pos
[
0
],
yolo_prob
[
0
],
origin_image_info
[
0
],
0.3
)
bboxes
=
utils
.
postprocess_boxes_new
(
yolo_pos
[
0
],
yolo_prob
[
0
],
origin_image_info
[
0
],
0.3
)
bboxes
=
utils
.
nms
(
bboxes
,
0.45
,
method
=
'nms'
)
else
:
bboxes
=
utils
.
postprocess_boxes
(
yolo_pos
[
0
],
yolo_prob
[
0
],
origin_image_info
[
0
],
0.3
)
bboxes
=
utils
.
postprocess_boxes
(
yolo_pos
[
0
],
yolo_prob
[
0
],
origin_image_info
[
0
],
0.3
)
utils
.
save_detected_images
([
args
.
image_paths
[
i
]],
[
bboxes
],
args
.
label_path
,
args
.
output_dir
)
print
(
'%s >> bboxes:'
%
args
.
image_paths
[
i
],
bboxes
,
'
\n
------------------------------------------------------------------------'
)
utils
.
save_detected_images
([
args
.
image_paths
[
i
]],
[
bboxes
],
args
.
label_path
,
args
.
output_dir
)
print
(
'%s >> bboxes:'
%
args
.
image_paths
[
i
],
bboxes
,
'
\n
------------------------------------------------------------------------'
)
oneflow_yolov3/model/python_load_data_predict_with_print_box.py
浏览文件 @
ab3718cf
import
oneflow
as
flow
import
time
import
argparse
import
math
import
os
import
time
import
oneflow
as
flow
import
utils
from
data_preprocess
import
batch_image_preprocess_v2
from
yolo_net
import
YoloPredictNet
from
data_preprocess
import
image_preprocess_v2
,
batch_image_preprocess_v2
import
oneflow_yolov3
import
utils
import
math
parser
=
argparse
.
ArgumentParser
(
description
=
"flags for predict"
)
parser
.
add_argument
(
"-g"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-g"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-load"
,
"--pretrained_model"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-label_path"
,
"--label_path"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
8
,
required
=
False
)
parser
.
add_argument
(
"-loss_print_steps"
,
"--loss_print_steps"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-use_tensorrt"
,
"--use_tensorrt"
,
type
=
int
,
default
=
0
,
required
=
False
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
8
,
required
=
False
)
parser
.
add_argument
(
"-loss_print_steps"
,
"--loss_print_steps"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-use_tensorrt"
,
"--use_tensorrt"
,
type
=
int
,
default
=
0
,
required
=
False
)
parser
.
add_argument
(
"-input_dir"
,
"--input_dir"
,
type
=
str
,
required
=
False
)
parser
.
add_argument
(
"-output_dir"
,
"--output_dir"
,
type
=
str
,
default
=
'data/result'
,
required
=
False
)
parser
.
add_argument
(
"-image_paths"
,
"--image_paths"
,
type
=
str
,
nargs
=
'+'
,
required
=
False
)
parser
.
add_argument
(
"-output_dir"
,
"--output_dir"
,
type
=
str
,
default
=
'data/result'
,
required
=
False
)
parser
.
add_argument
(
"-image_paths"
,
"--image_paths"
,
type
=
str
,
nargs
=
'+'
,
required
=
False
)
args
=
parser
.
parse_args
()
...
...
@@ -31,19 +73,30 @@ func_config.default_distribute_strategy(flow.distribute.consistent_strategy())
func_config
.
default_data_type
(
flow
.
float
)
if
args
.
use_tensorrt
!=
0
:
func_config
.
use_tensorrt
(
True
)
#func_config.tensorrt.use_fp16()
#
func_config.tensorrt.use_fp16()
nms
=
True
input_blob_def_dict
=
{
"images"
:
flow
.
FixedTensorDef
((
args
.
batch_size
,
3
,
args
.
image_height
,
args
.
image_width
),
dtype
=
flow
.
float
),
"origin_image_info"
:
flow
.
FixedTensorDef
((
args
.
batch_size
,
2
),
dtype
=
flow
.
int32
),
"images"
:
flow
.
FixedTensorDef
(
(
args
.
batch_size
,
3
,
args
.
image_height
,
args
.
image_width
),
dtype
=
flow
.
float
),
"origin_image_info"
:
flow
.
FixedTensorDef
(
(
args
.
batch_size
,
2
),
dtype
=
flow
.
int32
),
}
@
flow
.
global_function
(
func_config
)
def
yolo_user_op_eval_job
(
images
=
input_blob_def_dict
[
"images"
],
origin_image_info
=
input_blob_def_dict
[
"origin_image_info"
]):
yolo_pos_result
,
yolo_prob_result
=
YoloPredictNet
(
images
,
origin_image_info
,
trainable
=
False
)
def
yolo_user_op_eval_job
(
images
=
input_blob_def_dict
[
"images"
],
origin_image_info
=
input_blob_def_dict
[
"origin_image_info"
]):
yolo_pos_result
,
yolo_prob_result
=
YoloPredictNet
(
images
,
origin_image_info
,
trainable
=
False
)
return
yolo_pos_result
,
yolo_prob_result
,
origin_image_info
...
...
@@ -53,7 +106,11 @@ if __name__ == "__main__":
assert
os
.
path
.
exists
(
args
.
label_path
)
if
args
.
input_dir
and
os
.
path
.
exists
(
args
.
input_dir
):
args
.
image_paths
=
[
args
.
input_dir
+
os
.
sep
+
path
for
path
in
os
.
listdir
(
args
.
input_dir
)]
args
.
image_paths
=
[
args
.
input_dir
+
os
.
sep
+
path
for
path
in
os
.
listdir
(
args
.
input_dir
)]
flow
.
config
.
gpu_device_num
(
args
.
gpu_num_per_node
)
# load model
...
...
@@ -64,21 +121,27 @@ if __name__ == "__main__":
python_nms
=
True
path_list
=
args
.
image_paths
iter_num
=
math
.
floor
(
len
(
args
.
image_paths
)
/
float
(
args
.
batch_size
))
iter_num
=
math
.
floor
(
len
(
args
.
image_paths
)
/
float
(
args
.
batch_size
))
for
i
in
range
(
iter_num
):
paths
=
path_list
[
i
*
args
.
batch_size
:(
i
+
1
)
*
args
.
batch_size
]
images
,
origin_image_info
=
batch_image_preprocess_v2
(
paths
,
args
.
image_height
,
args
.
image_width
)
paths
=
path_list
[
i
*
args
.
batch_size
:(
i
+
1
)
*
args
.
batch_size
]
images
,
origin_image_info
=
batch_image_preprocess_v2
(
paths
,
args
.
image_height
,
args
.
image_width
)
start
=
time
.
time
()
yolo_pos
,
yolo_prob
,
origin_image_info
=
yolo_user_op_eval_job
(
images
,
origin_image_info
).
get
()
yolo_pos
,
yolo_prob
,
origin_image_info
=
yolo_user_op_eval_job
(
images
,
origin_image_info
).
get
()
print
(
'cost: %.4f ms'
%
(
1000
*
(
time
.
time
()
-
start
)))
if
python_nms
:
bboxes
=
utils
.
batch_postprocess_boxes_new
(
yolo_pos
,
yolo_prob
,
origin_image_info
,
0.3
)
bboxes
=
utils
.
batch_postprocess_boxes_new
(
yolo_pos
,
yolo_prob
,
origin_image_info
,
0.3
)
else
:
bboxes
=
utils
.
batch_postprocess_boxes
(
yolo_pos
,
yolo_prob
,
origin_image_info
,
0.3
)
utils
.
save_detected_images
(
paths
,
bboxes
,
args
.
label_path
,
args
.
output_dir
)
print
(
'iter:%d >> bboxes:'
%
i
,
bboxes
,
'
\n
------------------------------------------------------------------------'
)
bboxes
=
utils
.
batch_postprocess_boxes
(
yolo_pos
,
yolo_prob
,
origin_image_info
,
0.3
)
utils
.
save_detected_images
(
paths
,
bboxes
,
args
.
label_path
,
args
.
output_dir
)
print
(
'iter:%d >> bboxes:'
%
i
,
bboxes
,
'
\n
------------------------------------------------------------------------'
)
oneflow_yolov3/model/python_load_data_predict_with_print_box_multiprocessing.py
浏览文件 @
ab3718cf
import
oneflow
as
flow
import
oneflow.core.operator.op_conf_pb2
as
op_conf_util
import
time
import
argparse
import
numpy
as
np
import
os
from
yolo_net
import
YoloPredictNet
import
time
import
numpy
as
np
import
oneflow
as
flow
from
data_preprocess_multiprocess
import
make_work
from
yolo_net
import
YoloPredictNet
import
oneflow_yolov3
from
oneflow_yolov3.ops.yolo_decode
import
yolo_predict_decoder
parser
=
argparse
.
ArgumentParser
(
description
=
"flags for predict"
)
parser
.
add_argument
(
"-g"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-g"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-load"
,
"--pretrained_model"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-img_list"
,
"--image_list_path"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-label_to_name_file"
,
"--label_to_name_file"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-total_batch_num"
,
"--total_batch_num"
,
type
=
int
,
default
=
308
,
required
=
False
)
parser
.
add_argument
(
"-loss_print_steps"
,
"--loss_print_steps"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-use_tensorrt"
,
"--use_tensorrt"
,
type
=
int
,
default
=
0
,
required
=
False
)
parser
.
add_argument
(
"-image_path_list"
,
"--image_path_list"
,
type
=
str
,
nargs
=
'+'
,
required
=
True
)
parser
.
add_argument
(
"-label_to_name_file"
,
"--label_to_name_file"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-total_batch_num"
,
"--total_batch_num"
,
type
=
int
,
default
=
308
,
required
=
False
)
parser
.
add_argument
(
"-loss_print_steps"
,
"--loss_print_steps"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-use_tensorrt"
,
"--use_tensorrt"
,
type
=
int
,
default
=
0
,
required
=
False
)
parser
.
add_argument
(
"-image_path_list"
,
"--image_path_list"
,
type
=
str
,
nargs
=
'+'
,
required
=
True
)
args
=
parser
.
parse_args
()
...
...
@@ -36,50 +80,73 @@ func_config.default_distribute_strategy(flow.distribute.consistent_strategy())
func_config
.
default_data_type
(
flow
.
float
)
if
args
.
use_tensorrt
!=
0
:
func_config
.
use_tensorrt
(
True
)
#func_config.tensorrt.use_fp16()
#
func_config.tensorrt.use_fp16()
label_2_name
=
[]
label_2_name
=
[]
with
open
(
args
.
label_to_name_file
,
'r'
)
as
f
:
label_2_name
=
f
.
readlines
()
with
open
(
args
.
label_to_name_file
,
'r'
)
as
f
:
label_2_name
=
f
.
readlines
()
nms
=
True
def
print_detect_box
(
positions
,
probs
):
if
nms
==
True
:
if
nms
:
batch_size
=
positions
.
shape
[
0
]
for
k
in
range
(
batch_size
):
for
i
in
range
(
1
,
81
):
for
j
in
range
(
positions
.
shape
[
2
]):
if
positions
[
k
][
i
][
j
][
1
]
!=
0
and
positions
[
k
][
i
][
j
][
2
]
!=
0
and
probs
[
k
][
i
][
j
]
!=
0
:
print
(
label_2_name
[
i
-
1
],
" "
,
probs
[
k
][
i
][
j
]
*
100
,
"%"
,
" "
,
positions
[
k
][
i
][
j
][
0
],
" "
,
positions
[
k
][
i
][
j
][
1
],
" "
,
positions
[
k
][
i
][
j
][
2
],
" "
,
positions
[
k
][
i
][
j
][
3
])
if
positions
[
k
][
i
][
j
][
1
]
!=
0
and
positions
[
k
][
i
][
j
][
2
]
!=
0
and
probs
[
k
][
i
][
j
]
!=
0
:
print
(
label_2_name
[
i
-
1
],
" "
,
probs
[
k
][
i
][
j
]
*
100
,
"%"
,
" "
,
positions
[
k
][
i
][
j
][
0
],
" "
,
positions
[
k
][
i
][
j
][
1
],
" "
,
positions
[
k
][
i
][
j
][
2
],
" "
,
positions
[
k
][
i
][
j
][
3
])
else
:
for
j
in
range
(
positions
.
shape
[
1
]):
for
i
in
range
(
1
,
81
):
if
positions
[
0
][
j
][
1
]
!=
0
and
positions
[
0
][
j
][
2
]
!=
0
and
probs
[
0
][
j
][
i
]
!=
0
:
print
(
label_2_name
[
i
-
1
],
" "
,
probs
[
0
][
j
][
i
]
*
100
,
"%"
,
" "
,
positions
[
0
][
j
][
0
],
" "
,
positions
[
0
][
j
][
1
],
" "
,
positions
[
0
][
j
][
2
],
" "
,
positions
[
0
][
j
][
3
])
if
positions
[
0
][
j
][
1
]
!=
0
and
positions
[
0
][
j
][
2
]
!=
0
and
probs
[
0
][
j
][
i
]
!=
0
:
print
(
label_2_name
[
i
-
1
],
" "
,
probs
[
0
][
j
][
i
]
*
100
,
"%"
,
" "
,
positions
[
0
][
j
][
0
],
" "
,
positions
[
0
][
j
][
1
],
" "
,
positions
[
0
][
j
][
2
],
" "
,
positions
[
0
][
j
][
3
])
def
xywh_2_x1y1x2y2
(
x
,
y
,
w
,
h
,
origin_image
):
x1
=
(
x
-
w
/
2.
)
*
origin_image
[
1
]
x2
=
(
x
+
w
/
2.
)
*
origin_image
[
1
]
y1
=
(
y
-
h
/
2.
)
*
origin_image
[
0
]
y2
=
(
y
+
h
/
2.
)
*
origin_image
[
0
]
x1
=
(
x
-
w
/
2.
)
*
origin_image
[
1
]
x2
=
(
x
+
w
/
2.
)
*
origin_image
[
1
]
y1
=
(
y
-
h
/
2.
)
*
origin_image
[
0
]
y2
=
(
y
+
h
/
2.
)
*
origin_image
[
0
]
return
x1
,
y1
,
x2
,
y2
def
batch_boxes
(
positions
,
probs
,
origin_image_info
):
batch_size
=
positions
.
shape
[
0
]
batch_list
=
[]
if
nms
==
True
:
batch_list
=
[]
if
nms
:
for
k
in
range
(
batch_size
):
box_list
=
[]
for
i
in
range
(
1
,
81
):
for
j
in
range
(
positions
.
shape
[
2
]):
if
positions
[
k
][
i
][
j
][
2
]
!=
0
and
positions
[
k
][
i
][
j
][
3
]
!=
0
and
probs
[
k
][
i
][
j
]
!=
0
:
x1
,
y1
,
x2
,
y2
=
xywh_2_x1y1x2y2
(
positions
[
k
][
i
][
j
][
0
],
positions
[
k
][
i
][
j
][
1
],
positions
[
k
][
i
][
j
][
2
],
positions
[
k
][
i
][
j
][
3
],
origin_image_info
[
k
])
bbox
=
[
i
-
1
,
x1
,
y1
,
x2
,
y2
,
probs
[
k
][
i
][
j
]]
if
positions
[
k
][
i
][
j
][
2
]
!=
0
and
positions
[
k
][
i
][
j
][
3
]
!=
0
and
probs
[
k
][
i
][
j
]
!=
0
:
x1
,
y1
,
x2
,
y2
=
xywh_2_x1y1x2y2
(
positions
[
k
][
i
][
j
][
0
],
positions
[
k
][
i
][
j
][
1
],
positions
[
k
][
i
][
j
][
2
],
positions
[
k
][
i
][
j
][
3
],
origin_image_info
[
k
])
bbox
=
[
i
-
1
,
x1
,
y1
,
x2
,
y2
,
probs
[
k
][
i
][
j
]]
box_list
.
append
(
bbox
)
batch_list
.
append
(
np
.
asarray
(
box_list
))
else
:
...
...
@@ -87,23 +154,35 @@ def batch_boxes(positions, probs, origin_image_info):
box_list
=
[]
for
j
in
range
(
positions
.
shape
[
1
]):
for
i
in
range
(
1
,
81
):
if
positions
[
k
][
j
][
2
]
!=
0
and
positions
[
k
][
j
][
3
]
!=
0
and
probs
[
k
][
j
][
i
]
!=
0
:
x1
,
y1
,
x2
,
y2
=
xywh_2_x1y1x2y2
(
positions
[
k
][
j
][
0
],
positions
[
k
][
j
][
1
],
positions
[
k
][
j
][
2
],
positions
[
k
][
j
][
3
],
origin_image_info
[
k
])
bbox
=
[
i
-
1
,
x1
,
y1
,
x2
,
y2
,
probs
[
k
][
j
][
i
]]
if
positions
[
k
][
j
][
2
]
!=
0
and
positions
[
k
][
j
][
3
]
!=
0
and
probs
[
k
][
j
][
i
]
!=
0
:
x1
,
y1
,
x2
,
y2
=
xywh_2_x1y1x2y2
(
positions
[
k
][
j
][
0
],
positions
[
k
][
j
][
1
],
positions
[
k
][
j
][
2
],
positions
[
k
][
j
][
3
],
origin_image_info
[
k
])
bbox
=
[
i
-
1
,
x1
,
y1
,
x2
,
y2
,
probs
[
k
][
j
][
i
]]
box_list
.
append
(
bbox
)
batch_list
.
append
(
np
.
asarray
(
box_list
))
return
batch_list
input_blob_def_dict
=
{
"images"
:
flow
.
FixedTensorDef
((
args
.
batch_size
,
3
,
args
.
image_height
,
args
.
image_width
),
dtype
=
flow
.
float
),
"origin_image_info"
:
flow
.
FixedTensorDef
((
args
.
batch_size
,
2
),
dtype
=
flow
.
int32
),
"images"
:
flow
.
FixedTensorDef
(
(
args
.
batch_size
,
3
,
args
.
image_height
,
args
.
image_width
),
dtype
=
flow
.
float
),
"origin_image_info"
:
flow
.
FixedTensorDef
(
(
args
.
batch_size
,
2
),
dtype
=
flow
.
int32
),
}
@
flow
.
global_function
((
func_config
))
def
yolo_user_op_eval_job
(
images
=
input_blob_def_dict
[
"images"
],
origin_image_info
=
input_blob_def_dict
[
"origin_image_info"
]):
yolo_pos_result
,
yolo_prob_result
=
YoloPredictNet
(
images
,
origin_image_info
,
trainable
=
False
)
def
yolo_user_op_eval_job
(
images
=
input_blob_def_dict
[
"images"
],
origin_image_info
=
input_blob_def_dict
[
"origin_image_info"
]):
yolo_pos_result
,
yolo_prob_result
=
YoloPredictNet
(
images
,
origin_image_info
,
trainable
=
False
)
return
yolo_pos_result
,
yolo_prob_result
,
origin_image_info
...
...
@@ -117,12 +196,17 @@ if __name__ == "__main__":
else
:
check_point
.
load
(
args
.
pretrained_model
)
fmt_str
=
"{:>12} {:>12.10f} {:>12.10f} {:>12.3f}"
print
(
"{:>12} {:>12} {:>12} {:>12}"
.
format
(
"iter"
,
"reg loss value"
,
"cls loss value"
,
"time"
))
print
(
"{:>12} {:>12} {:>12} {:>12}"
.
format
(
"iter"
,
"reg loss value"
,
"cls loss value"
,
"time"
))
global
cur_time
cur_time
=
time
.
time
()
num_worker
=
4
queue_size
=
args
.
batch_size
//
num_worker
num_worker
=
4
queue_size
=
args
.
batch_size
//
num_worker
image_height
=
608
image_width
=
608
workers
=
[]
...
...
@@ -131,31 +215,40 @@ if __name__ == "__main__":
for
w
in
workers
:
w
[
0
].
start
()
batch_image
=
np
.
zeros
((
num_worker
,
queue_size
*
3
*
image_height
*
image_width
),
dtype
=
np
.
float32
)
batch_origin_info
=
np
.
zeros
((
num_worker
,
queue_size
*
2
),
dtype
=
np
.
int32
)
batch_image
=
np
.
zeros
(
(
num_worker
,
queue_size
*
3
*
image_height
*
image_width
),
dtype
=
np
.
float32
)
batch_origin_info
=
np
.
zeros
((
num_worker
,
queue_size
*
2
),
dtype
=
np
.
int32
)
image_list_len
=
len
(
args
.
image_path_list
)
for
step
in
range
(
args
.
total_batch_num
):
#img_path = args.image_path_list[step % image_list_len]
path
=
[
'data/images/00000'
+
str
(
step
+
1
)
+
'.jpg'
]
*
16
path
=
[
'data/images/00000'
+
str
(
step
+
1
)
+
'.jpg'
]
*
16
for
i
in
range
(
num_worker
):
_
,
_
,
w_q
,
_
,
_
=
workers
[
i
]
for
j
in
range
(
queue_size
):
w_q
.
put
(
path
[
i
*
queue_size
+
j
])
for
i
in
range
(
num_worker
):
(
proc
,
w_f
,
_
,
w_b
,
w_origin_info_b
)
=
workers
[
i
]
while
w_f
.
value
==
0
:
time
.
sleep
(
0.01
)
ret
=
np
.
ctypeslib
.
as_array
(
w_b
)
batch_image
[
i
,:]
=
ret
batch_image
[
i
,
:]
=
ret
ret_origin_info
=
np
.
ctypeslib
.
as_array
(
w_origin_info_b
)
batch_origin_info
[
i
,:]
=
ret_origin_info
batch_origin_info
[
i
,
:]
=
ret_origin_info
w_f
.
value
=
0
yolo_pos
,
yolo_prob
,
origin_image_info
=
yolo_user_op_eval_job
(
batch_image
.
reshape
(
args
.
batch_size
,
3
,
image_height
,
image_width
),
batch_origin_info
.
reshape
(
args
.
batch_size
,
2
)).
get
()
yolo_pos
,
yolo_prob
,
origin_image_info
=
yolo_user_op_eval_job
(
batch_image
.
reshape
(
args
.
batch_size
,
3
,
image_height
,
image_width
),
batch_origin_info
.
reshape
(
args
.
batch_size
,
2
)).
get
()
batch_list
=
batch_boxes
(
yolo_pos
,
yolo_prob
,
origin_image_info
)
print
(
"batch_list"
,
batch_list
)
for
w
in
workers
:
w
[
0
].
terminate
()
...
...
oneflow_yolov3/model/utils.py
浏览文件 @
ab3718cf
...
...
@@ -23,7 +23,8 @@ def draw_bbox_for_batch(image, bboxes, classes, show_label=True):
image_h
,
image_w
,
_
=
image
.
shape
hsv_tuples
=
[(
1.0
*
x
/
num_classes
,
1.
,
1.
)
for
x
in
range
(
num_classes
)]
colors
=
list
(
map
(
lambda
x
:
colorsys
.
hsv_to_rgb
(
*
x
),
hsv_tuples
))
colors
=
list
(
map
(
lambda
x
:
(
int
(
x
[
0
]
*
255
),
int
(
x
[
1
]
*
255
),
int
(
x
[
2
]
*
255
)),
colors
))
colors
=
list
(
map
(
lambda
x
:
(
int
(
x
[
0
]
*
255
),
int
(
x
[
1
]
*
255
),
int
(
x
[
2
]
*
255
)),
colors
))
random
.
seed
(
0
)
random
.
shuffle
(
colors
)
...
...
@@ -43,11 +44,23 @@ def draw_bbox_for_batch(image, bboxes, classes, show_label=True):
if
show_label
:
bbox_mess
=
'%s: %.2f'
%
(
classes
[
class_ind
],
score
)
t_size
=
cv2
.
getTextSize
(
bbox_mess
,
0
,
fontScale
,
thickness
=
bbox_thick
//
2
)[
0
]
cv2
.
rectangle
(
image
,
c1
,
(
c1
[
0
]
+
t_size
[
0
],
c1
[
1
]
-
t_size
[
1
]
-
3
),
bbox_color
,
-
1
)
# filled
cv2
.
putText
(
image
,
bbox_mess
,
(
c1
[
0
],
c1
[
1
]
-
2
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
fontScale
,
(
0
,
0
,
0
),
bbox_thick
//
2
,
lineType
=
cv2
.
LINE_AA
)
t_size
=
cv2
.
getTextSize
(
bbox_mess
,
0
,
fontScale
,
thickness
=
bbox_thick
//
2
)[
0
]
cv2
.
rectangle
(
image
,
c1
,
(
c1
[
0
]
+
t_size
[
0
],
c1
[
1
]
-
t_size
[
1
]
-
3
),
bbox_color
,
-
1
)
# filled
cv2
.
putText
(
image
,
bbox_mess
,
(
c1
[
0
],
c1
[
1
]
-
2
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
fontScale
,
(
0
,
0
,
0
),
bbox_thick
//
2
,
lineType
=
cv2
.
LINE_AA
)
return
image
...
...
@@ -61,7 +74,8 @@ def draw_bbox(image, bboxes, classes, show_label=True):
image_h
,
image_w
,
_
=
image
.
shape
hsv_tuples
=
[(
1.0
*
x
/
num_classes
,
1.
,
1.
)
for
x
in
range
(
num_classes
)]
colors
=
list
(
map
(
lambda
x
:
colorsys
.
hsv_to_rgb
(
*
x
),
hsv_tuples
))
colors
=
list
(
map
(
lambda
x
:
(
int
(
x
[
0
]
*
255
),
int
(
x
[
1
]
*
255
),
int
(
x
[
2
]
*
255
)),
colors
))
colors
=
list
(
map
(
lambda
x
:
(
int
(
x
[
0
]
*
255
),
int
(
x
[
1
]
*
255
),
int
(
x
[
2
]
*
255
)),
colors
))
random
.
seed
(
0
)
random
.
shuffle
(
colors
)
...
...
@@ -83,21 +97,38 @@ def draw_bbox(image, bboxes, classes, show_label=True):
if
show_label
:
bbox_mess
=
'%s: %.2f'
%
(
classes
[
class_ind
],
score
)
t_size
=
cv2
.
getTextSize
(
bbox_mess
,
0
,
fontScale
,
thickness
=
bbox_thick
//
2
)[
0
]
cv2
.
rectangle
(
image
,
c1
,
(
c1
[
0
]
+
t_size
[
0
],
c1
[
1
]
-
t_size
[
1
]
-
3
),
bbox_color
,
-
1
)
# filled
cv2
.
putText
(
image
,
bbox_mess
,
(
c1
[
0
],
c1
[
1
]
-
2
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
fontScale
,
(
0
,
0
,
0
),
bbox_thick
//
2
,
lineType
=
cv2
.
LINE_AA
)
t_size
=
cv2
.
getTextSize
(
bbox_mess
,
0
,
fontScale
,
thickness
=
bbox_thick
//
2
)[
0
]
cv2
.
rectangle
(
image
,
c1
,
(
c1
[
0
]
+
t_size
[
0
],
c1
[
1
]
-
t_size
[
1
]
-
3
),
bbox_color
,
-
1
)
# filled
cv2
.
putText
(
image
,
bbox_mess
,
(
c1
[
0
],
c1
[
1
]
-
2
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
fontScale
,
(
0
,
0
,
0
),
bbox_thick
//
2
,
lineType
=
cv2
.
LINE_AA
)
return
image
def
save_detected_images
(
image_paths
,
bboxes
,
coco_label_path
,
output_dir
=
'data/result'
):
def
save_detected_images
(
image_paths
,
bboxes
,
coco_label_path
,
output_dir
=
'data/result'
):
"""
draw bbox on the origin image,and save batch detected result
bboxes:[[ class_id, x1, x2, y1, y2, possibility], [ class, x1, x2, y1, y2, possibility]...]
"""
assert
len
(
image_paths
)
>
0
and
os
.
path
.
exists
(
image_paths
[
0
])
and
len
(
image_paths
)
==
len
(
bboxes
)
assert
len
(
image_paths
)
>
0
and
os
.
path
.
exists
(
image_paths
[
0
])
and
len
(
image_paths
)
==
len
(
bboxes
)
if
not
os
.
path
.
exists
(
output_dir
):
os
.
makedirs
(
output_dir
)
classes
=
read_class_names
(
coco_label_path
)
...
...
@@ -107,18 +138,24 @@ def save_detected_images(image_paths, bboxes, coco_label_path, output_dir='data/
image
=
draw_bbox_for_batch
(
rgb_image
,
bboxes
[
i
],
classes
)
image
=
Image
.
fromarray
(
image
)
# image.show()
out_path
=
output_dir
+
os
.
sep
+
'detected_'
+
os
.
path
.
basename
(
image_paths
[
i
])
out_path
=
output_dir
+
os
.
sep
+
'detected_'
+
\
os
.
path
.
basename
(
image_paths
[
i
])
image
.
save
(
out_path
)
# 保存
def
batch_postprocess_boxes_new
(
yolo_pos
,
yolo_prob
,
org_img_shape
,
threshold
=
0.3
):
def
batch_postprocess_boxes_new
(
yolo_pos
,
yolo_prob
,
org_img_shape
,
threshold
=
0.3
):
"""
Generate batch bboxes for origin image based on network output,see >> postprocess_boxes()
"""
batch_size
,
box_num
,
class_num
=
yolo_prob
.
shape
[
0
],
yolo_prob
.
shape
[
1
],
yolo_prob
.
shape
[
2
]
-
1
batch_bboxes
=
[]
for
i
in
range
(
batch_size
):
bboxes
=
postprocess_boxes_new
(
yolo_pos
[
i
,
:,
:],
yolo_prob
[
i
,
:,
:],
org_img_shape
[
i
,
:],
threshold
)
bboxes
=
postprocess_boxes_new
(
yolo_pos
[
i
,
:,
:],
yolo_prob
[
i
,
:,
:],
org_img_shape
[
i
,
:],
threshold
)
bboxes
=
nms
(
bboxes
,
0.45
,
method
=
'nms'
)
batch_bboxes
.
append
(
bboxes
)
return
batch_bboxes
...
...
@@ -128,26 +165,36 @@ def batch_postprocess_boxes(yolo_pos, yolo_prob, org_img_shape, threshold=0.3):
"""
Generate batch bboxes for origin image based on network output,see >> postprocess_boxes()
"""
batch_size
,
class_num
,
box_num
=
yolo_prob
.
shape
[
0
],
yolo_prob
.
shape
[
1
]
-
1
,
yolo_prob
.
shape
[
2
]
batch_size
,
class_num
,
box_num
=
yolo_prob
.
shape
[
0
],
yolo_prob
.
shape
[
1
]
-
\
1
,
yolo_prob
.
shape
[
2
]
batch_bboxes
=
[]
for
i
in
range
(
batch_size
):
bboxes
=
postprocess_boxes
(
yolo_pos
[
i
,
:,
:,
:],
yolo_prob
[
i
,
:,
:],
org_img_shape
[
i
,
:],
threshold
)
bboxes
=
postprocess_boxes
(
yolo_pos
[
i
,
:,
:,
:],
yolo_prob
[
i
,
:,
:],
org_img_shape
[
i
,
:],
threshold
)
batch_bboxes
.
append
(
bboxes
)
return
batch_bboxes
def
batch_postprocess_boxes_nms
(
yolo_pos
,
yolo_prob
,
org_img_shape
,
conf_thld
=
0.3
,
nms_thld
=
0.45
):
def
batch_postprocess_boxes_nms
(
yolo_pos
,
yolo_prob
,
org_img_shape
,
conf_thld
=
0.3
,
nms_thld
=
0.45
):
"""
Generate batch bboxes for origin image based on network output,see >> postprocess_boxes()
"""
batch_size
,
class_num
,
box_num
=
yolo_prob
.
shape
[
0
],
yolo_prob
.
shape
[
1
]
-
1
,
yolo_prob
.
shape
[
2
]
batch_size
,
class_num
,
box_num
=
yolo_prob
.
shape
[
0
],
yolo_prob
.
shape
[
1
]
-
\
1
,
yolo_prob
.
shape
[
2
]
batch_bboxes
=
[]
for
i
in
range
(
batch_size
):
bboxes
=
postprocess_boxes_new
(
yolo_pos
[
i
,
:,
:],
yolo_prob
[
i
,
:,
:],
org_img_shape
[
i
,
:],
conf_thld
)
bboxes
=
postprocess_boxes_new
(
yolo_pos
[
i
,
:,
:],
yolo_prob
[
i
,
:,
:],
org_img_shape
[
i
,
:],
conf_thld
)
bboxes
=
nms
(
bboxes
,
nms_thld
,
method
=
'nms'
)
batch_bboxes
.
append
(
bboxes
)
return
batch_bboxes
def
postprocess_boxes
(
yolo_pos
,
yolo_prob
,
org_img_shape
,
threshold
=
0.3
):
"""
Generate bboxes for origin image based on network output
...
...
@@ -163,8 +210,10 @@ def postprocess_boxes(yolo_pos, yolo_prob, org_img_shape, threshold=0.3):
class_num
=
yolo_prob
.
shape
[
0
]
-
1
box_num
=
yolo_prob
.
shape
[
1
]
pred_xywh
=
yolo_pos
[
1
:
yolo_pos
.
shape
[
1
],
:,
0
:
4
]
# shape (B, C, 4)
pred_xywh
=
pred_xywh
.
reshape
((
class_num
*
box_num
,
4
),
order
=
'F'
)
# shape (B*C, 4)
pred_prob
=
yolo_prob
[
1
:
yolo_prob
.
shape
[
1
],
:].
reshape
((
class_num
*
box_num
),
order
=
'F'
)
# shape (B*C, )
pred_xywh
=
pred_xywh
.
reshape
(
(
class_num
*
box_num
,
4
),
order
=
'F'
)
# shape (B*C, 4)
pred_prob
=
yolo_prob
[
1
:
yolo_prob
.
shape
[
1
],
:].
reshape
(
(
class_num
*
box_num
),
order
=
'F'
)
# shape (B*C, )
# 1. (x, y, w, h) --> (xmin, ymin, xmax, ymax)
org_h
,
org_w
=
org_img_shape
...
...
@@ -175,21 +224,26 @@ def postprocess_boxes(yolo_pos, yolo_prob, org_img_shape, threshold=0.3):
pred_coor
=
np
.
vstack
((
xmin
,
ymin
,
xmax
,
ymax
)).
T
# 2. clip some boxes those are out of range
pred_coor
=
np
.
concatenate
([
np
.
maximum
(
pred_coor
[:,
:
2
],
[
0
,
0
]),
np
.
minimum
(
pred_coor
[:,
2
:],
[
org_w
-
1
,
org_h
-
1
])],
axis
=-
1
)
invalid_mask
=
np
.
logical_or
((
pred_coor
[:,
0
]
>
pred_coor
[:,
2
]),
(
pred_coor
[:,
1
]
>
pred_coor
[:,
3
]))
pred_coor
=
np
.
concatenate
([
np
.
maximum
(
pred_coor
[:,
:
2
],
[
0
,
0
]),
np
.
minimum
(
pred_coor
[:,
2
:],
[
org_w
-
1
,
org_h
-
1
])],
axis
=-
1
)
invalid_mask
=
np
.
logical_or
(
(
pred_coor
[:,
0
]
>
pred_coor
[:,
2
]),
(
pred_coor
[:,
1
]
>
pred_coor
[:,
3
]))
pred_coor
[
invalid_mask
]
=
0
# shape (B*C, 4)
# 3. discard some invalid boxes
bboxes_scale
=
np
.
sqrt
(
np
.
multiply
.
reduce
(
pred_coor
[:,
2
:
4
]
-
pred_coor
[:,
0
:
2
],
axis
=-
1
))
scale_mask
=
np
.
logical_and
((
0
<
bboxes_scale
),
(
bboxes_scale
<
np
.
inf
))
# (B*C, )
bboxes_scale
=
np
.
sqrt
(
np
.
multiply
.
reduce
(
pred_coor
[:,
2
:
4
]
-
pred_coor
[:,
0
:
2
],
axis
=-
1
))
scale_mask
=
np
.
logical_and
(
(
0
<
bboxes_scale
),
(
bboxes_scale
<
np
.
inf
))
# (B*C, )
# 4. discard some boxes with pred_problow scores
classes
=
np
.
tile
(
np
.
arange
(
class_num
),
box_num
)
score_mask
=
pred_prob
>
threshold
mask
=
np
.
logical_and
(
scale_mask
,
score_mask
)
# (B*C, )
coors
,
pred_prob
,
classes
=
pred_coor
[
mask
],
pred_prob
[
mask
],
classes
[
mask
]
bboxes
=
np
.
concatenate
([
classes
[:,
np
.
newaxis
],
coors
,
pred_prob
[:,
np
.
newaxis
]],
axis
=-
1
)
bboxes
=
np
.
concatenate
(
[
classes
[:,
np
.
newaxis
],
coors
,
pred_prob
[:,
np
.
newaxis
]],
axis
=-
1
)
return
bboxes
.
tolist
()
...
...
@@ -209,7 +263,6 @@ def postprocess_boxes_new(yolo_pos, yolo_prob, org_img_shape, threshold=0.3):
pred_xywh
=
yolo_pos
[:,
0
:
4
]
# shape (box_num, 4)
pred_prob
=
yolo_prob
[:,
1
:
yolo_prob
.
shape
[
1
]]
# shape (box_num, 80)
# 1. (x, y, w, h) --> (xmin, ymin, xmax, ymax)
org_h
,
org_w
=
org_img_shape
xmin
=
(
pred_xywh
[:,
0
]
-
pred_xywh
[:,
2
]
/
2.
)
*
org_w
...
...
@@ -219,14 +272,18 @@ def postprocess_boxes_new(yolo_pos, yolo_prob, org_img_shape, threshold=0.3):
pred_coor
=
np
.
vstack
((
xmin
,
ymin
,
xmax
,
ymax
)).
T
# shape (box_num, 4)
# 2. clip some boxes those are out of range
pred_coor
=
np
.
concatenate
([
np
.
maximum
(
pred_coor
[:,
:
2
],
[
0
,
0
]),
np
.
minimum
(
pred_coor
[:,
2
:],
[
org_w
-
1
,
org_h
-
1
])],
axis
=-
1
)
invalid_mask
=
np
.
logical_or
((
pred_coor
[:,
0
]
>
pred_coor
[:,
2
]),
(
pred_coor
[:,
1
]
>
pred_coor
[:,
3
]))
pred_coor
=
np
.
concatenate
([
np
.
maximum
(
pred_coor
[:,
:
2
],
[
0
,
0
]),
np
.
minimum
(
pred_coor
[:,
2
:],
[
org_w
-
1
,
org_h
-
1
])],
axis
=-
1
)
invalid_mask
=
np
.
logical_or
(
(
pred_coor
[:,
0
]
>
pred_coor
[:,
2
]),
(
pred_coor
[:,
1
]
>
pred_coor
[:,
3
]))
pred_coor
[
invalid_mask
]
=
0
# shape (box_num, 4)
# 3. discard some invalid boxes
bboxes_scale
=
np
.
sqrt
(
np
.
multiply
.
reduce
(
pred_coor
[:,
2
:
4
]
-
pred_coor
[:,
0
:
2
],
axis
=-
1
))
scale_mask
=
np
.
logical_and
((
0
<
bboxes_scale
),
(
bboxes_scale
<
np
.
inf
))
# (box_num, )
bboxes_scale
=
np
.
sqrt
(
np
.
multiply
.
reduce
(
pred_coor
[:,
2
:
4
]
-
pred_coor
[:,
0
:
2
],
axis
=-
1
))
scale_mask
=
np
.
logical_and
(
(
0
<
bboxes_scale
),
(
bboxes_scale
<
np
.
inf
))
# (box_num, )
# 4. discard some boxes with pred_problow scores
classes
=
np
.
argmax
(
pred_prob
,
axis
=-
1
)
# (box_num, )
...
...
@@ -236,7 +293,8 @@ def postprocess_boxes_new(yolo_pos, yolo_prob, org_img_shape, threshold=0.3):
mask
=
np
.
logical_and
(
scale_mask
,
score_mask
)
# (box_num, )
coors
,
scores
,
classes
=
pred_coor
[
mask
],
scores
[
mask
],
classes
[
mask
]
# bboxes.shape >> (box_num, 6)
bboxes
=
np
.
concatenate
([
classes
[:,
np
.
newaxis
],
coors
,
scores
[:,
np
.
newaxis
]],
axis
=-
1
)
bboxes
=
np
.
concatenate
(
[
classes
[:,
np
.
newaxis
],
coors
,
scores
[:,
np
.
newaxis
]],
axis
=-
1
)
return
bboxes
...
...
@@ -245,8 +303,10 @@ def bboxes_iou(boxes1, boxes2):
boxes1
=
np
.
array
(
boxes1
)
boxes2
=
np
.
array
(
boxes2
)
boxes1_area
=
(
boxes1
[...,
2
]
-
boxes1
[...,
0
])
*
(
boxes1
[...,
3
]
-
boxes1
[...,
1
])
boxes2_area
=
(
boxes2
[...,
2
]
-
boxes2
[...,
0
])
*
(
boxes2
[...,
3
]
-
boxes2
[...,
1
])
boxes1_area
=
(
boxes1
[...,
2
]
-
boxes1
[...,
0
])
*
\
(
boxes1
[...,
3
]
-
boxes1
[...,
1
])
boxes2_area
=
(
boxes2
[...,
2
]
-
boxes2
[...,
0
])
*
\
(
boxes2
[...,
3
]
-
boxes2
[...,
1
])
left_up
=
np
.
maximum
(
boxes1
[...,
:
2
],
boxes2
[...,
:
2
])
right_down
=
np
.
minimum
(
boxes1
[...,
2
:],
boxes2
[...,
2
:])
...
...
@@ -277,7 +337,8 @@ def nms(bboxes, iou_threshold, sigma=0.3, method='nms'):
max_ind
=
np
.
argmax
(
cls_bboxes
[:,
5
])
best_bbox
=
cls_bboxes
[
max_ind
]
best_bboxes
.
append
(
best_bbox
)
cls_bboxes
=
np
.
concatenate
([
cls_bboxes
[:
max_ind
],
cls_bboxes
[
max_ind
+
1
:]])
cls_bboxes
=
np
.
concatenate
(
[
cls_bboxes
[:
max_ind
],
cls_bboxes
[
max_ind
+
1
:]])
iou
=
bboxes_iou
(
best_bbox
[
np
.
newaxis
,
1
:
5
],
cls_bboxes
[:,
1
:
5
])
weight
=
np
.
ones
((
len
(
iou
),),
dtype
=
np
.
float32
)
...
...
@@ -335,13 +396,14 @@ def batch_boxes(positions, probs, origin_image_info, nms=True):
for
j
in
range
(
positions
.
shape
[
1
]):
for
i
in
range
(
1
,
81
):
if
positions
[
k
][
j
][
2
]
!=
0
and
positions
[
k
][
j
][
3
]
!=
0
and
probs
[
k
][
j
][
i
]
!=
0
:
x1
,
y1
,
x2
,
y2
=
xywh_2_x1y1x2y2
(
positions
[
k
][
j
][
0
],
positions
[
k
][
j
][
1
],
positions
[
k
][
j
][
2
],
positions
[
k
][
j
][
3
],
origin_image_info
[
k
])
x1
,
y1
,
x2
,
y2
=
xywh_2_x1y1x2y2
(
positions
[
k
][
j
][
0
],
positions
[
k
][
j
][
1
],
positions
[
k
][
j
][
2
],
positions
[
k
][
j
][
3
],
origin_image_info
[
k
])
bbox
=
[
i
-
1
,
x1
,
y1
,
x2
,
y2
,
probs
[
k
][
j
][
i
]]
box_list
.
append
(
bbox
)
batch_list
.
append
(
np
.
asarray
(
box_list
))
return
batch_list
def
ap_per_class
(
tp
,
conf
,
pred_cls
,
target_cls
):
""" Compute the average precision, given the recall and precision curves.
Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
...
...
@@ -406,6 +468,7 @@ def ap_per_class(tp, conf, pred_cls, target_cls):
return
p
,
r
,
ap
,
f1
,
unique_classes
.
astype
(
'int32'
)
def
compute_ap
(
recall
,
precision
):
""" Compute the average precision, given the recall and precision curves.
Source: https://github.com/rbgirshick/py-faster-rcnn.
...
...
@@ -430,15 +493,15 @@ def compute_ap(recall, precision):
x
=
np
.
linspace
(
0
,
1
,
101
)
# 101-point interp (COCO)
ap
=
np
.
trapz
(
np
.
interp
(
x
,
mrec
,
mpre
),
x
)
# integrate
else
:
# 'continuous'
i
=
np
.
where
(
mrec
[
1
:]
!=
mrec
[:
-
1
])[
0
]
# points where x axis (recall) changes
# points where x axis (recall) changes
i
=
np
.
where
(
mrec
[
1
:]
!=
mrec
[:
-
1
])[
0
]
ap
=
np
.
sum
((
mrec
[
i
+
1
]
-
mrec
[
i
])
*
mpre
[
i
+
1
])
# area under curve
return
ap
def
clip_coords
(
boxes
,
img_shape
):
# Clip bounding xyxy bounding boxes to image shape (height, width)
boxes
[:,
[
1
,
3
]]
=
boxes
[:,
[
1
,
3
]].
clip
(
0
,
img_shape
[
1
])
# clip x
boxes
[:,
[
2
,
4
]]
=
boxes
[:,
[
2
,
4
]].
clip
(
0
,
img_shape
[
0
])
# clip y
return
boxes
oneflow_yolov3/model/yolo_net.py
浏览文件 @
ab3718cf
此差异已折叠。
点击以展开。
oneflow_yolov3/model/yolo_test_python.py
浏览文件 @
ab3718cf
import
oneflow
as
flow
import
time
import
argparse
import
math
import
os
from
yolo_net
import
YoloPredictNet
from
data_preprocess
import
image_preprocess_v2
,
batch_image_preprocess_with_label
import
oneflow_yolov3
import
utils
import
numpy
as
np
import
math
import
oneflow
as
flow
import
utils
from
data_preprocess
import
batch_image_preprocess_with_label
from
tqdm
import
tqdm
from
yolo_net
import
YoloPredictNet
import
oneflow_yolov3
parser
=
argparse
.
ArgumentParser
(
description
=
"flags for predict"
)
parser
.
add_argument
(
"-g"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-g"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-load"
,
"--model_load_dir"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-label_path"
,
"--label_path"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
8
,
required
=
False
)
parser
.
add_argument
(
'--iou_thres'
,
type
=
float
,
default
=
0.5
,
help
=
'iou threshold required to qualify as detected'
)
parser
.
add_argument
(
'--conf_thres'
,
type
=
float
,
default
=
0.3
,
help
=
'object confidence threshold'
)
parser
.
add_argument
(
'--nms_thres'
,
type
=
float
,
default
=
0.5
,
help
=
'iou threshold for non-maximum suppression'
)
parser
.
add_argument
(
"-use_tensorrt"
,
"--use_tensorrt"
,
type
=
int
,
default
=
0
,
required
=
False
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
8
,
required
=
False
)
parser
.
add_argument
(
'--iou_thres'
,
type
=
float
,
default
=
0.5
,
help
=
'iou threshold required to qualify as detected'
)
parser
.
add_argument
(
'--conf_thres'
,
type
=
float
,
default
=
0.3
,
help
=
'object confidence threshold'
)
parser
.
add_argument
(
'--nms_thres'
,
type
=
float
,
default
=
0.5
,
help
=
'iou threshold for non-maximum suppression'
)
parser
.
add_argument
(
"-use_tensorrt"
,
"--use_tensorrt"
,
type
=
int
,
default
=
0
,
required
=
False
)
parser
.
add_argument
(
"-image_paths"
,
"--image_paths"
,
type
=
str
,
required
=
False
)
...
...
@@ -33,18 +65,29 @@ func_config.default_distribute_strategy(flow.distribute.consistent_strategy())
func_config
.
default_data_type
(
flow
.
float
)
if
args
.
use_tensorrt
!=
0
:
func_config
.
use_tensorrt
(
True
)
#func_config.tensorrt.use_fp16()
#
func_config.tensorrt.use_fp16()
input_blob_def_dict
=
{
"images"
:
flow
.
FixedTensorDef
((
args
.
batch_size
,
3
,
args
.
image_height
,
args
.
image_width
),
dtype
=
flow
.
float
),
"origin_image_info"
:
flow
.
FixedTensorDef
((
args
.
batch_size
,
2
),
dtype
=
flow
.
int32
),
"images"
:
flow
.
FixedTensorDef
(
(
args
.
batch_size
,
3
,
args
.
image_height
,
args
.
image_width
),
dtype
=
flow
.
float
),
"origin_image_info"
:
flow
.
FixedTensorDef
(
(
args
.
batch_size
,
2
),
dtype
=
flow
.
int32
),
}
@
flow
.
global_function
(
func_config
)
def
yolo_user_op_eval_job
(
images
=
input_blob_def_dict
[
"images"
],
origin_image_info
=
input_blob_def_dict
[
"origin_image_info"
]):
yolo_pos_result
,
yolo_prob_result
=
YoloPredictNet
(
images
,
origin_image_info
,
trainable
=
False
)
def
yolo_user_op_eval_job
(
images
=
input_blob_def_dict
[
"images"
],
origin_image_info
=
input_blob_def_dict
[
"origin_image_info"
]):
yolo_pos_result
,
yolo_prob_result
=
YoloPredictNet
(
images
,
origin_image_info
,
trainable
=
False
)
return
yolo_pos_result
,
yolo_prob_result
,
origin_image_info
...
...
@@ -65,8 +108,8 @@ if __name__ == "__main__":
with
open
(
args
.
image_paths
)
as
f
:
for
line
in
f
:
path_list
.
append
(
line
.
strip
(
'
\n
'
))
iter_num
=
math
.
floor
(
len
(
path_list
)
/
float
(
args
.
batch_size
))
iter_num
=
math
.
floor
(
len
(
path_list
)
/
float
(
args
.
batch_size
))
"""
reference:
https://github.com/ultralytics/yolov3/blob/master/test.py
...
...
@@ -75,11 +118,13 @@ if __name__ == "__main__":
p
,
r
,
f1
,
mp
,
mr
,
map
,
mf1
=
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
jdict
,
stats
,
ap
,
ap_class
=
[],
[],
[],
[]
for
i
in
tqdm
(
range
(
iter_num
)):
paths
=
path_list
[
i
*
args
.
batch_size
:(
i
+
1
)
*
args
.
batch_size
]
images
,
origin_image_info
,
targets
=
batch_image_preprocess_with_label
(
paths
,
args
.
image_height
,
args
.
image_width
)
yolo_pos
,
yolo_prob
,
origin_image_info
=
yolo_user_op_eval_job
(
images
,
origin_image_info
).
get
()
bboxes
=
utils
.
batch_postprocess_boxes_nms
(
yolo_pos
,
yolo_prob
,
origin_image_info
,
args
.
nms_thres
,
args
.
conf_thres
)
paths
=
path_list
[
i
*
args
.
batch_size
:(
i
+
1
)
*
args
.
batch_size
]
images
,
origin_image_info
,
targets
=
batch_image_preprocess_with_label
(
paths
,
args
.
image_height
,
args
.
image_width
)
yolo_pos
,
yolo_prob
,
origin_image_info
=
yolo_user_op_eval_job
(
images
,
origin_image_info
).
get
()
bboxes
=
utils
.
batch_postprocess_boxes_nms
(
yolo_pos
,
yolo_prob
,
origin_image_info
,
args
.
nms_thres
,
args
.
conf_thres
)
for
si
,
pred
in
enumerate
(
bboxes
):
labels
=
targets
[
targets
[:,
0
]
==
si
,
1
:]
nl
=
len
(
labels
)
...
...
@@ -87,7 +132,7 @@ if __name__ == "__main__":
seen
+=
1
pred
=
np
.
asarray
(
pred
).
astype
(
np
.
float32
)
if
len
(
pred
)
==
0
:
if
len
(
pred
)
==
0
:
if
nl
:
stats
.
append
(([],
[],
[],
tcls
))
continue
...
...
@@ -109,7 +154,7 @@ if __name__ == "__main__":
for
i
,
p
in
enumerate
(
pred
):
pcls
=
p
[
0
]
pbox
=
p
[
1
:
5
]
# Break if all targets already located in image
if
len
(
detected
)
==
nl
:
break
...
...
@@ -121,7 +166,7 @@ if __name__ == "__main__":
m
=
(
pcls
==
tcls_tensor
)
iou
=
utils
.
bboxes_iou
(
pbox
,
tbox
[
m
])
bi
=
np
.
argmax
(
iou
)
maxiou
=
iou
[
bi
]
maxiou
=
iou
[
bi
]
# If iou > threshold and class is correct mark as correct
if
maxiou
>
args
.
iou_thres
:
# and pcls == tcls[bi]:
correct
[
i
]
=
1
...
...
@@ -133,15 +178,14 @@ if __name__ == "__main__":
if
len
(
stats
):
p
,
r
,
ap
,
f1
,
ap_class
=
utils
.
ap_per_class
(
*
stats
)
mp
,
mr
,
map
,
mf1
=
p
.
mean
(),
r
.
mean
(),
ap
.
mean
(),
f1
.
mean
()
nt
=
np
.
bincount
(
stats
[
3
].
astype
(
np
.
int64
),
minlength
=
80
)
# number of targets per class
print
((
'%20s'
+
'%10s'
*
6
)
%
(
'Class'
,
'Images'
,
'Targets'
,
'P'
,
'R'
,
'mAP@0.5'
,
'F1'
))
nt
=
np
.
bincount
(
stats
[
3
].
astype
(
np
.
int64
),
minlength
=
80
)
# number of targets per class
print
((
'%20s'
+
'%10s'
*
6
)
%
(
'Class'
,
'Images'
,
'Targets'
,
'P'
,
'R'
,
'mAP@0.5'
,
'F1'
))
pf
=
'%20s'
+
'%10.3g'
*
6
# print format
print
(
pf
%
(
'all'
,
seen
,
nt
.
sum
(),
mp
,
mr
,
map
,
mf1
))
# Print results per class
for
i
,
c
in
enumerate
(
ap_class
):
print
(
pf
%
(
names
[
c
],
seen
,
nt
[
c
],
p
[
i
],
r
[
i
],
ap
[
i
],
f1
[
i
]))
oneflow_yolov3/model/yolo_train.py
浏览文件 @
ab3718cf
...
...
@@ -8,23 +8,88 @@ from yolo_net import YoloTrainNet
import
oneflow_yolov3
from
oneflow_yolov3.ops.yolo_decode
import
yolo_train_decoder
parser
=
argparse
.
ArgumentParser
(
description
=
"flags for multi-node and resource"
)
parser
=
argparse
.
ArgumentParser
(
description
=
"flags for multi-node and resource"
)
parser
.
add_argument
(
"-load"
,
"--model_load_dir"
,
type
=
str
,
required
=
False
)
parser
.
add_argument
(
"-gpu_num_per_node"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-classes"
,
"--classes"
,
type
=
int
,
default
=
80
,
required
=
False
)
parser
.
add_argument
(
"-total_images"
,
"--total_images"
,
type
=
int
,
default
=
117264
,
required
=
False
)
parser
.
add_argument
(
"-num_boxes"
,
"--num_boxes"
,
type
=
int
,
default
=
90
,
required
=
False
)
parser
.
add_argument
(
"-gpu_num_per_node"
,
"--gpu_num_per_node"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-batch_size"
,
"--batch_size"
,
type
=
int
,
default
=
1
,
required
=
False
)
parser
.
add_argument
(
"-image_height"
,
"--image_height"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-image_width"
,
"--image_width"
,
type
=
int
,
default
=
608
,
required
=
False
)
parser
.
add_argument
(
"-classes"
,
"--classes"
,
type
=
int
,
default
=
80
,
required
=
False
)
parser
.
add_argument
(
"-total_images"
,
"--total_images"
,
type
=
int
,
default
=
117264
,
required
=
False
)
parser
.
add_argument
(
"-num_boxes"
,
"--num_boxes"
,
type
=
int
,
default
=
90
,
required
=
False
)
parser
.
add_argument
(
"-hue"
,
"--hue"
,
type
=
float
,
default
=
0.1
,
required
=
False
)
parser
.
add_argument
(
"-jitter"
,
"--jitter"
,
type
=
float
,
default
=
0.3
,
required
=
False
)
parser
.
add_argument
(
"-saturation"
,
"--saturation"
,
type
=
float
,
default
=
1.5
,
required
=
False
)
parser
.
add_argument
(
"-exposure"
,
"--exposure"
,
type
=
float
,
default
=
1.5
,
required
=
False
)
parser
.
add_argument
(
"-jitter"
,
"--jitter"
,
type
=
float
,
default
=
0.3
,
required
=
False
)
parser
.
add_argument
(
"-saturation"
,
"--saturation"
,
type
=
float
,
default
=
1.5
,
required
=
False
)
parser
.
add_argument
(
"-exposure"
,
"--exposure"
,
type
=
float
,
default
=
1.5
,
required
=
False
)
parser
.
add_argument
(
"-dataset_dir"
,
"--dataset_dir"
,
type
=
str
,
required
=
True
)
parser
.
add_argument
(
"-num_epoch"
,
"--num_epoch"
,
type
=
int
,
default
=
10
,
required
=
False
)
parser
.
add_argument
(
"-base_lr"
,
"--base_lr"
,
type
=
float
,
default
=
0.001
,
required
=
False
)
parser
.
add_argument
(
"-save_frequency"
,
"--save_frequency"
,
type
=
int
,
required
=
True
)
parser
.
add_argument
(
"-num_epoch"
,
"--num_epoch"
,
type
=
int
,
default
=
10
,
required
=
False
)
parser
.
add_argument
(
"-base_lr"
,
"--base_lr"
,
type
=
float
,
default
=
0.001
,
required
=
False
)
parser
.
add_argument
(
"-save_frequency"
,
"--save_frequency"
,
type
=
int
,
required
=
True
)
parser
.
add_argument
(
"-save"
,
"--model_save_dir"
,
type
=
str
,
required
=
False
)
args
=
parser
.
parse_args
()
...
...
@@ -40,10 +105,19 @@ func_config.train.model_update_conf(dict(naive_conf={}))
@
flow
.
global_function
(
func_config
)
def
yolo_train_job
():
images
,
ground_truth
,
gt_valid_num
=
yolo_train_decoder
(
args
.
batch_size
,
args
.
image_height
,
args
.
image_width
,
args
.
classes
,
args
.
num_boxes
,
args
.
hue
,
args
.
jitter
,
args
.
saturation
,
args
.
exposure
,
args
.
dataset_dir
,
"yolo"
)
gt_boxes
=
flow
.
slice
(
ground_truth
,
[
None
,
0
,
0
],
[
None
,
-
1
,
4
],
name
=
'gt_box'
)
gt_labels
=
flow
.
cast
(
flow
.
slice
(
ground_truth
,
[
None
,
0
,
4
],
[
None
,
-
1
,
1
],
name
=
'gt_label'
),
dtype
=
flow
.
int32
)
yolo_loss_result
,
statistics_info_result
=
YoloTrainNet
(
images
,
gt_boxes
,
gt_labels
,
gt_valid_num
,
True
)
images
,
ground_truth
,
gt_valid_num
=
yolo_train_decoder
(
args
.
batch_size
,
args
.
image_height
,
args
.
image_width
,
args
.
classes
,
args
.
num_boxes
,
args
.
hue
,
args
.
jitter
,
args
.
saturation
,
args
.
exposure
,
args
.
dataset_dir
,
"yolo"
)
gt_boxes
=
flow
.
slice
(
ground_truth
,
[
None
,
0
,
0
],
[
None
,
-
1
,
4
],
name
=
'gt_box'
)
gt_labels
=
flow
.
cast
(
flow
.
slice
(
ground_truth
,
[
None
,
0
,
4
],
[
None
,
-
1
,
1
],
name
=
'gt_label'
),
dtype
=
flow
.
int32
)
yolo_loss_result
,
statistics_info_result
=
YoloTrainNet
(
images
,
gt_boxes
,
gt_labels
,
gt_valid_num
,
True
)
flow
.
losses
.
add_loss
(
yolo_loss_result
[
0
])
flow
.
losses
.
add_loss
(
yolo_loss_result
[
1
])
flow
.
losses
.
add_loss
(
yolo_loss_result
[
2
])
...
...
@@ -53,11 +127,20 @@ def yolo_train_job():
def
process_statistics_info
(
layer_name
,
statistics_info
):
count
=
statistics_info
[:,
3
].
sum
()
class_count
=
statistics_info
[:,
4
].
sum
()
avg_iou
=
statistics_info
[:,
0
].
sum
()
/
count
if
count
!=
0
else
0
avg_recall_5
=
statistics_info
[:,
1
].
sum
()
/
count
if
count
!=
0
else
0
avg_recall_75
=
statistics_info
[:,
2
].
sum
()
/
count
if
count
!=
0
else
0
avg_iou
=
statistics_info
[:,
0
].
sum
()
/
count
if
count
!=
0
else
0
avg_recall_5
=
statistics_info
[:,
1
].
sum
()
/
count
if
count
!=
0
else
0
avg_recall_75
=
statistics_info
[:,
2
].
sum
()
/
count
if
count
!=
0
else
0
print
(
layer_name
,
"Avg IOU: "
,
avg_iou
,
".5 Recall:"
,
avg_recall_5
,
".75 Recall:"
,
avg_recall_75
,
"count:"
,
count
)
print
(
layer_name
,
"Avg IOU: "
,
avg_iou
,
".5 Recall:"
,
avg_recall_5
,
".75 Recall:"
,
avg_recall_75
,
"count:"
,
count
)
if
__name__
==
"__main__"
:
...
...
@@ -67,19 +150,31 @@ if __name__ == "__main__":
else
:
check_point
.
load
(
args
.
model_load_dir
)
fmt_str
=
"{:>12} {:>12.4f} {:>12.4f} {:>12.4f} {:>12.3f}"
print
(
"{:>12} {:>12} {:>12}"
.
format
(
"iter"
,
"losses value"
,
"time"
))
print
(
"{:>12} {:>12} {:>12}"
.
format
(
"iter"
,
"losses value"
,
"time"
))
global
cur_time
cur_time
=
time
.
time
()
iter_num
=
math
.
ceil
(
args
.
total_images
/
args
.
batch_size
)
iter_num
=
math
.
ceil
(
args
.
total_images
/
args
.
batch_size
)
for
epoch
in
range
(
args
.
num_epoch
):
for
iter
in
range
(
iter_num
):
yolo_loss_result
,
statistics_info_result
=
yolo_train_job
().
get
()
process_statistics_info
(
"Region 82"
,
statistics_info_result
[
0
].
ndarray
())
process_statistics_info
(
"Region 94"
,
statistics_info_result
[
1
].
ndarray
())
process_statistics_info
(
"Region 106"
,
statistics_info_result
[
2
].
ndarray
())
print
(
fmt_str
.
format
(
iter
,
np
.
abs
(
yolo_loss_result
[
0
]).
mean
(),
np
.
abs
(
yolo_loss_result
[
1
]).
mean
(),
np
.
abs
(
yolo_loss_result
[
2
]).
mean
(),
time
.
time
()
-
cur_time
))
process_statistics_info
(
"Region 82"
,
statistics_info_result
[
0
].
ndarray
())
process_statistics_info
(
"Region 94"
,
statistics_info_result
[
1
].
ndarray
())
process_statistics_info
(
"Region 106"
,
statistics_info_result
[
2
].
ndarray
())
print
(
fmt_str
.
format
(
iter
,
np
.
abs
(
yolo_loss_result
[
0
]).
mean
(),
np
.
abs
(
yolo_loss_result
[
1
]).
mean
(),
np
.
abs
(
yolo_loss_result
[
2
]).
mean
(),
time
.
time
()
-
cur_time
))
cur_time
=
time
.
time
()
if
(
epoch
+
1
)
%
args
.
save_frequency
==
0
:
check_point
.
save
(
args
.
model_save_dir
+
"/yolov3_snapshot_"
+
str
(
epoch
+
1
))
check_point
.
save
(
args
.
model_save_dir
+
"/yolov3_snapshot_"
+
str
(
epoch
+
1
))
oneflow_yolov3/ops/upsample_nearest.py
浏览文件 @
ab3718cf
from
__future__
import
absolute_import
import
oneflow.core.operator.op_conf_pb2
as
op_conf_util
import
oneflow.core.register.logical_blob_id_pb2
as
logical_blob_id_util
from
oneflow.python.oneflow_export
import
oneflow_export
import
oneflow
as
flow
...
...
oneflow_yolov3/ops/yolo_decode.py
浏览文件 @
ab3718cf
from
__future__
import
absolute_import
import
oneflow
as
flow
def
yolo_predict_decoder
(
batch_size
,
image_height
,
image_width
,
image_paths
,
name
):
with
flow
.
fixed_placement
(
"cpu"
,
"0:0"
):
return
(
flow
.
user_op_builder
(
name
)
.
Op
(
"yolo_predict_decoder"
)
.
Output
(
"out"
)
.
Output
(
"origin_image_info"
)
.
Attr
(
"batch_size"
,
batch_size
,
"AttrTypeInt32"
)
.
Attr
(
"image_height"
,
image_height
,
"AttrTypeInt32"
)
.
Attr
(
"image_width"
,
image_width
,
"AttrTypeInt32"
)
.
Attr
(
"image_paths"
,
image_paths
,
"AttrTypeListString"
)
.
Build
().
InferAndTryRun
().
RemoteBlobList
()
.
Op
(
"yolo_predict_decoder"
)
.
Output
(
"out"
)
.
Output
(
"origin_image_info"
)
.
Attr
(
"batch_size"
,
batch_size
,
"AttrTypeInt32"
)
.
Attr
(
"image_height"
,
image_height
,
"AttrTypeInt32"
)
.
Attr
(
"image_width"
,
image_width
,
"AttrTypeInt32"
)
.
Attr
(
"image_paths"
,
image_paths
,
"AttrTypeListString"
)
.
Build
().
InferAndTryRun
().
RemoteBlobList
()
)
def
yolo_train_decoder
(
batch_size
,
image_height
,
image_width
,
classes
,
num_boxes
,
hue
,
jitter
,
saturation
,
exposure
,
image_path_file
,
name
):
def
yolo_train_decoder
(
batch_size
,
image_height
,
image_width
,
classes
,
num_boxes
,
hue
,
jitter
,
saturation
,
exposure
,
image_path_file
,
name
):
with
flow
.
fixed_placement
(
"cpu"
,
"0:0"
):
return
(
flow
.
user_op_builder
(
name
)
.
Op
(
"yolo_train_decoder"
)
.
Output
(
"data"
)
.
Output
(
"ground_truth"
)
.
Output
(
"gt_valid_num"
)
.
Attr
(
"batch_size"
,
batch_size
,
"AttrTypeInt32"
)
.
Attr
(
"image_height"
,
image_height
,
"AttrTypeInt32"
)
.
Attr
(
"image_width"
,
image_width
,
"AttrTypeInt32"
)
.
Attr
(
"classes"
,
classes
,
"AttrTypeInt32"
)
.
Attr
(
"num_boxes"
,
num_boxes
,
"AttrTypeInt32"
)
.
Attr
(
"hue"
,
hue
,
"AttrTypeFloat"
)
.
Attr
(
"jitter"
,
jitter
,
"AttrTypeFloat"
)
.
Attr
(
"saturation"
,
saturation
,
"AttrTypeFloat"
)
.
Attr
(
"exposure"
,
exposure
,
"AttrTypeFloat"
)
.
Attr
(
"image_path_file"
,
image_path_file
,
"AttrTypeString"
)
.
Build
().
InferAndTryRun
().
RemoteBlobList
()
.
Op
(
"yolo_train_decoder"
)
.
Output
(
"data"
)
.
Output
(
"ground_truth"
)
.
Output
(
"gt_valid_num"
)
.
Attr
(
"batch_size"
,
batch_size
,
"AttrTypeInt32"
)
.
Attr
(
"image_height"
,
image_height
,
"AttrTypeInt32"
)
.
Attr
(
"image_width"
,
image_width
,
"AttrTypeInt32"
)
.
Attr
(
"classes"
,
classes
,
"AttrTypeInt32"
)
.
Attr
(
"num_boxes"
,
num_boxes
,
"AttrTypeInt32"
)
.
Attr
(
"hue"
,
hue
,
"AttrTypeFloat"
)
.
Attr
(
"jitter"
,
jitter
,
"AttrTypeFloat"
)
.
Attr
(
"saturation"
,
saturation
,
"AttrTypeFloat"
)
.
Attr
(
"exposure"
,
exposure
,
"AttrTypeFloat"
)
.
Attr
(
"image_path_file"
,
image_path_file
,
"AttrTypeString"
)
.
Build
().
InferAndTryRun
().
RemoteBlobList
()
)
oneflow_yolov3/ops/yolo_detect.py
浏览文件 @
ab3718cf
from
__future__
import
absolute_import
import
os
import
oneflow
as
flow
def
yolo_detect
(
bbox
,
probs
,
origin_image_info
,
image_height
,
image_width
,
layer_height
,
layer_width
,
prob_thresh
,
num_classes
,
anchor_boxes
,
max_out_boxes
=
None
,
name
=
None
):
#if name is None:
def
yolo_detect
(
bbox
,
probs
,
origin_image_info
,
image_height
,
image_width
,
layer_height
,
layer_width
,
prob_thresh
,
num_classes
,
anchor_boxes
,
max_out_boxes
=
None
,
name
=
None
):
# if name is None:
# name = id_util.UniqueStr("YoloDetect_")
if
max_out_boxes
is
None
or
max_out_boxes
>
bbox
.
shape
[
1
]:
max_out_boxes
=
bbox
.
shape
[
1
]
...
...
@@ -11,81 +13,84 @@ def yolo_detect(bbox, probs, origin_image_info, image_height, image_width, layer
op
=
(
flow
.
user_op_builder
(
name
)
.
Op
(
"yolo_detect"
)
.
Input
(
"bbox"
,
[
bbox
])
.
Input
(
"probs"
,
[
probs
])
.
Input
(
"origin_image_info"
,
[
origin_image_info
])
.
Output
(
"out_bbox"
)
.
Output
(
"out_probs"
)
.
Output
(
"valid_num"
)
.
Attr
(
"image_height"
,
image_height
,
"AttrTypeInt32"
)
.
Attr
(
"image_width"
,
image_width
,
"AttrTypeInt32"
)
.
Attr
(
"layer_height"
,
layer_height
,
"AttrTypeInt32"
)
.
Attr
(
"layer_width"
,
layer_width
,
"AttrTypeInt32"
)
.
Attr
(
"prob_thresh"
,
prob_thresh
,
"AttrTypeFloat"
)
.
Attr
(
"num_classes"
,
num_classes
,
"AttrTypeInt32"
)
.
Attr
(
"max_out_boxes"
,
max_out_boxes
,
"AttrTypeInt32"
)
.
Attr
(
"anchor_boxes"
,
[
anchor_box
for
anchor_box
in
anchor_boxes
],
"AttrTypeListInt32"
)
.
Build
().
InferAndTryRun
()
.
Op
(
"yolo_detect"
)
.
Input
(
"bbox"
,
[
bbox
])
.
Input
(
"probs"
,
[
probs
])
.
Input
(
"origin_image_info"
,
[
origin_image_info
])
.
Output
(
"out_bbox"
)
.
Output
(
"out_probs"
)
.
Output
(
"valid_num"
)
.
Attr
(
"image_height"
,
image_height
,
"AttrTypeInt32"
)
.
Attr
(
"image_width"
,
image_width
,
"AttrTypeInt32"
)
.
Attr
(
"layer_height"
,
layer_height
,
"AttrTypeInt32"
)
.
Attr
(
"layer_width"
,
layer_width
,
"AttrTypeInt32"
)
.
Attr
(
"prob_thresh"
,
prob_thresh
,
"AttrTypeFloat"
)
.
Attr
(
"num_classes"
,
num_classes
,
"AttrTypeInt32"
)
.
Attr
(
"max_out_boxes"
,
max_out_boxes
,
"AttrTypeInt32"
)
.
Attr
(
"anchor_boxes"
,
[
anchor_box
for
anchor_box
in
anchor_boxes
],
"AttrTypeListInt32"
)
.
Build
().
InferAndTryRun
()
)
return
op
.
RemoteBlobList
()
def
yolo_box_diff
(
bbox
,
gt_boxes
,
gt_labels
,
gt_valid_num
,
image_height
,
image_width
,
layer_height
,
layer_width
,
ignore_thresh
,
truth_thresh
,
box_mask
,
anchor_boxes_size
,
name
=
None
):
def
yolo_box_diff
(
bbox
,
gt_boxes
,
gt_labels
,
gt_valid_num
,
image_height
,
image_width
,
layer_height
,
layer_width
,
ignore_thresh
,
truth_thresh
,
box_mask
,
anchor_boxes_size
,
name
=
None
):
assert
isinstance
(
anchor_boxes_size
,
(
list
,
tuple
))
assert
isinstance
(
box_mask
,
(
list
,
tuple
))
op
=
(
flow
.
user_op_builder
(
name
)
.
Op
(
"yolo_box_diff"
)
.
Input
(
"bbox"
,
[
bbox
])
.
Input
(
"gt_boxes"
,
[
gt_boxes
])
.
Input
(
"gt_labels"
,
[
gt_labels
])
.
Input
(
"gt_valid_num"
,
[
gt_valid_num
])
.
Output
(
"bbox_loc_diff"
)
.
Output
(
"pos_inds"
)
.
Output
(
"pos_cls_label"
)
.
Output
(
"neg_inds"
)
.
Output
(
"valid_num"
)
.
Output
(
"statistics_info"
)
.
Attr
(
"image_height"
,
image_height
,
"AttrTypeInt32"
)
.
Attr
(
"image_width"
,
image_width
,
"AttrTypeInt32"
)
.
Attr
(
"layer_height"
,
layer_height
,
"AttrTypeInt32"
)
.
Attr
(
"layer_width"
,
layer_width
,
"AttrTypeInt32"
)
.
Attr
(
"ignore_thresh"
,
ignore_thresh
,
"AttrTypeFloat"
)
.
Attr
(
"truth_thresh"
,
truth_thresh
,
"AttrTypeFloat"
)
.
Attr
(
"box_mask"
,
[
mask
for
mask
in
box_mask
],
"AttrTypeListInt32"
)
.
Attr
(
"anchor_boxes"
,
[
anchor_box
for
anchor_box
in
anchor_boxes_size
],
"AttrTypeListInt32"
)
.
Build
().
InferAndTryRun
()
.
Op
(
"yolo_box_diff"
)
.
Input
(
"bbox"
,
[
bbox
])
.
Input
(
"gt_boxes"
,
[
gt_boxes
])
.
Input
(
"gt_labels"
,
[
gt_labels
])
.
Input
(
"gt_valid_num"
,
[
gt_valid_num
])
.
Output
(
"bbox_loc_diff"
)
.
Output
(
"pos_inds"
)
.
Output
(
"pos_cls_label"
)
.
Output
(
"neg_inds"
)
.
Output
(
"valid_num"
)
.
Output
(
"statistics_info"
)
.
Attr
(
"image_height"
,
image_height
,
"AttrTypeInt32"
)
.
Attr
(
"image_width"
,
image_width
,
"AttrTypeInt32"
)
.
Attr
(
"layer_height"
,
layer_height
,
"AttrTypeInt32"
)
.
Attr
(
"layer_width"
,
layer_width
,
"AttrTypeInt32"
)
.
Attr
(
"ignore_thresh"
,
ignore_thresh
,
"AttrTypeFloat"
)
.
Attr
(
"truth_thresh"
,
truth_thresh
,
"AttrTypeFloat"
)
.
Attr
(
"box_mask"
,
[
mask
for
mask
in
box_mask
],
"AttrTypeListInt32"
)
.
Attr
(
"anchor_boxes"
,
[
anchor_box
for
anchor_box
in
anchor_boxes_size
],
"AttrTypeListInt32"
)
.
Build
().
InferAndTryRun
()
)
return
op
.
RemoteBlobList
()
def
yolo_prob_loss
(
bbox_objness
,
bbox_clsprob
,
pos_inds
,
pos_cls_label
,
neg_inds
,
valid_num
,
num_classes
,
name
=
None
):
def
yolo_prob_loss
(
bbox_objness
,
bbox_clsprob
,
pos_inds
,
pos_cls_label
,
neg_inds
,
valid_num
,
num_classes
,
name
=
None
):
op
=
(
flow
.
user_op_builder
(
name
)
.
Op
(
"yolo_prob_loss"
)
.
Input
(
"bbox_objness"
,
[
bbox_objness
])
.
Input
(
"bbox_clsprob"
,
[
bbox_clsprob
])
.
Input
(
"pos_inds"
,
[
pos_inds
])
.
Input
(
"pos_cls_label"
,
[
pos_cls_label
])
.
Input
(
"neg_inds"
,
[
neg_inds
])
.
Input
(
"valid_num"
,
[
valid_num
])
.
Output
(
"bbox_objness_out"
)
.
Output
(
"bbox_clsprob_out"
)
.
Attr
(
"num_classes"
,
num_classes
,
"AttrTypeInt32"
)
.
Build
().
InferAndTryRun
()
.
Op
(
"yolo_prob_loss"
)
.
Input
(
"bbox_objness"
,
[
bbox_objness
])
.
Input
(
"bbox_clsprob"
,
[
bbox_clsprob
])
.
Input
(
"pos_inds"
,
[
pos_inds
])
.
Input
(
"pos_cls_label"
,
[
pos_cls_label
])
.
Input
(
"neg_inds"
,
[
neg_inds
])
.
Input
(
"valid_num"
,
[
valid_num
])
.
Output
(
"bbox_objness_out"
)
.
Output
(
"bbox_clsprob_out"
)
.
Attr
(
"num_classes"
,
num_classes
,
"AttrTypeInt32"
)
.
Build
().
InferAndTryRun
()
)
return
op
.
RemoteBlobList
()
def
logistic
(
x
,
name
=
None
):
return
(
flow
.
user_op_builder
(
name
if
name
is
not
None
else
id_util
.
UniqueStr
(
"Logistic_"
)
)
.
Op
(
"logistic"
)
.
Input
(
"in"
,
[
x
])
.
Output
(
"out"
)
.
Build
()
.
InferAndTryRun
()
.
RemoteBlobList
()[
0
]
.
Op
(
"logistic"
)
.
Input
(
"in"
,
[
x
])
.
Output
(
"out"
)
.
Build
()
.
InferAndTryRun
()
.
RemoteBlobList
()[
0
]
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录