CenterCropPad#

  • Domain: ai.onnx

  • Since version: 18

Center crop or pad an input to given dimensions.

The crop/pad dimensions can be specified for a subset of the axes; unspecified dimensions will remain unchanged.

If the input dimensions are larger than the target crop dimensions, a centered cropping window will be extracted from the input. The starting value for the cropping window is rounded down, which means that if the difference between the input shape and the crop shape is odd, the cropping window will be shifted half a pixel to the left of the input center.

If the input dimensions are smaller than the target crop dimensions, the input will be padded equally on both sides to center it in the output. In cases where the total number of padding pixels is odd, an additional pixel will be added to the right side.

The padding value used is zero.

Inputs

  • input_data (T): Input to extract the centered crop from.

  • shape (Tind): 1-D tensor representing the cropping window dimensions.

Outputs

  • output_data (T): Output data.

Attributes

  • axes (int[]): 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.

Type Constraints

  • T: Constrain input and output types to all tensor types. Allowed types: 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).

  • Tind: Constrain indices to integer types Allowed types: tensor(int32), tensor(int64).

Examples#

test_cc_center_crop_pad_crop

Node:
  CenterCropPad(x, shape) -> (y)
Inputs:
  x: shape=(20, 10, 3), dtype=float32
    [[[  0.,   1.,   2.],
      [  3.,   4.,   5.],
      [  6.,   7.,   8.],
      ...,
      [ 21.,  22.,  23.],
      [ 24.,  25.,  26.],
      [ 27.,  28.,  29.]],

     [[ 30.,  31.,  32.],
      [ 33.,  34.,  35.],
      [ 36.,  37.,  38.],
      ...,
      [ 51.,  52.,  53.],
      [ 54.,  55.,  56.],
      [ 57.,  58.,  59.]],

     [[ 60.,  61.,  62.],
      [ 63.,  64.,  65.],
      [ 66.,  67.,  68.],
      ...,
      [ 81.,  82.,  83.],
      [ 84.,  85.,  86.],
      [ 87.,  88.,  89.]],

     ...,

     [[510., 511., 512.],
      [513., 514., 515.],
      [516., 517., 518.],
      ...,
      [531., 532., 533.],
      [534., 535., 536.],
      [537., 538., 539.]],

     [[540., 541., 542.],
      [543., 544., 545.],
      [546., 547., 548.],
      ...,
      [561., 562., 563.],
      [564., 565., 566.],
      [567., 568., 569.]],

     [[570., 571., 572.],
      [573., 574., 575.],
      [576., 577., 578.],
      ...,
      [591., 592., 593.],
      [594., 595., 596.],
      [597., 598., 599.]]]
  shape: shape=(3,), dtype=int64
    [10,  7,  3]

Outputs:
  y: shape=(10, 7, 3), dtype=float32
    [[[153., 154., 155.],
      [156., 157., 158.],
      [159., 160., 161.],
      ...,
      [165., 166., 167.],
      [168., 169., 170.],
      [171., 172., 173.]],

     [[183., 184., 185.],
      [186., 187., 188.],
      [189., 190., 191.],
      ...,
      [195., 196., 197.],
      [198., 199., 200.],
      [201., 202., 203.]],

     [[213., 214., 215.],
      [216., 217., 218.],
      [219., 220., 221.],
      ...,
      [225., 226., 227.],
      [228., 229., 230.],
      [231., 232., 233.]],

     ...,

     [[363., 364., 365.],
      [366., 367., 368.],
      [369., 370., 371.],
      ...,
      [375., 376., 377.],
      [378., 379., 380.],
      [381., 382., 383.]],

     [[393., 394., 395.],
      [396., 397., 398.],
      [399., 400., 401.],
      ...,
      [405., 406., 407.],
      [408., 409., 410.],
      [411., 412., 413.]],

     [[423., 424., 425.],
      [426., 427., 428.],
      [429., 430., 431.],
      ...,
      [435., 436., 437.],
      [438., 439., 440.],
      [441., 442., 443.]]]

test_cc_center_crop_pad_crop_and_pad

Node:
  CenterCropPad(x, shape) -> (y)
