Skip to main content

Discrete Cosine Transform in PyTorch

Project description

DCT-Autoencoder

A PyTorch-based implementation of a 2D Discrete Cosine Transform (DCT) autoencoder for RGB images.

PyPI version

Overview

dct-autoencoder is a non-trainable, analytically defined autoencoder that reproduces the core ideas behind JPEG compression:

  1. Convert RGB to YCbCr to separate luminance from chrominance.
  2. Partition each channel into non-overlapping blocks and apply the 2-D DCT via strided convolution.
  3. Optionally zero high-frequency coefficients for lossy compression.

The transform is fully differentiable and integrates cleanly into PyTorch pipelines. Encoding is lossless by default; compression is opt-in at construction time.

Project structure

dct-autoencoder/
├── src/dct_autoencoder/       # Package source
│   ├── __init__.py            # Public API exports
│   ├── basis.py               # DCTBasis, get_dct_basis()
│   ├── core.py                # DCTAutoencoder (nn.Module)
│   └── utils.py               # RGB ↔ YCbCr conversions
├── notebooks/
│   ├── quick_start.ipynb      # Minimal getting-started example
│   ├── full_tutorial.ipynb    # In-depth walkthrough
│   ├── visualization.py       # DCT basis-function plotting helpers
│   └── test_images/           # Sample images for notebooks
├── assets/figures/            # Diagrams and figures
├── pyproject.toml
└── README.md

Installation

Requires Python 3.12+.

1. Install PyTorch

Install PyTorch and torchvision first, following the official PyTorch installation guide. Choose the build that matches your hardware (CPU, CUDA, ROCm, etc.).

2. Install this package

Then install dct-autoencoder from PyPI:

pip install dct-autoencoder

The package depends on NumPy; PyTorch is required to use DCTAutoencoder but is not bundled so you can pick the correct build for your system.

Development setup

This project uses uv for dependency management:

git clone https://github.com/dariush-bahrami/dct-autoencoder.git
cd dct-autoencoder
uv sync --group dev

Quick start

import torch
from dct_autoencoder import DCTAutoencoder

model = DCTAutoencoder(
    block_size=8,
    num_luminance_compressed_channels=10,   # optional low-pass on Y
    num_chrominance_compressed_channels=5,  # optional low-pass on Cb/Cr
)

# Input: (batch, 3, H, W), values in [0, 1]; H and W must be divisible by block_size
images = torch.rand(1, 3, 256, 256)

encoded = model.encode(images)
compressed = model.compress(encoded)
reconstructed = model.decode(model.decompress(compressed))

Public API

Symbol Module Description
DCTAutoencoder core Encode/decode RGB images; optional frequency-domain compression
DCTBasis basis Named tuple of precomputed DCT basis data for a block size
get_dct_basis basis Build a DCTBasis for a given block size

Notebooks

Notebook Description
notebooks/quick_start.ipynb Minimal example: load an image, encode, compress, decode
notebooks/full_tutorial.ipynb Full tutorial covering math, API details, and interactive exploration

Visualizations

Computation graph

Computation Graph

DCT basis functions

DCT basis functions for a block size of 16:

DCT Basis Functions

You can regenerate basis-function plots locally with notebooks/visualization.py and get_dct_basis().

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dct_autoencoder-1.0.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dct_autoencoder-1.0.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file dct_autoencoder-1.0.0.tar.gz.

File metadata

  • Download URL: dct_autoencoder-1.0.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dct_autoencoder-1.0.0.tar.gz
Algorithm Hash digest
SHA256 38be5d6058a088b03132b76962e23187ab2346aa24f1c32e9b9a1b242f47badc
MD5 c02146bce3f7e99c57dc771e8188224a
BLAKE2b-256 aa82d4fa13cd0e8573b1d8cb5708fa82eaa45859739d01bf39fb4db395b8c9b7

See more details on using hashes here.

File details

Details for the file dct_autoencoder-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: dct_autoencoder-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dct_autoencoder-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bc8b1853f82d7b58ae2d5e7a6ccecf07392b060f00c4fee6d33086a618d8415
MD5 8c11df8cc970ecde942a676457b21a0a
BLAKE2b-256 9b7b4a6f8c3b0c4626eb0cfa43647fe815ba3734561ab3b3a212be9c78db9e14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page