Skip to content

Commit

Permalink
fix links add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
isentropic committed Mar 11, 2024
1 parent ebeb620 commit fb8aab2
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 18 deletions.
11 changes: 8 additions & 3 deletions docs/src/buffering.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ apply!(buffer, tfm, item)

However, for some transformations, a different buffer is needed. [`Sequence`](@ref), for example, needs to reuse all intermediate results. That is why the buffer creation can be customized:

- [`makebuffer`](@ref)`(tfm, item)` creates a buffer `buf` that can be used in an `apply!` call: `apply!(buf, tfm, item)`.
- [`DataAugmentation.makebuffer`](@ref)`(tfm, item)` creates a buffer `buf` that can be used in an `apply!` call: `apply!(buf, tfm, item)`.

---

Managing the buffers manually quickly becomes tedious. For convenience, this library implements [`Buffered`](@ref), a transformation wrapper that will use a buffer internally. `btfm = Buffered(tfm)` will create a buffer the first time it is `apply`ed and then use it by internally calling `apply!`.
Managing the buffers manually quickly becomes tedious. For convenience, this library implements [`DataAugmentation.Buffered`](@ref), a transformation wrapper that will use a buffer internally. `btfm = Buffered(tfm)` will create a buffer the first time it is `apply`ed and then use it by internally calling `apply!`.

```julia
buffered = Buffered(tfm)
buffer = apply(tfm, item) # uses apply! internally
```

Since `Buffered` only stores one buffer, you may run into problems when using it in a multi-threading context where different threads invalidate the buffer before it can be used. In that case, you can use [`BufferedThreadsafe`](@ref), a version of `Buffered` that keeps a separate buffer for every thread.
Since `Buffered` only stores one buffer, you may run into problems when using it in a multi-threading context where different threads invalidate the buffer before it can be used. In that case, you can use [`DataAugmentation.BufferedThreadsafe`](@ref), a version of `Buffered` that keeps a separate buffer for every thread.

```@docs
DataAugmentation.Buffered
DataAugmentation.BufferedThreadsafe
```
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The above example is simple, but there are more requirements of data augmentatio
A transformation is stochastic (as opposed to deterministic) if it produces different outputs based on some random state.
This randomness can become a problem when applying an transformation to an aligned pair of input and target. If we have an image and a corresponding segmentation mask, using different scaling factors results in misalignment of the two; the segmentation no longer matches up with the image pixels.

To handle this, the random state is explicitly passed to the transformations, rendering them deterministic. A generator for the random state can be defined with [`getrandstate`](@ref)`(tfm)` and passed to `apply` with the `randstate` keyword argument.
To handle this, the random state is explicitly passed to the transformations, rendering them deterministic. A generator for the random state can be defined with [`DataAugmentation.getrandstate`](@ref)`(tfm)` and passed to `apply` with the `randstate` keyword argument.

### Composition

Expand Down
4 changes: 2 additions & 2 deletions docs/src/iteminterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ struct MyItem <: Item
end
```

The only function that is expected to be implemented is [`itemdata`](@ref), which simply returns the wrapped data. If, as above, you simply call the field holding the data `data`, you do not need to implement it. The same goes for the [`setdata`](@ref) helper.
The only function that is expected to be implemented is [`itemdata`](@ref), which simply returns the wrapped data. If, as above, you simply call the field holding the data `data`, you do not need to implement it. The same goes for the [`DataAugmentation.setdata`](@ref) helper.

For some items, it also makes sense to implement the following:

- [`showitem!`](@ref)`(img, item::I)` creates a visual representation of an item on top of `img`.
- [`DataAugmentation.showitem!`](@ref)`(img, item::I)` creates a visual representation of an item on top of `img`.
2 changes: 1 addition & 1 deletion docs/src/preprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This library also implements some general transformations useful for getting data ready to be put into a model.

- [`ToEltype`](@ref)`(T)` converts the element type of any [`AbstractArrayItem`](@ref) to `T`.
- [`ToEltype`](@ref)`(T)` converts the element type of any [`DataAugmentation.AbstractArrayItem`](@ref) to `T`.
- [`ImageToTensor`](@ref) converts an image to an `ArrayItem` with another dimension for the color channels
- [`Normalize`](@ref) normalizes image tensors
- [`OneHot`](@ref) to one-hot encode multi-class masks ([`MaskMulti`](@ref)s)
Expand Down
4 changes: 2 additions & 2 deletions docs/src/projective/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ By default, the bounds of a projected item will be chosen so they still encase a

## Projective transformations interface

The abstract type [`ProjectiveTransform`](@ref) represents a projective transformation.
A `ProjectiveTransform` needs to implement [`getprojection`](@ref)`(tfm, bounds; randstate)` that should return a `Transformation` from [CoordinateTransformations.jl](https://github.com/JuliaGeometry/CoordinateTransformations.jl).
The abstract type [`DataAugmentation.ProjectiveTransform`](@ref) represents a projective transformation.
A `ProjectiveTransform` needs to implement [`DataAugmentation.getprojection`](@ref)`(tfm, bounds; randstate)` that should return a `Transformation` from [CoordinateTransformations.jl](https://github.com/JuliaGeometry/CoordinateTransformations.jl).

To add support for projective transformations to an item `I`, you need to implement

Expand Down
43 changes: 43 additions & 0 deletions docs/src/ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,47 @@ Crop
Rotate
itemdata
showitems
DataAugmentation.AbstractArrayItem
DataAugmentation.AbstractItem
DataAugmentation.ArrayItem
DataAugmentation.Categorify
DataAugmentation.ComposedProjectiveTransform
DataAugmentation.FillMissing
DataAugmentation.Identity
DataAugmentation.Item
DataAugmentation.ItemWrapper
DataAugmentation.MapElem
DataAugmentation.Normalize
DataAugmentation.NormalizeRow
DataAugmentation.OneHot
DataAugmentation.PinOrigin
DataAugmentation.ProjectiveTransform
DataAugmentation.ResizePadDivisible
DataAugmentation.ScaleFixed
DataAugmentation.Sequence
DataAugmentation.ToEltype
DataAugmentation.Transform
DataAugmentation.Zoom
DataAugmentation.apply
DataAugmentation.apply!
DataAugmentation.boundsof
DataAugmentation.centered
DataAugmentation.compose
DataAugmentation.getbounds
DataAugmentation.getprojection
DataAugmentation.getrandstate
DataAugmentation.makebuffer
DataAugmentation.offsetcropbounds
DataAugmentation.project
DataAugmentation.project!
DataAugmentation.projectionbounds
DataAugmentation.setdata
DataAugmentation.showitem!
DataAugmentation.testapply
DataAugmentation.testapply!
DataAugmentation.testitem
DataAugmentation.testprojective
DataAugmentation.threepointwarpaffine
DataAugmentation.transformbounds
```
6 changes: 3 additions & 3 deletions docs/src/tfminterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ A transformation is a type that subtypes [`Transform`](@ref). The only *required

