zh217 / torch-dct

DCT (discrete cosine transform) functions for pytorch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

module 'torch' has no attribute 'rfft'

aoliao12138 opened this issue · comments

When I try to run the example, I get a runtime error

>>> X = dct.dct(x)   # DCT-II done through the last dimension
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/torch_dct/_dct.py", line 48, in dct
    Vc = torch.rfft(v, 1, onesided=False)
AttributeError: module 'torch' has no attribute 'rfft'

my torch version is '1.10.2+cu113'. Did I miss something?

Also, I want to ask what is scaled DCT? Does the input of scaled DCT need to be in range [0,1]?

Thank you very much!

commented

'torch.rfft' is removed after torch1.9

if you can read Chinese, here is the solution:
https://aitechtogether.com/article/37264.html

Found the solution hidden in another post: #15 (comment)

To migrate the DCT code to the newest pytorch version, you only have to change two lines; one in dct, one in idct.

dct:

#Vc = torch.rfft(v, 1, onesided=False)           # comment this line
Vc = torch.view_as_real(torch.fft.fft(v, dim=1))  # add this line

idct:

# v = torch.irfft(V, 1, onesided=False)                             # comment this line
v= torch.fft.irfft(torch.view_as_complex(V), n=V.shape[1], dim=1)   # add this line

Took me wayyy too long to figure out. Hope it helps you!

Thanks for the solution @MaloShady .
But I have a question.
In dct1():
return torch.rfft(torch.cat([x, x.flip([1])[:, 1:-1]], dim=1), 1)[:, :, 0].view(*x_shape).
I think new torch use fft to replace rfft, so we need change torch.rfft to torch.fft.rfft.
If we don't use the dct1() and its inverse, there will be no bug report for this one.

This can be closed now. Fixed in PR #24