Patches

class keras_mml.layers.misc.Patches[source]

Layer that creates patches for an image.

This layer implements the patch extraction algorithm described in An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. Useful for use in Vision Transformers (ViT).

Adapted from the Patches class in the Keras code example Image classification with Vision Transformer.

patch_size

Size of the patches.

__init__(patch_size, **kwargs)[source]

Initializes a new instance of the layer.

Parameters:
  • patch_size (int) – Size of the patches.

  • **kwargs – Keyword arguments for keras.Layer.

Raises:

ValueError – If the patch size provided is not a positive integer.

call(inputs)[source]

Calling method of the layer.

Note

patch_dim is equal to channels * (patch_size)**2.

Parameters:

inputs (Float[ndarray, 'batch_size height width channels']) – Inputs into the layer.

Returns:

Float[ndarray, 'batch_size patch_count patch_dim'] – Transformed inputs.