Onnx to coreml model conversion

972 views Asked by At

I am converting pytprch model into coreml via onnx . I am having a memory error while using onnx_coreml convertor ( pic attached). Code: '''

import torch
import torchvision
from onnx_coreml import convert
import torch
from  model import U2NET
 Create the model and load the weights
model = U2NET()
model.load_state_dict(torch.load('u2net.pth',map_location=torch.device('cpu')))
 Create dummy input
.rand(1, 3, 32, 32)
 Define input / output names
input_names = ["my_input"]
output_names = ["my_output"]
 Convert the PyTorch model to ONNX
torch.onnx.export(model,
                "unets.onnx",
                verbose=True,
                input_names=input_names,
               output_names=output_names)
 Step 2 - ONNX to CoreML model
mlmodel = convert(model='./unets.onnx', minimum_ios_deployment_target='13')
 Save converted CoreML model
mlmodel.save('my_model.mlmodel')

This is the error I get at step 2

Traceback (most recent call last):
  File "/Users/apple/Downloads/IOS_project/main/py.py", line 28, in <module>
    mlmodel = convert(model='./unets.onnx', minimum_ios_deployment_target='13')
  File "/Users/apple/Downloads/IOS_project/venv/lib/python3.7/site-packages/onnx_coreml/converter.py", line 501, in convert
    graph = _prepare_onnx_graph(onnx_model.graph, transformers, onnx_model.ir_version)
  File "/Users/apple/Downloads/IOS_project/venv/lib/python3.7/site-packages/onnx_coreml/converter.py", line 373, in _prepare_onnx_graph
    graph_ = graph_.transformed(transformers)
  File "/Users/apple/Downloads/IOS_project/venv/lib/python3.7/site-packages/onnx_coreml/_graph.py", line 201, in transformed
    return _apply_graph_transformations(graph, transformers) # type: ignore
  File "/Users/apple/Downloads/IOS_project/venv/lib/python3.7/site-packages/onnx_coreml/_graph.py", line 60, in _apply_graph_transformations
    graph = transformer(graph)
  File "/Users/apple/Downloads/IOS_project/venv/lib/python3.7/site-packages/onnx_coreml/_transformers.py", line 768, in __call__
    output = np.take(x, range(s, e), axis=a) # type: ignore
  File "<__array_function__ internals>", line 6, in take
  File "/Users/apple/Downloads/IOS_project/venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 191, in take
    return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)
  File "/Users/apple/Downloads/IOS_project/venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 58, in _wrapfunc
    return bound(*args, **kwds)
MemoryError*
0

There are 0 answers