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
1dc0318f
Unverified
Commit
1dc0318f
authored
Feb 23, 2023
by
Nicolas Hug
Committed by
GitHub
Feb 23, 2023
Browse files
Add docs for containers and undeprecate p for RandomChoice (#7311)
Co-authored-by:
vfdev
<
vfdev.5@gmail.com
>
parent
14c003bd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
22 deletions
+20
-22
test/test_transforms_v2.py
test/test_transforms_v2.py
+1
-4
torchvision/transforms/v2/_container.py
torchvision/transforms/v2/_container.py
+19
-18
No files found.
test/test_transforms_v2.py
View file @
1dc0318f
...
@@ -1359,11 +1359,8 @@ class TestContainers:
...
@@ -1359,11 +1359,8 @@ class TestContainers:
class
TestRandomChoice
:
class
TestRandomChoice
:
def
test_assertions
(
self
):
def
test_assertions
(
self
):
with
pytest
.
warns
(
UserWarning
,
match
=
"Argument p is deprecated and will be removed"
):
transforms
.
RandomChoice
([
transforms
.
Pad
(
2
),
transforms
.
RandomCrop
(
28
)],
p
=
[
1
,
2
])
with
pytest
.
raises
(
ValueError
,
match
=
"The number of probabilities doesn't match the number of transforms"
):
with
pytest
.
raises
(
ValueError
,
match
=
"The number of probabilities doesn't match the number of transforms"
):
transforms
.
RandomChoice
([
transforms
.
Pad
(
2
),
transforms
.
RandomCrop
(
28
)],
p
robabilities
=
[
1
])
transforms
.
RandomChoice
([
transforms
.
Pad
(
2
),
transforms
.
RandomCrop
(
28
)],
p
=
[
1
])
class
TestRandomIoUCrop
:
class
TestRandomIoUCrop
:
...
...
torchvision/transforms/v2/_container.py
View file @
1dc0318f
import
warnings
from
typing
import
Any
,
Callable
,
Dict
,
List
,
Optional
,
Sequence
,
Union
from
typing
import
Any
,
Callable
,
Dict
,
List
,
Optional
,
Sequence
,
Union
import
torch
import
torch
...
@@ -78,7 +77,7 @@ class RandomApply(Transform):
...
@@ -78,7 +77,7 @@ class RandomApply(Transform):
Args:
Args:
transforms (sequence or torch.nn.Module): list of transformations
transforms (sequence or torch.nn.Module): list of transformations
p (float): probability
p (float): probability
of applying the list of transforms
"""
"""
_v1_transform_cls
=
_transforms
.
RandomApply
_v1_transform_cls
=
_transforms
.
RandomApply
...
@@ -119,39 +118,38 @@ class RandomChoice(Transform):
...
@@ -119,39 +118,38 @@ class RandomChoice(Transform):
.. betastatus:: RandomChoice transform
.. betastatus:: RandomChoice transform
This transform does not support torchscript."""
This transform does not support torchscript.
Args:
transforms (sequence or torch.nn.Module): list of transformations
p (list of floats or None, optional): probability of each transform being picked.
If ``p`` doesn't sum to 1, it is automatically normalized. If ``None``
(default), all transforms have the same probability.
"""
def
__init__
(
def
__init__
(
self
,
self
,
transforms
:
Sequence
[
Callable
],
transforms
:
Sequence
[
Callable
],
p
:
Optional
[
List
[
float
]]
=
None
,
p
:
Optional
[
List
[
float
]]
=
None
,
probabilities
:
Optional
[
List
[
float
]]
=
None
,
)
->
None
:
)
->
None
:
if
not
isinstance
(
transforms
,
Sequence
):
if
not
isinstance
(
transforms
,
Sequence
):
raise
TypeError
(
"Argument transforms should be a sequence of callables"
)
raise
TypeError
(
"Argument transforms should be a sequence of callables"
)
if
p
is
not
None
:
warnings
.
warn
(
"Argument p is deprecated and will be removed in a future release. "
"Please use probabilities argument instead."
)
probabilities
=
p
if
p
robabilities
is
None
:
if
p
is
None
:
p
robabilities
=
[
1
]
*
len
(
transforms
)
p
=
[
1
]
*
len
(
transforms
)
elif
len
(
p
robabilities
)
!=
len
(
transforms
):
elif
len
(
p
)
!=
len
(
transforms
):
raise
ValueError
(
raise
ValueError
(
f
"The number of probabilities doesn't match the number of transforms: "
f
"The number of p doesn't match the number of transforms: "
f
"
{
len
(
p
)
}
!=
{
len
(
transforms
)
}
"
f
"
{
len
(
probabilities
)
}
!=
{
len
(
transforms
)
}
"
)
)
super
().
__init__
()
super
().
__init__
()
self
.
transforms
=
transforms
self
.
transforms
=
transforms
total
=
sum
(
p
robabilities
)
total
=
sum
(
p
)
self
.
p
robabilities
=
[
prob
/
total
for
prob
in
p
robabilities
]
self
.
p
=
[
prob
/
total
for
prob
in
p
]
def
forward
(
self
,
*
inputs
:
Any
)
->
Any
:
def
forward
(
self
,
*
inputs
:
Any
)
->
Any
:
idx
=
int
(
torch
.
multinomial
(
torch
.
tensor
(
self
.
p
robabilities
),
1
))
idx
=
int
(
torch
.
multinomial
(
torch
.
tensor
(
self
.
p
),
1
))
transform
=
self
.
transforms
[
idx
]
transform
=
self
.
transforms
[
idx
]
return
transform
(
*
inputs
)
return
transform
(
*
inputs
)
...
@@ -162,6 +160,9 @@ class RandomOrder(Transform):
...
@@ -162,6 +160,9 @@ class RandomOrder(Transform):
.. betastatus:: RandomOrder transform
.. betastatus:: RandomOrder transform
This transform does not support torchscript.
This transform does not support torchscript.
Args:
transforms (sequence or torch.nn.Module): list of transformations
"""
"""
def
__init__
(
self
,
transforms
:
Sequence
[
Callable
])
->
None
:
def
__init__
(
self
,
transforms
:
Sequence
[
Callable
])
->
None
:
...
...
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