CenterCropPad#
CenterCropPad - 18#
Version
name: CenterCropPad (GitHub)
domain: main
since_version: 18
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 18.
Summary
Center crop or pad an input to given dimensions.
The crop/pad dimensions can be specified for a subset of the axes. Non-specified dimensions will not be cropped or padded.
If the input dimensions are bigger than the crop shape, a centered cropping window is extracted from the input. If the input dimensions are smaller than the crop shape, the input is padded on each side equally, so that the input is centered in the output.
Attributes
axes: If provided, it specifies a subset of axes that ‘shape’ refer to. If not provided, all axes are assumed [0, 1, …, r-1], where r = rank(data). Negative value means counting dimensions from the back. Accepted range is [-r, r-1], where r = rank(data). Behavior is undefined if an axis is repeated.
Inputs
input_data (heterogeneous) - T: Input to extract the centered crop from.
shape (heterogeneous) - Tind: 1-D tensor representing the cropping window dimensions.
Outputs
output_data (heterogeneous) - T: Output data.
Type Constraints
T in ( tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output types to all tensor types.
Tind in ( tensor(int32), tensor(int64) ): Constrain indices to integer types
Examples
_center_crop_pad_crop
import numpy as np
import onnx
node = onnx.helper.make_node(
"CenterCropPad",
inputs=["x", "shape"],
outputs=["y"],
)
# First dim is even diff, second is uneven
x = np.random.randn(20, 10, 3).astype(np.float32)
shape = np.array([10, 7, 3], dtype=np.int64)
y = x[5:15, 1:8, :]
expect(node, inputs=[x, shape], outputs=[y], name="test_center_crop_pad_crop")
_center_crop_pad_pad
import numpy as np
import onnx
node = onnx.helper.make_node(
"CenterCropPad",
inputs=["x", "shape"],
outputs=["y"],
)
# First dim is even diff, second is uneven
x = np.random.randn(10, 7, 3).astype(np.float32)
shape = np.array([20, 10, 3], dtype=np.int64)
y = np.zeros([20, 10, 3], dtype=np.float32)
y[5:15, 1:8, :] = x
expect(node, inputs=[x, shape], outputs=[y], name="test_center_crop_pad_pad")
_center_crop_pad_crop_and_pad
import numpy as np
import onnx
node = onnx.helper.make_node(
"CenterCropPad",
inputs=["x", "shape"],
outputs=["y"],
)
# Cropping on first dim, padding on second, third stays the same
x = np.random.randn(20, 8, 3).astype(np.float32)
shape = np.array([10, 10, 3], dtype=np.int64)
y = np.zeros([10, 10, 3], dtype=np.float32)
y[:, 1:9, :] = x[5:15, :, :]
expect(
node,
inputs=[x, shape],
outputs=[y],
name="test_center_crop_pad_crop_and_pad",
)
_center_crop_pad_crop_axes_hwc
import numpy as np
import onnx
node = onnx.helper.make_node(
"CenterCropPad",
inputs=["x", "shape"],
outputs=["y"],
axes=[0, 1],
)
# Cropping on first dim, padding on second, third stays the same
x = np.random.randn(20, 8, 3).astype(np.float32)
shape = np.array([10, 9], dtype=np.int64)
y = np.zeros([10, 9, 3], dtype=np.float32)
y[:, :8, :] = x[5:15, :, :]
expect(
node,
inputs=[x, shape],
outputs=[y],
name="test_center_crop_pad_crop_axes_hwc",
)
_center_crop_pad_crop_axes_chw
import numpy as np
import onnx
node = onnx.helper.make_node(
"CenterCropPad",
inputs=["x", "shape"],
outputs=["y"],
axes=[1, 2],
)
# Cropping on second dim, padding on third, first stays the same
x = np.random.randn(3, 20, 8).astype(np.float32)
shape = np.array([10, 9], dtype=np.int64)
y = np.zeros([3, 10, 9], dtype=np.float32)
y[:, :, :8] = x[:, 5:15, :]
expect(
node,
inputs=[x, shape],
outputs=[y],
name="test_center_crop_pad_crop_axes_chw",
)