Skip to content

Commit

Permalink
Clean up documentation, add 3D rotation to gallery
Browse files Browse the repository at this point in the history
* Use `@docs; canonical=false` for documentation outside of ref.md.
  This allows documentation to exist on a second page. Previously,
  documentation was split between multiple pages. Some were in ref.md,
  and some were in other pages (transformations.md, buffering.md). Now,
  all documentation is at least in ref.md, but can also be duplicated
  on other pages with `canonical=false`.
* Add missing documentation to ref.md, and transformations.md.
* Enable the visualizations in gallery.md, and clean up the code a bit.
* Add visualizations in gallery.md for 3D rotations.
  • Loading branch information
paulnovo committed Jul 20, 2024
1 parent 112fb66 commit 301319b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 78 deletions.
2 changes: 1 addition & 1 deletion docs/src/buffering.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 [`DataAugmentation.BufferedThreadsafe`](@ref), a version of `Buffered` that keeps a separate buffer for every thread.

```@docs
```@docs; canonical=false
DataAugmentation.Buffered
DataAugmentation.BufferedThreadsafe
```
98 changes: 31 additions & 67 deletions docs/src/projective/gallery.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ the keypoint-based items [`Keypoints`](@ref), [`Polygon`](@ref), and [`BoundingB

Let's take this picture of a light house:

```@setup deps
using DataAugmentation
using MosaicViews
using Images
using TestImages
using StaticArrays
```

```@example
```@example deps
using DataAugmentation
using MosaicViews
using Images
Expand All @@ -26,11 +18,6 @@ imagedata = testimage("lighthouse")
imagedata = imresize(imagedata, ratio = 196 / size(imagedata, 1))
```

```@example deps
imagedata = testimage("lighthouse")
imagedata
```

To apply a transformation `tfm` to it, wrap it in
`Image`, apply the transformation and unwrap it using [`itemdata`](@ref):

Expand Down Expand Up @@ -74,59 +61,29 @@ apply(tfm, (image, bbox)) |> showitems

Of course, you have to create a 3-dimensional transformation, i.e. `CenterCrop((128, 128, 128))` instead of `CenterCrop((128, 128))`.

## Gallery
```julia
function showtransform(tfm, item, n = 8; ncol = 4)
return mosaicview(
[showitems(apply(tfm, item)) for _ in 1:n],
fillvalue = RGBA(1, 1, 1, 0),
npad = 8,
rowmajor = true,
ncol = ncol)
end


function showtransforms(tfms, item; ncol = length(tfms))
return mosaicview(
[parent(showitems(apply(tfm, item))) for tfm in tfms],
fillvalue = RGBA(1, 1, 1, 0),
npad = 8,
rowmajor = true,
ncol = ncol)
end

nothing # hide
```

### [`RandomResizeCrop`](@ref)`(sz)`
## [`RandomResizeCrop`](@ref)`(sz)`

Resizes the sides so that one of them is no longer than `sz` and crops a region of size `sz` *from a random location*.

```julia
```@example deps
tfm = RandomResizeCrop((128, 128))
showgrid([apply(tfm, (image, bbox)) for _ in 1:6]; ncol=6, npad=8)
```

```julia
o = showtransform(tfm, (image, bbox), 6, ncol=6)
```

### [`CenterResizeCrop`](@ref)
## [`CenterResizeCrop`](@ref)

Resizes the sides so that one of them is no longer than `sz` and crops a region of size `sz` *from the center*.

```julia
```@example deps
tfm = CenterResizeCrop((128, 128))
showgrid([apply(tfm, (image, bbox))]; ncol=6, npad=8)
```

```julia
o = showtransform(tfm, (image, bbox), 1)
```

### [`Crop`](@ref)`(sz[, from])`
## [`Crop`](@ref)`(sz[, from])`

Crops a region of size `sz` from the image, *without resizing* the image first.

```julia
```@example deps
using DataAugmentation: FromOrigin, FromCenter, FromRandom
tfms = [
Crop((128, 128), FromOrigin()),
Expand All @@ -136,36 +93,43 @@ tfms = [
Crop((128, 128), FromRandom()),
Crop((128, 128), FromRandom()),
]
showgrid([apply(tfm, (image, bbox)) for tfm in tfms]; ncol=6, npad=8)
```

```julia
o = showtransforms(tfms, (image, bbox))
```

### [`FlipX`](@ref), [`FlipY`](@ref), [`Reflect`](@ref)
## [`FlipX`](@ref), [`FlipY`](@ref), [`Reflect`](@ref)

