Coremltools: ValueError: unexpected number of inputs for node input.2 (_convolution): 13

Created on 27 Nov 2020  路  1Comment  路  Source: apple/coremltools

When I tried to convert a Pytorch model to CoreML, there is an error.
coremltools version: 4.0

Use cases

import torch
import numpy 
import coremltools as ct
from coremltools.models.neural_network import quantization_utils
from model import U2NET

model = U2NET()

model_in = "./saved_models/u2net_portrait.pth"

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.load_state_dict(torch.load(model_in, map_location=device))
model.cpu() 
model.eval()

dummy_input = torch.rand(1, 3, 512, 512)
traced_model = torch.jit.trace(model, dummy_input, check_trace=True)


converted_model = ct.converters.convert(
    traced_model,       # convert using Unified Conversion API
    inputs=[ct.TensorType(shape=dummy_input.shape)],
)

quantized_model = quantization_utils.quantize_weights(converted_model, 8)
converted_model.save(model_in.replace(".pth", "")+".mlmodel")
quantized_model.save(model_in.replace(".pth", "_quantized")+".mlmodel")

After running it in the terminal
python convert.py

it showed a error message


/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/torch/nn/functional.py:2952: UserWarning: nn.functional.upsample is deprecated. Use nn.functional.interpolate instead.
  warnings.warn("nn.functional.upsample is deprecated. Use nn.functional.interpolate instead.")
/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/torch/nn/functional.py:3060: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  warnings.warn("Default upsampling behavior when mode={} is changed "
/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/torch/nn/functional.py:1639: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.
  warnings.warn("nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.")
WARNING:root:Tuple detected at graph output. This will be flattened in the converted model.
Converting Frontend ==> MIL Ops:   1%|  | 13/1490 [00:00<00:00, 1738.65 ops/s]
Traceback (most recent call last):
  File "convert.py", line 24, in <module>
    converted_model = ct.converters.convert(
  File "/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/coremltools/converters/_converters_entry.py", line 176, in convert
    mlmodel = mil_convert(
  File "/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 128, in mil_convert
    proto = mil_convert_to_proto(model, convert_from, convert_to,
  File "/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 171, in mil_convert_to_proto
    prog = frontend_converter(model, **kwargs)
  File "/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 85, in __call__
    return load(*args, **kwargs)
  File "/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 85, in load
    raise e
  File "/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 75, in load
    prog = converter.convert()
  File "/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 224, in convert
    convert_nodes(self.context, self.graph)
  File "/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 56, in convert_nodes
    _add_op(context, node)
  File "/Users/leegang/Project/Python/pytorchToCoreML/venv/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 417, in _convolution
    raise ValueError(
ValueError: unexpected number of inputs for node input.2 (_convolution): 13

My system info :
image

question

Most helpful comment

This is due to the wrong version of pytorch, when i use Pytorch 1.6 , it fixed.

>All comments

This is due to the wrong version of pytorch, when i use Pytorch 1.6 , it fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

G-mel picture G-mel  路  5Comments

baaj2109 picture baaj2109  路  4Comments

skymaze picture skymaze  路  5Comments

ghost picture ghost  路  3Comments

haifengkao picture haifengkao  路  5Comments