apple / apple/coremltools

Unrecognized sequence type in KNearestNeighbors

Open
#1,206 8 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## 🐞Describe the bug
I am trying to test a KNearestNeighbors classifier. The classifier is made using `KNearestNeighborsClassifierBuilder`. When I load and test the `mlmodel` file, I encounter the error "RuntimeError: Error: Unrecognized sequence type."

## Trace

```
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
in
1 test_input = np.random.rand(20)
----> 2 model.predict({'my_in': test_input})

~/opt/anaconda3/lib/python3.7/site-packages/coremltools/models/model.py in predict(self, data, useCPUOnly, **kwargs)
326
327 if self.__proxy__:
--> 328 return self.__proxy__.predict(data, useCPUOnly)
329 else:
330 if _macos_version() < (10, 13):

RuntimeError: Error: Unrecognized sequence type.
```

## To Reproduce

Here is a minimal example: first create the classifier:
```
from coremltools.models.nearest_neighbors import KNearestNeighborsClassifierBuilder
from coremltools.models.utils import save_spec

my_inputs = np.random.rand(10,20)
my_outputs = (5* np.random.rand(10)).astype(int)

builder = KNearestNeighborsClassifierBuilder(
input_name='my_in',
output_name='my_out',
number_of_dimensions=my_inputs.shape[1],
default_class_label=0,
number_of_neighbors=20
)

save_spec(builder.spec, "test.mlmodel")
```

Then load and test it:
```
model = coremltools.models.MLModel('test.mlmodel')
test_input = np.random.rand(20)
model.predict({'my_in': test_input}) # Here the error is thrown
```

## System environment (please complete the following information):
- coremltools version 4.1
- MacOS 11.4
- python anaconda 3.7.4
- Run over `jupyter-lab` v. 1.1.4

## Related

[Related #898](https://github.com/apple/coremltools/issues/898) was closed but no explanation.

Contributor guide

Open the contributing guide

Research direction

Start with KNearestNeighborsClassifierBuilder and the MLModel.predict path shown in the reproduction, focusing on how the 20-element NumPy input is interpreted. Re-run the minimal example from the issue and trace the sequence-type handling. Done means the generated test.mlmodel loads and predicts with the provided input without raising "Unrecognized sequence type."

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.