Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.bio.face
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bob
bob.bio.face
Commits
992e72c0
Commit
992e72c0
authored
4 years ago
by
Amir MOHAMMADI
Browse files
Options
Downloads
Patches
Plain Diff
[preprocessor][scale] Rewrite the Scale transformer using simpler and more efficient code
parent
39eb03b8
No related branches found
No related tags found
2 merge requests
!71
Face crop improvements
,
!64
Dask pipelines
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
bob/bio/face/preprocessor/Scale.py
+33
-53
33 additions, 53 deletions
bob/bio/face/preprocessor/Scale.py
with
33 additions
and
53 deletions
bob/bio/face/preprocessor/Scale.py
+
33
−
53
View file @
992e72c0
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
from
sklearn.
base
import
TransformerMixin
,
BaseEstimato
r
from
sklearn.
preprocessing
import
FunctionTransforme
r
from
skimage.transform
import
resize
import
numpy
as
np
from
sklearn.utils
import
check_array
from
bob.io.image
import
to_matplotlib
,
to_bob
class
Scale
(
TransformerMixin
,
BaseEstimator
):
"""
Simple scales an images
def
scale
(
images
,
target_img_size
):
"""
Scales a list of images to the target size
Parameters
-----------
----------
images : array_like
A list of images (in Bob format) to be scaled to the target size
target_img_size : tuple
Target image size
A tuple of size 2 as (H, W)
Returns
-------
numpy.ndarray
Scaled images
"""
images
=
check_array
(
images
,
allow_nd
=
True
)
images
=
to_matplotlib
(
images
)
def
__init__
(
self
,
target_img_size
,
**
kwargs
):
# images are always batched
output_shape
=
tuple
(
target_img_size
)
output_shape
=
tuple
(
images
.
shape
[
0
:
1
])
+
output_shape
images
=
resize
(
images
,
output_shape
=
output_shape
)
self
.
target_img_size
=
target_img_size
return
to_bob
(
images
)
def
_more_tags
(
self
):
return
{
"
stateless
"
:
True
,
"
requires_fit
"
:
False
}
def
fit
(
self
,
X
,
y
=
None
):
return
self
def
transform
(
self
,
X
,
annotations
=
None
):
def
Scale
(
target_img_size
):
"""
Resize an image given a shape
A transformer that scales images.
It accepts a list of inputs
Parameters
----------
img:
Input image
-----------
target_img_size: tuple
Target image size
"""
def
_resize
(
x
):
return
resize
(
x
,
self
.
target_img_size
,
anti_aliasing
=
True
)
Target image size, specified as a tuple of (H, W)
X
=
check_array
(
X
,
allow_nd
=
True
)
if
X
.
ndim
<
2
or
X
.
ndim
>
4
:
raise
ValueError
(
f
"
Invalid image shape
{
X
.
shape
}
"
)
if
X
.
ndim
==
2
:
# Checking if it's bob format CxHxW
return
_resize
(
X
)
if
X
.
ndim
==
3
:
# Checking if it's bob format CxHxW
if
X
.
shape
[
0
]
==
3
:
X
=
np
.
moveaxis
(
X
,
-
1
,
0
)
return
_resize
(
X
)
# Batch of images
if
X
.
ndim
==
4
:
# Checking if it's bob format NxCxHxW
if
X
.
shape
[
1
]
==
3
:
X
=
np
.
moveaxis
(
X
,
1
,
-
1
)
return
[
_resize
(
x
)
for
x
in
X
]
"""
return
FunctionTransformer
(
func
=
scale
,
validate
=
False
,
kw_args
=
dict
(
target_img_size
=
target_img_size
)
)
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