Skip to content

Commit

Permalink
add abstract node ImageOperation
Browse files Browse the repository at this point in the history
  • Loading branch information
Evizero committed Jun 3, 2017
1 parent ff205bf commit 4514d61
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 44 deletions.
File renamed without changes
6 changes: 3 additions & 3 deletions src/operations/cache.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
CacheImage <: Augmentor.Operation
CacheImage <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -51,7 +51,7 @@ see also
[`augment`](@ref)
"""
immutable CacheImage <: Operation end
immutable CacheImage <: ImageOperation end

applyeager(op::CacheImage, img::Array) = img
applyeager(op::CacheImage, img::OffsetArray) = img
Expand All @@ -72,7 +72,7 @@ end

# --------------------------------------------------------------------

immutable CacheImageInto{T<:AbstractArray} <: Operation
immutable CacheImageInto{T<:AbstractArray} <: ImageOperation
buffer::T
end
CacheImage(buffer::AbstractArray) = CacheImageInto(buffer)
Expand Down
20 changes: 10 additions & 10 deletions src/operations/crop.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Crop <: Augmentor.Operation
Crop <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -48,7 +48,7 @@ see also
[`CropNative`](@ref), [`CropSize`](@ref), [`CropRatio`](@ref), [`augment`](@ref)
"""
immutable Crop{N,I<:Tuple} <: Operation
immutable Crop{N,I<:Tuple} <: ImageOperation
indexes::I

function (::Type{Crop{N}}){N}(indexes::NTuple{N,UnitRange})
Expand Down Expand Up @@ -87,7 +87,7 @@ end
# --------------------------------------------------------------------

"""
CropNative <: Augmentor.Operation
CropNative <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -141,7 +141,7 @@ see also
[`Crop`](@ref), [`CropSize`](@ref), [`CropRatio`](@ref), [`augment`](@ref)
"""
immutable CropNative{N,I<:Tuple} <: Operation
immutable CropNative{N,I<:Tuple} <: ImageOperation
indexes::I

function (::Type{CropNative{N}}){N}(indexes::NTuple{N,UnitRange})
Expand Down Expand Up @@ -183,7 +183,7 @@ end
# --------------------------------------------------------------------

"""
CropSize <: Augmentor.Operation
CropSize <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -224,7 +224,7 @@ see also
[`CropRatio`](@ref), [`Crop`](@ref), [`CropNative`](@ref), [`augment`](@ref)
"""
immutable CropSize{N} <: Operation
immutable CropSize{N} <: ImageOperation
size::NTuple{N,Int}

function (::Type{CropSize{N}}){N}(size::NTuple{N,Int})
Expand Down Expand Up @@ -280,7 +280,7 @@ end
# --------------------------------------------------------------------

"""
CropRatio <: Augmentor.Operation
CropRatio <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -324,7 +324,7 @@ see also
[`RCropRatio`](@ref), [`CropSize`](@ref), [`Crop`](@ref), [`CropNative`](@ref), [`augment`](@ref)
"""
immutable CropRatio <: Operation
immutable CropRatio <: ImageOperation
ratio::Float64

function (::Type{CropRatio})(ratio::Real)
Expand Down Expand Up @@ -403,7 +403,7 @@ end
# --------------------------------------------------------------------

"""
RCropRatio <: Augmentor.Operation
RCropRatio <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -446,7 +446,7 @@ see also
[`CropRatio`](@ref), [`CropSize`](@ref), [`Crop`](@ref), [`CropNative`](@ref), [`augment`](@ref)
"""
immutable RCropRatio <: Operation
immutable RCropRatio <: ImageOperation
ratio::Float64

function (::Type{RCropRatio})(ratio::Real)
Expand Down
4 changes: 2 additions & 2 deletions src/operations/distortion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const ED_DEFAULT_BORDER = false
const ED_DEFAULT_NORM = true

