Skip to content

Commit 2fe1cca

Browse files
authored
Merge pull request #41 from lorenzoh/lorenzoh/develop
v0.2.0 - fixes and internal refactoring
2 parents f731cb3 + 266764e commit 2fe1cca

32 files changed

+540
-290
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DataAugmentation"
22
uuid = "88a5189c-e7ff-4f85-ac6b-e6158070f02e"
33
authors = ["lorenzoh <lorenz.ohly@gmail.com>"]
4-
version = "0.1.8"
4+
version = "0.2.0"
55

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

2425
[compat]
2526
ColorBlendModes = "0.2"

src/DataAugmentation.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,27 @@ using Rotations
1818
using Setfield
1919
using StaticArrays
2020
using Statistics
21+
using Test: @test, @test_nowarn
2122

2223

2324
include("./base.jl")
2425
include("./wrapper.jl")
2526
include("./buffered.jl")
2627
include("./sequence.jl")
27-
include("./visualization.jl")
2828
include("./items/arrayitem.jl")
2929
include("./projective/base.jl")
3030
include("./items/image.jl")
3131
include("./items/keypoints.jl")
3232
include("./items/mask.jl")
33-
include("./projective/bounds.jl")
3433
include("./projective/compose.jl")
3534
include("./projective/crop.jl")
3635
include("./projective/affine.jl")
3736
include("./projective/warp.jl")
3837
include("./oneof.jl")
3938
include("./preprocessing.jl")
4039
include("./colortransforms.jl")
40+
include("testing.jl")
41+
include("./visualization.jl")
4142

4243

4344
export Item,
@@ -86,7 +87,8 @@ export Item,
8687
ResizePadDivisible,
8788
onehot,
8889
showitems,
89-
showgrid
90+
showgrid,
91+
Bounds
9092

9193

9294
end # module

