zh217 / torch-dct Goto Github PK
View Code? Open in Web Editor NEWDCT (discrete cosine transform) functions for pytorch
License: MIT License
DCT (discrete cosine transform) functions for pytorch
License: MIT License
assert (torch.abs(x - y)).sum() < 1e-10 # x == y within numerical tolerance
numerical tolerance error cannot satisfy 1e-10, far beyond this
Hi,
Is there any hope of adding a 4D version, as well? I want to run experiments in encoding 2D-convolution layers as a subset of 4D DCT coefficients.
Hi @zh217,
I just installed torch-dct and tried the test on the README page. I am getting an AssertionError. The value of (torch.abs(x - y)).sum() is small though: 3.0896e-05.
Any idea what maybe the issue? Happy to help. Thanks
AssertionError Traceback (most recent call last)
in
X = dct.dct(x) # DCT-II done through the last dimension
y = dct.idct(X) # scaled DCT-III done through the last dimension
assert (torch.abs(x - y)).sum() < 1e-10 # x == y within numerical tolerance
Hi,
I read the docs of torch. rfft. It says that torch. rfft is implementation of FFT. But FFT is not dct. In your code you use torch. rfft to implement dct. Do you think your code is right?
Thanks.
in https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.fftpack.dct.html, it has parameter "n" which is described as optional Length of the transform. I want to use this parameter but this package's dct function seems to have no such parameter. Is there any possible way to use this parameter or should it be reimplemented?
刚刚使用dct_2d函数试了一下,结果和cv2的dct()函数结果出入较大。手动计算了DC系数,手算的结果和cv2.dct()是吻合的,但是与dct_2d()的DC系数相差较大,其它系数暂未验证。请问您遇到过这样的情况吗
I was working on using your DCT implementation to implement ACDC layers, and I found that the DCT implementation you have here is slower than using a Linear layer with the DCT matrix. The code in my fork doing the timing testing is here: https://github.com/BayesWatch/torch-dct/blob/master/torch_dct/_dct.py#L233-L259
I found it could be made a bit faster by using the 2N Makhoul method (and also storing the scaling, but that didn't make much difference): https://github.com/BayesWatch/torch-dct/blob/master/torch_dct/_dct.py#L64-L98
I could open a pull request with just the 2N Makhoul method if you like, otherwise I just wanted to let you know.
def test_dct_3d():
for N1 in [2, 5, 32]:
x = np.random.normal(size=(1, N1, N1, N1))
ref = fftpack.dct(x, axis=3, type=2)
ref = fftpack.dct(ref, axis=2, type=2)
ref = fftpack.dct(ref, axis=1, type=2)
act = dct_3d(torch.tensor(x).float()).numpy()
assert np.abs(ref - act).max() < EPS, (ref, act)
def test_idct_3d():
for N1 in [2, 5, 32]:
x = np.random.normal(size=(1, N1, N1, N1))
X = dct_3d(torch.tensor(x).float())
y = idct_3d(X).numpy()
assert np.abs(x - y).max() < EPS, x
In the above code, the last three dims are the same. If they are different, met with a problem:
RuntimeError: size mismatch
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!
input image "im_gray" of uint8 and size of [256,256], then i made following operations with opencv and torch-dct, respectively:
a = cv2.dct(im_gray.astype("float"))
b = torch_dct.dct_2d(torch.from_numpy(im_gray.astype("float"))).
But a and b are greatly different.
The function dct is not equal with scipy.fftpack.dct.
I assume that scipy.fftpack.dct is correct, since scipy.fftpack.dct is equal with matlab's dct.
assert (torch.abs(x - y)).sum() < 1e-10 # x == y within numerical tolerance
numerical tolerance error cannot satisfy 1e-10, far beyond this
I am a new learner of discrete cosine transform. Can you tell me what is the difference between DCT-I and DCT-II?
Could please tell me how can i apply LinearDCT which is faster in gpu on a batch of images ?
The current module is incompatible in my projects with torch.compile
. I was able to resolve the error by changing the np.pi
calls to torch.tensor(math.pi)
and convert all numpy
operations to torch
operations.
X = dct.dct(x)
UserWarning: The function torch.rfft is deprecated and will be removed in a future PyTorch release. Use the new torch.fft module functions, instead, by importing torch.fft and calling torch.fft.fft or torch.fft.rfft. (Triggered internally at /pytorch/aten/src/ATen/native/SpectralOps.cpp:590.)
Vc = torch.rfft(v, 1, onesided=False)
When I import and use this package, it sometimes appears the segmentation fault (core dumped)problem. Has any one meet this kind of problem before?
Hi, thanks for this great Pytorch version of DCT, it helps me and our team a lot.
I'm not quite familiar with autograd mechanism and not sure what the difference between require_grad and requires_grad is, are they the same thing but different variable name in different pytorch version? or is it a typo?
Line 202 in b6823ea
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.