2018-05-30 10:23:57 +00:00
|
|
|
|
using NNlib: conv, depthwiseconv
|
2018-02-26 22:43:07 +00:00
|
|
|
|
|
2018-06-26 13:05:07 +00:00
|
|
|
|
@generated sub2(::Type{Val{N}}) where N = :(Val{$(N-2)})
|
|
|
|
|
|
|
|
|
|
expand(N, i::Tuple) = i
|
|
|
|
|
expand(N, i::Integer) = ntuple(_ -> i, N)
|
2018-04-15 15:02:40 +00:00
|
|
|
|
|
2017-12-18 18:05:48 +00:00
|
|
|
|
"""
|
2018-02-26 22:43:07 +00:00
|
|
|
|
Conv(size, in=>out)
|
|
|
|
|
Conv(size, in=>out, relu)
|
2017-12-18 18:05:48 +00:00
|
|
|
|
|
|
|
|
|
Standard convolutional layer. `size` should be a tuple like `(2, 2)`.
|
|
|
|
|
`in` and `out` specify the number of input and output channels respectively.
|
|
|
|
|
|
2018-02-16 00:06:15 +00:00
|
|
|
|
Data should be stored in WHCN order. In other words, a 100×100 RGB image would
|
2017-12-18 18:05:48 +00:00
|
|
|
|
be a `100×100×3` array, and a batch of 50 would be a `100×100×3×50` array.
|
|
|
|
|
|
2018-05-21 19:20:43 +00:00
|
|
|
|
Takes the keyword arguments `pad`, `stride` and `dilation`.
|
2017-12-18 18:05:48 +00:00
|
|
|
|
"""
|
2018-02-26 22:43:07 +00:00
|
|
|
|
struct Conv{N,F,A,V}
|
2017-12-15 13:22:57 +00:00
|
|
|
|
σ::F
|
|
|
|
|
weight::A
|
2018-02-15 20:15:41 +00:00
|
|
|
|
bias::V
|
2018-02-26 22:43:07 +00:00
|
|
|
|
stride::NTuple{N,Int}
|
|
|
|
|
pad::NTuple{N,Int}
|
2018-05-21 19:20:43 +00:00
|
|
|
|
dilation::NTuple{N,Int}
|
2017-12-15 13:22:57 +00:00
|
|
|
|
end
|
|
|
|
|
|
2018-06-26 13:05:07 +00:00
|
|
|
|
Conv(w::AbstractArray{T,N}, b::AbstractVector{T}, σ = identity;
|
|
|
|
|
stride = 1, pad = 0, dilation = 1) where {T,N} =
|
|
|
|
|
Conv(σ, w, b, expand.(sub2(Val{N}), (stride, pad, dilation))...)
|
2018-02-15 20:52:29 +00:00
|
|
|
|
|
2018-02-26 22:43:07 +00:00
|
|
|
|
Conv(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ = identity; init = initn,
|
2018-06-26 13:05:07 +00:00
|
|
|
|
stride = 1, pad = 0, dilation = 1) where N =
|
2018-07-18 07:01:06 +00:00
|
|
|
|
Conv(param(init(k..., ch...)), param(zero(ch[2])), σ,
|
2018-05-21 19:20:43 +00:00
|
|
|
|
stride = stride, pad = pad, dilation = dilation)
|
2017-12-15 13:22:57 +00:00
|
|
|
|
|
2018-07-12 21:43:11 +00:00
|
|
|
|
@treelike Conv
|
2017-12-15 13:22:57 +00:00
|
|
|
|
|
2018-02-26 22:43:07 +00:00
|
|
|
|
function (c::Conv)(x)
|
2018-02-28 23:06:53 +00:00
|
|
|
|
# TODO: breaks gpu broadcast :(
|
|
|
|
|
# ndims(x) == ndims(c.weight)-1 && return squeezebatch(c(reshape(x, size(x)..., 1)))
|
2018-02-26 22:43:07 +00:00
|
|
|
|
σ, b = c.σ, reshape(c.bias, map(_->1, c.stride)..., :, 1)
|
2018-05-21 19:20:43 +00:00
|
|
|
|
σ.(conv(x, c.weight, stride = c.stride, pad = c.pad, dilation = c.dilation) .+ b)
|
2018-02-15 20:15:41 +00:00
|
|
|
|
end
|
2017-12-15 16:24:45 +00:00
|
|
|
|
|
2018-02-26 22:43:07 +00:00
|
|
|
|
function Base.show(io::IO, l::Conv)
|
|
|
|
|
print(io, "Conv(", size(l.weight)[1:ndims(l.weight)-2])
|
|
|
|
|
print(io, ", ", size(l.weight, ndims(l.weight)-1), "=>", size(l.weight, ndims(l.weight)))
|
2017-12-15 16:24:45 +00:00
|
|
|
|
l.σ == identity || print(io, ", ", l.σ)
|
|
|
|
|
print(io, ")")
|
|
|
|
|
end
|
2018-02-26 22:43:07 +00:00
|
|
|
|
|
2018-05-30 10:23:57 +00:00
|
|
|
|
"""
|
2018-06-09 05:32:15 +00:00
|
|
|
|
DepthwiseConv(size, in)
|
2018-05-30 10:23:57 +00:00
|
|
|
|
DepthwiseConv(size, in=>mul)
|
|
|
|
|
DepthwiseConv(size, in=>mul, relu)
|
|
|
|
|
|
|
|
|
|
Depthwise convolutional layer. `size` should be a tuple like `(2, 2)`.
|
|
|
|
|
`in` and `mul` specify the number of input channels and channel multiplier respectively.
|
2018-06-09 05:32:15 +00:00
|
|
|
|
In case the `mul` is not specified it is taken as 1.
|
2018-05-30 10:23:57 +00:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
Takes the keyword arguments `pad` and `stride`.
|
|
|
|
|
"""
|
|
|
|
|
struct DepthwiseConv{N,F,A,V}
|
|
|
|
|
σ::F
|
|
|
|
|
weight::A
|
|
|
|
|
bias::V
|
|
|
|
|
stride::NTuple{N,Int}
|
|
|
|
|
pad::NTuple{N,Int}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
DepthwiseConv(w::AbstractArray{T}, b::AbstractVector{T}, σ = identity;
|
|
|
|
|
stride = 1, pad = 0) where T =
|
|
|
|
|
DepthwiseConv(σ, w, b, stride, pad)
|
|
|
|
|
|
2018-06-09 05:32:15 +00:00
|
|
|
|
DepthwiseConv(k::NTuple{N,Integer}, ch::Integer, σ = identity; init = initn,
|
|
|
|
|
stride::NTuple{N,Integer} = map(_->1,k),
|
|
|
|
|
pad::NTuple{N,Integer} = map(_->0,k)) where N =
|
|
|
|
|
DepthwiseConv(param(init(k..., 1, ch)), param(zeros(ch)), σ,
|
|
|
|
|
stride = stride, pad = pad)
|
|
|
|
|
|
2018-05-30 10:23:57 +00:00
|
|
|
|
DepthwiseConv(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ = identity; init = initn,
|
|
|
|
|
stride::NTuple{N,Integer} = map(_->1,k),
|
|
|
|
|
pad::NTuple{N,Integer} = map(_->0,k)) where N =
|
|
|
|
|
DepthwiseConv(param(init(k..., ch[2], ch[1])), param(zeros(ch[2]*ch[1])), σ,
|
|
|
|
|
stride = stride, pad = pad)
|
|
|
|
|
|
|
|
|
|
Flux.treelike(DepthwiseConv)
|
|
|
|
|
|
|
|
|
|
function (c::DepthwiseConv)(x)
|
|
|
|
|
σ, b = c.σ, reshape(c.bias, map(_->1, c.stride)..., :, 1)
|
|
|
|
|
σ.(depthwiseconv(x, c.weight, stride = c.stride, pad = c.pad) .+ b)
|
|
|
|
|
end
|
|
|
|
|
|
2018-06-09 05:32:15 +00:00
|
|
|
|
function Base.show(io::IO, l::DepthwiseConv)
|
2018-05-30 10:23:57 +00:00
|
|
|
|
print(io, "DepthwiseConv(", size(l.weight)[1:ndims(l.weight)-2])
|
|
|
|
|
print(io, ", ", size(l.weight, ndims(l.weight)), "=>", size(l.weight, ndims(l.weight)-1))
|
|
|
|
|
l.σ == identity || print(io, ", ", l.σ)
|
|
|
|
|
print(io, ")")
|
|
|
|
|
end
|
|
|
|
|
|
2018-02-26 22:43:07 +00:00
|
|
|
|
# v0.5
|
|
|
|
|
@deprecate Conv2D(args...; kw...) Conv(args...; kw...)
|