Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions dm_pix/_src/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""This module provides image patching functionality."""

import math
from typing import Sequence

import chex
Expand Down Expand Up @@ -89,7 +90,8 @@ def extract_patches(

# `conv_general_dilated_patches` returns `patches` in channel-major order,
# rearrange to match interface of `tf.image.extract_patches`.
patches = jnp.reshape(patches, patches.shape[:-1] + (channels, -1))
output_shape = patches.shape
patch_size = math.prod(sizes[1:-1])
patches = jnp.reshape(patches, output_shape[:-1] + (channels, patch_size))
patches = jnp.moveaxis(patches, -2, -1)
patches = jnp.reshape(patches, patches.shape[:-2] + (-1,))
return patches
return jnp.reshape(patches, output_shape)
9 changes: 4 additions & 5 deletions dm_pix/_src/patch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ def _create_test_images(shape):
class PatchTest(chex.TestCase, parameterized.TestCase):

@chex.all_variants
@parameterized.named_parameters(
('padding_valid', 'VALID'),
('padding_same', 'SAME'),
@parameterized.product(
padding=('VALID', 'SAME'),
image_shape=((2, 5, 7, 3), (0, 5, 7, 3), (2, 2, 7, 3)),
)
def test_extract_patches(self, padding):
image_shape = (2, 5, 7, 3)
def test_extract_patches(self, padding, image_shape):
images = _create_test_images(image_shape)

sizes = (1, 2, 3, 1)
Expand Down
Loading