{"id":9648,"library":"dctorch","title":"DCTorch: Discrete Cosine Transforms for PyTorch","description":"DCTorch is a Python library providing fast discrete cosine transform (DCT) and inverse discrete cosine transform (IDCT) implementations optimized for PyTorch tensors. It enables efficient frequency domain analysis and manipulation within deep learning models, supporting 2D and 3D transforms. The current version is 0.1.2, and it appears to be actively maintained with recent commits.","status":"active","version":"0.1.2","language":"en","source_language":"en","source_url":"https://github.com/mario-carrasco/dctorch","tags":["pytorch","deep-learning","signal-processing","dct","discrete-cosine-transform","gpu-accelerated"],"install":[{"cmd":"pip install dctorch","lang":"bash","label":"Install dctorch"}],"dependencies":[{"reason":"core functionality relies on PyTorch tensors for inputs and outputs","package":"torch","optional":false}],"imports":[{"symbol":"dct_2d","correct":"from dctorch import dct_2d"},{"symbol":"idct_2d","correct":"from dctorch import idct_2d"},{"symbol":"dct_3d","correct":"from dctorch import dct_3d"},{"symbol":"idct_3d","correct":"from dctorch import idct_3d"}],"quickstart":{"code":"import torch\nfrom dctorch import dct_2d, idct_2d\n\n# Determine device (CPU or CUDA if available)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(f\"Using device: {device}\")\n\n# Create a random tensor (Batch size, Channels, Height, Width)\nx = torch.randn(1, 3, 224, 224, device=device)\nprint(f\"Original tensor shape: {x.shape}\")\n\n# Perform 2D Discrete Cosine Transform\ny = dct_2d(x)\nprint(f\"DCT transformed tensor shape: {y.shape}\")\n\n# Perform Inverse 2D Discrete Cosine Transform\nx_recon = idct_2d(y)\nprint(f\"Reconstructed tensor shape: {x_recon.shape}\")\n\n# Verify reconstruction accuracy\nreconstruction_error = torch.norm(x - x_recon).item()\nprint(f\"Reconstruction error (L2 norm): {reconstruction_error:.6f}\")\n\n# Note: Error is typically very small due to floating point precision\nassert reconstruction_error < 1e-4","lang":"python","description":"This quickstart demonstrates how to perform 2D Discrete Cosine Transform (DCT) and its inverse (IDCT) on a PyTorch tensor. It includes device selection for GPU acceleration and verifies the reconstruction accuracy."},"warnings":[{"fix":"Verify your PyTorch installation and device setup. Ensure CUDA drivers are up-to-date if using a GPU. Always move tensors to the correct device (e.g., `tensor.to('cuda')`).","message":"Performance heavily depends on PyTorch's underlying CUDA/CPU implementation. Ensure PyTorch is correctly installed and configured for your hardware for optimal speed, especially with large tensors.","severity":"gotcha","affected_versions":"All"},{"fix":"Convert input tensors to a float type using `.to(torch.float32)` or `.to(torch.float64)` before passing them to `dctorch` functions.","message":"DCTorch functions assume input tensors are floating-point types (e.g., `torch.float32`, `torch.float64`). Passing integer tensors will result in type errors or unexpected behavior.","severity":"gotcha","affected_versions":"All"},{"fix":"No specific fix, but users should be aware of this behavior and ensure input dimensions are managed appropriately for their use case.","message":"While DCT and IDCT are often used with square inputs in traditional signal processing, `dctorch` functions `dct_2d` and `idct_2d` will work with rectangular inputs. However, ensure the spatial dimensions (height, width) are consistent for corresponding DCT and IDCT operations to correctly reconstruct the original signal.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Convert the input tensor to a floating-point type: `x = x.to(torch.float32)` or `x = x.float()`.","cause":"The input tensor provided to a dctorch function has an integer data type (e.g., `torch.long`), but dctorch expects floating-point numbers.","error":"RuntimeError: Expected object of scalar type Float but got scalar type Long for argument 'input'"},{"fix":"Run the operation on a supported device (CPU or CUDA). Move your tensor to a different device: `tensor = tensor.to('cpu')` or `tensor = tensor.to('cuda')`.","cause":"dctorch internally relies on PyTorch's FFT operations, which may not be fully supported on all PyTorch devices (e.g., Apple's MPS backend for certain FFT functions).","error":"NotImplementedError: The operator 'aten::fft_rfft' is not currently implemented for the MPS device."},{"fix":"Use the correct function name: `dctorch.dct_2d()` for 2D transforms or `dctorch.dct_3d()` for 3D transforms.","cause":"The user is trying to call a generic `dct` function, but `dctorch` provides specific functions for 2D (`dct_2d`) and 3D (`dct_3d`) transforms.","error":"AttributeError: module 'dctorch' has no attribute 'dct'"}]}