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

slow JPEG loading #960

Closed
Sixzero opened this issue Apr 30, 2021 · 4 comments
Closed

slow JPEG loading #960

Sixzero opened this issue Apr 30, 2021 · 4 comments

Comments

@Sixzero
Copy link

Sixzero commented Apr 30, 2021

Image reading, specifically ".jpg" is pretty slow, even a workaround with python opencv outperforms the simple load function by 10x.

using PyCall
cv2 = pyimport("cv2")
img_path = "testimg.jpg"
py_load(f) = begin
	imgpy::Array{UInt8, 3} = cv2[:imread](f)
	arr = Matrix{RGB{N0f8}}(undef, size(imgpy)[1:2]...)
	n_img = N0f8.(imgpy./255)
	@inbounds for i in 1:size(arr, 1)
		for j in 1:size(arr, 2)
			arr[i, j] = RGB{N0f8}(n_img[i,j,3], n_img[i,j,2], n_img[i,j,1])
		end
	end
	arr
end

@btime load(img_path)
#  1.443 s (48000093 allocations: 1.64 GiB)
@btime py_load(img_path)
#  111.280 ms (40 allocations: 34.33 MiB)

Am I missing something, or simple ".jpg" loads are implemented inefficiently?

I am on julia1.7 nightly, and just installed Images.jl package to test speeds.
I saw other related issues #747 and many others, but they got closed with minor improvements.

@johnnychen94
Copy link
Member

Currently jpg is loaded via the Julia wrapper of ImageMagick.jl. I don't know if it's the Julia wrapper that makes it slow, or the ImageMagick itself that is slow.

For other commonly used formats such as png, tif, they're supported by ImageIO, where you can find the benchmark in JuliaIO/ImageIO.jl#21

@Sixzero
Copy link
Author

Sixzero commented Apr 30, 2021

Interesting I would like to know how you could find the function which is getting called, I did a small research with @edit calls, but didn't get too far.
I am pretty sure using pycall for img load is just considered a bad practice, so the above soultion should not be implemented in this lib, but we should strive for better solutions. I read there is libjpg and turbo-libjpg, I would like to know how to implement and use such a solution. :)

@johnnychen94
Copy link
Member

I read there is libjpg and turbo-libjpg, I would like to know how to implement and use such a solution.

There're approximately three steps:

  1. cross-compiling the C library (say, libjpeg-turbo) for multiple targets using https://github.com/JuliaPackaging/BinaryBuilder.jl, register the compiling scripts in https://github.com/JuliaPackaging/Yggdrasil and make a libjpeg-turbo_jll release.
  2. write the Julia part of the wrapper, say, JPEGFiles.jl, and release it
  3. register in FileIO (and in ImageIO) so that JPEGFiles has higher priority when users call load("xxx.jpg").

As an example, you can find how png files are supported via libpng_jll build script, libpng_jll and PNGFiles.jl. @IanButterworth (who wrote PNGFiles) knows more than I do so I believe he can provide more help here.

@giordano
Copy link

giordano commented May 1, 2021

  1. has been already done long time ago: https://github.com/JuliaBinaryWrappers/JpegTurbo_jll.jl

@johnnychen94 johnnychen94 changed the title Slow image loading times, even python opencv workaround is faster. slow JPEG loading Jul 3, 2021
@Sixzero Sixzero closed this as completed Nov 15, 2021
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

3 participants