You may additionally also implement:

- [`getrandstate`](@ref)`(tfm)` for *stochastic* transformations
- [`DataAugmentation.getrandstate`](@ref)`(tfm)` for *stochastic* transformations

Generates random state to be used inside `apply`. Calling `apply(tfm, item)` is equivalent to
`apply(tfm, item; randstate = getrandstate(tfm))`. It defaults to `nothing`, so we need not implement it for deterministic transformations.
Expand All @@ -27,7 +27,7 @@ You may additionally also implement:
Buffered version of `apply` that mutates `bufitem`. If not implemented,
falls back to regular `apply`.

- [`compose`](@ref)`(tfm1, tfm2)` for custom *composition* with other transformations
- [`DataAugmentation.compose`](@ref)`(tfm1, tfm2)` for custom *composition* with other transformations

Composes transformations. By default, returns a [`Sequence`](@ref) transformation that applies the transformations one after the other.

Expand All @@ -43,7 +43,7 @@ struct MapElem <: Transform
end
```

The `apply` implementation dispatches on [`AbstractArrayItem`](@ref), 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 [`setdata`](@ref) helper to update the item data.
The `apply` implementation dispatches on [`DataAugmentation.AbstractArrayItem`](@ref), 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`](@ref) helper to update the item data.

```julia
function apply(tfm::MapElem, item::AbstractArrayItem; randstate = nothing)
Expand Down
4 changes: 4 additions & 0 deletions docs/src/transformations.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ tfm = Maybe(FlipX())
titems = [apply(tfm, item) for _ in 1:8]
showgrid(titems; ncol = 4, npad = 16)
```

```@docs
DataAugmentation.ImageToTensor
```
9 changes: 5 additions & 4 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ compose(::Identity, ::Identity) = Identity()
compose(tfm::Transform, ::Identity) = tfm
compose(::Identity, tfm::Transform) = tfm

# Lastly, [`setdata`](@ref) provides a convenient way to create a copy
# of an item, replacing only the wrapped data. This relies on the
# wrapped data field being named `data`, though.

"""
Provides a convenient way to create a copy
of an item, replacing only the wrapped data. This relies on the
wrapped data field being named `data`, though.
"""
function setdata(item::Item, data)
item = Setfield.@set item.data = data
return item
Expand Down
6 changes: 6 additions & 0 deletions src/buffered.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ end

# ## Buffered transforms

"""
Buffer to store transform results
"""
mutable struct Buffered{T<:Transform} <: Transform
tfm::T
buffers::Dict
Expand Down Expand Up @@ -73,6 +76,9 @@ function apply!(buf, buffered::Buffered, items::T; randstate = getrandstate(buff
end


"""
Buffer to store transform results (threadsafe)
"""
struct BufferedThreadsafe <: Transform
buffereds::Vector{Buffered}
function BufferedThreadsafe(tfm; n = Threads.nthreads())
Expand Down
3 changes: 3 additions & 0 deletions src/projective/affine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ end

Zoom(scales::NTuple{2, T} = (1., 1.2)) where T = Zoom(Uniform(scales[1], scales[2]))

"""
Return random state of the transform
"""
getrandstate(tfm::Zoom) = rand(tfm.dist)

function getprojection(tfm::Zoom, bounds::Bounds{N}; randstate = getrandstate(tfm)) where N
Expand Down
1 change: 0 additions & 1 deletion src/projective/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
abstract type ProjectiveTransform <: Transform
Abstract supertype for projective transformations. See
[Projective transformations](../docs/projective/interface.md).
"""
abstract type ProjectiveTransform <: Transform end

Expand Down
2 changes: 1 addition & 1 deletion src/projective/warp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end
"""
threepointwarpaffine(srcps, dstps)
Calculate an affine [`CoordinateTransformations.LinearMap`](@ref)
Calculate an affine `CoordinateTransformations.LinearMap`
from 3 source points to 3 destination points.
Adapted from [CoordinateTransformations.jl#30](https://github.com/JuliaGeometry/CoordinateTransformations.jl/issues/30#issuecomment-610337378).
Expand Down

0 comments on commit fb8aab2

Please sign in to comment.