-
-
Couldn't load subscription status.
- Fork 2
Addition of the adjoint of the fdiff function from ImageBase.jl #26
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
Open
arcAman07
wants to merge
14
commits into
FluxML:main
Choose a base branch
from
arcAman07:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7d99c31
Update adjoints.jl
arcAman07 e345063
Errored tests fixed
arcAman07 3a090d8
Fix whitespace
arcAman07 28e1da0
Merge pull request #1 from arcAman07/format-fix
arcAman07 55b9b4d
Fix whitespaces
arcAman07 f5a852b
Fix whitespaces
arcAman07 8ca8234
Merge branch 'SomTambe:main' into main
arcAman07 a379823
fdiff adjoint added
arcAman07 4ecaf4e
Merge pull request #2 from arcAman07/format-fix
arcAman07 01e033d
More tests added
arcAman07 bf70f5f
Add compat field
arcAman07 ee7834c
Updated adjoint by making it shorter
arcAman07 6ef1cb7
Remove fdiff to be exported
arcAman07 5263947
function exported
arcAman07 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # TODO(arcAman07): support RGB inputs, currently works only for GrayScale Images | ||
| # TODO(arcAman07): support N dimensional case, currently works only for 2 dimensional case | ||
| @adjoint function fdiff(A::AbstractArray; kwargs...) | ||
| y = fdiff!(similar(A, maybe_floattype(eltype(A))), A; kwargs...) | ||
| function pullback(Δ) | ||
| return (fill(Δ, size(A)),) | ||
| end | ||
| return (y, pullback) | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using ImageBase.FiniteDiff: fdiff, fdiff! | ||
| @testset "fdiff" begin | ||
| # Base.diff doesn't promote integer to float | ||
| @test ImageBase.FiniteDiff.maybe_floattype(Int) == Int | ||
| @test ImageBase.FiniteDiff.maybe_floattype(N0f8) == Float32 | ||
| @test ImageBase.FiniteDiff.maybe_floattype(RGB{N0f8}) == RGB{Float32} | ||
| @testset "NumericalTests" begin | ||
| a = reshape(collect(1:9), 3, 3) | ||
| b_fd_1 = [1 1 1; 1 1 1; -2 -2 -2] | ||
| b_fd_2 = [3 3 -6; 3 3 -6; 3 3 -6] | ||
| b_bd_1 = [-2 -2 -2; 1 1 1; 1 1 1] | ||
| b_bd_2 = [-6 3 3; -6 3 3; -6 3 3] | ||
| out = similar(a) | ||
|
|
||
| @test fdiff(a, dims=1) == b_fd_1 | ||
| @test fdiff(a, dims=2) == b_fd_2 | ||
| @test fdiff(a, dims=1, rev=true) == b_bd_1 | ||
| @test fdiff(a, dims=2, rev=true) == b_bd_2 | ||
| fdiff!(out, a, dims=1) | ||
| @test out == b_fd_1 | ||
| fdiff!(out, a, dims=2) | ||
| @test out == b_fd_2 | ||
| fdiff!(out, a, dims=1, rev=true) | ||
| @test out == b_bd_1 | ||
| fdiff!(out, a, dims=2, rev=true) | ||
| @test out == b_bd_2 | ||
| end | ||
| @testset "Differentiability" begin | ||
| a_fd_1 = [2 4 8; 3 9 27; 4 16 64] | ||
| a_fd_2 = [3 6 9; 6 18 27; 9 27 54; 12 36 81] | ||
| @testset "Testing basic fdiff" begin | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=1))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=1))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=1))[1] == ones(Float64,size(a_fd_2)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=2))[1] == ones(Float64,size(a_fd_2)) | ||
| end | ||
| @testset "Testing fdiff with rev" begin | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=1,rev=true))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=2,rev=true))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=1,rev=true))[1] == ones(Float64,size(a_fd_2)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=2,rev=true))[1] == ones(Float64,size(a_fd_2)) | ||
| end | ||
| @testset "Testing fdiff with boundary condition" begin | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=1,boundary=:periodic))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=2,boundary=:periodic))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=1,boundary=:zero))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=2,boundary=:zero))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=1,rev=true,boundary=:periodic))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=2,rev=true,boundary=:periodic))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=1,rev=true,boundary=:zero))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_1,dims=2,rev=true,boundary=:zero))[1] == ones(Float64,size(a_fd_1)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=1,boundary=:periodic))[1] == ones(Float64,size(a_fd_2)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=2,boundary=:periodic))[1] == ones(Float64,size(a_fd_2)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=1,boundary=:zero))[1] == ones(Float64,size(a_fd_2)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=2,boundary=:zero))[1] == ones(Float64,size(a_fd_2)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=1,rev=true,boundary=:periodic))[1] == ones(Float64,size(a_fd_2)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=2,rev=true,boundary=:periodic))[1] == ones(Float64,size(a_fd_2)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=1,rev=true,boundary=:zero))[1] == ones(Float64,size(a_fd_2)) | ||
| @test Zygote.gradient(x -> sum(x),fdiff(a_fd_2,dims=2,rev=true,boundary=:zero))[1] == ones(Float64,size(a_fd_2)) | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.