Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting the coefficients #99

Open
rachelglenn opened this issue Jul 8, 2024 · 3 comments
Open

Extracting the coefficients #99

rachelglenn opened this issue Jul 8, 2024 · 3 comments
Assignees

Comments

@rachelglenn
Copy link

Hi. I am trying to pull out the coefficients as a matrix. Similar to pywt. How do I do that (last line)?

import ptwt, pywt, torch
import numpy as np
import scipy.misc

face = np.transpose(scipy.datasets.face(),
                        [2, 0, 1]).astype(np.float64)
pytorch_face = torch.tensor(face)
coefficients = ptwt.wavedec2(pytorch_face, pywt.Wavelet("haar"),
                                level=2, mode="constant")
reconstruction = ptwt.waverec2(coefficients, pywt.Wavelet("haar"))
np.max(np.abs(face - reconstruction.squeeze(1).numpy()))


cA, (cH, cV, cD) = coefficients 
@v0lta
Copy link
Owner

v0lta commented Jul 9, 2024

Dear @rachelglenn ,
You are probably getting a ValueError: too many values to unpack (expected 2) because the code assigns a coefficient tuple of length 3 to an expression of length 2.

You have two options:

  1. You can add another detail tuple:
import ptwt, pywt, torch
import numpy as np
import scipy.misc

face = np.transpose(scipy.datasets.face(),
                        [2, 0, 1]).astype(np.float64)
pytorch_face = torch.tensor(face)
coefficients = ptwt.wavedec2(pytorch_face, pywt.Wavelet("haar"),
                                level=2, mode="constant")
reconstruction = ptwt.waverec2(coefficients, pywt.Wavelet("haar"))
np.max(np.abs(face - reconstruction.squeeze(1).numpy()))

(cA2, (cH2, cV2, cD2), (cH1, cV1, cD1)) = coefficients 
  1. You can set the decomposition to 1:
import ptwt, pywt, torch
import numpy as np
import scipy.misc

face = np.transpose(scipy.datasets.face(),
                        [2, 0, 1]).astype(np.float64)
pytorch_face = torch.tensor(face)
coefficients = ptwt.wavedec2(pytorch_face, pywt.Wavelet("haar"),
                                level=1, mode="constant")
reconstruction = ptwt.waverec2(coefficients, pywt.Wavelet("haar"))
np.max(np.abs(face - reconstruction.squeeze(1).numpy()))


cA, (cH, cV, cD) = coefficients 

I hope this helps you solve your problem.

@v0lta v0lta self-assigned this Jul 9, 2024
@felixblanke
Copy link
Collaborator

I am not quite sure what you mean with "coefficients as a matrix". In case you mean the functionality of pywt.coeffs_to_array I wrote a small snippet:

import ptwt, pywt, torch
import numpy as np
import scipy.misc

def coeffs_to_array(coeffs: ptwt.WaveletCoeff2d) -> torch.Tensor:
    cA = coeffs[0]

    for detail_coeffs in coeffs[1:]:
        cH, cV, cD = detail_coeffs
        row0 = torch.cat([cA, cV], dim=-1)
        row1 = torch.cat([cH, cD], dim=-1)
        cA = torch.cat([row0, row1], dim=-2)
    return cA


face = np.transpose(scipy.datasets.[2, 0, 1]).astype(np.float64)
pytorch_face = torch.tensor(face)

coefficients = ptwt.wavedec2(pytorch_face, pywt.Wavelet("haar"), level=2, mode="constant")
coefficients_pywt = pywt.wavedec2(face, pywt.Wavelet("haar"), level=2, mode="constant")

ptwt_arr = coeffs_to_array(coefficients)
pywt_arr, _ = pywt.coeffs_to_array(coefficients_pywt, axes=(-2, -1))

assert ptwt_arr.shape == pywt_arr.shape
assert np.isclose(ptwt_arr.numpy(), pywt_arr).all()

@v0lta
Copy link
Owner

v0lta commented Jul 10, 2024

@rachelglenn, is your question answered?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants