torch_model = Net()
torch_model.eval()
torch_x = torch.randn(batch_size, receptive_field, input_feat_size, requires_grad=True)
torch_out = torch_model(torch_x)
onnx_file_name = 'model.onnx'
input_names = ['input']
output_names = ['output']
# Export the model
torch.onnx.export(torch_model, # model being run
torch_x, # model input (or a tuple for multiple inputs)
onnx_file_name, # where to save the model (can be a file or file-like object)
export_params=True, # store the trained parameter weights inside the model file
# opset_version=10, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names=input_names, # the model's input names
output_names=output_names, # the model's output names
# dynamic_axes={'input': {0: 'batch_size'}, # variable lenght axes
# 'output': {0: 'batch_size'}}
)
onnx_model = onnx.load(onnx_file_name)
coreml_file_name = 'model.mlmodel'
cml = onnx_coreml.convert(onnx_model,
target_ios='13',
custom_conversion_functions={"Pad": _convert_pad})
print(type(cml))
cml.save(coreml_file_name)
print('saved coreml model...')
open('cml.spec.txt', "w").write(str(cml.get_spec()))
if name == 'main':
main()
_add_conv_like_op with rank 3 -> generates duplicate output names if in series and fails in validation on xcode
## System environment (please complete the following information):
- coremltools version : 3.0
- onnx-coreml version: 1.0
- OS (e.g., MacOS, Linux): MacOS
- macOS version (if applicable): 10.13.6 (17G65)
- How you install python (anaconda, virtualenv, system): virtualenv
- python version (e.g. 3.7): 3.6
- any other relevant information:
## Additional context
proposed fix:
use node name instead of original output name to build the expanded output names:
Index: venv/lib/python3.6/site-packages/onnx_coreml/_operators_nd.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
🐞Describe the bug
when converting the attached model, xcode complains about duplicate output names during validation:
validator error: Layer '27_ip_expand' produces an output named '27_expanded' which is also an output produced by the layer '28_expand'.
Trace
not applicable
To Reproduce
def causal_pad_size(dilation, filter_size): return int(dilation * (filter_size - 1))
def causal_pad(x, dilation, filter_size, dimension=2): if filter_size > 1: pad_size = causal_pad_size(dilation, filtersize) pads = [0 for in range(4)] pads[(dimension - 1) * 2] = pad_size x = torch_func.pad(x, pads, mode='constant') return x
class Net(nn.Module):
fix for https://github.com/onnx/onnx-coreml/issues/498
def _convert_pad(builder, node, graph, err): ''' convert to CoreML Padding / ConstantPadding Layer: https://github.com/apple/coremltools/blob/655b3be5cc0d42c3c4fa49f0f0e4a93a26b3e492/mlmodel/format/NeuralNetwork.proto#L4397 https://github.com/apple/coremltools/blob/655b3be5cc0d42c3c4fa49f0f0e4a93a26b3e492/mlmodel/format/NeuralNetwork.proto#L1822 ''' mode = node.attrs.get('mode', 'constant')
def main(): batch_size = 1 receptive_field = 2 input_feat_size = 3
if name == 'main': main()
_add_conv_like_op with rank 3 -> generates duplicate output names if in series and fails in validation on xcode
Index: venv/lib/python3.6/site-packages/onnx_coreml/_operators_nd.py IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
--- venv/lib/python3.6/site-packages/onnx_coreml/_operators_nd.py (date 1571925857000) +++ venv/lib/python3.6/site-packages/onnx_coreml/_operators_nd.py (date 1571925857000) @@ -46,16 +46,17 @@ add_func(node.inputs, node.outputs, params_dict=params_dict, builder=builder, node=node, graph=graph, err=err) elif rank == 3: axes = [0, 3]
Make 5d tensor
Add conversion op