我有一堆图像(3D数组),我想提高其分辨率(上采样)。我运行以下代码片段,发现有点慢...
有什么方法可以提高这段代码的速度吗?(不使用多重处理)
using BenchmarkTools
using Interpolations
function doInterpol(arr::Array{Int, 2}, h, w)
A = interpolate(arr, BSpline(Linear()))
return A[1:2/(h-1)/2:2, 1:2/(w-1)/2:2]
end
function applyResize!(arr3D_hd::Array, arr3D_ld::Array, t::Int, h::Int, w::Int)
for i = 1:1:t
@inbounds arr3D_hd[i, :, :] = doInterpol(arr3D_ld[i, :, :], h, w)
end
end
t, h, w = 502, 65, 47
h_target, w_target = 518, 412
arr3D_ld = reshape(collect(1:t*h*w), (t, h, w))
arr3D_hd = Array{Float32}(undef, t, h_target, w_target)
applyResize!(arr3D_hd, arr3D_ld, t, h_target, w_target)
当我进行以下基准测试时:
@btime applyResize!(arr3D_hd, arr3D_ld, t, h_target, w_target)
我有 :
2.334 s (68774 allocations: 858.01 MiB)
我多次运行它,结果间隔为[1.8s-2.8s]。
朱莉娅以列优先顺序存储数组。这意味着像这样的片的arr[i, : ,:]
性能要比arr[:,:,i]
(在内存中是连续的)差得多。因此,提高速度的一种方法是使用(h,w,t)
而不是索引数组(t, w, h)
。
第二个问题是像切片一样arr[i,:,:]
复制数据。它的影响似乎可以忽略不计,但是养成尽可能使用数组视图而不是切片的习惯可能是个好习惯。视图是一个小的包装对象,其行为与较大数组的一部分相同,但不保存数据副本:它直接访问父数组的数据(请参见下面的示例,也许可以更好地理解一个视图是)。
请注意,Julia性能提示中提到了这两个问题;阅读本页面中的其他建议可能会很有用。
放在一起,您的示例可以重写为:
function applyResize2!(arr3D_hd::Array, arr3D_ld::Array, h::Int, w::Int, t)
@inbounds for i = 1:1:t
A = interpolate(@view(arr3D_ld[:, :, i]), BSpline(Linear()))
arr3D_hd[:, :, i] .= A(1:2/(h-1)/2:2, 1:2/(w-1)/2:2)
end
end
用于存储的数组与您的情况有些不同:
# Note the order of indices
julia> arr3D_ld = reshape(collect(1:t*h*w), (h, w, t));
julia> arr3D_hd = Array{Float32}(undef, h_target, w_target, t);
# Don't forget to escape arguments with a $ when using btime
# (not really an issue here, but could have been one)
julia> @btime applyResize2!($arr3D_hd, $arr3D_ld, h_target, w_target, t)
506.449 ms (6024 allocations: 840.11 MiB)
原始代码的速度大约提高了3.4倍,原始代码在我的计算机上像这样进行基准测试:
julia> arr3D_ld = reshape(collect(1:t*h*w), (t, h, w));
julia> arr3D_hd = Array{Float32}(undef, t, h_target, w_target);
julia> @btime applyResize!($arr3D_hd, $arr3D_ld, t, h_target, w_target)
1.733 s (50200 allocations: 857.30 MiB)
注意:您的原始代码使用的语法类似于A[x, y]
获取内插值。似乎已不赞成使用A(x, y)
。不过,我的版本可能Interpolations
与您不同。
示例说明视图的行为
julia> a = rand(3,3)
3×3 Array{Float64,2}:
0.042097 0.767261 0.0433798
0.791878 0.764044 0.605218
0.332268 0.197196 0.722173
julia> v = @view(a[:,2]) # creates a view instead of a slice
3-element view(::Array{Float64,2}, :, 2) with eltype Float64:
0.7672610491393876
0.7640443797187411
0.19719581867637093
julia> v[3] = 42 # equivalent to a[3,2] = 42
42
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句