Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
e8cf24b6
提交
e8cf24b6
authored
7月 07, 2020
作者:
Glenn Jocher
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Initial model ensemble capability #318
上级
121d90b3
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
33 行增加
和
20 行删除
+33
-20
detect.py
detect.py
+3
-4
experimental.py
models/experimental.py
+17
-0
test.py
test.py
+12
-15
datasets.py
utils/datasets.py
+1
-1
没有找到文件。
detect.py
浏览文件 @
e8cf24b6
...
...
@@ -2,7 +2,7 @@ import argparse
import
torch.backends.cudnn
as
cudnn
from
utils
import
google_utils
from
models.experimental
import
*
from
utils.datasets
import
*
from
utils.utils
import
*
...
...
@@ -20,8 +20,7 @@ def detect(save_img=False):
half
=
device
.
type
!=
'cpu'
# half precision only supported on CUDA
# Load model
google_utils
.
attempt_download
(
weights
)
model
=
torch
.
load
(
weights
,
map_location
=
device
)[
'model'
]
.
float
()
.
eval
()
# 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
if
half
:
model
.
half
()
# to FP16
...
...
@@ -137,7 +136,7 @@ def detect(save_img=False):
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'--weights'
,
type
=
str
,
default
=
'weights/yolov5s.pt'
,
help
=
'model.pt path
'
)
parser
.
add_argument
(
'--weights'
,
nargs
=
'+'
,
type
=
str
,
default
=
'yolov5s.pt'
,
help
=
'model.pt path(s)
'
)
parser
.
add_argument
(
'--source'
,
type
=
str
,
default
=
'inference/images'
,
help
=
'source'
)
# file/folder, 0 for webcam
parser
.
add_argument
(
'--output'
,
type
=
str
,
default
=
'inference/output'
,
help
=
'output folder'
)
# output folder
parser
.
add_argument
(
'--img-size'
,
type
=
int
,
default
=
640
,
help
=
'inference size (pixels)'
)
...
...
models/experimental.py
浏览文件 @
e8cf24b6
# This file contains experimental modules
from
models.common
import
*
from
utils
import
google_utils
class
CrossConv
(
nn
.
Module
):
...
...
@@ -119,3 +120,19 @@ class Ensemble(nn.ModuleList):
for
module
in
self
:
y
.
append
(
module
(
x
,
augment
)[
0
])
return
torch
.
cat
(
y
,
1
),
None
# ensembled inference output, train output
def
attempt_load
(
weights
,
map_location
=
None
):
# Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
model
=
Ensemble
()
for
w
in
weights
if
isinstance
(
weights
,
list
)
else
[
weights
]:
google_utils
.
attempt_download
(
w
)
model
.
append
(
torch
.
load
(
w
,
map_location
=
map_location
)[
'model'
]
.
float
()
.
fuse
()
.
eval
())
# load FP32 model
if
len
(
model
)
==
1
:
return
model
[
-
1
]
# return model
else
:
print
(
'Ensemble created with
%
s
\n
'
%
weights
)
for
k
in
[
'names'
,
'stride'
]:
setattr
(
model
,
k
,
getattr
(
model
[
-
1
],
k
))
return
model
# return ensemble
test.py
浏览文件 @
e8cf24b6
import
argparse
import
json
from
utils
import
google_utils
from
models.experimental
import
*
from
utils.datasets
import
*
from
utils.utils
import
*
def
test
(
data
,
...
...
@@ -20,28 +19,26 @@ def test(data,
dataloader
=
None
,
merge
=
False
):
# Initialize/load model and set device
if
model
is
None
:
training
=
False
merge
=
opt
.
merge
# use Merge NMS
training
=
model
is
not
None
if
training
:
# called by train.py
device
=
next
(
model
.
parameters
())
.
device
# get model device
else
:
# called directly
device
=
torch_utils
.
select_device
(
opt
.
device
,
batch_size
=
batch_size
)
merge
=
opt
.
merge
# use Merge NMS
# Remove previous
for
f
in
glob
.
glob
(
'test_batch*.jpg'
):
os
.
remove
(
f
)
# Load model
google_utils
.
attempt_download
(
weights
)
model
=
torch
.
load
(
weights
,
map_location
=
device
)[
'model'
]
.
float
()
.
fuse
()
.
to
(
device
)
# load to FP32
model
=
attempt_load
(
weights
,
map_location
=
device
)
# load FP32 model
imgsz
=
check_img_size
(
imgsz
,
s
=
model
.
stride
.
max
())
# check img_size
# Multi-GPU disabled, incompatible with .half() https://github.com/ultralytics/yolov5/issues/99
# if device.type != 'cpu' and torch.cuda.device_count() > 1:
# model = nn.DataParallel(model)
else
:
# called by train.py
training
=
True
device
=
next
(
model
.
parameters
())
.
device
# get model device
# Half
half
=
device
.
type
!=
'cpu'
and
torch
.
cuda
.
device_count
()
==
1
# half precision only supported on single-GPU
if
half
:
...
...
@@ -56,11 +53,11 @@ def test(data,
niou
=
iouv
.
numel
()
# Dataloader
if
dataloader
is
None
:
# not training
if
not
training
:
img
=
torch
.
zeros
((
1
,
3
,
imgsz
,
imgsz
),
device
=
device
)
# init img
_
=
model
(
img
.
half
()
if
half
else
img
)
if
device
.
type
!=
'cpu'
else
None
# run once
path
=
data
[
'test'
]
if
opt
.
task
==
'test'
else
data
[
'val'
]
# path to val/test images
dataloader
=
create_dataloader
(
path
,
imgsz
,
batch_size
,
int
(
max
(
model
.
stride
)
),
opt
,
dataloader
=
create_dataloader
(
path
,
imgsz
,
batch_size
,
model
.
stride
.
max
(
),
opt
,
hyp
=
None
,
augment
=
False
,
cache
=
False
,
pad
=
0.5
,
rect
=
True
)[
0
]
seen
=
0
...
...
@@ -193,7 +190,7 @@ def test(data,
if
save_json
and
map50
and
len
(
jdict
):
imgIds
=
[
int
(
Path
(
x
)
.
stem
.
split
(
'_'
)[
-
1
])
for
x
in
dataloader
.
dataset
.
img_files
]
f
=
'detections_val2017_
%
s_results.json'
%
\
(
weights
.
split
(
os
.
sep
)[
-
1
]
.
replace
(
'.pt'
,
''
)
if
weights
else
''
)
# filename
(
weights
.
split
(
os
.
sep
)[
-
1
]
.
replace
(
'.pt'
,
''
)
if
isinstance
(
weights
,
str
)
else
''
)
# filename
print
(
'
\n
COCO mAP with pycocotools... saving
%
s...'
%
f
)
with
open
(
f
,
'w'
)
as
file
:
json
.
dump
(
jdict
,
file
)
...
...
@@ -226,7 +223,7 @@ def test(data,
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
(
prog
=
'test.py'
)
parser
.
add_argument
(
'--weights'
,
type
=
str
,
default
=
'weights/yolov5s.pt'
,
help
=
'model.pt path
'
)
parser
.
add_argument
(
'--weights'
,
nargs
=
'+'
,
type
=
str
,
default
=
'yolov5s.pt'
,
help
=
'model.pt path(s)
'
)
parser
.
add_argument
(
'--data'
,
type
=
str
,
default
=
'data/coco128.yaml'
,
help
=
'*.data path'
)
parser
.
add_argument
(
'--batch-size'
,
type
=
int
,
default
=
32
,
help
=
'size of each image batch'
)
parser
.
add_argument
(
'--img-size'
,
type
=
int
,
default
=
640
,
help
=
'inference size (pixels)'
)
...
...
utils/datasets.py
浏览文件 @
e8cf24b6
...
...
@@ -48,7 +48,7 @@ def create_dataloader(path, imgsz, batch_size, stride, opt, hyp=None, augment=Fa
rect
=
rect
,
# rectangular training
cache_images
=
cache
,
single_cls
=
opt
.
single_cls
,
stride
=
stride
,
stride
=
int
(
stride
)
,
pad
=
pad
)
batch_size
=
min
(
batch_size
,
len
(
dataset
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论