Skip to content

Commit

Permalink
Merge pull request #41 from lorenzoh/lorenzoh/develop
Browse files Browse the repository at this point in the history
v0.2.0 - fixes and internal refactoring
  • Loading branch information
lorenzoh authored Apr 28, 2021
2 parents f731cb3 + 266764e commit 2fe1cca
Show file tree
Hide file tree
Showing 32 changed files with 540 additions and 290 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DataAugmentation"
uuid = "88a5189c-e7ff-4f85-ac6b-e6158070f02e"
authors = ["lorenzoh <[email protected]>"]
version = "0.1.8"
version = "0.2.0"

[deps]
ColorBlendModes = "60508b50-96e1-4007-9d6c-f475c410f16b"
Expand All @@ -20,6 +20,7 @@ Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
ColorBlendModes = "0.2"
Expand Down
8 changes: 5 additions & 3 deletions src/DataAugmentation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@ using Rotations
using Setfield
using StaticArrays
using Statistics
using Test: @test, @test_nowarn


include("./base.jl")
include("./wrapper.jl")
include("./buffered.jl")
include("./sequence.jl")
include("./visualization.jl")
include("./items/arrayitem.jl")
include("./projective/base.jl")
include("./items/image.jl")
include("./items/keypoints.jl")
include("./items/mask.jl")
include("./projective/bounds.jl")
include("./projective/compose.jl")
include("./projective/crop.jl")
include("./projective/affine.jl")
include("./projective/warp.jl")
include("./oneof.jl")
include("./preprocessing.jl")
include("./colortransforms.jl")
include("testing.jl")
include("./visualization.jl")


export Item,
Expand Down Expand Up @@ -86,7 +87,8 @@ export Item,
ResizePadDivisible,
onehot,
showitems,
showgrid
showgrid,
Bounds


