-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
[WIP] Make ImageFiltering.imfilter
pipeline differentiable
#21
Open
SomTambe
wants to merge
4
commits into
main
Choose a base branch
from
som/kernels
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
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
using ImageFiltering | ||
using ImageFiltering.TiledIteration | ||
using ImageFiltering: imfilter, | ||
imfilter!, | ||
default_resource, | ||
alg_defaults, | ||
Alg, | ||
ProcessedKernel, | ||
AbstractBorder, | ||
__imfilter_inbounds!, | ||
safe_for_prod, | ||
copydata!, | ||
factorkernel, | ||
factorstridedkernel, | ||
padarray, | ||
filter_algorithm | ||
|
||
function ChainRulesCore.rrule(::typeof(imfilter!), out::AbstractArray, | ||
img::AbstractArray, | ||
kernel::ProcessedKernel, | ||
border::AbstractBorder, | ||
alg::Alg) | ||
# imfilter! places a snag because of a try catch block. | ||
y = imfilter!(out, img, kernel, border, alg) | ||
function ∇imfilter!_try(Δy) | ||
k = default_resource(alg_defaults(alg, out, kernel)) | ||
ret = imfilter!(k, out, img, kernel, border) | ||
_, ∇ret = rrule_via_ad(Zygote.ZygoteRuleConfig(), imfilter!, k, out, img, kernel, border) | ||
∇k, ∇out, ∇img, ∇kernel, ∇border = ∇ret(Δy) | ||
return NoTangent(), ∇out, ∇img, ∇kernel, ∇border, ∇k | ||
end | ||
return y, ∇imfilter!_try | ||
end # needed and works | ||
|
||
## writing adjoints for `__imfilter_inbounds!` -> where the mutation takes place | ||
function ChainRulesCore.rrule(::typeof(__imfilter_inbounds!), r, | ||
out, | ||
A::OffsetArray, | ||
kern::OffsetArray, | ||
border, | ||
R, | ||
z) | ||
y = __imfilter_inbounds!(r, out, A, kern, border, R, z) | ||
function ∇__imfilter_inbounds!(Δy) | ||
# ∇out should not have any gradients | ||
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. Since it will be used further down in the computation pipeline, i think you still need to provide it the gradient so it can be accumulated properly. Else the answer would be incorrect |
||
# since it is just being alloted the values | ||
# after processing. ∇border also should not have | ||
# gradients since it does not make sense (for now). | ||
∇out = NoTangent() | ||
∇border = NoTangent() | ||
|
||
# Don't exactly know what r, R and z are actually. | ||
|
||
off, k = CartesianIndex(kern.offsets), parent(kern) | ||
o, O = safehead(off), safetail(off) | ||
Rnew = CartesianIndices(map((x,y)->x.+y, R.indices, Tuple(off))) | ||
Rk = CartesianIndices(axes(k)) | ||
offA, pA = CartesianIndex(A.offsets), parent(A) | ||
oA, OA = safehead(offA), safetail(offA) | ||
# ∇A, ∇kern should have some values. | ||
∇A = 0 | ||
∇kern = 0 # since k is not an OffsetArray | ||
|
||
for I in safetail(Rnew) | ||
IA = I-OA | ||
for i in safehead(Rnew) | ||
tmp = z | ||
iA = i-oA | ||
dk = zeros(eltype(k), size(k)) | ||
dA = zeros(eltype(pA), size(pA)) | ||
@inbounds for J in safetail(Rk), j in safehead(Rk) | ||
_, ∇prod = rrule_via_ad(Zygote.ZygoteRuleConfig(), (a, b, c) -> safe_for_prod(a, b) * c, | ||
pA[iA+j, IA+J], | ||
tmp, | ||
k[j, J]) | ||
dA_j_J, _, dk_j_J = ∇prod(Δy[iA+j, IA+J]) | ||
dA[iA+j, IA+J] += dA_j_J | ||
dk[j+J] += dk_j_J | ||
end | ||
∇A += dA | ||
∇kern += dk | ||
end | ||
end | ||
∇z = NoTangent() | ||
∇R = NoTangent() | ||
∇r = NoTangent() | ||
|
||
return NoTangent(), ∇r, ∇out, ∇A, ∇kern, ∇border, ∇R, ∇z | ||
end | ||
return y, ∇__imfilter_inbounds! | ||
end | ||
|
||
Zygote.@nograd TiledIteration.TileBuffer # needed, works | ||
# Zygote.@nograd ImageFiltering.padindices # not needed | ||
Zygote.@nograd ImageFiltering.filter_algorithm # ~~should be correct~~ is correct | ||
Zygote.@nograd ImageFiltering.Pad{N} where N | ||
|
||
# what should the gradient of copyto! be? It is being used in various places throughout the filters | ||
|
||
function ChainRulesCore.rrule(::typeof(padarray), t::Type{T}, img::AbstractArray, border) where T | ||
y = padarray(t, img, border) | ||
function padarray_pb(Δy) | ||
ba, ba_pb = rrule_via_ad(Zygote.ZygoteRuleConfig(), BorderArray, img, border) | ||
out = similar(ba, T, axes(ba)) | ||
copy!(out, ba) | ||
∇img, ∇border = ba_pb(Δy) | ||
return NoTangent(), NoTangent(), ∇img, ∇border | ||
end | ||
return y, padarray_pb | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(factorkernel), kernel::AbstractMatrix{T}) where T | ||
y = factorkernel(kernel) | ||
function factorkernel_pb(Δy) | ||
## | ||
inds = axes(kernel) | ||
m, n = map(length, inds) | ||
kern = Array{T}(undef, m, n) | ||
copyto!(kern, 1:m, 1:n, kernel, inds[1], inds[2]) | ||
## | ||
_, kernel_pb = rrule_via_ad(Zygote.ZygoteRuleConfig(), factorstridedkernel, inds, kern) | ||
|
||
return NoTangent(), kernel_pb(Δy) | ||
end | ||
return y, factorkernel_pb | ||
end | ||
|
||
# function ChainRulesCore.rrule(::typeof(copydata!), dest::OffsetArray, img, inds::Tuple{Vararg{OffsetArray}}) | ||
# y = copydata!(dest, img, inds) | ||
# function copydata!_pb(Δy) | ||
# @show typeof(Δy) | ||
# println("copydata! here") | ||
# # dest = parent(dest) | ||
# # inds = map(parent, inds) | ||
# # if isempty(img) | ||
# # ∇img = canonicalize(Tangent{typeof(img)}()) | ||
# # else | ||
# # ∇img = Tangent{typeof(img)}(;ones(eltype(img), size(img))) | ||
# # end | ||
# return NoTangent(), NoTangent(), Δy, NoTangent() | ||
# end | ||
# return y, copydata!_pb | ||
# end | ||
|
||
## ~~make copyto! gradients correct~~ final task | ||
|
||
## it is still not getting inside the final mutation loop adjoint, figure that out asap. |
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.
Is this true generically?