@doc """
ElasticDistortion <: Augmentor.Operation
ElasticDistortion <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -94,7 +94,7 @@ see also
[`RandomDistortion`](@ref), [`augment`](@ref)
""" ->
immutable ElasticDistortion <: Operation
immutable ElasticDistortion <: ImageOperation
gridheight::Int
gridwidth::Int
scale::Float64
Expand Down
20 changes: 10 additions & 10 deletions src/operations/either.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Either <: Augmentor.Operation
Either <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -37,7 +37,7 @@ Usage
Arguments
--------------
- **`operations`** : `NTuple` or `Vararg` of `Augmentor.Operation`
- **`operations`** : `NTuple` or `Vararg` of `Augmentor.ImageOperation`
that denote the possible choices to sample from when applied.
- **`chances`** : Optional. Denotes the relative chances for an
Expand All @@ -48,7 +48,7 @@ Arguments
argument. If omitted every operation will have equal
probability of occurring.
- **`pairs`** : `Vararg` of `Pair{<:Real,<:Augmentor.Operation}`.
- **`pairs`** : `Vararg` of `Pair{<:Real,<:Augmentor.ImageOperation}`.
A compact way to specify an operation and its chance of
occurring together.
Expand All @@ -75,12 +75,12 @@ see also
[`augment`](@ref)
"""
immutable Either{N,T<:Tuple} <: Operation
immutable Either{N,T<:Tuple} <: ImageOperation
operations::T
chances::SVector{N,Float64}
cum_chances::SVector{N,Float64}

function (::Type{Either}){N,T}(operations::NTuple{N,Operation}, chances::SVector{N,T})
function (::Type{Either}){N,T}(operations::NTuple{N,ImageOperation}, chances::SVector{N,T})
all(c->c>=0, chances) || throw(ArgumentError("All provided \"chances\" must be positive"))
length(operations) > 0 || throw(ArgumentError("Must provide at least one operation in the constructor of \"Either\""))
sum_chances = sum(chances)
Expand All @@ -93,29 +93,29 @@ end

Either() = throw(ArgumentError("Must provide at least one operation in the constructor of \"Either\""))

function Either{N}(operations::NTuple{N,Operation}, chances::NTuple{N,Real} = map(op -> 1/length(operations), operations))
function Either{N}(operations::NTuple{N,ImageOperation}, chances::NTuple{N,Real} = map(op -> 1/length(operations), operations))
Either(operations, SVector{N}(chances))
end

function Either{N}(operations::Vararg{Operation,N}; chances = map(op -> 1/length(operations), operations))
function Either{N}(operations::Vararg{ImageOperation,N}; chances = map(op -> 1/length(operations), operations))
Either(operations, SVector{N}(map(Float64, chances)))
end

function Either{N}(operations::Vararg{Pair,N})
Either(map(last, operations), map(first, operations))
end

function Either(op::Operation, p::Real = .5)
function Either(op::ImageOperation, p::Real = .5)
0 <= p <= 1. || throw(ArgumentError("The propability \"p\" has to be in the interval [0, 1]"))
p1 = Float64(p)
p2 = 1 - p1
Either((op, NoOp()), (p1, p2))
end


Base.:*{T<:Number,O<:Operation}(op1::Pair{T,O}, ops::Pair...) =
Base.:*{T<:Number,O<:ImageOperation}(op1::Pair{T,O}, ops::Pair...) =
Either(op1, ops...)
Base.:*(op1::Operation, ops::Operation...) = Either((op1, ops...))
Base.:*(op1::ImageOperation, ops::ImageOperation...) = Either((op1, ops...))

@inline supports_permute{N,T}(::Type{Either{N,T}}) = all(map(supports_permute, T.types))
@inline supports_view{N,T}(::Type{Either{N,T}}) = all(map(supports_view, T.types))
Expand Down
4 changes: 2 additions & 2 deletions src/operations/resize.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Resize <: Augmentor.Operation
Resize <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -41,7 +41,7 @@ see also
[`CropSize`](@ref), [`augment`](@ref)
"""
immutable Resize{N} <: Operation
immutable Resize{N} <: ImageOperation
size::NTuple{N,Int}

