Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
205602af
Unverified
Commit
205602af
authored
May 08, 2023
by
Philip Meier
Committed by
GitHub
May 08, 2023
Browse files
fix v2.Lambda (#7566)
parent
e5a1b71d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
1 deletion
+38
-1
test/test_transforms_v2.py
test/test_transforms_v2.py
+35
-0
torchvision/transforms/v2/_misc.py
torchvision/transforms/v2/_misc.py
+3
-1
No files found.
test/test_transforms_v2.py
View file @
205602af
...
...
@@ -2137,3 +2137,38 @@ def test_no_warnings_v1_namespace():
from torchvision.datasets import ImageNet
"""
assert_run_python_script
(
textwrap
.
dedent
(
source
))
class
TestLambda
:
inputs
=
pytest
.
mark
.
parametrize
(
"input"
,
[
object
(),
torch
.
empty
(()),
np
.
empty
(()),
"string"
,
1
,
0.0
])
@
inputs
def
test_default
(
self
,
input
):
was_applied
=
False
def
was_applied_fn
(
input
):
nonlocal
was_applied
was_applied
=
True
return
input
transform
=
transforms
.
Lambda
(
was_applied_fn
)
transform
(
input
)
assert
was_applied
@
inputs
def
test_with_types
(
self
,
input
):
was_applied
=
False
def
was_applied_fn
(
input
):
nonlocal
was_applied
was_applied
=
True
return
input
types
=
(
torch
.
Tensor
,
np
.
ndarray
)
transform
=
transforms
.
Lambda
(
was_applied_fn
,
*
types
)
transform
(
input
)
assert
was_applied
is
isinstance
(
input
,
types
)
torchvision/transforms/v2/_misc.py
View file @
205602af
...
...
@@ -32,10 +32,12 @@ class Lambda(Transform):
lambd (function): Lambda/function to be used for transform.
"""
_transformed_types
=
(
object
,)
def
__init__
(
self
,
lambd
:
Callable
[[
Any
],
Any
],
*
types
:
Type
):
super
().
__init__
()
self
.
lambd
=
lambd
self
.
types
=
types
or
(
object
,)
self
.
types
=
types
or
self
.
_transformed_types
def
_transform
(
self
,
inpt
:
Any
,
params
:
Dict
[
str
,
Any
])
->
Any
:
if
isinstance
(
inpt
,
self
.
types
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment