cornucopia.special
IdentityTransform
SequentialTransform
Bases: SharedMixin, Transform
A sequence of transforms
Example
Sequences can be built explicitly, or simply by adding transforms together:
t1 = MultFieldTransform()
t2 = GaussianNoiseTransform()
seq = SequentialTransform([t1, t2]) # explicit
seq = t1 + t2 # implicit
Sequences can also be extended by addition:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
list[Transform]
|
A list of transforms to apply sequentially. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
shared |
(channels, tensors, channels + tensor, '')
|
|
include |
str or list[str]
|
List of keys to which the transform should apply |
exclude |
str or list[str]
|
List of keys to which the transform should not apply |
PerChannelTransform
MaybeTransform
Bases: SharedMixin, Transform
Randomly apply a transform
20% chance of adding noise
Explicit call to the class: Implicit call using syntactic sugar: ``` Default for
shared changed from False to True
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Transform
|
A transform to randomly apply |
required |
prob
|
float
|
Probability to apply the transform |
0.5
|
shared
|
(channels, tensors, channels + tensor, '')
|
Roll the dice once for all input tensors |
'channels'
|
SwitchTransform
Bases: SharedMixin, Transform
Randomly choose a transform to apply
Randomly apply either Gaussian or Chi noise
Explicit call to the class: Implicit call using syntactic sugar: Functional call: Default for
shared changed from False to True
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
list[Transform]
|
A list of transforms to sample from |
required |
prob
|
list[float]
|
Probability of applying each transform |
0
|
shared
|
(channels, tensors, channels + tensor, '')
|
Roll the dice once for all input tensors |
'channels'
|
IncludeKeysTransform
Bases: Transform
Context manager for keys to include
Use as a transform
Use as a context manager with as
Use as a context manager with
Use as a context manager (alias)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Transform
|
Transform to apply |
required |
keys
|
[sequence of] str
|
Keys to include |
required |
union
|
bool
|
Include the union of what was already included and |
True
|
ExcludeKeysTransform
Bases: Transform
Context manager for keys to exclude. Can also be used as a transform.
Use as a transform
Use as a context manager with as
Use as a context manager with
Use as a context manager (alias)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Transform
|
Transform to apply |
required |
keys
|
[sequence of] str
|
Keys to include |
required |
union
|
bool
|
Exclude the union of what was already excluded and |
True
|
ConsumeKeysTransform
Bases: Transform
Context manager for keys to consume. Can also be used as a transform.
Use as a transform
Use as a context manager with as
Use as a context manager with
Use as a context manager (alias)
Added in
v0.5
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Transform
|
Transform to apply |
required |
keys
|
[sequence of] str
|
Keys to include |
required |
union
|
bool
|
Consume the union of what was already consumed and |
True
|
SharedTransform
Bases: SharedMixin, Transform
Context manager for sharing transforms across channels / tensors. Can also be used as a transform.
Use as a context manager (alias)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Transform
|
Transform to apply |
required |
mode
|
(channels, tensors, channels + tensor, '')
|
|
'channels'
|
ReturningTransform
Bases: Transform
Context manager for sharing transforms across channels / tensors
Use as a context manager (alias)
MappedTransform
Bases: Transform
Transforms that are applied to specific positional or arguments
Example
img = torch.randn([1, 32, 32])
seg = torch.randn([3, 32, 32]).softmax(0)
# positional variant
trf = MappedTransform(GaussianNoise(), None)
img, seg = trf(img, seg)
# keyword variant
trf = MappedTransform(image=GaussianNoise())
img, seg = trf(image=img, label=seg)
# alternative version
dat = {'img': torch.randn([1, 32, 32]),
'seg': torch.randn([3, 32, 32]).softmax(0)}
dat = MappedTransform(img=GaussianNoise(), nested=True)(dat)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapargs
|
tuple[Transform]
|
Transform to apply to positional arguments |
()
|
mapkwargs
|
dict[str, Transform]
|
Transform to apply to keyword arguments |
{}
|
nested
|
bool
|
Recursively traverse the inputs until we find matching
dictionaries. Only |
False
|
default
|
Transform
|
Transform to apply if nothing is specifically mapped |
None
|
RandomizedTransform
Bases: NonFinalTransform
Transform generated by randomizing some parameters of another transform.
ctx.randomize is an alias for RandomizedTransform
Gaussian noise with randomized variance
Object call
import cornucopia as cc
hypernoise = cc.RandomizedTransform(cc.GaussianNoise, [cc.Uniform()])
img = hypernoise(img)
Delayed call
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
callable(...) -> Transform
|
A Transform subclass or a function that constructs a Transform. |
required |
sample
|
[list or dict of] callable
|
A collection of functions that generate parameter values provided
to |
tuple()
|
ksample
|
dict[callable]
|
Must be kwargs-like arguments. |
dict()
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
shared |
(channels, tensors, channels + tensors, '')
|
Share random parameters across tensors and/or channels |
BatchedTransform
Bases: Module
Apply a transform to a batch
Example
Functional call:
Object call:Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Transform
|
Transform to apply to a batched tensor or to a nested structure of batched tensors |
required |
SplitChannels
CatChannels
Bases: Transform
Concatenate tensors across first dimension Assumes that the nested-most level in the structure is the one to concatenate.