Extracts patches from the image(s) or volume(s).
tf.keras.ops.image.extract_patches(
images,
size,
strides=None,
dilation_rate=1,
padding='valid',
data_format=None
)
This function supports both 2D and 3D patch extraction based on the
size argument length, similar to how keras.ops.conv handles
different dimensions.
Args |
images
|
Input image/volume or batch of images/volumes.
For 2D patches: 3D (H, W, C) or 4D (N, H, W, C).
For 3D patches: 4D (D, H, W, C) or 5D (N, D, H, W, C).
|
size
|
Patch size as int or tuple.
Length 2 tuple (patch_height, patch_width) or int for 2D patches.
Length 3 tuple (patch_depth, patch_height, patch_width) for
3D patches.
|
strides
|
Strides for patch extraction. If not specified, defaults
to size (non-overlapping patches).
|
dilation_rate
|
Dilation rate for patch extraction. Note that
dilation_rate > 1 is not supported with strides > 1.
|
padding
|
The type of padding algorithm to use: "same" or "valid".
|
data_format
|
A string specifying the data format of the input tensor.
It can be either "channels_last" or "channels_first".
If not specified, defaults to keras.config.image_data_format.
|
Returns |
Extracted patches with shape depending on input and size:
- 2D patches: 3D (unbatched) or 4D (batched)
- 3D patches: 4D (unbatched) or 5D (batched)
|
Examples:
# 2D patches from batch of images
image = np.random.random(
(2, 20, 20, 3)
).astype("float32")
patches = keras.ops.image.extract_patches(image, (5, 5))
patches.shape
(2, 4, 4, 75)
# 2D patches from single image
image = np.random.random((20, 20, 3)).astype("float32")
patches = keras.ops.image.extract_patches(image, (3, 3), (1, 1))
patches.shape
(18, 18, 27)
# 3D patches from batch of volumes
volumes = np.random.random(
(2, 10, 10, 10, 3)
).astype("float32")
patches = keras.ops.image.extract_patches(volumes, (3, 3, 3))
patches.shape
(2, 3, 3, 3, 81)
# 3D patches from single volume
volume = np.random.random((10, 10, 10, 3)).astype("float32")
patches = keras.ops.image.extract_patches(volume, (3, 3, 3))
patches.shape
(3, 3, 3, 81)