Defining transformations
The transformation interface is the centrepiece of this library. Beside straightforward transform application it also enables stochasticity, composition and buffering (see Design).
A transformation is a type that subtypes Transform. The only required function to implement for your transformation type T is
apply(tfm::T, item::I; randstate)Calling
tfm(item)forwards to this method.randstateencapsulates the random state needed for stochastic transformations. The method should be deterministic for a givenrandstate.You may dispatch on a specific item type
Ior use the abstractItemif one implementation works for all item types.
You may additionally also implement:
DataAugmentation.getrandstate(tfm)for stochastic transformationsGenerates the default random state for
tfm(item). The fallback returnsnothing.apply!(bufitem, tfm::T, item; randstate)to support bufferingBuffered version of
applythat mutatesbufitem. If not implemented, falls back to regularapply. See Supporting buffering below.DataAugmentation.compose(tfm1, tfm2)for custom composition with other transformationsComposes transformations. By default, returns a
Sequencetransformation that applies the transformations one after the other.
Spatial transformations that should be fused and cropped efficiently are better defined as projective transformations.
Example
The implementation of the MapElem transformation illustrates this interface well. It transforms any item with array data by mapping a function over the array's elements, just like Base.map.
struct MapElem <: Transform
f
endThe apply implementation dispatches on DataAugmentation.AbstractArrayItem, an abstract item type for items that wrap arrays. Note that the randstate keyword argument needs to be given even for implementations of deterministic transformations. We also make use of the DataAugmentation.setdata helper to update the item data.
function apply(tfm::MapElem, item::AbstractArrayItem; randstate = nothing)
a = itemdata(item)
a_ = map(tfm.f, a)
return setdata(item, a_)
endThe buffered version applies the function inplace using Base.map!:
function apply!(
bufitem::I,
tfm::MapElem,
item::I;
randstate = nothing) where I <: AbstractArrayItem
map!(tfm.f, itemdata(bufitem), itemdata(item))
return bufitem
endFinally, compose could be customized so that two MapElems are fused: instead of applying them sequentially, the composed function is applied once. MapElem does not do this at the moment, but it would look like this:
compose(tfm1::MapElem, tfm2::MapElem) = MapElem(tfm2.f ∘ tfm1.f)Supporting buffering
Usually, buffer = tfm(item) gives you an item that you can reuse with apply!(buffer, tfm, item).
However, for some transformations, a different buffer is needed. Sequence, for example, needs to reuse all intermediate results. That is why the buffer creation can be customized:
DataAugmentation.makebuffer(tfm, item)creates a bufferbufthat can be used in anapply!call:apply!(buf, tfm, item).
Users do not have to handle buffers themselves, see Buffering.