Skip to content

Commit

Permalink
Introduce CenterCropPad, add axes to Pad (onnx#4190)
Browse files Browse the repository at this point in the history
* Introduce ai.onnx.image.CenterCropPad

Signed-off-by: Joaquin Anton <[email protected]>

* Remove ai.onnx.image domain

Signed-off-by: Joaquin Anton <[email protected]>

* Add Pad axes

Signed-off-by: Joaquin Anton <[email protected]>

* Add axes to Shape, update CenterCropPad to work with arbitrary axes

Signed-off-by: Joaquin Anton <[email protected]>

* Fix shape inference file

Signed-off-by: Joaquin Anton <[email protected]>

* Undo Shape-17

Signed-off-by: Joaquin Anton <[email protected]>

* Update doc

Signed-off-by: Joaquin Anton <[email protected]>

* Linter fix

Signed-off-by: Joaquin Anton <[email protected]>

* Create Const directly from attribute

Signed-off-by: Joaquin Anton <[email protected]>

* Move to opset 18

Signed-off-by: Joaquin Anton <[email protected]>

* Apply clang-format

Signed-off-by: Joaquin Anton <[email protected]>

* Update opset in shape_inference_tests

Signed-off-by: Joaquin Anton <[email protected]>

* Fix shape inference tests

Signed-off-by: Joaquin Anton <[email protected]>

* Add automatic upgrade test

Signed-off-by: Joaquin Anton <[email protected]>

* Update CenterCropPad tests

Signed-off-by: Joaquin Anton <[email protected]>

* Refactor tests

Signed-off-by: Joaquin Anton <[email protected]>

* Update tests

Signed-off-by: Joaquin Anton <[email protected]>

* Check for repeated axes

Signed-off-by: Joaquin Anton <[email protected]>

* Fix flake8 issues

Signed-off-by: Joaquin Anton <[email protected]>

* Fix clang-format issue

Signed-off-by: Joaquin Anton <[email protected]>

* Fix CenterCropPad tests with axes

Signed-off-by: Joaquin Anton <[email protected]>

* Update doc

Signed-off-by: Joaquin Anton <[email protected]>
  • Loading branch information
jantonguirao authored and Bjarke Roune committed May 6, 2023
1 parent 11044ce commit a589643
Show file tree
Hide file tree
Showing 60 changed files with 1,143 additions and 41 deletions.
165 changes: 165 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21222,6 +21222,171 @@ This version of the operator has been available since version 17 of the default
</dl>

## Version 18 of the default ONNX operator set
### <a name="CenterCropPad-18"></a>**CenterCropPad-18**</a>

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.

#### Version

This version of the operator has been available since version 18 of the default ONNX operator set.

#### Attributes

<dl>
<dt><tt>axes</tt> : list of ints</dt>
<dd>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.</dd>
</dl>

#### Inputs

<dl>
<dt><tt>input_data</tt> (differentiable) : T</dt>
<dd>Input to extract the centered crop from.</dd>
<dt><tt>shape</tt> (non-differentiable) : Tind</dt>
<dd>1-D tensor representing the cropping window dimensions.</dd>
</dl>

#### Outputs

<dl>
<dt><tt>output_data</tt> (differentiable) : T</dt>
<dd>Output data.</dd>
</dl>

#### Type Constraints

<dl>
<dt><tt>T</tt> : tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(bfloat16), tensor(float16), tensor(float), tensor(double), tensor(string), tensor(bool), tensor(complex64), tensor(complex128)</dt>
<dd>Constrain input and output types to all tensor types.</dd>
<dt><tt>Tind</tt> : tensor(int32), tensor(int64)</dt>
<dd>Constrain indices to integer types</dd>
</dl>

### <a name="Pad-18"></a>**Pad-18**</a>

Given a tensor containing the data to be padded (`data`), a tensor containing the number of start and end pad values for axis (`pads`), (optionally) a `mode`, and (optionally) `constant_value`,
a padded tensor (`output`) is generated.

The three supported `modes` are (similar to corresponding modes supported by `numpy.pad`):

1) `constant`(default) - pads with a given constant value as specified by `constant_value` (which defaults to 0, empty string, or False)

2) `reflect` - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis

3) `edge` - pads with the edge values of array


Example 1 (`constant` mode):
Insert 0 pads to the beginning of the second dimension.

data =
[
[1.0, 1.2],
[2.3, 3.4],
[4.5, 5.7],
]

pads = [0, 2, 0, 0]

mode = 'constant'

constant_value = 0.0

output =
[
[0.0, 0.0, 1.0, 1.2],
[0.0, 0.0, 2.3, 3.4],
[0.0, 0.0, 4.5, 5.7],
]


Example 2 (`reflect` mode):
data =
[
[1.0, 1.2],
[2.3, 3.4],
[4.5, 5.7],
]

pads = [0, 2, 0, 0]

mode = 'reflect'

output =
[
[1.0, 1.2, 1.0, 1.2],
[2.3, 3.4, 2.3, 3.4],
[4.5, 5.7, 4.5, 5.7],
]


Example 3 (`edge` mode):
data =
[
[1.0, 1.2],
[2.3, 3.4],
[4.5, 5.7],
]

pads = [0, 2, 0, 0]

mode = 'edge'

output =
[
[1.0, 1.0, 1.0, 1.2],
[2.3, 2.3, 2.3, 3.4],
[4.5, 4.5, 4.5, 5.7],
]


#### Version

This version of the operator has been available since version 18 of the default ONNX operator set.

#### Attributes

<dl>
<dt><tt>mode</tt> : string (default is constant)</dt>
<dd>Supported modes: `constant`(default), `reflect`, `edge`</dd>
</dl>

#### Inputs (2 - 4)

<dl>
<dt><tt>data</tt> (differentiable) : T</dt>
<dd>Input tensor.</dd>
<dt><tt>pads</tt> (non-differentiable) : tensor(int64)</dt>
<dd>Tensor of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D input tensor, it is the number of pixels. `pads` should be a 1D tensor of shape [2 * num_axes] where `num_axes` refers to the number of elements in the `axes` input or the input rank if `axes` are not provided explicitly. `pads` format should be: [x1_begin, x2_begin, ..., x1_end, x2_end,...], where xi_begin is the number of pad values added at the beginning of axis `axes[i]` and xi_end, the number of pad values added at the end of axis `axes[i]`.</dd>
<dt><tt>constant_value</tt> (optional, non-differentiable) : T</dt>
<dd>(Optional) A scalar value to be used if the mode chosen is `constant` (by default it is 0, empty string or False).</dd>
<dt><tt>axes</tt> (optional, non-differentiable) : Tind</dt>
<dd>1-D tensor of axes that `pads` apply to. 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. If not provided, all axes are assumed (`[0, 1, ..., input_rank-1]`).</dd>
</dl>

#### Outputs

<dl>
<dt><tt>output</tt> (differentiable) : T</dt>
<dd>Tensor after padding.</dd>
</dl>

#### Type Constraints

<dl>
<dt><tt>T</tt> : tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(bfloat16), tensor(float16), tensor(float), tensor(double), tensor(string), tensor(bool), tensor(complex64), tensor(complex128)</dt>
<dd>Constrain input and output types to all tensor types.</dd>
<dt><tt>Tind</tt> : tensor(int32), tensor(int64)</dt>
<dd>Constrain indices to integer types</dd>
</dl>

### <a name="Resize-18"></a>**Resize-18**</a>

Resize the input tensor. In general, it calculates every value in the output tensor as a weighted average of neighborhood (a.k.a. sampling locations) in the input tensor.
Expand Down
Loading

0 comments on commit a589643

Please sign in to comment.