Composite loss function for neural glyph-image reconstruction.
Project description
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 termlambda_mag: weight for gradient magnitude termlambda_dir: weight for gradient direction term (cosine loss)lambda_spectral: weight for spectral loss termpatch_size: spatial size of patches for spectral losspatch_stride: stride for patch extractiondir_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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file glyphloss-0.1.0.tar.gz.
File metadata
- Download URL: glyphloss-0.1.0.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fdbf9fd6719e3495942e089ec6618936c367789246e6816822e53d7b24b91b8
|
|
| MD5 |
b48f82e9ad21d452b474f91141f6562c
|
|
| BLAKE2b-256 |
e6febf70aa3abb4195e2468c39d5e3735ce6deea9c925b5bcefb464857be3245
|
File details
Details for the file glyphloss-0.1.0-py3-none-any.whl.
File metadata
- Download URL: glyphloss-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8de8145c28692949a0bb1dc7fc4578ee27fa3aba15b0b126ef6817eaa9c8aa13
|
|
| MD5 |
6d584f10b1786aa2c2be0c95c4e35114
|
|
| BLAKE2b-256 |
a015d5a929472a2235ac70dc758f075d87ae98ba1bc62cce2ff9117ca37d1d14
|