Skip to content
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

imfilter "plan" ? #219

Open
JeffFessler opened this issue Jun 22, 2021 · 7 comments
Open

imfilter "plan" ? #219

JeffFessler opened this issue Jun 22, 2021 · 7 comments

Comments

@JeffFessler
Copy link
Contributor

The imfilter docstring implies that the choice of FFT or FIR algorithm "will be chosen based
on the size of the image and kernel"

(Fourier-based filtering). If no choice is specified, one will be chosen based

but I've been hunting and cannot find that decision point in the code. Any hints?

Now on to the "plan" question. I need to apply filters of the same size to many images and my filter is big enough (e.g., 27 by 27) that I suspect that it will end up using the FFT algorithm. FFTW supports use of a plan to optimize performance:
https://juliamath.github.io/AbstractFFTs.jl/stable/api/#AbstractFFTs.plan_fft
It might be natural then for imfilter to have something akin to a "plan" mode where one initially provides the image size and filter size and the decisions about FFT and FIR are made in advance, along with the FFT plan if relevant and desired. Then subsequent imfilter! calls with that plan are as efficient as possible.

Any thoughts about this?
I might be willing to try it if it sounds useful, but first I need to a pointer to the code that decides FFT vs FIR...

@johnnychen94
Copy link
Member

but I've been hunting and cannot find that decision point in the code. Any hints?

function filter_algorithm(out, img, kernel::Union{ArrayType,Tuple{Vararg{ArrayType}}})
L = maxlen(kernel)
if L > 30 && eltype(img) <: Union{Number,Colorant} && all(isfinite, img)
return FFT()
end
sz = map(length, calculate_padding(kernel))
isa(kernel, Tuple) && length(kernel) > 1 ? FIRTiled(padded_tilesize(eltype(out), sz)) : FIR()
end
filter_algorithm(out, img, kernel::Tuple{AnyIIR,Vararg{AnyIIR}}) = IIR()
filter_algorithm(out, img, kernel) = Mixed()

Looks like you can try this API:

function imfilter!(out::AbstractArray, img::AbstractArray, kernel::ProcessedKernel, border::AbstractBorder, alg::Alg)

@JeffFessler
Copy link
Contributor Author

Thanks! I was looking for size and couldn't find anything relevant...

So the docstring is not quite accurate then:

on the size of the image and kernel in a way that strives to deliver good

because the choice is based solely on the kernel size, not on the image size.
I might submit a PR to tweak the docstring.

@IanButterworth
Copy link
Member

IanButterworth commented Jan 8, 2024

@johnnychen94 it's not quite clear to me how to reuse an fft plan (the 2nd part of the OP).

Basically, I have a bunch of same size images I want to filter with the same kernel, and I want to avoid each imfilter! calling FFTW.plan_rfft because it has a lock because FFTW planning isn't threadsafe. That way we can multithread via julia.

cc. @Octogonapus

@timholy
Copy link
Member

timholy commented Jan 8, 2024

@IanButterworth
Copy link
Member

Thanks. Though I'm looking for a way to do so through imfilter!

@IanButterworth
Copy link
Member

If it's not currently possible we can try to look into adding that option, but I wanted to check first.

@IanButterworth
Copy link
Member

WIP #271

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants