Skip to main content

Glyph Loss

A Torch loss function and module designed specifically for training neural networks to reconstruct glyph images rendered from font files.

This attempts to improve upon the standard L1/L2 pixel-wise loss by adding loss components specific to rendered glyph images. In particular:

  • Grey weighting: The interesting part of glyph images is the edge of the glyph contours, not the background or the foreground. At the resolutions used in ML training (typically 128 pixels or less), the glyph edges are anti-aliased subpixels, and appear as grey pixels. The loss function weights these grey pixels considerably more heavily than the black/white pixels making up the bulk of the image.

  • Gradient loss: The gradient loss component penalizes differences in the image gradients, which helps to preserve the sharpness of the glyph edges. This is similar to Sobel loss, but it decomposes the magnitude and direction of the gradients, and penalizes differences in both. This attempts to preserve fine details around terminals and the distinctions between rounded and sharp corners.

  • Frequency loss: The frequency loss component penalizes differences in the frequency domain, which helps to preserve the spectral characteristics of the glyphs. This also attempts to prevent deliberately roughened edges in display fonts or sharp corners from becoming smoothed or blurred by the network.

Installation

# Core loss (requires only PyTorch):
pip install glyphloss

# With training harness (adds font rendering & TensorBoard deps):
pip install "glyphloss[train]"

Requires Python ≥ 3.8.

Using the loss function

Available as an nn.Module class and as a pre-instantiated default singleton:

from glyphloss import GlyphReconstructionLoss, glyph_reconstruction_loss

The nn.Module version has the following parameters:

  • weight_epsilon : floor weight for pure black/white pixels (keeps some gradient signal even outside the grey zone)
  • lambda_pixel : weight for pixel loss term
  • lambda_mag : weight for gradient magnitude term
  • lambda_dir : weight for gradient direction term (cosine loss)
  • lambda_spectral : weight for spectral loss term
  • patch_size : spatial size of patches for spectral loss
  • patch_stride : stride for patch extraction
  • dir_eps : numerical floor in cosine denominator

Testing/development

The Python package also includes a training harness with configurable loss functions and TensorBoard logging. To use it, install the package with the [train] extra dependencies, put some fonts into the data/ directory, and run:

python -m glyphloss.train --data-dir data --epochs 50

License

This projected is licensed under the Apache License 2.0. See the LICENSE file for details.

Metadata

Release files for glyphloss 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for glyphloss 0.1.0
File Size Uploaded Provenance Actions
glyphloss-0.1.0.tar.gz 16.0 kB Without provenance Details for glyphloss-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for glyphloss 0.1.0
File Interpreter ABI Platform Actions
glyphloss-0.1.0-py3-none-any.whl Python 3 none any Details for glyphloss-0.1.0-py3-none-any.whl

Total release size: 33.2 kB

Release files / glyphloss-0.1.0.tar.gz

Download URL glyphloss-0.1.0.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9fdbf9fd6719e3495942e089ec6618936c367789246e6816822e53d7b24b91b8
BLAKE2b-256 checksum
How to use checksums
e6febf70aa3abb4195e2468c39d5e3735ce6deea9c925b5bcefb464857be3245
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / glyphloss-0.1.0-py3-none-any.whl

Download URL glyphloss-0.1.0-py3-none-any.whl
Size 17.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8de8145c28692949a0bb1dc7fc4578ee27fa3aba15b0b126ef6817eaa9c8aa13
BLAKE2b-256 checksum
How to use checksums
a015d5a929472a2235ac70dc758f075d87ae98ba1bc62cce2ff9117ca37d1d14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Security for glyphloss 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page