Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python
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
Releases
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
Tom Rink
python
Commits
13d901ab
Commit
13d901ab
authored
1 year ago
by
tomrink
Browse files
Options
Downloads
Patches
Plain Diff
snapshot...
parent
10bacbda
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
modules/util/augment.py
+95
-0
95 additions, 0 deletions
modules/util/augment.py
with
95 additions
and
0 deletions
modules/util/augment.py
0 → 100644
+
95
−
0
View file @
13d901ab
from
functools
import
partial
import
tensorflow
as
tf
def
augment_image
(
brightness_delta
=
0.05
,
contrast_factor
=
[
0.7
,
1.3
],
saturation
=
[
0.6
,
1.6
]):
"""
Helper function used for augmentation of images in the dataset.
Args:
brightness_delta: maximum value for randomly assigning brightness of the image.
contrast_factor: list / tuple of minimum and maximum value of factor to set random contrast.
None, if not to be used.
saturation: list / tuple of minimum and maximum value of factor to set random saturation.
None, if not to be used.
Returns:
tf.data.Dataset mappable function for image augmentation
"""
def
augment_fn
(
low_resolution
,
high_resolution
,
*
args
,
**
kwargs
):
# Augmenting data (~ 80%)
def
augment_steps_fn
(
low_resolution
,
high_resolution
):
# Randomly rotating image (~50%)
def
rotate_fn
(
low_resolution
,
high_resolution
):
times
=
tf
.
random
.
uniform
(
minval
=
1
,
maxval
=
4
,
dtype
=
tf
.
int32
,
shape
=
[])
return
(
tf
.
image
.
rot90
(
low_resolution
,
times
),
tf
.
image
.
rot90
(
high_resolution
,
times
))
low_resolution
,
high_resolution
=
tf
.
cond
(
tf
.
less_equal
(
tf
.
random
.
uniform
([]),
0.5
),
lambda
:
rotate_fn
(
low_resolution
,
high_resolution
),
lambda
:
(
low_resolution
,
high_resolution
))
# Randomly flipping image (~50%)
def
flip_fn
(
low_resolution
,
high_resolution
):
return
(
tf
.
image
.
flip_left_right
(
low_resolution
),
tf
.
image
.
flip_left_right
(
high_resolution
))
low_resolution
,
high_resolution
=
tf
.
cond
(
tf
.
less_equal
(
tf
.
random
.
uniform
([]),
0.5
),
lambda
:
flip_fn
(
low_resolution
,
high_resolution
),
lambda
:
(
low_resolution
,
high_resolution
))
# Randomly setting brightness of image (~50%)
# def brightness_fn(low_resolution, high_resolution):
# delta = tf.random.uniform(minval=0, maxval=brightness_delta, dtype=tf.float32, shape=[])
# return (tf.image.adjust_brightness(low_resolution, delta=delta),
# tf.image.adjust_brightness(high_resolution, delta=delta))
#
# low_resolution, high_resolution = tf.cond(
# tf.less_equal(tf.random.uniform([]), 0.5),
# lambda: brightness_fn(low_resolution, high_resolution),
# lambda: (low_resolution, high_resolution))
#
# # Randomly setting constrast (~50%)
# def contrast_fn(low_resolution, high_resolution):
# factor = tf.random.uniform(
# minval=contrast_factor[0],
# maxval=contrast_factor[1],
# dtype=tf.float32, shape=[])
# return (tf.image.adjust_contrast(low_resolution, factor),
# tf.image.adjust_contrast(high_resolution, factor))
#
# if contrast_factor:
# low_resolution, high_resolution = tf.cond(
# tf.less_equal(tf.random.uniform([]), 0.5),
# lambda: contrast_fn(low_resolution, high_resolution),
# lambda: (low_resolution, high_resolution))
#
# # Randomly setting saturation(~50%)
# def saturation_fn(low_resolution, high_resolution):
# factor = tf.random.uniform(
# minval=saturation[0],
# maxval=saturation[1],
# dtype=tf.float32,
# shape=[])
# return (tf.image.adjust_saturation(low_resolution, factor),
# tf.image.adjust_saturation(high_resolution, factor))
#
# if saturation:
# low_resolution, high_resolution = tf.cond(
# tf.less_equal(tf.random.uniform([]), 0.5),
# lambda: saturation_fn(low_resolution, high_resolution),
# lambda: (low_resolution, high_resolution))
return
low_resolution
,
high_resolution
# Randomly returning unchanged data (~20%)
return
tf
.
cond
(
tf
.
less_equal
(
tf
.
random
.
uniform
([]),
0.2
),
lambda
:
(
low_resolution
,
high_resolution
),
partial
(
augment_steps_fn
,
low_resolution
,
high_resolution
))
return
augment_fn
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