Giter Club home page Giter Club logo

torch-dct's Introduction

DCT (Discrete Cosine Transform) for pytorch

Build Status codecov PyPI version PyPI version PyPI status GitHub license

This library implements DCT in terms of the built-in FFT operations in pytorch so that back propagation works through it, on both CPU and GPU. For more information on DCT and the algorithms used here, see Wikipedia and the paper by J. Makhoul. This StackExchange article might also be helpful.

The following are currently implemented:

  • 1-D DCT-I and its inverse (which is a scaled DCT-I)
  • 1-D DCT-II and its inverse (which is a scaled DCT-III)
  • 2-D DCT-II and its inverse (which is a scaled DCT-III)
  • 3-D DCT-II and its inverse (which is a scaled DCT-III)

Install

pip install torch-dct

Requires torch>=0.4.1 (lower versions are probably OK but I haven't tested them).

You can run test by getting the source and run pytest. To run the test you also need scipy installed.

Usage

import torch
import torch_dct as dct

x = torch.randn(200)
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

dct.dct1 and dct.idct1 are for DCT-I and its inverse. The usage is the same.

Just replace dct and idct by dct_2d, dct_3d, idct_2d, idct_3d, etc to get the multidimensional versions.

torch-dct's People

Contributors

adam-dziedzic avatar gngdb avatar jbojar avatar professorsearcher avatar travishsu avatar zh217 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

torch-dct's Issues

numerical precision

assert (torch.abs(x - y)).sum() < 1e-10 # x == y within numerical tolerance
numerical tolerance error cannot satisfy 1e-10, far beyond this

Segmantation Fault (core dumped)

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?

module 'torch' has no attribute 'rfft'

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!

These results are different from those with cv2.dft

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.

4D DCT?

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.

numerical precision

assert (torch.abs(x - y)).sum() < 1e-10 # x == y within numerical tolerance
numerical tolerance error cannot satisfy 1e-10, far beyond this

Issue in torch 1.7.1

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

x = torch.randn(200)
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

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

rfft warning

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)

The last dimensions must be the same ???

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

DCT结果貌似不太一样呀

刚刚使用dct_2d函数试了一下,结果和cv2的dct()函数结果出入较大。手动计算了DC系数,手算的结果和cv2.dct()是吻合的,但是与dct_2d()的DC系数相差较大,其它系数暂未验证。请问您遇到过这样的情况吗

torch. rfft is not the right implementation of DCT

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.

DCT with Makhoul's method might be slower than Linear

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.

Incompatible with torch.compile

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.

not equal with scipy.fftpack.dct

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.

require_grad or requires_grad

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?

self.weight.require_grad = False # don't learn this!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.