function (::Type{Resize{N}}){N}(size::NTuple{N,Int})
Expand Down
4 changes: 2 additions & 2 deletions src/operations/zoom.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Zoom <: Augmentor.Operation
Zoom <: Augmentor.ImageOperation
Description
--------------
Expand Down Expand Up @@ -63,7 +63,7 @@ see also
[`Scale`](@ref), [`Resize`](@ref), [`augment`](@ref)
"""
immutable Zoom{N,T<:AbstractVector} <: Operation
immutable Zoom{N,T<:AbstractVector} <: ImageOperation
factors::NTuple{N,T}

function (::Type{Zoom{N}}){N,T<:AbstractVector}(factors::NTuple{N,T})
Expand Down
3 changes: 2 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@compat abstract type Operation end
@compat abstract type AffineOperation <: Operation end
@compat abstract type ImageOperation <: Operation end
@compat abstract type AffineOperation <: ImageOperation end
@compat abstract type Pipeline end
@compat const AbstractPipeline = Union{Pipeline,Tuple{Vararg{Operation}}}
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ thus well suited to prototype an augmentation pipeline, because
it makes it easy to see what kind of effects one can achieve with
it.
"""
testpattern() = load(joinpath(dirname(@__FILE__()), "resources", "testpattern.png"))
testpattern() = load(joinpath(dirname(@__FILE__()), "..", "resources", "testpattern.png"))

function use_testpattern()
info("No custom image specifed. Using \"testpattern()\" for demonstration.")
Expand Down
4 changes: 2 additions & 2 deletions test/operations/tst_cache.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testset "CacheImage" begin
@test (CacheImage <: Augmentor.AffineOperation) == false
@test (CacheImage <: Augmentor.Operation) == true
@test (CacheImage <: Augmentor.ImageOperation) == true
@test typeof(@inferred(CacheImage())) <: CacheImage
@test str_show(CacheImage()) == "Augmentor.CacheImage()"
@test str_showconst(CacheImage()) == "CacheImage()"
Expand Down Expand Up @@ -37,7 +37,7 @@ end
@testset "CacheImageInto" begin
@test_throws UndefVarError CacheImageInto
@test (Augmentor.CacheImageInto <: Augmentor.AffineOperation) == false
@test (Augmentor.CacheImageInto <: Augmentor.Operation) == true
@test (Augmentor.CacheImageInto <: Augmentor.ImageOperation) == true
@test_throws MethodError Augmentor.CacheImageInto()

buf = copy(rect)
Expand Down
14 changes: 7 additions & 7 deletions test/operations/tst_crop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
@testset "constructor" begin
@test_throws MethodError Crop()
@test_throws MethodError Crop(())
@test typeof(@inferred(Crop(1:10))) <: Crop{1} <: Crop <: Augmentor.Operation
@test typeof(@inferred(Crop(1:10,3:5))) <: Crop{2} <: Crop <: Augmentor.Operation
@test typeof(@inferred(Crop(1:10))) <: Crop{1} <: Crop <: Augmentor.ImageOperation
@test typeof(@inferred(Crop(1:10,3:5))) <: Crop{2} <: Crop <: Augmentor.ImageOperation
@test @inferred(Crop(1,4,10,5)) === @inferred(Crop((4:8,1:10)))
@test str_show(Crop(3:4)) == "Augmentor.Crop{1}((3:4,))"
@test str_showconst(Crop(3:4)) == "Crop(3:4)"
Expand Down Expand Up @@ -55,8 +55,8 @@ end
@testset "constructor" begin
@test_throws MethodError CropNative()
@test_throws MethodError CropNative(())
@test typeof(@inferred(CropNative(1:10))) <: CropNative{1} <: CropNative <: Augmentor.Operation
@test typeof(@inferred(CropNative(1:10,3:5))) <: CropNative{2} <: CropNative <: Augmentor.Operation
@test typeof(@inferred(CropNative(1:10))) <: CropNative{1} <: CropNative <: Augmentor.ImageOperation
@test typeof(@inferred(CropNative(1:10,3:5))) <: CropNative{2} <: CropNative <: Augmentor.ImageOperation
@test @inferred(CropNative(1,4,10,5)) === @inferred(CropNative((4:8,1:10)))
@test str_show(CropNative(3:4)) == "Augmentor.CropNative{1}((3:4,))"
@test str_showconst(CropNative(3:4)) == "CropNative(3:4)"
Expand Down Expand Up @@ -107,7 +107,7 @@ end