end # module
4 changes: 2 additions & 2 deletions src/buffered.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ apply!(buf, tfm::Transform, items; randstate = getrandstate(tfm)) = apply(tfm, i
# bounding boxes per sample) the number of items doesn't match up with the buffer and
# we fall back to regular `apply`.

function apply!(bufs::Tuple, tfm::Transform, items::Tuple; randstate = getrandstate(tfm))
function apply!(bufs::Union{Tuple, AbstractVector}, tfm::Transform, items::Union{Tuple, AbstractVector}; randstate = getrandstate(tfm))
if length(bufs) == length(items)
return map((item, buf) -> apply!(buf, tfm, item; randstate = randstate), items, bufs)
else
Expand Down Expand Up @@ -71,7 +71,7 @@ function apply!(buf, buffered::Buffered, items; randstate = getrandstate(buffere
end


struct BufferedThreadsafe
struct BufferedThreadsafe <: Transform
buffereds::Vector{Buffered}
function BufferedThreadsafe(tfm; n = Threads.nthreads())
@assert n >= 1
Expand Down
30 changes: 16 additions & 14 deletions src/items/image.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,20 @@ showitems(item)
```
"""
struct Image{N,T,B} <: AbstractArrayItem{N,T}
struct Image{N,T} <: AbstractArrayItem{N,T}
data::AbstractArray{T,N}
bounds::AbstractArray{<:SVector{N,B},N}
bounds::Bounds{N}
end

Image(data) = Image(data, size(data))
Image(data) = Image(data, Bounds(axes(data)))

function Image(data::AbstractArray{T,N}, sz::NTuple{N,Int}) where {T,N}
bounds = makebounds(sz)
return Image(data, bounds)
return Image(data, Bounds(sz))
end


Base.show(io::IO, item::Image{N,T}) where {N,T} =
print(io, "Image{$N, $T}() with size $(size(itemdata(item)))")
print(io, "Image{$N, $T}() with bounds $(item.bounds)")


function showitem!(img, image::Image{2, <:Colorant})
Expand All @@ -72,22 +71,25 @@ getbounds(image::Image) = image.bounds
# We have to pass the inverse of the projection `P` as it uses backward
# mode warping.

function project(P, image::Image{N, T}, indices) where {N, T}
## Transform the bounds along with the image
bounds_ = P.(getbounds(image))
data_ = warp(itemdata(image), inv(P), indices, zero(T))
return Image(data_, makebounds(indices))
function project(P, image::Image{N, T}, bounds::Bounds) where {N, T}
# TODO: make interpolation scheme and boundary conditions configurable
data_ = warp(
itemdata(image),
inv(P),
bounds.rs,
zero(T))
return Image(data_, bounds)
end

# The inplace version `project!` is quite similar. Note `indices` are not needed
# as they are implicitly given by the buffer.

function project!(bufimage::Image, P, image::Image{N, T}, indices) where {N, T}
a = OffsetArray(parent(itemdata(bufimage)), indices)
function project!(bufimage::Image, P, image::Image{N, T}, bounds::Bounds{N}) where {N, T}
a = OffsetArray(parent(itemdata(bufimage)), bounds.rs)
res = warp!(
a,
box_extrapolation(itemdata(image), zero(T)),
inv(P),
)
return Image(res, P.(getbounds(image)))
return Image(res, bounds)
end
11 changes: 6 additions & 5 deletions src/items/keypoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ showitems(item)
"""
struct Keypoints{N, T, S<:Union{SVector{N, T}, Nothing}, M} <: AbstractArrayItem{M, S}
data::AbstractArray{S, M}
bounds::AbstractArray{<:SVector{N, Float32}, N}
bounds::Bounds{N}
end


function Keypoints(data::AbstractArray{S, M}, sz::NTuple{N, Int}) where {T, N, S<:Union{SVector{N, T}, Nothing}, M}
return Keypoints{N, T, S, M}(data, makebounds(sz, Float32))
function Keypoints(data, sz::NTuple{N, Int}) where N
return Keypoints(data, Bounds(sz))
end


Expand All @@ -39,10 +39,11 @@ Base.show(io::IO, item::Keypoints{N, T, M}) where {N, T, M} =
getbounds(keypoints::Keypoints) = keypoints.bounds


function project(P, keypoints::Keypoints{N, T}, indices) where {N, T}
function project(P, keypoints::Keypoints{N, T}, bounds::Bounds{N}) where {N, T}
# TODO: convert back to `T`?
return Keypoints(
map(fmap(P), keypoints.data),
makebounds(indices),
bounds,
)
end

Expand Down
48 changes: 27 additions & 21 deletions src/items/mask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ mask = MaskMulti(rand(1:3, 100, 100))
showitems(mask)
```
"""
struct MaskMulti{N, T<:Integer, U, B} <: AbstractArrayItem{N, T}
struct MaskMulti{N, T<:Integer, U} <: AbstractArrayItem{N, T}
data::AbstractArray{T, N}
classes::AbstractVector{U}
bounds::AbstractArray{<:SVector{N, B}, N}
bounds::Bounds{N}
end


function MaskMulti(a::AbstractArray, classes = unique(a))
bounds = makebounds(size(a))
bounds = Bounds(size(a))
minimum(a) >= 1 || error("Class values must start at 1")
return MaskMulti(a, classes, bounds)
end
Expand All @@ -39,26 +39,30 @@ Base.show(io::IO, mask::MaskMulti{N, T}) where {N, T} =
getbounds(mask::MaskMulti) = mask.bounds


function project(P, mask::MaskMulti, indices)
function project(P, mask::MaskMulti, bounds::Bounds)
a = itemdata(mask)
etp = mask_extrapolation(a)
res = warp(etp, inv(P), bounds.rs)
return MaskMulti(
warp(etp, inv(P), indices),
res,
mask.classes,
P.(mask.bounds)
bounds
)
end


function project!(bufmask::MaskMulti, P, mask::MaskMulti, indices)
a = OffsetArray(parent(itemdata(bufmask)), indices)
bounds_ = P.(getbounds(mask))
res = warp!(
function project!(bufmask::MaskMulti, P, mask::MaskMulti, bounds)
a = OffsetArray(parent(itemdata(bufmask)), bounds.rs)
warp!(
a,
mask_extrapolation(itemdata(mask)),
inv(P),
)
return MaskMulti(a, mask.classes, P.(getbounds(mask)))
return MaskMulti(
a,
mask.classes,
bounds
)
end


Expand Down Expand Up @@ -91,12 +95,12 @@ mask = MaskBinary(rand(Bool, 100, 100))
showitems(mask)
```
"""
struct MaskBinary{N, B} <: AbstractArrayItem{N, Bool}
struct MaskBinary{N} <: AbstractArrayItem{N, Bool}
data::AbstractArray{Bool, N}
bounds::AbstractArray{<:SVector{N, B}, N}
bounds::Bounds{N}
end

function MaskBinary(a::AbstractArray{Bool, N}, bounds = makebounds(size(a))) where N
function MaskBinary(a::AbstractArray{Bool, N}, bounds = Bounds(size(a))) where N
return MaskBinary(a, bounds)
end

Expand All @@ -105,24 +109,26 @@ Base.show(io::IO, mask::MaskBinary{N}) where {N} =

getbounds(mask::MaskBinary) = mask.bounds

function project(P, mask::MaskBinary, indices)
function project(P, mask::MaskBinary, bounds::Bounds)
etp = mask_extrapolation(itemdata(mask))
return MaskBinary(
warp(etp, inv(P), indices),
P.(getbounds(mask)),
warp(etp, inv(P), bounds.rs),
bounds,
)
end


function project!(bufmask::MaskBinary, P, mask::MaskBinary, indices)
bounds_ = P.(getbounds(mask))
a = OffsetArray(parent(itemdata(bufmask)), indices)
function project!(bufmask::MaskBinary, P, mask::MaskBinary, bounds)
a = OffsetArray(parent(itemdata(bufmask)), bounds.rs)
res = warp!(
a,
mask_extrapolation(itemdata(mask)),
inv(P),
)
return MaskBinary(res, P.(getbounds(mask)))
return MaskBinary(
a,
bounds
)
end

function showitem!(img, mask::MaskBinary)
Expand Down
4 changes: 2 additions & 2 deletions src/oneof.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getrandstate(oneof::OneOf)
end


function apply(oneof::OneOf, item::AbstractItem; randstate = getrandstate(oneof))
function apply(oneof::OneOf, item::Item; randstate = getrandstate(oneof))
i, tfmrandstate = randstate
return apply(oneof.tfms[i], item; randstate = tfmrandstate)
end
Expand All @@ -31,7 +31,7 @@ function makebuffer(oneof::OneOf, items)
return Tuple([makebuffer(tfm, items) for tfm in oneof.tfms])
end

function apply!(bufs, oneof::OneOf, item::AbstractItem; randstate = getrandstate(tfm))
function apply!(bufs, oneof::OneOf, item::Item; randstate = getrandstate(oneof))
i, tfmrandstate = randstate
buf = bufs[i]
return apply!(buf, oneof.tfms[i], item; randstate = tfmrandstate)
Expand Down
Loading

0 comments on commit 2fe1cca

Please sign in to comment.