Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
a18efc3a
Unverified
提交
a18efc3a
authored
1月 30, 2021
作者:
Glenn Jocher
提交者:
GitHub
1月 30, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add variable-stride inference support (#2091)
上级
aa02b948
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
17 行增加
和
13 行删除
+17
-13
detect.py
detect.py
+4
-3
datasets.py
utils/datasets.py
+13
-10
没有找到文件。
detect.py
浏览文件 @
a18efc3a
...
@@ -31,7 +31,8 @@ def detect(save_img=False):
...
@@ -31,7 +31,8 @@ def detect(save_img=False):
# Load model
# Load model
model
=
attempt_load
(
weights
,
map_location
=
device
)
# load FP32 model
model
=
attempt_load
(
weights
,
map_location
=
device
)
# load FP32 model
imgsz
=
check_img_size
(
imgsz
,
s
=
model
.
stride
.
max
())
# check img_size
stride
=
int
(
model
.
stride
.
max
())
# model stride
imgsz
=
check_img_size
(
imgsz
,
s
=
stride
)
# check img_size
if
half
:
if
half
:
model
.
half
()
# to FP16
model
.
half
()
# to FP16
...
@@ -46,10 +47,10 @@ def detect(save_img=False):
...
@@ -46,10 +47,10 @@ def detect(save_img=False):
if
webcam
:
if
webcam
:
view_img
=
True
view_img
=
True
cudnn
.
benchmark
=
True
# set True to speed up constant image size inference
cudnn
.
benchmark
=
True
# set True to speed up constant image size inference
dataset
=
LoadStreams
(
source
,
img_size
=
imgsz
)
dataset
=
LoadStreams
(
source
,
img_size
=
imgsz
,
stride
=
stride
)
else
:
else
:
save_img
=
True
save_img
=
True
dataset
=
LoadImages
(
source
,
img_size
=
imgsz
)
dataset
=
LoadImages
(
source
,
img_size
=
imgsz
,
stride
=
stride
)
# Get names and colors
# Get names and colors
names
=
model
.
module
.
names
if
hasattr
(
model
,
'module'
)
else
model
.
names
names
=
model
.
module
.
names
if
hasattr
(
model
,
'module'
)
else
model
.
names
...
...
utils/datasets.py
浏览文件 @
a18efc3a
...
@@ -119,7 +119,7 @@ class _RepeatSampler(object):
...
@@ -119,7 +119,7 @@ class _RepeatSampler(object):
class
LoadImages
:
# for inference
class
LoadImages
:
# for inference
def
__init__
(
self
,
path
,
img_size
=
640
):
def
__init__
(
self
,
path
,
img_size
=
640
,
stride
=
32
):
p
=
str
(
Path
(
path
))
# os-agnostic
p
=
str
(
Path
(
path
))
# os-agnostic
p
=
os
.
path
.
abspath
(
p
)
# absolute path
p
=
os
.
path
.
abspath
(
p
)
# absolute path
if
'*'
in
p
:
if
'*'
in
p
:
...
@@ -136,6 +136,7 @@ class LoadImages: # for inference
...
@@ -136,6 +136,7 @@ class LoadImages: # for inference
ni
,
nv
=
len
(
images
),
len
(
videos
)
ni
,
nv
=
len
(
images
),
len
(
videos
)
self
.
img_size
=
img_size
self
.
img_size
=
img_size
self
.
stride
=
stride
self
.
files
=
images
+
videos
self
.
files
=
images
+
videos
self
.
nf
=
ni
+
nv
# number of files
self
.
nf
=
ni
+
nv
# number of files
self
.
video_flag
=
[
False
]
*
ni
+
[
True
]
*
nv
self
.
video_flag
=
[
False
]
*
ni
+
[
True
]
*
nv
...
@@ -181,7 +182,7 @@ class LoadImages: # for inference
...
@@ -181,7 +182,7 @@ class LoadImages: # for inference
print
(
f
'image {self.count}/{self.nf} {path}: '
,
end
=
''
)
print
(
f
'image {self.count}/{self.nf} {path}: '
,
end
=
''
)
# Padded resize
# Padded resize
img
=
letterbox
(
img0
,
new_shape
=
self
.
img_siz
e
)[
0
]
img
=
letterbox
(
img0
,
self
.
img_size
,
stride
=
self
.
strid
e
)[
0
]
# Convert
# Convert
img
=
img
[:,
:,
::
-
1
]
.
transpose
(
2
,
0
,
1
)
# BGR to RGB, to 3x416x416
img
=
img
[:,
:,
::
-
1
]
.
transpose
(
2
,
0
,
1
)
# BGR to RGB, to 3x416x416
...
@@ -199,8 +200,9 @@ class LoadImages: # for inference
...
@@ -199,8 +200,9 @@ class LoadImages: # for inference
class
LoadWebcam
:
# for inference
class
LoadWebcam
:
# for inference
def
__init__
(
self
,
pipe
=
'0'
,
img_size
=
640
):
def
__init__
(
self
,
pipe
=
'0'
,
img_size
=
640
,
stride
=
32
):
self
.
img_size
=
img_size
self
.
img_size
=
img_size
self
.
stride
=
stride
if
pipe
.
isnumeric
():
if
pipe
.
isnumeric
():
pipe
=
eval
(
pipe
)
# local camera
pipe
=
eval
(
pipe
)
# local camera
...
@@ -243,7 +245,7 @@ class LoadWebcam: # for inference
...
@@ -243,7 +245,7 @@ class LoadWebcam: # for inference
print
(
f
'webcam {self.count}: '
,
end
=
''
)
print
(
f
'webcam {self.count}: '
,
end
=
''
)
# Padded resize
# Padded resize
img
=
letterbox
(
img0
,
new_shape
=
self
.
img_siz
e
)[
0
]
img
=
letterbox
(
img0
,
self
.
img_size
,
stride
=
self
.
strid
e
)[
0
]
# Convert
# Convert
img
=
img
[:,
:,
::
-
1
]
.
transpose
(
2
,
0
,
1
)
# BGR to RGB, to 3x416x416
img
=
img
[:,
:,
::
-
1
]
.
transpose
(
2
,
0
,
1
)
# BGR to RGB, to 3x416x416
...
@@ -256,9 +258,10 @@ class LoadWebcam: # for inference
...
@@ -256,9 +258,10 @@ class LoadWebcam: # for inference
class
LoadStreams
:
# multiple IP or RTSP cameras
class
LoadStreams
:
# multiple IP or RTSP cameras
def
__init__
(
self
,
sources
=
'streams.txt'
,
img_size
=
640
):
def
__init__
(
self
,
sources
=
'streams.txt'
,
img_size
=
640
,
stride
=
32
):
self
.
mode
=
'stream'
self
.
mode
=
'stream'
self
.
img_size
=
img_size
self
.
img_size
=
img_size
self
.
stride
=
stride
if
os
.
path
.
isfile
(
sources
):
if
os
.
path
.
isfile
(
sources
):
with
open
(
sources
,
'r'
)
as
f
:
with
open
(
sources
,
'r'
)
as
f
:
...
@@ -284,7 +287,7 @@ class LoadStreams: # multiple IP or RTSP cameras
...
@@ -284,7 +287,7 @@ class LoadStreams: # multiple IP or RTSP cameras
print
(
''
)
# newline
print
(
''
)
# newline
# check for common shapes
# check for common shapes
s
=
np
.
stack
([
letterbox
(
x
,
new_shape
=
self
.
img_size
)[
0
]
.
shape
for
x
in
self
.
imgs
],
0
)
# inference
shapes
s
=
np
.
stack
([
letterbox
(
x
,
self
.
img_size
,
stride
=
self
.
stride
)[
0
]
.
shape
for
x
in
self
.
imgs
],
0
)
#
shapes
self
.
rect
=
np
.
unique
(
s
,
axis
=
0
)
.
shape
[
0
]
==
1
# rect inference if all shapes equal
self
.
rect
=
np
.
unique
(
s
,
axis
=
0
)
.
shape
[
0
]
==
1
# rect inference if all shapes equal
if
not
self
.
rect
:
if
not
self
.
rect
:
print
(
'WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.'
)
print
(
'WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.'
)
...
@@ -313,7 +316,7 @@ class LoadStreams: # multiple IP or RTSP cameras
...
@@ -313,7 +316,7 @@ class LoadStreams: # multiple IP or RTSP cameras
raise
StopIteration
raise
StopIteration
# Letterbox
# Letterbox
img
=
[
letterbox
(
x
,
new_shape
=
self
.
img_size
,
auto
=
self
.
rect
)[
0
]
for
x
in
img0
]
img
=
[
letterbox
(
x
,
self
.
img_size
,
auto
=
self
.
rect
,
stride
=
self
.
stride
)[
0
]
for
x
in
img0
]
# Stack
# Stack
img
=
np
.
stack
(
img
,
0
)
img
=
np
.
stack
(
img
,
0
)
...
@@ -784,8 +787,8 @@ def replicate(img, labels):
...
@@ -784,8 +787,8 @@ def replicate(img, labels):
return
img
,
labels
return
img
,
labels
def
letterbox
(
img
,
new_shape
=
(
640
,
640
),
color
=
(
114
,
114
,
114
),
auto
=
True
,
scaleFill
=
False
,
scaleup
=
True
):
def
letterbox
(
img
,
new_shape
=
(
640
,
640
),
color
=
(
114
,
114
,
114
),
auto
=
True
,
scaleFill
=
False
,
scaleup
=
True
,
stride
=
32
):
# Resize
image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
# Resize
and pad image while meeting stride-multiple constraints
shape
=
img
.
shape
[:
2
]
# current shape [height, width]
shape
=
img
.
shape
[:
2
]
# current shape [height, width]
if
isinstance
(
new_shape
,
int
):
if
isinstance
(
new_shape
,
int
):
new_shape
=
(
new_shape
,
new_shape
)
new_shape
=
(
new_shape
,
new_shape
)
...
@@ -800,7 +803,7 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scale
...
@@ -800,7 +803,7 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scale
new_unpad
=
int
(
round
(
shape
[
1
]
*
r
)),
int
(
round
(
shape
[
0
]
*
r
))
new_unpad
=
int
(
round
(
shape
[
1
]
*
r
)),
int
(
round
(
shape
[
0
]
*
r
))
dw
,
dh
=
new_shape
[
1
]
-
new_unpad
[
0
],
new_shape
[
0
]
-
new_unpad
[
1
]
# wh padding
dw
,
dh
=
new_shape
[
1
]
-
new_unpad
[
0
],
new_shape
[
0
]
-
new_unpad
[
1
]
# wh padding
if
auto
:
# minimum rectangle
if
auto
:
# minimum rectangle
dw
,
dh
=
np
.
mod
(
dw
,
32
),
np
.
mod
(
dh
,
32
)
# wh padding
dw
,
dh
=
np
.
mod
(
dw
,
stride
),
np
.
mod
(
dh
,
stride
)
# wh padding
elif
scaleFill
:
# stretch
elif
scaleFill
:
# stretch
dw
,
dh
=
0.0
,
0.0
dw
,
dh
=
0.0
,
0.0
new_unpad
=
(
new_shape
[
1
],
new_shape
[
0
])
new_unpad
=
(
new_shape
[
1
],
new_shape
[
0
])
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论