src/buffered.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ apply!(buf, tfm::Transform, items; randstate = getrandstate(tfm)) = apply(tfm, i
3030
# bounding boxes per sample) the number of items doesn't match up with the buffer and
3131
# we fall back to regular `apply`.
3232

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

7373

74-
struct BufferedThreadsafe
74+
struct BufferedThreadsafe <: Transform
7575
buffereds::Vector{Buffered}
7676
function BufferedThreadsafe(tfm; n = Threads.nthreads())
7777
@assert n >= 1

src/items/image.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,20 @@ showitems(item)
3333
```
3434
3535
"""
36-
struct Image{N,T,B} <: AbstractArrayItem{N,T}
36+
struct Image{N,T} <: AbstractArrayItem{N,T}
3737
data::AbstractArray{T,N}
38-
bounds::AbstractArray{<:SVector{N,B},N}
38+
bounds::Bounds{N}
3939
end
4040

41-
Image(data) = Image(data, size(data))
41+
Image(data) = Image(data, Bounds(axes(data)))
4242

4343
function Image(data::AbstractArray{T,N}, sz::NTuple{N,Int}) where {T,N}
44-
bounds = makebounds(sz)
45-
return Image(data, bounds)
44+
return Image(data, Bounds(sz))
4645
end
4746

4847

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

5251

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

75-
function project(P, image::Image{N, T}, indices) where {N, T}
76-
## Transform the bounds along with the image
77-
bounds_ = P.(getbounds(image))
78-
data_ = warp(itemdata(image), inv(P), indices, zero(T))
79-
return Image(data_, makebounds(indices))
74+
function project(P, image::Image{N, T}, bounds::Bounds) where {N, T}
75+
# TODO: make interpolation scheme and boundary conditions configurable
76+
data_ = warp(
77+
itemdata(image),
78+
inv(P),
79+
bounds.rs,
80+
zero(T))
81+
return Image(data_, bounds)
8082
end
8183

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

85-
function project!(bufimage::Image, P, image::Image{N, T}, indices) where {N, T}
86-
a = OffsetArray(parent(itemdata(bufimage)), indices)
87+
function project!(bufimage::Image, P, image::Image{N, T}, bounds::Bounds{N}) where {N, T}
88+
a = OffsetArray(parent(itemdata(bufimage)), bounds.rs)
8789
res = warp!(
8890
a,
8991
box_extrapolation(itemdata(image), zero(T)),
9092
inv(P),
9193
)
92-
return Image(res, P.(getbounds(image)))
94+
return Image(res, bounds)
9395
end

src/items/keypoints.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ showitems(item)
2323
"""
2424
struct Keypoints{N, T, S<:Union{SVector{N, T}, Nothing}, M} <: AbstractArrayItem{M, S}
2525
data::AbstractArray{S, M}
26-
bounds::AbstractArray{<:SVector{N, Float32}, N}
26+
bounds::Bounds{N}
2727
end
2828

2929

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

3434

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

4141

42-
function project(P, keypoints::Keypoints{N, T}, indices) where {N, T}
42+
function project(P, keypoints::Keypoints{N, T}, bounds::Bounds{N}) where {N, T}
43+
# TODO: convert back to `T`?
4344
return Keypoints(
4445
map(fmap(P), keypoints.data),
45-
makebounds(indices),
46+
bounds,
4647
)
4748
end
4849

src/items/mask.jl

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ mask = MaskMulti(rand(1:3, 100, 100))
1616
showitems(mask)
1717
```
1818
"""
19-
struct MaskMulti{N, T<:Integer, U, B} <: AbstractArrayItem{N, T}
19+
struct MaskMulti{N, T<:Integer, U} <: AbstractArrayItem{N, T}
2020
data::AbstractArray{T, N}
2121
classes::AbstractVector{U}
22-
bounds::AbstractArray{<:SVector{N, B}, N}
22+
bounds::Bounds{N}
2323
end
2424

2525

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

4141

42-
function project(P, mask::MaskMulti, indices)
42+
function project(P, mask::MaskMulti, bounds::Bounds)
4343
a = itemdata(mask)
4444
etp = mask_extrapolation(a)
45+
res = warp(etp, inv(P), bounds.rs)
4546
return MaskMulti(
46-
warp(etp, inv(P), indices),
47+
res,
4748
mask.classes,
48-
P.(mask.bounds)
49+
bounds
4950
)
5051
end
5152

5253

53-
function project!(bufmask::MaskMulti, P, mask::MaskMulti, indices)
54-
a = OffsetArray(parent(itemdata(bufmask)), indices)
55-
bounds_ = P.(getbounds(mask))
56-
res = warp!(
54+
function project!(bufmask::MaskMulti, P, mask::MaskMulti, bounds)
55+
a = OffsetArray(parent(itemdata(bufmask)), bounds.rs)
56+
warp!(
5757
a,
5858
mask_extrapolation(itemdata(mask)),
5959
inv(P),
6060
)
61-
return MaskMulti(a, mask.classes, P.(getbounds(mask)))
61+
return MaskMulti(
62+
a,
63+
mask.classes,
64+
bounds
65+
)
6266
end
6367

6468

@@ -91,12 +95,12 @@ mask = MaskBinary(rand(Bool, 100, 100))
9195
showitems(mask)
9296
```
9397
"""
94-
struct MaskBinary{N, B} <: AbstractArrayItem{N, Bool}
98+
struct MaskBinary{N} <: AbstractArrayItem{N, Bool}
9599
data::AbstractArray{Bool, N}
96-
bounds::AbstractArray{<:SVector{N, B}, N}
100+
bounds::Bounds{N}
97101
end
98102

99-
function MaskBinary(a::AbstractArray{Bool, N}, bounds = makebounds(size(a))) where N
103+
function MaskBinary(a::AbstractArray{Bool, N}, bounds = Bounds(size(a))) where N
100104
return MaskBinary(a, bounds)
101105
end
102106

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

106110
getbounds(mask::MaskBinary) = mask.bounds
107111

108-
function project(P, mask::MaskBinary, indices)
112+
function project(P, mask::MaskBinary, bounds::Bounds)
109113
etp = mask_extrapolation(itemdata(mask))
110114
return MaskBinary(
111-
warp(etp, inv(P), indices),
112-
P.(getbounds(mask)),
115+
warp(etp, inv(P), bounds.rs),
116+
bounds,
113117
)
114118
end
115119

116120

117-
function project!(bufmask::MaskBinary, P, mask::MaskBinary, indices)
118-
bounds_ = P.(getbounds(mask))
119-
a = OffsetArray(parent(itemdata(bufmask)), indices)
121+
function project!(bufmask::MaskBinary, P, mask::MaskBinary, bounds)
122+
a = OffsetArray(parent(itemdata(bufmask)), bounds.rs)
120123
res = warp!(
121124
a,
122125
mask_extrapolation(itemdata(mask)),
123126
inv(P),
124127
)
125-
return MaskBinary(res, P.(getbounds(mask)))
128+
return MaskBinary(
129+
a,
130+
bounds
131+
)
126132
end
127133

128134
function showitem!(img, mask::MaskBinary)

src/oneof.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function getrandstate(oneof::OneOf)
2121
end
2222

2323

24-
function apply(oneof::OneOf, item::AbstractItem; randstate = getrandstate(oneof))
24+
function apply(oneof::OneOf, item::Item; randstate = getrandstate(oneof))
2525
i, tfmrandstate = randstate
2626
return apply(oneof.tfms[i], item; randstate = tfmrandstate)
2727
end
@@ -31,7 +31,7 @@ function makebuffer(oneof::OneOf, items)
3131
return Tuple([makebuffer(tfm, items) for tfm in oneof.tfms])
3232
end
3333

34-
function apply!(bufs, oneof::OneOf, item::AbstractItem; randstate = getrandstate(tfm))
34+
function apply!(bufs, oneof::OneOf, item::Item; randstate = getrandstate(oneof))
3535
i, tfmrandstate = randstate
3636
buf = bufs[i]
3737
return apply!(buf, oneof.tfms[i], item; randstate = tfmrandstate)

0 commit comments

Comments
 (0)