Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
c8e52304
Unverified
提交
c8e52304
authored
9月 24, 2022
作者:
Glenn Jocher
提交者:
GitHub
9月 24, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
New `scale_segments()` function (#9570)
* Rename scale_coords to scale_boxes * add scale_segments
上级
d669a746
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
54 行增加
和
28 行删除
+54
-28
detect.py
detect.py
+2
-2
common.py
models/common.py
+2
-2
predict.py
segment/predict.py
+2
-2
val.py
segment/val.py
+3
-3
general.py
utils/general.py
+36
-10
__init__.py
utils/loggers/comet/__init__.py
+4
-4
plots.py
utils/plots.py
+2
-2
val.py
val.py
+3
-3
没有找到文件。
detect.py
浏览文件 @
c8e52304
...
...
@@ -42,7 +42,7 @@ ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
from
models.common
import
DetectMultiBackend
from
utils.dataloaders
import
IMG_FORMATS
,
VID_FORMATS
,
LoadImages
,
LoadScreenshots
,
LoadStreams
from
utils.general
import
(
LOGGER
,
Profile
,
check_file
,
check_img_size
,
check_imshow
,
check_requirements
,
colorstr
,
cv2
,
increment_path
,
non_max_suppression
,
print_args
,
scale_
coord
s
,
strip_optimizer
,
xyxy2xywh
)
increment_path
,
non_max_suppression
,
print_args
,
scale_
boxe
s
,
strip_optimizer
,
xyxy2xywh
)
from
utils.plots
import
Annotator
,
colors
,
save_one_box
from
utils.torch_utils
import
select_device
,
smart_inference_mode
...
...
@@ -148,7 +148,7 @@ def run(
annotator
=
Annotator
(
im0
,
line_width
=
line_thickness
,
example
=
str
(
names
))
if
len
(
det
):
# Rescale boxes from img_size to im0 size
det
[:,
:
4
]
=
scale_
coord
s
(
im
.
shape
[
2
:],
det
[:,
:
4
],
im0
.
shape
)
.
round
()
det
[:,
:
4
]
=
scale_
boxe
s
(
im
.
shape
[
2
:],
det
[:,
:
4
],
im0
.
shape
)
.
round
()
# Print results
for
c
in
det
[:,
5
]
.
unique
():
...
...
models/common.py
浏览文件 @
c8e52304
...
...
@@ -23,7 +23,7 @@ from torch.cuda import amp
from
utils.dataloaders
import
exif_transpose
,
letterbox
from
utils.general
import
(
LOGGER
,
ROOT
,
Profile
,
check_requirements
,
check_suffix
,
check_version
,
colorstr
,
increment_path
,
make_divisible
,
non_max_suppression
,
scale_
coord
s
,
xywh2xyxy
,
xyxy2xywh
,
increment_path
,
make_divisible
,
non_max_suppression
,
scale_
boxe
s
,
xywh2xyxy
,
xyxy2xywh
,
yaml_load
)
from
utils.plots
import
Annotator
,
colors
,
save_one_box
from
utils.torch_utils
import
copy_attr
,
smart_inference_mode
...
...
@@ -703,7 +703,7 @@ class AutoShape(nn.Module):
self
.
multi_label
,
max_det
=
self
.
max_det
)
# NMS
for
i
in
range
(
n
):
scale_
coord
s
(
shape1
,
y
[
i
][:,
:
4
],
shape0
[
i
])
scale_
boxe
s
(
shape1
,
y
[
i
][:,
:
4
],
shape0
[
i
])
return
Detections
(
ims
,
y
,
files
,
dt
,
self
.
names
,
x
.
shape
)
...
...
segment/predict.py
浏览文件 @
c8e52304
...
...
@@ -42,7 +42,7 @@ ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
from
models.common
import
DetectMultiBackend
from
utils.dataloaders
import
IMG_FORMATS
,
VID_FORMATS
,
LoadImages
,
LoadScreenshots
,
LoadStreams
from
utils.general
import
(
LOGGER
,
Profile
,
check_file
,
check_img_size
,
check_imshow
,
check_requirements
,
colorstr
,
cv2
,
increment_path
,
non_max_suppression
,
print_args
,
scale_
coord
s
,
strip_optimizer
,
xyxy2xywh
)
increment_path
,
non_max_suppression
,
print_args
,
scale_
boxe
s
,
strip_optimizer
,
xyxy2xywh
)
from
utils.plots
import
Annotator
,
colors
,
save_one_box
from
utils.segment.general
import
process_mask
from
utils.torch_utils
import
select_device
,
smart_inference_mode
...
...
@@ -152,7 +152,7 @@ def run(
masks
=
process_mask
(
proto
[
i
],
det
[:,
6
:],
det
[:,
:
4
],
im
.
shape
[
2
:],
upsample
=
True
)
# HWC
# Rescale boxes from img_size to im0 size
det
[:,
:
4
]
=
scale_
coord
s
(
im
.
shape
[
2
:],
det
[:,
:
4
],
im0
.
shape
)
.
round
()
det
[:,
:
4
]
=
scale_
boxe
s
(
im
.
shape
[
2
:],
det
[:,
:
4
],
im0
.
shape
)
.
round
()
# Print results
for
c
in
det
[:,
5
]
.
unique
():
...
...
segment/val.py
浏览文件 @
c8e52304
...
...
@@ -44,7 +44,7 @@ from models.yolo import SegmentationModel
from
utils.callbacks
import
Callbacks
from
utils.general
import
(
LOGGER
,
NUM_THREADS
,
Profile
,
check_dataset
,
check_img_size
,
check_requirements
,
check_yaml
,
coco80_to_coco91_class
,
colorstr
,
increment_path
,
non_max_suppression
,
print_args
,
scale_
coord
s
,
xywh2xyxy
,
xyxy2xywh
)
scale_
boxe
s
,
xywh2xyxy
,
xyxy2xywh
)
from
utils.metrics
import
ConfusionMatrix
,
box_iou
from
utils.plots
import
output_to_target
,
plot_val_study
from
utils.segment.dataloaders
import
create_dataloader
...
...
@@ -298,12 +298,12 @@ def run(
if
single_cls
:
pred
[:,
5
]
=
0
predn
=
pred
.
clone
()
scale_
coord
s
(
im
[
si
]
.
shape
[
1
:],
predn
[:,
:
4
],
shape
,
shapes
[
si
][
1
])
# native-space pred
scale_
boxe
s
(
im
[
si
]
.
shape
[
1
:],
predn
[:,
:
4
],
shape
,
shapes
[
si
][
1
])
# native-space pred
# Evaluate
if
nl
:
tbox
=
xywh2xyxy
(
labels
[:,
1
:
5
])
# target boxes
scale_
coord
s
(
im
[
si
]
.
shape
[
1
:],
tbox
,
shape
,
shapes
[
si
][
1
])
# native-space labels
scale_
boxe
s
(
im
[
si
]
.
shape
[
1
:],
tbox
,
shape
,
shapes
[
si
][
1
])
# native-space labels
labelsn
=
torch
.
cat
((
labels
[:,
0
:
1
],
tbox
),
1
)
# native-space labels
correct_bboxes
=
process_batch
(
predn
,
labelsn
,
iouv
)
correct_masks
=
process_batch
(
predn
,
labelsn
,
iouv
,
pred_masks
,
gt_masks
,
overlap
=
overlap
,
masks
=
True
)
...
...
utils/general.py
浏览文件 @
c8e52304
...
...
@@ -725,7 +725,7 @@ def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0):
def
xyxy2xywhn
(
x
,
w
=
640
,
h
=
640
,
clip
=
False
,
eps
=
0.0
):
# Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right
if
clip
:
clip_
coord
s
(
x
,
(
h
-
eps
,
w
-
eps
))
# warning: inplace clip
clip_
boxe
s
(
x
,
(
h
-
eps
,
w
-
eps
))
# warning: inplace clip
y
=
x
.
clone
()
if
isinstance
(
x
,
torch
.
Tensor
)
else
np
.
copy
(
x
)
y
[:,
0
]
=
((
x
[:,
0
]
+
x
[:,
2
])
/
2
)
/
w
# x center
y
[:,
1
]
=
((
x
[:,
1
]
+
x
[:,
3
])
/
2
)
/
h
# y center
...
...
@@ -769,7 +769,23 @@ def resample_segments(segments, n=1000):
return
segments
def
scale_coords
(
img1_shape
,
coords
,
img0_shape
,
ratio_pad
=
None
):
def
scale_boxes
(
img1_shape
,
boxes
,
img0_shape
,
ratio_pad
=
None
):
# Rescale boxes (xyxy) from img1_shape to img0_shape
if
ratio_pad
is
None
:
# calculate from img0_shape
gain
=
min
(
img1_shape
[
0
]
/
img0_shape
[
0
],
img1_shape
[
1
]
/
img0_shape
[
1
])
# gain = old / new
pad
=
(
img1_shape
[
1
]
-
img0_shape
[
1
]
*
gain
)
/
2
,
(
img1_shape
[
0
]
-
img0_shape
[
0
]
*
gain
)
/
2
# wh padding
else
:
gain
=
ratio_pad
[
0
][
0
]
pad
=
ratio_pad
[
1
]
boxes
[:,
[
0
,
2
]]
-=
pad
[
0
]
# x padding
boxes
[:,
[
1
,
3
]]
-=
pad
[
1
]
# y padding
boxes
[:,
:
4
]
/=
gain
clip_boxes
(
boxes
,
img0_shape
)
return
boxes
def
scale_segments
(
img1_shape
,
segments
,
img0_shape
,
ratio_pad
=
None
):
# Rescale coords (xyxy) from img1_shape to img0_shape
if
ratio_pad
is
None
:
# calculate from img0_shape
gain
=
min
(
img1_shape
[
0
]
/
img0_shape
[
0
],
img1_shape
[
1
]
/
img0_shape
[
1
])
# gain = old / new
...
...
@@ -778,15 +794,15 @@ def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
gain
=
ratio_pad
[
0
][
0
]
pad
=
ratio_pad
[
1
]
coords
[:,
[
0
,
2
]
]
-=
pad
[
0
]
# x padding
coords
[:,
[
1
,
3
]
]
-=
pad
[
1
]
# y padding
coords
[:,
:
4
]
/=
gain
clip_
coords
(
coord
s
,
img0_shape
)
return
coord
s
segments
[:,
0
]
-=
pad
[
0
]
# x padding
segments
[:,
1
]
-=
pad
[
1
]
# y padding
segments
/=
gain
clip_
segments
(
segment
s
,
img0_shape
)
return
segment
s
def
clip_
coord
s
(
boxes
,
shape
):
# Clip bo
unding xyxy bounding boxes
to image shape (height, width)
def
clip_
boxe
s
(
boxes
,
shape
):
# Clip bo
xes (xyxy)
to image shape (height, width)
if
isinstance
(
boxes
,
torch
.
Tensor
):
# faster individually
boxes
[:,
0
]
.
clamp_
(
0
,
shape
[
1
])
# x1
boxes
[:,
1
]
.
clamp_
(
0
,
shape
[
0
])
# y1
...
...
@@ -797,6 +813,16 @@ def clip_coords(boxes, shape):
boxes
[:,
[
1
,
3
]]
=
boxes
[:,
[
1
,
3
]]
.
clip
(
0
,
shape
[
0
])
# y1, y2
def
clip_segments
(
boxes
,
shape
):
# Clip segments (xy1,xy2,...) to image shape (height, width)
if
isinstance
(
boxes
,
torch
.
Tensor
):
# faster individually
boxes
[:,
0
]
.
clamp_
(
0
,
shape
[
1
])
# x
boxes
[:,
1
]
.
clamp_
(
0
,
shape
[
0
])
# y
else
:
# np.array (faster grouped)
boxes
[:,
0
]
=
boxes
[:,
0
]
.
clip
(
0
,
shape
[
1
])
# x
boxes
[:,
1
]
=
boxes
[:,
1
]
.
clip
(
0
,
shape
[
0
])
# y
def
non_max_suppression
(
prediction
,
conf_thres
=
0.25
,
...
...
@@ -980,7 +1006,7 @@ def apply_classifier(x, model, img, im0):
d
[:,
:
4
]
=
xywh2xyxy
(
b
)
.
long
()
# Rescale boxes from img_size to im0 size
scale_
coord
s
(
img
.
shape
[
2
:],
d
[:,
:
4
],
im0
[
i
]
.
shape
)
scale_
boxe
s
(
img
.
shape
[
2
:],
d
[:,
:
4
],
im0
[
i
]
.
shape
)
# Classes
pred_cls1
=
d
[:,
5
]
.
long
()
...
...
utils/loggers/comet/__init__.py
浏览文件 @
c8e52304
...
...
@@ -28,7 +28,7 @@ import torchvision.transforms as T
import
yaml
from
utils.dataloaders
import
img2label_paths
from
utils.general
import
check_dataset
,
scale_
coord
s
,
xywh2xyxy
from
utils.general
import
check_dataset
,
scale_
boxe
s
,
xywh2xyxy
from
utils.metrics
import
box_iou
COMET_PREFIX
=
"comet://"
...
...
@@ -293,14 +293,14 @@ class CometLogger:
pred
[:,
5
]
=
0
predn
=
pred
.
clone
()
scale_
coord
s
(
image
.
shape
[
1
:],
predn
[:,
:
4
],
shape
[
0
],
shape
[
1
])
scale_
boxe
s
(
image
.
shape
[
1
:],
predn
[:,
:
4
],
shape
[
0
],
shape
[
1
])
labelsn
=
None
if
nl
:
tbox
=
xywh2xyxy
(
labels
[:,
1
:
5
])
# target boxes
scale_
coord
s
(
image
.
shape
[
1
:],
tbox
,
shape
[
0
],
shape
[
1
])
# native-space labels
scale_
boxe
s
(
image
.
shape
[
1
:],
tbox
,
shape
[
0
],
shape
[
1
])
# native-space labels
labelsn
=
torch
.
cat
((
labels
[:,
0
:
1
],
tbox
),
1
)
# native-space labels
scale_
coord
s
(
image
.
shape
[
1
:],
predn
[:,
:
4
],
shape
[
0
],
shape
[
1
])
# native-space pred
scale_
boxe
s
(
image
.
shape
[
1
:],
predn
[:,
:
4
],
shape
[
0
],
shape
[
1
])
# native-space pred
return
predn
,
labelsn
...
...
utils/plots.py
浏览文件 @
c8e52304
...
...
@@ -20,7 +20,7 @@ import torch
from
PIL
import
Image
,
ImageDraw
,
ImageFont
from
utils
import
TryExcept
,
threaded
from
utils.general
import
(
CONFIG_DIR
,
FONT
,
LOGGER
,
check_font
,
check_requirements
,
clip_
coord
s
,
increment_path
,
from
utils.general
import
(
CONFIG_DIR
,
FONT
,
LOGGER
,
check_font
,
check_requirements
,
clip_
boxe
s
,
increment_path
,
is_ascii
,
xywh2xyxy
,
xyxy2xywh
)
from
utils.metrics
import
fitness
from
utils.segment.general
import
scale_image
...
...
@@ -565,7 +565,7 @@ def save_one_box(xyxy, im, file=Path('im.jpg'), gain=1.02, pad=10, square=False,
b
[:,
2
:]
=
b
[:,
2
:]
.
max
(
1
)[
0
]
.
unsqueeze
(
1
)
# attempt rectangle to square
b
[:,
2
:]
=
b
[:,
2
:]
*
gain
+
pad
# box wh * gain + pad
xyxy
=
xywh2xyxy
(
b
)
.
long
()
clip_
coord
s
(
xyxy
,
im
.
shape
)
clip_
boxe
s
(
xyxy
,
im
.
shape
)
crop
=
im
[
int
(
xyxy
[
0
,
1
]):
int
(
xyxy
[
0
,
3
]),
int
(
xyxy
[
0
,
0
]):
int
(
xyxy
[
0
,
2
]),
::(
1
if
BGR
else
-
1
)]
if
save
:
file
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
# make directory
...
...
val.py
浏览文件 @
c8e52304
...
...
@@ -40,7 +40,7 @@ from utils.callbacks import Callbacks
from
utils.dataloaders
import
create_dataloader
from
utils.general
import
(
LOGGER
,
Profile
,
check_dataset
,
check_img_size
,
check_requirements
,
check_yaml
,
coco80_to_coco91_class
,
colorstr
,
increment_path
,
non_max_suppression
,
print_args
,
scale_
coord
s
,
xywh2xyxy
,
xyxy2xywh
)
scale_
boxe
s
,
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
,
smart_inference_mode
...
...
@@ -244,12 +244,12 @@ def run(
if
single_cls
:
pred
[:,
5
]
=
0
predn
=
pred
.
clone
()
scale_
coord
s
(
im
[
si
]
.
shape
[
1
:],
predn
[:,
:
4
],
shape
,
shapes
[
si
][
1
])
# native-space pred
scale_
boxe
s
(
im
[
si
]
.
shape
[
1
:],
predn
[:,
:
4
],
shape
,
shapes
[
si
][
1
])
# native-space pred
# Evaluate
if
nl
:
tbox
=
xywh2xyxy
(
labels
[:,
1
:
5
])
# target boxes
scale_
coord
s
(
im
[
si
]
.
shape
[
1
:],
tbox
,
shape
,
shapes
[
si
][
1
])
# native-space labels
scale_
boxe
s
(
im
[
si
]
.
shape
[
1
:],
tbox
,
shape
,
shapes
[
si
][
1
])
# native-space labels
labelsn
=
torch
.
cat
((
labels
[:,
0
:
1
],
tbox
),
1
)
# native-space labels
correct
=
process_batch
(
predn
,
labelsn
,
iouv
)
if
plots
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论