MCPcopy Create free account
hub / github.com/apple/ml-pointersect / sample_random_patch_uv

Function sample_random_patch_uv

plib/utils.py:2886–2944  ·  view source on GitHub ↗

Samples random uv coordinates ([0, w), [0, h)) to create random patches. Args: b_shape: (*b,) determines the number of patches to sample width_px: width (in pixel) of the image to sample from, determines the range of u [0,w) height_px:

(
        b_shape: T.Union[int, T.List[int]],
        width_px: int,
        height_px: int,
        patch_width_px: int,
        patch_width_pitch_scale: T.Union[float, torch.Tensor] = 1.,  # (*b,)
        patch_height_px: int = None,  # (*b,)
        patch_height_pitch_scale: T.Union[float, torch.Tensor] = None,  # (*b,)
        int_only: bool = True,
        device: torch.device = torch.device('cpu'),
)

Source from the content-addressed store, hash-verified

2884
2885
2886def sample_random_patch_uv(
2887 b_shape: T.Union[int, T.List[int]],
2888 width_px: int,
2889 height_px: int,
2890 patch_width_px: int,
2891 patch_width_pitch_scale: T.Union[float, torch.Tensor] = 1., # (*b,)
2892 patch_height_px: int = None, # (*b,)
2893 patch_height_pitch_scale: T.Union[float, torch.Tensor] = None, # (*b,)
2894 int_only: bool = True,
2895 device: torch.device = torch.device('cpu'),
2896) -> torch.Tensor:
2897 """
2898 Samples random uv coordinates ([0, w), [0, h)) to create random patches.
2899
2900 Args:
2901 b_shape:
2902 (*b,) determines the number of patches to sample
2903 width_px:
2904 width (in pixel) of the image to sample from, determines the range of u [0,w)
2905 height_px:
2906 height (in pixel) of the images to sample from, determines the range of v [0,h)
2907 patch_width_px:
2908 number of pixels in the patch in width
2909 patch_width_pitch_scale:
2910 (*b,) the pitch of the patch (new_pitch / old_pitch)
2911 patch_height_px:
2912 if None, the same as `patch_width_px`
2913 patch_height_pitch_scale:
2914 if None, the same as `patch_width_pitch_scale`
2915 int_only:
2916 whether the center is always at an integer index
2917
2918 Returns:
2919 (*b, patch_height_px, patch_width_px, 2) the u (first dimension) and v (second dimension)
2920
2921 Note that the patch can go out of bound
2922 """
2923 if isinstance(b_shape, int):
2924 b_shape = [b_shape]
2925
2926 # randomly sample patch center
2927 patch_center = torch.rand(*b_shape, 2, device=device) # (*b, 2) [0,1)
2928
2929 patch_center[..., 0] = patch_center[..., 0] * width_px # (*b, 2) [0,w) [0,h]
2930 patch_center[..., 1] = patch_center[..., 1] * height_px
2931
2932 if int_only:
2933 # we need to snap to 0.5, 1.5, 2.5, which are the actual pixel center
2934 patch_center = torch.floor(patch_center) + 0.5
2935
2936 uv = generate_patch_uv(
2937 patch_center=patch_center, # (*b, 2)
2938 patch_width_px=patch_width_px,
2939 patch_width_pitch_scale=patch_width_pitch_scale,
2940 patch_height_px=patch_height_px,
2941 patch_height_pitch_scale=patch_height_pitch_scale,
2942 device=device,
2943 )

Callers

nothing calls this directly

Calls 2

generate_patch_uvFunction · 0.85
deviceMethod · 0.80

Tested by

no test coverage detected