Julia 中的fft慢的问题

对于同样的一张图片,我在matlab上进行快速傅里叶变换,只要0.08秒
image

但在Julia上,却要消耗0.5秒左右(使用的是FFTW库)
image
已经经过多次编译,时间在0.5-0.8秒直接。这是为什么,请问有什么可以优化的地方么

fftshift(fft!(fftshift(A)))

如果你能用 circshift 来算可以考虑用inplace版本的 circshift! 。MATLAB在计算的时候不会区分inplace(虽然这并不正确),所以有些inplace的函数需要你在Julia里找对应的版本(带 ! 的版本)。

注意你的图片需要转成复数。因为fft运算需要复数。

从测试来看这里面大部分时间应该是花在内存分配上了,看起来这65M的内存分配上可以优化的余地还很多

另外一个值得注意的地方是,FFTW下区分了一般的fft和对实数优化的版本rfft。对于图像类型来说用rfft就好了

我试了一下inplace版本的 发现速度会变慢 而且需要另外将图片转成复数,运行时间变长了。不过还是谢谢哈

谢谢 我有试过rfft版本 但会让最终的图片显示不完整

而且会错位。不知道是不是我的rfft使用错误 我是直接将fft换成了rfft

我后来将fft分成两步:先P = plan_fft(holo_img), 再将P与holo_img相乘 这样只要我的图片大小不变,我后面的循环就可以减少第一步的内存开销了

这是我目前想到的最优的解决方案

会让最终的图片显示不完整

ifft也需要换成irfft

img = testimage("cameraman") .|> float32
img_freq = rfft(channelview(img))
img_spatial = irfft(img, size(img, 1))

错位的话大概是因为fftshift的使用有点问题,如果可以的话请把你的代码贴出来才好判断。

如果不清楚如何贴代码的话,直接用markdown语法复制julia代码就可以了

# 大概类似于这样子(去掉#)
#```julia
# codes
#```

在利用fft做图像处理的时候,fftshift并不是一定要做的一个操作。例如imfilter的fft部分是这样写的:

FYI, FFT performance compared to MATLAB

