Skip to content

Commit 63e4d33

Browse files
authored
Support the offset member in Core ML crop layer parameter. (#119)
* Support the offset member in Core ML crop layer parameter. * fix the order. * correct the border value. * add a docs string.
1 parent ca888a9 commit 63e4d33

File tree

2 files changed

+19
-8
lines changed
  • onnxmltools/convert/coreml
    • operator_converters/neural_network
    • shape_calculators/neural_network

2 files changed

+19
-8
lines changed

onnxmltools/convert/coreml/operator_converters/neural_network/Crop.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,22 @@ def convert_crop(scope, operator, container):
1414
op_type = 'Crop'
1515
attrs = {'name': operator.full_name}
1616
border = operator.raw_operator.crop.cropAmounts.borderAmounts
17-
left = border[1].startEdgeSize
18-
top = border[0].startEdgeSize
19-
right = border[1].endEdgeSize
20-
bottom = border[0].endEdgeSize
21-
22-
attrs['border'] = [left, top, right, bottom]
17+
left_border, top_border, right_border, bottom_border = (None,) * 4
18+
if len(border):
19+
left_border = border[1].startEdgeSize
20+
top_border = border[0].startEdgeSize
21+
right_border = border[1].endEdgeSize
22+
bottom_border = border[0].endEdgeSize
23+
else:
24+
offset = operator.raw_operator.crop.offset
25+
in_shape = operator.inputs[0].type.shape
26+
out_shape = operator.outputs[0].type.shape
27+
left_border = offset[1]
28+
top_border = offset[0]
29+
right_border = in_shape[3] - left_border - out_shape[3]
30+
bottom_border = in_shape[2] - top_border - out_shape[2]
31+
32+
attrs['border'] = [left_border, top_border, right_border, bottom_border]
2333

2434
container.add_node(op_type, operator.input_full_names, operator.output_full_names, **attrs)
2535

onnxmltools/convert/coreml/shape_calculators/neural_network/Crop.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def calculate_crop_output_shapes(operator):
1414
'''
1515
Allowed input/output patterns are
1616
1. [N, C, H, W] ---> [N, C, H', W']
17+
2. [N, C, H, W], shape-ref [N', C', H', W'] ---> [N, C, H', W']
1718
'''
1819
check_input_and_output_numbers(operator, input_count_range=[1, 2], output_count_range=1)
1920
check_input_and_output_types(operator, good_input_types=[FloatTensorType])
@@ -28,8 +29,8 @@ def calculate_crop_output_shapes(operator):
2829
output_shape[3] -= params.cropAmounts.borderAmounts[1].startEdgeSize
2930
output_shape[3] -= params.cropAmounts.borderAmounts[1].endEdgeSize
3031
elif len(operator.inputs) == 2:
31-
output_shape[2] = operator.raw_operator.inputs[1].type.shape[2]
32-
output_shape[3] = operator.raw_operator.inputs[1].type.shape[3]
32+
output_shape[2] = operator.inputs[1].type.shape[2]
33+
output_shape[3] = operator.inputs[1].type.shape[3]
3334
else:
3435
raise RuntimeError('Too many inputs for Crop operator')
3536

0 commit comments

Comments
 (0)