756: Change `DepthwiseConv()` to use `in=>out` instead of `in=>mult`. r=MikeInnes a=staticfloat

This is an API change, but I think it makes more sense, and is more consistent with our `Conv()` API.  This also dumps the `DepthwiseConv((3,3), C_in)` API, as I'm not sure why you would want to specify only the input channel count and default the output to a channel multiplier of 1; if anything I would think you'd want to specify the channel output and leave the input to be default.  In any case, I think consistency with `Conv()` is the best thing to chase after here.

Co-authored-by: Elliot Saba <staticfloat@gmail.com>
This commit is contained in:
bors[bot] 2019-05-13 16:37:57 +00:00
commit 16fc41cd00
4 changed files with 28 additions and 32 deletions

View File

@ -1,3 +1,6 @@
# v0.9.0
* [Depthwise comvolutional layer API changes](https://github.com/FluxML/Flux.jl/pull/756) from `in => mult` channel specification to `in => out` channel specification, and deprecates implicit `out` constructor.
# v0.8.0 # v0.8.0
* New [ConvTranspose layer](https://github.com/FluxML/Flux.jl/pull/311). * New [ConvTranspose layer](https://github.com/FluxML/Flux.jl/pull/311).

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

View File

@ -252,7 +252,6 @@ end
@test !m.active @test !m.active
x = m(x).data x = m(x).data
println(x[1])
@test isapprox(x[1], (1 - 0.95) / sqrt(1.25 + 1f-5), atol = 1.0e-5) @test isapprox(x[1], (1 - 0.95) / sqrt(1.25 + 1f-5), atol = 1.0e-5)
end end
# with activation function # with activation function