想问一个问题,TestImages在build时出了乱码错误是怎么回事呢
���á�SecurityProtocol��ʱ�����쳣:������ö��ֵ��Ч���޷��� Null ת��Ϊ���͡�System.Net.SecurityProtocolType������ָ������ö��ֵ֮һ��Ȼ�����ԡ�
���ܵ�ö��ֵΪ��Ssl3, Tls������
����λ�� ��:1 �ַ�: 1

  • [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtoco …
  •   + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
      + FullyQualifiedErrorId : ExceptionWhenSetting
    
    

ʹ�á�2�����������á�DownloadFile��ʱ�����쳣:�����������Ѿ��ر�: ����ʱ�������󡣡�
����λ�� ��:1 �ַ�: 96

  • [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtoco …
  •   + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : WebException
    
    

[ Info: Downloading standard test images
[ Info: Downloading C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps…\images\cameraman.tif
ERROR: LoadError: failed process: Process('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Version 3 -NoProfile -Command "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; (New-Object System.Net.Webclient).DownloadFile('https://github.com/timholy/TestImages.jl/blob/gh-pages/images/cameraman.tif?raw=true', 'C:\Users\Administrator\.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\..\images\cameraman.tif')", ProcessExited(1)) [1]

Stacktrace:
[1] pipeline_error at .\process.jl:813 [inlined]
[2] download_powershell(::String, ::String) at .\download.jl:20
[3] download(::String, ::String) at .\download.jl:54
[4] top-level scope at C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\build.jl:35
[5] include at .\boot.jl:328 [inlined]
[6] include_relative(::Module, ::String) at .\loading.jl:1094
[7] include(::Module, ::String) at .\Base.jl:31
[8] include(::String) at .\client.jl:431
[9] top-level scope at none:5
in expression starting at C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\build.jl:31


┌ Error: Error building TestImages:
│ ���á�SecurityProtocol��ʱ�����쳣:������ö��ֵ��Ч���޷��� Null ת��Ϊ���͡�System.Net.SecurityProtocolType���
���ָ������ö��ֵ֮һ��Ȼ�����ԡ�
│ ���ܵ�ö��ֵΪ��Ssl3, Tls������
│ ����λ�� ��:1 �ַ�: 1
│ + [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtoco …
│ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ + CategoryInfo : NotSpecified: (:slight_smile: [], SetValueInvocationException
│ + FullyQualifiedErrorId : ExceptionWhenSetting

│ ʹ�á�2���������á�DownloadFile��ʱ�����쳣:����l���Ѿ��ر�: ����ʱ��������
│ ����λ�� ��:1 �ַ�: 96
│ + [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtoco …
│ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ + CategoryInfo : NotSpecified: (:slight_smile: [], MethodInvocationException
│ + FullyQualifiedErrorId : WebException

│ [ Info: Downloading standard test images
│ [ Info: Downloading C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps…\images\cameraman.tif
│ ERROR: LoadError: failed process: Process('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Version 3 -NoProfile -Command "[System. Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; (New-Object System.Net.Webclient).DownloadFile('https://github.com/timholy/TestImages.jl/blob/gh-pages/images/cameraman.tif?raw=true', 'C:\Users\Administrator\.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\..\images\cameraman.tif')", ProcessExited(1)) [1]

看起来还是网不太行

我刚才好不容易把乱码整成中文的了,
设置“SecurityProtocol”时发生异常:“由于枚举值无效,无法将 Null 转换为类型“System.Net.SecurityProtocolType”。请指定以下枚举值之一,然后重试。
可能的枚举值为“Ssl3, Tls”。”
所在位置 行:1 字符: 1

  • [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtoco …
  •   + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
      + FullyQualifiedErrorId : ExceptionWhenSetting
    
    

使用“2”个参数调用“DownloadFile”时发生异常:“基础连接已经关闭: 发送时发生错误。”
所在位置 行:1 字符: 96

  • [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtoco …
  •   + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : WebException
    
    

[ Info: Downloading standard test images
[ Info: Downloading C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps…\images\cameraman.tif
ERROR: LoadError: failed process: Process('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Version 3 -NoProfile -Command "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; (New-Object System.Net.Webclient).DownloadFile('https://github.com/timholy/TestImages.jl/blob/gh-pages/images/cameraman.tif?raw=true', 'C:\Users\Administrator\.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\..\images\cameraman.tif')", ProcessExited(1)) [1]

Stacktrace:
[1] pipeline_error at .\process.jl:813 [inlined]
[2] download_powershell(::String, ::String) at .\download.jl:20
[3] download(::String, ::String) at .\download.jl:54
[4] top-level scope at C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\build.jl:35
[5] include at .\boot.jl:328 [inlined]
[6] include_relative(::Module, ::String) at .\loading.jl:1094
[7] include(::Module, ::String) at .\Base.jl:31
[8] include(::String) at .\client.jl:431
[9] top-level scope at none:5
in expression starting at C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\build.jl:31

我换了台电脑主要是 忘记升级powershell 和 . NET FrameWork 4.0了,之前错误和这个是不一样的好像提示powershell有问题 我才想起来升级这个,但现在的错误好像和 . NET FrameWork 4.0没升级有关?
更新:好吧,现在. NET FrameWork 4.0也升级完了,还是出错了,调试信息变成这样了
使用“2”个参数调用“DownloadFile”时发生异常:“在 WebClient 请求期间发生异常。”
所在位置 行:1 字符: 96

  • [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtoco …
  •   + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : WebException
    
    

[ Info: Downloading standard test images
[ Info: Downloading C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps…\images\cameraman.tif
[ Info: Downloading C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps…\images\house.tif
ERROR: LoadError: failed process: Process('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Version 3 -NoProfile -Command "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; (New-Object System.Net.Webclient).DownloadFile('https://github.com/timholy/TestImages.jl/blob/gh-pages/images/house.tif?raw=true', 'C:\Users\Administrator\.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\..\images\house.tif')", ProcessExited(1)) [1]

Stacktrace:
[1] pipeline_error at .\process.jl:813 [inlined]
[2] download_powershell(::String, ::String) at .\download.jl:20
[3] download(::String, ::String) at .\download.jl:54
[4] top-level scope at C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\build.jl:35
[5] include at .\boot.jl:328 [inlined]
[6] include_relative(::Module, ::String) at .\loading.jl:1094
[7] include(::Module, ::String) at .\Base.jl:31
[8] include(::String) at .\client.jl:431
[9] top-level scope at none:5
in expression starting at C:\Users\Administrator.juliapro\JuliaPro_v1.2.0-2\packages\TestImages\Aoq3X\deps\build.jl:31

下载东西时出错,应该挂个代理就好了

哎,后来挂了一下,没用。。。而且下载其他包都没问题

京ICP备17009874号-2