Inputs:
  x: shape=(20, 8, 3), dtype=float32
    [[[  0.,   1.,   2.],
      [  3.,   4.,   5.],
      [  6.,   7.,   8.],
      ...,
      [ 15.,  16.,  17.],
      [ 18.,  19.,  20.],
      [ 21.,  22.,  23.]],

     [[ 24.,  25.,  26.],
      [ 27.,  28.,  29.],
      [ 30.,  31.,  32.],
      ...,
      [ 39.,  40.,  41.],
      [ 42.,  43.,  44.],
      [ 45.,  46.,  47.]],

     [[ 48.,  49.,  50.],
      [ 51.,  52.,  53.],
      [ 54.,  55.,  56.],
      ...,
      [ 63.,  64.,  65.],
      [ 66.,  67.,  68.],
      [ 69.,  70.,  71.]],

     ...,

     [[408., 409., 410.],
      [411., 412., 413.],
      [414., 415., 416.],
      ...,
      [423., 424., 425.],
      [426., 427., 428.],
      [429., 430., 431.]],

     [[432., 433., 434.],
      [435., 436., 437.],
      [438., 439., 440.],
      ...,
      [447., 448., 449.],
      [450., 451., 452.],
      [453., 454., 455.]],

     [[456., 457., 458.],
      [459., 460., 461.],
      [462., 463., 464.],
      ...,
      [471., 472., 473.],
      [474., 475., 476.],
      [477., 478., 479.]]]
  shape: shape=(3,), dtype=int64
    [10, 10,  3]

Outputs:
  y: shape=(10, 10, 3), dtype=float32
    [[[  0.,   0.,   0.],
      [120., 121., 122.],
      [123., 124., 125.],
      ...,
      [138., 139., 140.],
      [141., 142., 143.],
      [  0.,   0.,   0.]],

     [[  0.,   0.,   0.],
      [144., 145., 146.],
      [147., 148., 149.],
      ...,
      [162., 163., 164.],
      [165., 166., 167.],
      [  0.,   0.,   0.]],

     [[  0.,   0.,   0.],
      [168., 169., 170.],
      [171., 172., 173.],
      ...,
      [186., 187., 188.],
      [189., 190., 191.],
      [  0.,   0.,   0.]],

     ...,

     [[  0.,   0.,   0.],
      [288., 289., 290.],
      [291., 292., 293.],
      ...,
      [306., 307., 308.],
      [309., 310., 311.],
      [  0.,   0.,   0.]],

     [[  0.,   0.,   0.],
      [312., 313., 314.],
      [315., 316., 317.],
      ...,
      [330., 331., 332.],
      [333., 334., 335.],
      [  0.,   0.,   0.]],

     [[  0.,   0.,   0.],
      [336., 337., 338.],
      [339., 340., 341.],
      ...,
      [354., 355., 356.],
      [357., 358., 359.],
      [  0.,   0.,   0.]]]

test_cc_center_crop_pad_crop_axes_chw

Node:
  CenterCropPad(x, shape) -> (y)
  Attributes:
    axes = [1, 2]
Inputs:
  x: shape=(3, 20, 8), dtype=float32
    [[[  0.,   1.,   2., ...,   5.,   6.,   7.],
      [  8.,   9.,  10., ...,  13.,  14.,  15.],
      [ 16.,  17.,  18., ...,  21.,  22.,  23.],
      ...,
      [136., 137., 138., ..., 141., 142., 143.],
      [144., 145., 146., ..., 149., 150., 151.],
      [152., 153., 154., ..., 157., 158., 159.]],

     [[160., 161., 162., ..., 165., 166., 167.],
      [168., 169., 170., ..., 173., 174., 175.],
      [176., 177., 178., ..., 181., 182., 183.],
      ...,
      [296., 297., 298., ..., 301., 302., 303.],
      [304., 305., 306., ..., 309., 310., 311.],
      [312., 313., 314., ..., 317., 318., 319.]],

     [[320., 321., 322., ..., 325., 326., 327.],
      [328., 329., 330., ..., 333., 334., 335.],
      [336., 337., 338., ..., 341., 342., 343.],
      ...,
      [456., 457., 458., ..., 461., 462., 463.],
      [464., 465., 466., ..., 469., 470., 471.],
      [472., 473., 474., ..., 477., 478., 479.]]]
  shape: shape=(2,), dtype=int64
    [10,  9]

Outputs:
  y: shape=(3, 10, 9), dtype=float32
    [[[ 40.,  41.,  42., ...,  46.,  47.,   0.],
      [ 48.,  49.,  50., ...,  54.,  55.,   0.],
      [ 56.,  57.,  58., ...,  62.,  63.,   0.],
      ...,
      [ 96.,  97.,  98., ..., 102., 103.,   0.],
      [104., 105., 106., ..., 110., 111.,   0.],
      [112., 113., 114., ..., 118., 119.,   0.]],

     [[200., 201., 202., ..., 206., 207.,   0.],
      [208., 209., 210., ..., 214., 215.,   0.],
      [216., 217., 218., ..., 222., 223.,   0.],
      ...,
      [256., 257., 258., ..., 262., 263.,   0.],
      [264., 265., 266., ..., 270., 271.,   0.],
      [272., 273., 274., ..., 278., 279.,   0.]],

     [[360., 361., 362., ..., 366., 367.,   0.],
      [368., 369., 370., ..., 374., 375.,   0.],
      [376., 377., 378., ..., 382., 383.,   0.],
      ...,
      [416., 417., 418., ..., 422., 423.,   0.],
      [424., 425., 426., ..., 430., 431.,   0.],
      [432., 433., 434., ..., 438., 439.,   0.]]]

test_cc_center_crop_pad_crop_axes_hwc

Node:
  CenterCropPad(x, shape) -> (y)
  Attributes:
    axes = [0, 1]
Inputs:
  x: shape=(20, 8, 3), dtype=float32
    [[[  0.,   1.,   2.],
      [  3.,   4.,   5.],
      [  6.,   7.,   8.],
      ...,
      [ 15.,  16.,  17.],
      [ 18.,  19.,  20.],
      [ 21.,  22.,  23.]],

     [[ 24.,  25.,  26.],
      [ 27.,  28.,  29.],
      [ 30.,  31.,  32.],
      ...,
      [ 39.,  40.,  41.],
      [ 42.,  43.,  44.],
      [ 45.,  46.,  47.]],

     [[ 48.,  49.,  50.],
      [ 51.,  52.,  53.],
      [ 54.,  55.,  56.],
      ...,
      [ 63.,  64.,  65.],
      [ 66.,  67.,  68.],
      [ 69.,  70.,  71.]],

     ...,

     [[408., 409., 410.],
      [411., 412., 413.],
      [414., 415., 416.],
      ...,
      [423., 424., 425.],
      [426., 427., 428.],
      [429., 430., 431.]],

     [[432., 433., 434.],
      [435., 436., 437.],
      [438., 439., 440.],
      ...,
      [447., 448., 449.],
      [450., 451., 452.],
      [453., 454., 455.]],

     [[456., 457., 458.],
      [459., 460., 461.],
      [462., 463., 464.],
      ...,
      [471., 472., 473.],
      [474., 475., 476.],
      [477., 478., 479.]]]
  shape: shape=(2,), dtype=int64
    [10,  9]

Outputs:
  y: shape=(10, 9, 3), dtype=float32
    [[[120., 121., 122.],
      [123., 124., 125.],
      [126., 127., 128.],
      ...,
      [138., 139., 140.],
      [141., 142., 143.],
      [  0.,   0.,   0.]],

     [[144., 145., 146.],
      [147., 148., 149.],
      [150., 151., 152.],
      ...,
      [162., 163., 164.],
      [165., 166., 167.],
      [  0.,   0.,   0.]],

     [[168., 169., 170.],
      [171., 172., 173.],
      [174., 175., 176.],
      ...,
      [186., 187., 188.],
      [189., 190., 191.],
      [  0.,   0.,   0.]],

     ...,

     [[288., 289., 290.],
      [291., 292., 293.],
      [294., 295., 296.],
      ...,
      [306., 307., 308.],
      [309., 310., 311.],
      [  0.,   0.,   0.]],

     [[312., 313., 314.],
      [315., 316., 317.],
      [318., 319., 320.],
      ...,
      [330., 331., 332.],
      [333., 334., 335.],
      [  0.,   0.,   0.]],

     [[336., 337., 338.],
      [339., 340., 341.],
      [342., 343., 344.],
      ...,
      [354., 355., 356.],
      [357., 358., 359.],
      [  0.,   0.,   0.]]]

test_cc_center_crop_pad_crop_negative_axes_hwc

Node:
  CenterCropPad(x, shape) -> (y)
  Attributes:
    axes = [-3, -2]
Inputs:
  x: shape=(20, 8, 3), dtype=float32
    [[[  0.,   1.,   2.],
      [  3.,   4.,   5.],
      [  6.,   7.,   8.],
      ...,
      [ 15.,  16.,  17.],
      [ 18.,  19.,  20.],
      [ 21.,  22.,  23.]],

     [[ 24.,  25.,  26.],
      [ 27.,  28.,  29.],
      [ 30.,  31.,  32.],
      ...,
      [ 39.,  40.,  41.],
      [ 42.,  43.,  44.],
      [ 45.,  46.,  47.]],

     [[ 48.,  49.,  50.],
      [ 51.,  52.,  53.],
      [ 54.,  55.,  56.],
      ...,
      [ 63.,  64.,  65.],
      [ 66.,  67.,  68.],
      [ 69.,  70.,  71.]],

     ...,

     [[408., 409., 410.],
      [411., 412., 413.],
      [414., 415., 416.],
      ...,
      [423., 424., 425.],
      [426., 427., 428.],
      [429., 430., 431.]],

     [[432., 433., 434.],
      [435., 436., 437.],
      [438., 439., 440.],
      ...,
      [447., 448., 449.],
      [450., 451., 452.],
      [453., 454., 455.]],

     [[456., 457., 458.],
      [459., 460., 461.],
      [462., 463., 464.],
      ...,
      [471., 472., 473.],
      [474., 475., 476.],
      [477., 478., 479.]]]
  shape: shape=(2,), dtype=int64
    [10,  9]

Outputs:
  y: shape=(10, 9, 3), dtype=float32
    [[[120., 121., 122.],
      [123., 124., 125.],
      [126., 127., 128.],
      ...,
      [138., 139., 140.],
      [141., 142., 143.],
      [  0.,   0.,   0.]],

     [[144., 145., 146.],
      [147., 148., 149.],
      [150., 151., 152.],
      ...,
      [162., 163., 164.],
      [165., 166., 167.],
      [  0.,   0.,   0.]],

     [[168., 169., 170.],
      [171., 172., 173.],
      [174., 175., 176.],
      ...,
      [186., 187., 188.],
      [189., 190., 191.],
      [  0.,   0.,   0.]],

     ...,

     [[288., 289., 290.],
      [291., 292., 293.],
      [294., 295., 296.],
      ...,
      [306., 307., 308.],
      [309., 310., 311.],
      [  0.,   0.,   0.]],

     [[312., 313., 314.],
      [315., 316., 317.],
      [318., 319., 320.],
      ...,
      [330., 331., 332.],
      [333., 334., 335.],
      [  0.,   0.,   0.]],

     [[336., 337., 338.],
      [339., 340., 341.],
      [342., 343., 344.],
      ...,
      [354., 355., 356.],
      [357., 358., 359.],
      [  0.,   0.,   0.]]]

test_cc_center_crop_pad_pad

Node:
  CenterCropPad(x, shape) -> (y)
Inputs:
  x: shape=(10, 7, 3), dtype=float32
    [[[  1.,   2.,   3.],
      [  4.,   5.,   6.],
      [  7.,   8.,   9.],
      ...,
      [ 13.,  14.,  15.],
      [ 16.,  17.,  18.],
      [ 19.,  20.,  21.]],

     [[ 22.,  23.,  24.],
      [ 25.,  26.,  27.],
      [ 28.,  29.,  30.],
      ...,
      [ 34.,  35.,  36.],
      [ 37.,  38.,  39.],
      [ 40.,  41.,  42.]],

     [[ 43.,  44.,  45.],
      [ 46.,  47.,  48.],
      [ 49.,  50.,  51.],
      ...,
      [ 55.,  56.,  57.],
      [ 58.,  59.,  60.],
      [ 61.,  62.,  63.]],

     ...,

     [[148., 149., 150.],
      [151., 152., 153.],
      [154., 155., 156.],
      ...,
      [160., 161., 162.],
      [163., 164., 165.],
      [166., 167., 168.]],

     [[169., 170., 171.],
      [172., 173., 174.],
      [175., 176., 177.],
      ...,
      [181., 182., 183.],
      [184., 185., 186.],
      [187., 188., 189.]],

     [[190., 191., 192.],
      [193., 194., 195.],
      [196., 197., 198.],
      ...,
      [202., 203., 204.],
      [205., 206., 207.],
      [208., 209., 210.]]]
  shape: shape=(3,), dtype=int64
    [20, 10,  3]

Outputs:
  y: shape=(20, 10, 3), dtype=float32
    [[[0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.],
      ...,
      [0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.]],

     [[0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.],
      ...,
      [0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.]],

     [[0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.],
      ...,
      [0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.]],

     ...,

     [[0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.],
      ...,
      [0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.]],

     [[0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.],
      ...,
      [0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.]],

     [[0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.],
      ...,
      [0., 0., 0.],
      [0., 0., 0.],
      [0., 0., 0.]]]