Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
谢爱卿
yolov4-pytorch
提交
ff81789c
Y
yolov4-pytorch
项目概览
谢爱卿
/
yolov4-pytorch
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
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,发现更多精彩内容 >>
未验证
提交
ff81789c
编写于
9月 21, 2021
作者:
B
Bubbliiiing
提交者:
GitHub
9月 21, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Delete get_dr_txt.py
上级
d3d01e29
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
0 addition
and
137 deletion
+0
-137
get_dr_txt.py
get_dr_txt.py
+0
-137
未找到文件。
get_dr_txt.py
已删除
100644 → 0
浏览文件 @
d3d01e29
#-------------------------------------#
# mAP所需文件计算代码
# 具体教程请查看Bilibili
# Bubbliiiing
#-------------------------------------#
import
os
import
numpy
as
np
import
torch
from
PIL
import
Image
from
tqdm
import
tqdm
from
utils.utils
import
letterbox_image
,
non_max_suppression
,
yolo_correct_boxes
from
yolo
import
YOLO
'''
这里设置的门限值较低是因为计算map需要用到不同门限条件下的Recall和Precision值。
所以只有保留的框足够多,计算的map才会更精确,详情可以了解map的原理。
计算map时输出的Recall和Precision值指的是门限为0.5时的Recall和Precision值。
此处获得的./input/detection-results/里面的txt的框的数量会比直接predict多一些,这是因为这里的门限低,
目的是为了计算不同门限条件下的Recall和Precision值,从而实现map的计算。
这里的self.iou指的是非极大抑制所用到的iou,具体的可以了解非极大抑制的原理,
如果低分框与高分框的iou大于这里设定的self.iou,那么该低分框将会被剔除。
可能有些同学知道有0.5和0.5:0.95的mAP,这里的self.iou=0.5不代表mAP0.5。
如果想要设定mAP0.x,比如设定mAP0.75,可以去get_map.py设定MINOVERLAP。
'''
class
mAP_Yolo
(
YOLO
):
#---------------------------------------------------#
# 检测图片
#---------------------------------------------------#
def
detect_image
(
self
,
image_id
,
image
):
self
.
confidence
=
0.01
self
.
iou
=
0.5
f
=
open
(
"./input/detection-results/"
+
image_id
+
".txt"
,
"w"
)
image_shape
=
np
.
array
(
np
.
shape
(
image
)[
0
:
2
])
#---------------------------------------------------------#
# 给图像增加灰条,实现不失真的resize
# 也可以直接resize进行识别
#---------------------------------------------------------#
if
self
.
letterbox_image
:
crop_img
=
np
.
array
(
letterbox_image
(
image
,
(
self
.
model_image_size
[
1
],
self
.
model_image_size
[
0
])))
else
:
crop_img
=
image
.
convert
(
'RGB'
)
crop_img
=
crop_img
.
resize
((
self
.
model_image_size
[
1
],
self
.
model_image_size
[
0
]),
Image
.
BICUBIC
)
photo
=
np
.
array
(
crop_img
,
dtype
=
np
.
float32
)
/
255.0
photo
=
np
.
transpose
(
photo
,
(
2
,
0
,
1
))
#---------------------------------------------------------#
# 添加上batch_size维度
#---------------------------------------------------------#
images
=
[
photo
]
with
torch
.
no_grad
():
images
=
torch
.
from_numpy
(
np
.
asarray
(
images
))
if
self
.
cuda
:
images
=
images
.
cuda
()
#---------------------------------------------------------#
# 将图像输入网络当中进行预测!
#---------------------------------------------------------#
outputs
=
self
.
net
(
images
)
output_list
=
[]
for
i
in
range
(
3
):
output_list
.
append
(
self
.
yolo_decodes
[
i
](
outputs
[
i
]))
#---------------------------------------------------------#
# 将预测框进行堆叠,然后进行非极大抑制
#---------------------------------------------------------#
output
=
torch
.
cat
(
output_list
,
1
)
batch_detections
=
non_max_suppression
(
output
,
len
(
self
.
class_names
),
conf_thres
=
self
.
confidence
,
nms_thres
=
self
.
iou
)
#---------------------------------------------------------#
# 如果没有检测出物体,返回原图
#---------------------------------------------------------#
try
:
batch_detections
=
batch_detections
[
0
].
cpu
().
numpy
()
except
:
return
#---------------------------------------------------------#
# 对预测框进行得分筛选
#---------------------------------------------------------#
top_index
=
batch_detections
[:,
4
]
*
batch_detections
[:,
5
]
>
self
.
confidence
top_conf
=
batch_detections
[
top_index
,
4
]
*
batch_detections
[
top_index
,
5
]
top_label
=
np
.
array
(
batch_detections
[
top_index
,
-
1
],
np
.
int32
)
top_bboxes
=
np
.
array
(
batch_detections
[
top_index
,:
4
])
top_xmin
,
top_ymin
,
top_xmax
,
top_ymax
=
np
.
expand_dims
(
top_bboxes
[:,
0
],
-
1
),
np
.
expand_dims
(
top_bboxes
[:,
1
],
-
1
),
np
.
expand_dims
(
top_bboxes
[:,
2
],
-
1
),
np
.
expand_dims
(
top_bboxes
[:,
3
],
-
1
)
#-----------------------------------------------------------------#
# 在图像传入网络预测前会进行letterbox_image给图像周围添加灰条
# 因此生成的top_bboxes是相对于有灰条的图像的
# 我们需要对其进行修改,去除灰条的部分。
#-----------------------------------------------------------------#
if
self
.
letterbox_image
:
boxes
=
yolo_correct_boxes
(
top_ymin
,
top_xmin
,
top_ymax
,
top_xmax
,
np
.
array
([
self
.
model_image_size
[
0
],
self
.
model_image_size
[
1
]]),
image_shape
)
else
:
top_xmin
=
top_xmin
/
self
.
model_image_size
[
1
]
*
image_shape
[
1
]
top_ymin
=
top_ymin
/
self
.
model_image_size
[
0
]
*
image_shape
[
0
]
top_xmax
=
top_xmax
/
self
.
model_image_size
[
1
]
*
image_shape
[
1
]
top_ymax
=
top_ymax
/
self
.
model_image_size
[
0
]
*
image_shape
[
0
]
boxes
=
np
.
concatenate
([
top_ymin
,
top_xmin
,
top_ymax
,
top_xmax
],
axis
=-
1
)
for
i
,
c
in
enumerate
(
top_label
):
predicted_class
=
self
.
class_names
[
c
]
score
=
str
(
top_conf
[
i
])
top
,
left
,
bottom
,
right
=
boxes
[
i
]
f
.
write
(
"%s %s %s %s %s %s
\n
"
%
(
predicted_class
,
score
[:
6
],
str
(
int
(
left
)),
str
(
int
(
top
)),
str
(
int
(
right
)),
str
(
int
(
bottom
))))
f
.
close
()
return
yolo
=
mAP_Yolo
()
image_ids
=
open
(
'VOCdevkit/VOC2007/ImageSets/Main/test.txt'
).
read
().
strip
().
split
()
if
not
os
.
path
.
exists
(
"./input"
):
os
.
makedirs
(
"./input"
)
if
not
os
.
path
.
exists
(
"./input/detection-results"
):
os
.
makedirs
(
"./input/detection-results"
)
if
not
os
.
path
.
exists
(
"./input/images-optional"
):
os
.
makedirs
(
"./input/images-optional"
)
for
image_id
in
tqdm
(
image_ids
):
image_path
=
"./VOCdevkit/VOC2007/JPEGImages/"
+
image_id
+
".jpg"
image
=
Image
.
open
(
image_path
)
# 开启后在之后计算mAP可以可视化
# image.save("./input/images-optional/"+image_id+".jpg")
yolo
.
detect_image
(
image_id
,
image
)
print
(
"Conversion completed!"
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录