Skip to content
class

Posterize

extendsPhotometricTransform[Empty]
Posterize(num_bits: int = 4, mode: str = 'uint8_mask', p: float = 0.5)

Reduce bits per channel (Albumentations Posterize).

Two quantisation modes — pick based on whether you need bit-exact Albumentations / cv2 parity or just a cheap float-only posterise.

Parameters

num_bitsint= 4
Number of bits to keep per channel; output has 2**num_bits distinct levels. Must be in [1, 7].
mode(uint8_mask, float)= "uint8_mask"
Quantisation algorithm:
  • "uint8_mask" — matches Albumentations / OpenCV bit-exactly. Round-trips through uint8 and applies the bit mask ~((1 << (8 - num_bits)) - 1), then divides by 255. Use this for parity with reference pipelines.
  • "float" — pure-float quantisation via floor(x * 2**num_bits) / 2**num_bits. Slightly different mid-bin placement than the OpenCV bit-mask; cheaper (no uint8 round-trip). Use for novel pipelines where Albu parity isn't required.
pfloat= 0.5
Probability of applying the transform.

Raises

ValueError
If num_bits falls outside [1, 7] or mode is not one of the two recognised names.

Notes

The G3 parity suite verifies the "uint8_mask" mode matches Albumentations to float32 epsilon across num_bits ∈ {1, …, 7}. A 200-seed statistical aggregate (G4e) confirms the mean-of-max diff stays in the same regime.

Examples

>>> import lucid
>>> import lucid.utils.transforms as T
>>> tf = T.Posterize(num_bits=3, mode="uint8_mask", p=1.0)
>>> out = tf(T.Image(lucid.rand(3, 32, 32))).data
>>> # 3-bit → 8 distinct quantisation levels per channel
>>> len(set(out.numpy().reshape(-1).tolist())) <= 8
True

Used by 1

Constructors

1
dunder

__init__

→None
__init__(num_bits: int = 4, mode: str = 'uint8_mask', p: float = 0.5)

Instance methods

1
fn

make_params

→Empty
make_params(img: Tensor)

Return the no-op parameter sentinel for Posterize.

Parameters

Image tensor; not inspected.

Returns

Empty

The shared NO_PARAMS sentinel — Posterize is deterministic given num_bits / mode, so no per-call sampling is required.

Dunder methods

1