Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
dc38cd03
Unverified
提交
dc38cd03
authored
8月 13, 2022
作者:
Glenn Jocher
提交者:
GitHub
8月 13, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
New `smart_inference_mode()` conditional decorator (#8957)
New smart_inference_mode()
上级
6aed0a7c
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
27 行增加
和
19 行删除
+27
-19
detect.py
detect.py
+2
-2
export.py
export.py
+2
-2
common.py
models/common.py
+2
-2
yolo.py
models/yolo.py
+2
-2
torch_utils.py
utils/torch_utils.py
+17
-9
val.py
val.py
+2
-2
没有找到文件。
detect.py
浏览文件 @
dc38cd03
...
...
@@ -44,10 +44,10 @@ from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
from
utils.general
import
(
LOGGER
,
check_file
,
check_img_size
,
check_imshow
,
check_requirements
,
colorstr
,
cv2
,
increment_path
,
non_max_suppression
,
print_args
,
scale_coords
,
strip_optimizer
,
xyxy2xywh
)
from
utils.plots
import
Annotator
,
colors
,
save_one_box
from
utils.torch_utils
import
select_device
,
time_sync
from
utils.torch_utils
import
select_device
,
smart_inference_mode
,
time_sync
@
torch.no_grad
()
@
smart_inference_mode
()
def
run
(
weights
=
ROOT
/
'yolov5s.pt'
,
# model.pt path(s)
source
=
ROOT
/
'data/images'
,
# file/dir/URL/glob, 0 for webcam
...
...
export.py
浏览文件 @
dc38cd03
...
...
@@ -69,7 +69,7 @@ from models.yolo import Detect
from
utils.dataloaders
import
LoadImages
from
utils.general
import
(
LOGGER
,
check_dataset
,
check_img_size
,
check_requirements
,
check_version
,
check_yaml
,
colorstr
,
file_size
,
print_args
,
url2file
)
from
utils.torch_utils
import
select_device
from
utils.torch_utils
import
select_device
,
smart_inference_mode
def
export_formats
():
...
...
@@ -455,7 +455,7 @@ def export_tfjs(file, prefix=colorstr('TensorFlow.js:')):
LOGGER
.
info
(
f
'
\n
{prefix} export failure: {e}'
)
@
torch.no_grad
()
@
smart_inference_mode
()
def
run
(
data
=
ROOT
/
'data/coco128.yaml'
,
# 'dataset.yaml path'
weights
=
ROOT
/
'yolov5s.pt'
,
# weights path
...
...
models/common.py
浏览文件 @
dc38cd03
...
...
@@ -25,7 +25,7 @@ from utils.dataloaders import exif_transpose, letterbox
from
utils.general
import
(
LOGGER
,
check_requirements
,
check_suffix
,
check_version
,
colorstr
,
increment_path
,
make_divisible
,
non_max_suppression
,
scale_coords
,
xywh2xyxy
,
xyxy2xywh
)
from
utils.plots
import
Annotator
,
colors
,
save_one_box
from
utils.torch_utils
import
copy_attr
,
time_sync
from
utils.torch_utils
import
copy_attr
,
smart_inference_mode
,
time_sync
def
autopad
(
k
,
p
=
None
):
# kernel, padding
...
...
@@ -578,7 +578,7 @@ class AutoShape(nn.Module):
m
.
anchor_grid
=
list
(
map
(
fn
,
m
.
anchor_grid
))
return
self
@
torch.no_grad
()
@
smart_inference_mode
()
def
forward
(
self
,
imgs
,
size
=
640
,
augment
=
False
,
profile
=
False
):
# Inference from various sources. For height=640, width=1280, RGB images example inputs are:
# file: imgs = 'data/images/zidane.jpg' # str or PosixPath
...
...
models/yolo.py
浏览文件 @
dc38cd03
...
...
@@ -76,12 +76,12 @@ class Detect(nn.Module):
return
x
if
self
.
training
else
(
torch
.
cat
(
z
,
1
),)
if
self
.
export
else
(
torch
.
cat
(
z
,
1
),
x
)
def
_make_grid
(
self
,
nx
=
20
,
ny
=
20
,
i
=
0
):
def
_make_grid
(
self
,
nx
=
20
,
ny
=
20
,
i
=
0
,
torch_1_10
=
check_version
(
torch
.
__version__
,
'1.10.0'
)
):
d
=
self
.
anchors
[
i
]
.
device
t
=
self
.
anchors
[
i
]
.
dtype
shape
=
1
,
self
.
na
,
ny
,
nx
,
2
# grid shape
y
,
x
=
torch
.
arange
(
ny
,
device
=
d
,
dtype
=
t
),
torch
.
arange
(
nx
,
device
=
d
,
dtype
=
t
)
if
check_version
(
torch
.
__version__
,
'1.10.0'
)
:
# torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
if
torch_1_10
:
# torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
yv
,
xv
=
torch
.
meshgrid
(
y
,
x
,
indexing
=
'ij'
)
else
:
yv
,
xv
=
torch
.
meshgrid
(
y
,
x
)
...
...
utils/torch_utils.py
浏览文件 @
dc38cd03
...
...
@@ -34,6 +34,14 @@ except ImportError:
warnings
.
filterwarnings
(
'ignore'
,
message
=
'User provided device_type of
\'
cuda
\'
, but CUDA is not available. Disabling'
)
def
smart_inference_mode
(
torch_1_9
=
check_version
(
torch
.
__version__
,
'1.9.0'
)):
# Applies torch.inference_mode() decorator if torch>=1.9.0 else torch.no_grad() decorator
def
decorate
(
fn
):
return
(
torch
.
inference_mode
if
torch_1_9
else
torch
.
no_grad
)()(
fn
)
return
decorate
def
smart_DDP
(
model
):
# Model DDP creation with checks
assert
not
check_version
(
torch
.
__version__
,
'1.12.0'
,
pinned
=
True
),
\
...
...
@@ -364,17 +372,17 @@ class ModelEMA:
for
p
in
self
.
ema
.
parameters
():
p
.
requires_grad_
(
False
)
@smart_inference_mode
()
def
update
(
self
,
model
):
# Update EMA parameters
with
torch
.
no_grad
():
self
.
updates
+=
1
d
=
self
.
decay
(
self
.
updates
)
msd
=
de_parallel
(
model
)
.
state_dict
()
# model state_dict
for
k
,
v
in
self
.
ema
.
state_dict
()
.
items
():
if
v
.
dtype
.
is_floating_point
:
v
*=
d
v
+=
(
1
-
d
)
*
msd
[
k
]
.
detach
()
self
.
updates
+=
1
d
=
self
.
decay
(
self
.
updates
)
msd
=
de_parallel
(
model
)
.
state_dict
()
# model state_dict
for
k
,
v
in
self
.
ema
.
state_dict
()
.
items
():
if
v
.
dtype
.
is_floating_point
:
v
*=
d
v
+=
(
1
-
d
)
*
msd
[
k
]
.
detach
()
def
update_attr
(
self
,
model
,
include
=
(),
exclude
=
(
'process_group'
,
'reducer'
)):
# Update EMA attributes
...
...
val.py
浏览文件 @
dc38cd03
...
...
@@ -42,7 +42,7 @@ from utils.general import (LOGGER, check_dataset, check_img_size, check_requirem
scale_coords
,
xywh2xyxy
,
xyxy2xywh
)
from
utils.metrics
import
ConfusionMatrix
,
ap_per_class
,
box_iou
from
utils.plots
import
output_to_target
,
plot_images
,
plot_val_study
from
utils.torch_utils
import
select_device
,
time_sync
from
utils.torch_utils
import
select_device
,
smart_inference_mode
,
time_sync
def
save_one_txt
(
predn
,
save_conf
,
shape
,
file
):
...
...
@@ -93,7 +93,7 @@ def process_batch(detections, labels, iouv):
return
torch
.
tensor
(
correct
,
dtype
=
torch
.
bool
,
device
=
iouv
.
device
)
@
torch.no_grad
()
@
smart_inference_mode
()
def
run
(
data
,
weights
=
None
,
# model.pt path(s)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论