-
Notifications
You must be signed in to change notification settings - Fork 34
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
Comments
Dear @rachelglenn , You have two options:
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
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. |
I am not quite sure what you mean with "coefficients as a matrix". In case you mean the functionality of 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() |
@rachelglenn, is your question answered? |
Hi. I am trying to pull out the coefficients as a matrix. Similar to pywt. How do I do that (last line)?
The text was updated successfully, but these errors were encountered: