Change DepthwiseConv() to use in=>out instead of in=>mult.

This is an API change, but I think it makes more sense, and is more
consistent with our `Conv()` api.
This commit is contained in:
Elliot Saba 2019-04-26 10:52:14 -07:00
parent 79534caca1
commit 2e6561bb6a
2 changed files with 25 additions and 31 deletions

View File

@ -136,18 +136,17 @@ end
(a::ConvTranspose{<:Any,<:Any,W})(x::AbstractArray{<:Real}) where {T <: Union{Float32,Float64}, W <: AbstractArray{T}} = (a::ConvTranspose{<:Any,<:Any,W})(x::AbstractArray{<:Real}) where {T <: Union{Float32,Float64}, W <: AbstractArray{T}} =
a(T.(x)) a(T.(x))
""" """
DepthwiseConv(size, in) DepthwiseConv(size, in=>out)
DepthwiseConv(size, in=>mul) DepthwiseConv(size, in=>out, relu)
DepthwiseConv(size, in=>mul, relu)
Depthwise convolutional layer. `size` should be a tuple like `(2, 2)`. Depthwise convolutional layer. `size` should be a tuple like `(2, 2)`.
`in` and `mul` specify the number of input channels and channel multiplier respectively. `in` and `out` specify the number of input and output channels respectively.
In case the `mul` is not specified it is taken as 1. Note that `out` must be an integer multiple of `in`.
Data should be stored in WHCN order. In other words, a 100×100 RGB image would Data should be stored in WHCN order. In other words, a 100×100 RGB image would
be a `100×100×3` array, and a batch of 50 would be a `100×100×3×50` array. be a `100×100×3` array, and a batch of 50 would be a `100×100×3×50` array.
Takes the keyword arguments `pad` and `stride`. Takes the keyword arguments `pad`, `stride` and `dilation`.
""" """
struct DepthwiseConv{N,M,F,A,V} struct DepthwiseConv{N,M,F,A,V}
σ::F σ::F
@ -166,17 +165,18 @@ function DepthwiseConv(w::AbstractArray{T,N}, b::AbstractVector{T}, σ = identit
return DepthwiseConv(σ, w, b, stride, pad, dilation) return DepthwiseConv(σ, w, b, stride, pad, dilation)
end end
DepthwiseConv(k::NTuple{N,Integer}, ch::Integer, σ = identity; init = glorot_uniform, function DepthwiseConv(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ = identity;
stride = 1, pad = 0, dilation = 1) where N = init = glorot_uniform, stride = 1, pad = 0, dilation = 1) where N
DepthwiseConv(param(init(k..., 1, ch)), param(zeros(ch)), σ, @assert ch[2] % ch[1] == 0 "Output channels must be integer multiple of input channels"
stride = stride, pad = pad, dilation=dilation) return DepthwiseConv(
param(init(k..., div(ch[2], ch[1]), ch[1])),
DepthwiseConv(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ = identity; init = glorot_uniform, param(zeros(ch[2])),
stride::NTuple{N,Integer} = map(_->1,k), σ;
pad::NTuple{N,Integer} = map(_->0,2 .* k), stride = stride,
dilation::NTuple{N,Integer} = map(_->1,k)) where N = pad = pad,
DepthwiseConv(param(init(k..., ch[2], ch[1])), param(zeros(ch[2]*ch[1])), σ, dilation = dilation
stride = stride, pad = pad) )
end
@treelike DepthwiseConv @treelike DepthwiseConv
@ -187,8 +187,8 @@ function (c::DepthwiseConv)(x)
end end
function Base.show(io::IO, l::DepthwiseConv) function Base.show(io::IO, l::DepthwiseConv)
print(io, "DepthwiseConv(", size(l.weight)[1:ndims(l.weight)-2]) print(io, "DepthwiseConv(", size(l.weight)[1:end-2])
print(io, ", ", size(l.weight, ndims(l.weight)), "=>", size(l.weight, ndims(l.weight)-1)) print(io, ", ", size(l.weight)[end], "=>", prod(size(l.weight)[end-1:end]))
l.σ == identity || print(io, ", ", l.σ) l.σ == identity || print(io, ", ", l.σ)
print(io, ")") print(io, ")")
end end

View File

@ -39,20 +39,14 @@ end
@testset "Depthwise Conv" begin @testset "Depthwise Conv" begin
r = zeros(Float32, 28, 28, 3, 5) r = zeros(Float32, 28, 28, 3, 5)
m1 = DepthwiseConv((2, 2), 3=>5) m1 = DepthwiseConv((2, 2), 3=>15)
@test size(m1(r), 3) == 15 @test size(m1(r), 3) == 15
m2 = DepthwiseConv((2, 2), 3)
@test size(m2(r), 3) == 3
x = zeros(Float64, 28, 28, 3, 5) m3 = DepthwiseConv((2, 3), 3=>9)
@test size(m3(r), 3) == 9
m3 = DepthwiseConv((2, 2), 3 => 5) # Test that we cannot ask for non-integer multiplication factors
@test_throws AssertionError DepthwiseConv((2,2), 3=>10)
@test size(m3(r), 3) == 15
m4 = DepthwiseConv((2, 2), 3)
@test size(m4(r), 3) == 3
end end
@testset "ConvTranspose" begin @testset "ConvTranspose" begin