coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

Array input with integers results in "value type not convertible"

Open znation opened this issue 7 years ago • 6 comments

Repro steps:

import coremltools
import numpy as np

spec = coremltools.proto.Model_pb2.Model()
spec.specificationVersion = 1
spec.identity.MergeFromString(b'')
input = spec.description.input.add()
input.type.multiArrayType.shape.append(3)
input.type.multiArrayType.dataType = coremltools.proto.FeatureTypes_pb2.ArrayFeatureType.INT32
input.name = "input"
output = spec.description.output.add()
output.type.multiArrayType.shape.append(3)
output.type.multiArrayType.dataType = coremltools.proto.FeatureTypes_pb2.ArrayFeatureType.INT32
output.name = "output"
model = coremltools.models.MLModel(spec)
model.predict({'input': [1,2,3]})

Expected: something like

{'input': np.array([ 1.,  2.,  3.])}

Actual:

/Users/zach/venv/lib/python2.7/site-packages/coremltools/models/model.pyc in predict(self, data, useCPUOnly, **kwargs)
    318 
    319         if self.__proxy__:
--> 320             return self.__proxy__.predict(data,useCPUOnly)
    321         else:
    322             if _macos_version() < (10, 13):

RuntimeError: value type not convertible

Note that changing the input to [1.0, 2, 3] seems to fix the issue; so despite the multiArrayType being INT32, it only seems to allow float input (at least in some cases).

znation avatar Jun 12 '18 00:06 znation

I see this too. Thanks for the workaround!

lauriebyrum avatar Dec 17 '18 23:12 lauriebyrum

Faced the same error. I was able to resolve it by using float64 numpy array input_array.astype(np.float64)

openjamoses avatar Nov 06 '21 10:11 openjamoses

I'm fairly certain I read that CoreML has a max precision of 32. I had a PyTorch model that had input types of type long or torch.int64. Trying to call the predict() method on my mlmodel raised this error. My solution was to cast to type np.int32 when calling predict:

y = model.predict('one_hot': one_hot_array.astype(np.int32))

Doing so solved my issue

porterjenkins avatar Feb 28 '22 23:02 porterjenkins

With coremltools 6.0, the predict call from the original code now hangs.

TobyRoseman avatar Oct 24 '22 18:10 TobyRoseman

float arithmetic might be wrong in some cases with large integer values (e.g. time series)

den-run-ai avatar Mar 27 '23 14:03 den-run-ai

+1 seems like this is still an issue?

darylsew avatar Sep 29 '24 04:09 darylsew