class
Posterize
extends
PhotometricTransform[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= 4Number 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 throughuint8and applies the bit mask~((1 << (8 - num_bits)) - 1), then divides by255. Use this for parity with reference pipelines."float"— pure-float quantisation viafloor(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.5Probability of applying the transform.
Raises
ValueErrorIf
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