-
-
Notifications
You must be signed in to change notification settings - Fork 608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2D Conv transpose support #311
Merged
Merged
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d5d9441
make docs on 1.0
MikeInnes e86365e
1.0 fix for conv transpose
tejank10 e9bf86d
Merge branch 'master' of https://github.com/FluxML/Flux.jl into conv_…
tejank10 f540a0d
merge with upstream
tejank10 0dc4ec4
conv_data grad api change
tejank10 fca9347
in accordance with conv_data api
tejank10 387df8c
conv_filter api changes
tejank10 a657c28
in accordance with conv_filter api
tejank10 a71ee38
1.0 fix for conv transpose
tejank10 9c3e34b
conv_data grad api change
tejank10 ca8ad63
in accordance with conv_data api
tejank10 10f3a8e
conv_filter api changes
tejank10 bc9bda9
in accordance with conv_filter api
tejank10 89f2709
resolved conflicts
tejank10 95e490a
merge conflict resolved
tejank10 519c3db
clean code
tejank10 1648414
fixes for layer and test
tejank10 ed835f2
printing ConvTranspose layer
tejank10 e54df2d
Merge branch 'master' into conv_transpose
tejank10 84eabcd
fixed DepthwiseConv dilation
tejank10 cc4438c
Update NNlib to master in Manofest
tejank10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using NNlib: conv, depthwiseconv | ||
using NNlib: conv, ∇conv_data, depthwiseconv | ||
|
||
@generated sub2(::Val{N}) where N = :(Val($(N-2))) | ||
|
||
|
@@ -57,6 +57,47 @@ end | |
(a::Conv{<:Any,<:Any,W})(x::AbstractArray{<:Real}) where {T <: Union{Float32,Float64}, W <: AbstractArray{T}} = | ||
a(T.(x)) | ||
|
||
""" | ||
ConvTranspose(size, in=>out) | ||
ConvTranspose(size, in=>out, relu) | ||
|
||
Standard convolutional transpose layer. `size` should be a tuple like `(2, 2)`. | ||
`in` and `out` specify the number of input and output channels respectively. | ||
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`, `stride` and `dilation`. | ||
""" | ||
struct ConvTranspose{N,F,A,V} | ||
σ::F | ||
weight::A | ||
bias::V | ||
stride::NTuple{N,Int} | ||
pad::NTuple{N,Int} | ||
dilation::NTuple{N,Int} | ||
end | ||
|
||
ConvTranspose(w::AbstractArray{T,N}, b::AbstractVector{T}, σ = identity; | ||
stride = 1, pad = 0, dilation = 1) where {T,N} = | ||
ConvTranspose(σ, w, b, expand.(sub2(Val(N)), (stride, pad, dilation))...) | ||
|
||
ConvTranspose(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ = identity; init = initn, | ||
stride = 1, pad = 0, dilation = 1) where N = | ||
ConvTranspose(param(init(k..., reverse(ch)...)), param(zeros(ch[2])), σ, | ||
stride = stride, pad = pad, dilation = dilation) | ||
|
||
@treelike ConvTranspose | ||
|
||
function (c::ConvTranspose)(x) | ||
# ndims(x) == ndims(c.weight)-1 && return squeezebatch(c(reshape(x, size(x)..., 1))) | ||
σ, b = c.σ, reshape(c.bias, map(_->1, c.stride)..., :, 1) | ||
σ.(∇conv_data(x, c.weight, stride = c.stride, pad = c.pad, dilation = c.dilation) .+ b) | ||
end | ||
|
||
function Base.show(io::IO, l::ConvTranspose) | ||
print(io, "ConvTranspose(", size(l.weight)[1:ndims(l.weight)-2]) | ||
end | ||
|
||
|
||
""" | ||
DepthwiseConv(size, in) | ||
DepthwiseConv(size, in=>mul) | ||
|
@@ -77,6 +118,7 @@ struct DepthwiseConv{N,F,A,V} | |
bias::V | ||
stride::NTuple{N,Int} | ||
pad::NTuple{N,Int} | ||
dilation::NTuple{N,Int} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like perhaps an unrelated change? |
||
end | ||
|
||
DepthwiseConv(w::AbstractArray{T,N}, b::AbstractVector{T}, σ = identity; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
using Flux | ||
using Flux.Tracker, Test, NNlib | ||
<<<<<<< HEAD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge artificat |
||
using Flux.Tracker: TrackedReal, gradcheck, grad, checkpoint | ||
using NNlib: conv, depthwiseconv | ||
======= | ||
using Flux.Tracker: TrackedReal, gradcheck, grad, derivative, checkpoint | ||
>>>>>>> a657c287d0590fdd9e49bb68c35bf96febe45e6d | ||
using NNlib: conv, ∇conv_data, depthwiseconv | ||
using Printf: @sprintf | ||
using LinearAlgebra: diagm, dot, LowerTriangular, norm | ||
using Statistics: mean, std | ||
|
@@ -182,12 +186,20 @@ end | |
2y + x | ||
end | ||
|
||
@test gradtest(conv, rand(10, 3, 2), randn(Float64,2, 3, 2)) | ||
@test gradtest(conv, rand(10, 10, 3, 2), randn(Float64,2, 2, 3, 2)) | ||
@test gradtest(conv, rand(10, 10, 10, 3, 2), randn(Float64,2, 2, 2, 3, 2)) | ||
@test gradtest(conv, rand(10, 3, 2), randn(Float64, 2, 3, 2)) | ||
@test gradtest(conv, rand(10, 10, 3, 2), randn(Float64, 2, 2, 3, 2)) | ||
@test gradtest(conv, rand(10, 10, 10, 3, 2), randn(Float64, 2, 2, 2, 3, 2)) | ||
|
||
@test gradtest(∇conv_data, rand(10, 3, 2), randn(Float64, 2, 2, 3)) | ||
@test gradtest(∇conv_data, rand(10, 10, 3, 2), randn(Float64,2, 2, 2, 3)) | ||
@test gradtest(∇conv_data, rand(10, 10, 10, 3, 2), randn(Float64,2, 2, 2, 2, 3)) | ||
|
||
@test gradtest(depthwiseconv, rand(10,10,3,2), randn(2, 2, 2, 3)) | ||
|
||
@test gradtest(∇conv_data, rand(10, 3, 2), randn(Float64, 2, 2, 3)) | ||
@test gradtest(∇conv_data, rand(10, 10, 3, 2), randn(Float64, 2, 2, 2, 3)) | ||
@test gradtest(∇conv_data, rand(10, 10, 10, 3, 2), randn(Float64, 2, 2, 2, 2, 3)) | ||
|
||
@test gradtest(x -> maxpool(x, (2,2)), rand(10, 10, 3, 2)) | ||
@test gradtest(x -> maxpool(x, (2,2,2)), rand(10, 10, 10, 3, 2)) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing these out, I've fixed them in the latest commit.