@testset "CropSize" begin
@test (CropSize <: Augmentor.AffineOperation) == false
@test typeof(@inferred(CropSize())) <: CropSize <: Augmentor.Operation
@test typeof(@inferred(CropSize())) <: CropSize <: Augmentor.ImageOperation
@testset "constructor" begin
@test_throws MethodError CropSize(())
@test_throws MethodError CropSize(1.,2.)
Expand Down Expand Up @@ -175,7 +175,7 @@ end

@testset "CropRatio" begin
@test (CropRatio <: Augmentor.AffineOperation) == false
@test typeof(@inferred(CropRatio())) <: CropRatio <: Augmentor.Operation
@test typeof(@inferred(CropRatio())) <: CropRatio <: Augmentor.ImageOperation
@testset "constructor" begin
@test_throws MethodError CropRatio(())
@test_throws MethodError CropRatio(1.,2.)
Expand Down Expand Up @@ -249,7 +249,7 @@ end

@testset "RCropRatio" begin
@test (RCropRatio <: Augmentor.AffineOperation) == false
@test typeof(@inferred(RCropRatio())) <: RCropRatio <: Augmentor.Operation
@test typeof(@inferred(RCropRatio())) <: RCropRatio <: Augmentor.ImageOperation
@testset "constructor" begin
@test_throws MethodError RCropRatio(())
@test_throws MethodError RCropRatio(1.,2.)
Expand Down
2 changes: 1 addition & 1 deletion test/operations/tst_distortions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testset "ElasticDistortion" begin
@test (ElasticDistortion <: Augmentor.AffineOperation) == false
@test (ElasticDistortion <: Augmentor.Operation) == true
@test (ElasticDistortion <: Augmentor.ImageOperation) == true

@testset "constructor" begin
@test_throws MethodError ElasticDistortion()
Expand Down
1 change: 1 addition & 0 deletions test/operations/tst_either.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@test (Either <: Augmentor.AffineOperation) == false
@test Either <: Augmentor.ImageOperation

@testset "constructor" begin
@test_throws ArgumentError Either()
Expand Down
2 changes: 1 addition & 1 deletion test/operations/tst_resize.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testset "Resize" begin
@test (Resize <: Augmentor.AffineOperation) == false
@test typeof(@inferred(Resize())) <: Resize <: Augmentor.Operation
@test typeof(@inferred(Resize())) <: Resize <: Augmentor.ImageOperation
@testset "constructor" begin
@test_throws MethodError Resize(())
@test_throws MethodError Resize(1.,2.)
Expand Down
2 changes: 1 addition & 1 deletion test/operations/tst_zoom.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testset "Zoom" begin
@test (Zoom <: Augmentor.AffineOperation) == false
@test typeof(@inferred(Zoom(1))) <: Zoom <: Augmentor.Operation
@test typeof(@inferred(Zoom(1))) <: Zoom <: Augmentor.ImageOperation
@testset "constructor" begin
@test_throws MethodError Zoom()
@test_throws MethodError Zoom(())
Expand Down
4 changes: 3 additions & 1 deletion test/tst_operations.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# test not exported
@test_throws UndefVarError Operation
@test_throws UndefVarError ImageOperation
@test_throws UndefVarError AffineOperation

@test Augmentor.AffineOperation <: Augmentor.Operation
@test Augmentor.ImageOperation <: Augmentor.Operation
@test Augmentor.AffineOperation <: Augmentor.ImageOperation

@testset "prepare" begin
@test @inferred(Augmentor.prepareview(rect)) === rect
Expand Down

0 comments on commit 4514d61

Please sign in to comment.