Flip the data on the horizontally and vertically, respectively. More generally, reflect around an angle from the x-axis.

```julia
```@example deps
tfms = [
FlipX(),
FlipY(),
Reflect(30),
]
showgrid([apply(tfm, (image, bbox)) for tfm in tfms]; ncol=6, npad=8)
```

```julia
o = showtransforms(tfms, (image, bbox))
```

### [`Rotate`](@ref)
## [`Rotate`](@ref), [`RotateX`](@ref), [`RotateY`](@ref), [`RotateZ`](@ref)

Rotate counter-clockwise by an angle.
Rotate a 2D image counter-clockwise by an angle.

```julia
```@example deps
tfm = Rotate(20) |> CenterCrop((256, 256))
showgrid([apply(tfm, (image, bbox)) for _ in 1:6]; ncol=6, npad=8)
```

```julia
o = showtransform(tfm, (image, bbox), 1)
Rotate also works with 3D images in addition to 3D specific transforms RotateX, RotateY, and RotateZ.

```@example deps
image3D = Image([RGB(i, j, k) for i=0:0.01:1, j=0:0.01:1, k=0:0.01:1])
tfms = [
Rotate(20, 30, 40),
Rotate{3}(45),
RotateX(45),
RotateY(45),
RotateZ(45),
]
transformed = [apply(tfm, image3D) |> itemdata for tfm in tfms]
slices = [Image(parent(t[:, :, 50])) for t in transformed]
showgrid(slices; ncol=6, npad=8)
```
20 changes: 18 additions & 2 deletions docs/src/ref.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
# Reference
```@docs
AdjustBrightness
AdjustContrast
BoundingBox
CenterCrop
CenterResizeCrop
Crop
FlipX
FlipY
Image
Keypoints
MaskBinary
MaskMulti
Maybe
OneOf
PermuteDims
Polygon
RandomCrop
RandomResizeCrop
CenterResizeCrop
Crop
Reflect
Rotate
RotateX
RotateY
RotateZ
ScaleKeepAspect
ScaleRatio
WarpAffine
itemdata
showitems
DataAugmentation.AbstractArrayItem
DataAugmentation.AbstractItem
DataAugmentation.ArrayItem
DataAugmentation.Buffered
DataAugmentation.BufferedThreadsafe
DataAugmentation.Categorify
DataAugmentation.ComposedProjectiveTransform
DataAugmentation.FillMissing
DataAugmentation.Identity
DataAugmentation.ImageToTensor
DataAugmentation.Item
DataAugmentation.ItemWrapper
DataAugmentation.MapElem
Expand Down
22 changes: 14 additions & 8 deletions docs/src/transformations.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,26 @@ Projective transformations include:

Affine transformations are a subgroup of projective transformations that can be composed very efficiently: composing two affine transformations results in another affine transformation. Affine transformations can represent translation, scaling, reflection and rotation. Available `Transform`s are:

```@docs
ScaleRatio
ScaleKeepAspect
```@docs; canonical=false
FlipX
FlipY
Reflect
Rotate
RotateX
RotateY
RotateZ
ScaleKeepAspect
ScaleFixed
ScaleRatio
WarpAffine
Zoom
```

## Crops

To get a cropped result, simply `compose` any `ProjectiveTransform` with

```@docs
```@docs; canonical=false
CenterCrop
RandomCrop
```
Expand All @@ -47,17 +53,17 @@ RandomCrop

DataAugmentation.jl currently supports the following color transformations for augmentation:

```@docs
AdjustContrast
```@docs; canonical=false
AdjustBrightness
AdjustContrast
```

# Stochastic transformations
When augmenting data, it is often useful to apply a transformation only with some probability or choose from a set of transformations. Unlike in other data augmentation libraries like *albumentations*, in DataAugmentation.jl you can use wrapper transformations for this functionality.

- [`Maybe`](@ref)`(tfm, p = 0.5)` applies a transformation with probability `p`; and
- [`OneOf`](@ref)`([tfm1, tfm2])` randomly selects a transformation to apply.
```@docs
```@docs; canonical=false
Maybe
OneOf
```
Expand All @@ -72,6 +78,6 @@ titems = [apply(tfm, item) for _ in 1:8]
showgrid(titems; ncol = 4, npad = 16)
```

```@docs
```@docs; canonical=false
DataAugmentation.ImageToTensor
```

0 comments on commit 301319b

Please sign in to comment.