Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RAVEN2YOLO
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stephane Chavin
RAVEN2YOLO
Commits
73753d7c
Commit
73753d7c
authored
2 years ago
by
Stephane Chavin
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
9648f086
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
yolov5/models/utils/segment/augmentations.py
+104
-0
104 additions, 0 deletions
yolov5/models/utils/segment/augmentations.py
with
104 additions
and
0 deletions
yolov5/models/utils/segment/augmentations.py
0 → 100644
+
104
−
0
View file @
73753d7c
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
"""
Image augmentation functions
"""
import
math
import
random
import
cv2
import
numpy
as
np
from
..augmentations
import
box_candidates
from
..general
import
resample_segments
,
segment2box
def
mixup
(
im
,
labels
,
segments
,
im2
,
labels2
,
segments2
):
# Applies MixUp augmentation https://arxiv.org/pdf/1710.09412.pdf
r
=
np
.
random
.
beta
(
32.0
,
32.0
)
# mixup ratio, alpha=beta=32.0
im
=
(
im
*
r
+
im2
*
(
1
-
r
)).
astype
(
np
.
uint8
)
labels
=
np
.
concatenate
((
labels
,
labels2
),
0
)
segments
=
np
.
concatenate
((
segments
,
segments2
),
0
)
return
im
,
labels
,
segments
def
random_perspective
(
im
,
targets
=
(),
segments
=
(),
degrees
=
10
,
translate
=
.
1
,
scale
=
.
1
,
shear
=
10
,
perspective
=
0.0
,
border
=
(
0
,
0
)):
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
# targets = [cls, xyxy]
height
=
im
.
shape
[
0
]
+
border
[
0
]
*
2
# shape(h,w,c)
width
=
im
.
shape
[
1
]
+
border
[
1
]
*
2
# Center
C
=
np
.
eye
(
3
)
C
[
0
,
2
]
=
-
im
.
shape
[
1
]
/
2
# x translation (pixels)
C
[
1
,
2
]
=
-
im
.
shape
[
0
]
/
2
# y translation (pixels)
# Perspective
P
=
np
.
eye
(
3
)
P
[
2
,
0
]
=
random
.
uniform
(
-
perspective
,
perspective
)
# x perspective (about y)
P
[
2
,
1
]
=
random
.
uniform
(
-
perspective
,
perspective
)
# y perspective (about x)
# Rotation and Scale
R
=
np
.
eye
(
3
)
a
=
random
.
uniform
(
-
degrees
,
degrees
)
# a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations
s
=
random
.
uniform
(
1
-
scale
,
1
+
scale
)
# s = 2 ** random.uniform(-scale, scale)
R
[:
2
]
=
cv2
.
getRotationMatrix2D
(
angle
=
a
,
center
=
(
0
,
0
),
scale
=
s
)
# Shear
S
=
np
.
eye
(
3
)
S
[
0
,
1
]
=
math
.
tan
(
random
.
uniform
(
-
shear
,
shear
)
*
math
.
pi
/
180
)
# x shear (deg)
S
[
1
,
0
]
=
math
.
tan
(
random
.
uniform
(
-
shear
,
shear
)
*
math
.
pi
/
180
)
# y shear (deg)
# Translation
T
=
np
.
eye
(
3
)
T
[
0
,
2
]
=
(
random
.
uniform
(
0.5
-
translate
,
0.5
+
translate
)
*
width
)
# x translation (pixels)
T
[
1
,
2
]
=
(
random
.
uniform
(
0.5
-
translate
,
0.5
+
translate
)
*
height
)
# y translation (pixels)
# Combined rotation matrix
M
=
T
@
S
@
R
@
P
@
C
# order of operations (right to left) is IMPORTANT
if
(
border
[
0
]
!=
0
)
or
(
border
[
1
]
!=
0
)
or
(
M
!=
np
.
eye
(
3
)).
any
():
# image changed
if
perspective
:
im
=
cv2
.
warpPerspective
(
im
,
M
,
dsize
=
(
width
,
height
),
borderValue
=
(
114
,
114
,
114
))
else
:
# affine
im
=
cv2
.
warpAffine
(
im
,
M
[:
2
],
dsize
=
(
width
,
height
),
borderValue
=
(
114
,
114
,
114
))
# Visualize
# import matplotlib.pyplot as plt
# ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
# ax[0].imshow(im[:, :, ::-1]) # base
# ax[1].imshow(im2[:, :, ::-1]) # warped
# Transform label coordinates
n
=
len
(
targets
)
new_segments
=
[]
if
n
:
new
=
np
.
zeros
((
n
,
4
))
segments
=
resample_segments
(
segments
)
# upsample
for
i
,
segment
in
enumerate
(
segments
):
xy
=
np
.
ones
((
len
(
segment
),
3
))
xy
[:,
:
2
]
=
segment
xy
=
xy
@
M
.
T
# transform
xy
=
(
xy
[:,
:
2
]
/
xy
[:,
2
:
3
]
if
perspective
else
xy
[:,
:
2
])
# perspective rescale or affine
# clip
new
[
i
]
=
segment2box
(
xy
,
width
,
height
)
new_segments
.
append
(
xy
)
# filter candidates
i
=
box_candidates
(
box1
=
targets
[:,
1
:
5
].
T
*
s
,
box2
=
new
.
T
,
area_thr
=
0.01
)
targets
=
targets
[
i
]
targets
[:,
1
:
5
]
=
new
[
i
]
new_segments
=
np
.
array
(
new_segments
)[
i
]
return
im
,
targets
,
new_segments
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment