Skip to content

Commit c09f3d1

Browse files
authored
Merge pull request #31 from lorenzoh/zoom
Add `Zoom` affine transformation.
2 parents 8b6c6ef + 0712498 commit c09f3d1

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

src/DataAugmentation.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export Item,
6868
Buffered,
6969
BufferedThreadsafe,
7070
OneHot,
71+
Zoom,
7172
apply,
7273
Reflect,
7374
WarpAffine,

src/projective/affine.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ function getprojection(scale::ScaleFixed{N}, bounds; randstate = nothing) where
6363
end
6464

6565

66+
"""
67+
Zoom(scales = (1, 1.2)) <: ProjectiveTransform
68+
Zoom(distribution)
69+
70+
Zoom into an item by a factor chosen from the interval `scales`
71+
or `distribution`.
72+
"""
73+
struct Zoom{D<:Sampleable} <: ProjectiveTransform
74+
dist::D
75+
end
76+
77+
Zoom(scales::NTuple{2, T} = (1., 1.2)) where T = Zoom(Uniform(scales[1], scales[2]))
78+
79+
getrandstate(tfm::Zoom) = rand(tfm.dist)
80+
81+
function getprojection(tfm::Zoom, bounds::AbstractArray{<:SVector{N}}; randstate = getrandstate(tfm)) where N
82+
ratio = randstate
83+
return scaleprojection(ntuple(_ -> ratio, N))
84+
end
85+
6686
"""
6787
Rotate(γ)
6888
Rotate(γs)

src/projective/crop.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ end
9191

9292

9393
compose(tfm::ProjectiveTransform, crop::AbstractCrop) = CroppedProjectiveTransform(tfm, crop)
94+
compose(tfm::ProjectiveTransform, crop::CroppedProjectiveTransform) =
95+
CroppedProjectiveTransform(tfm |> crop.tfm, crop.crop)
9496

97+
function compose(composed::ComposedProjectiveTransform, cropped::CroppedProjectiveTransform)
98+
return CroppedProjectiveTransform(composed |> cropped.tfm, cropped.crop)
99+
100+
end
95101

96102

97103
"""

src/sequence.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ after each other.
1111
1212
You should not use this explicitly. Instead use [`compose`](#).
1313
"""
14-
struct Sequence{T<:Tuple where N} <: Transform
14+
struct Sequence{T<:Tuple} <: Transform
1515
transforms::T
1616
end
1717

1818
Sequence(tfms...) = Sequence{typeof(tfms)}(tfms)
19+
Sequence(tfm::Transform) = tfm
1920

2021
getrandstate(seq::Sequence) = getrandstate.(seq.transforms)
2122

2223

2324
compose(tfm1::Transform, tfm2::Transform) = Sequence(tfm1, tfm2)
2425
compose(seq::Sequence, tfm::Transform) = Sequence(seq.transforms..., tfm)
2526
compose(seq::Sequence, ::Identity) = seq
27+
compose(tfm::Transform, seq::Sequence) = compose(tfm, seq.transforms...)
2628

2729

2830
function apply(seq::Sequence, items::Tuple; randstate = getrandstate(seq))

test/projective/affine.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ include("../imports.jl")
102102

103103
end
104104

105+
@testset ExtendedTestSet "Rotate" begin
106+
tfm = Zoom((0.1, 2.))
107+
image = Image(rand(RGB, 50, 50))
108+
@test_nowarn apply(tfm, image)
109+
110+
end
111+
105112
@testset ExtendedTestSet "Reflect" begin
106113
tfm = Reflect(10)
107114
image = Image(rand(RGB, 50, 50))

0 commit comments

Comments
 (0)