apple / apple/coremltools

Input shape ignored when converting from milinternal

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

Description

## ❓Question

I am building a custom MIL program using the Python `Builder` class for MIL.
As stated in the coremltools API Reference I am starting my custom program as follows:

```python
import coremltools as ct
from coremltools.converters.mil import Builder as mb
@mb.program(input_specs=[mb.TensorSpec(shape=(1,3,640,640))])
def prog(a):
return mb.add(x=a, y=2)
```

How can I assign a flexible input/enumerated input shape to the program?

It seems that converting the program to a MLPackage using `ct.convert()` and using the attributes `inputs` and/or `outputs` do not affect the converted model. In the code snippet below I specifically renamed the input name to `new_a`. This code snippet will work just fine and will convert the program without any issues. However, if I open the MLProgram in Xcode and look at the Predictions tab I cannot see the changes applied to the input (see image bellow).

Is there a way to give the program a flexible input?

Full code sample:
```python
import coremltools as ct
from coremltools.converters.mil import Builder as mb

@mb.program(input_specs=[mb.TensorSpec(shape=(1,3,640,640))])
def prog(a):
return mb.add(x=a, y=2.0)

# Set the input_shape to use EnumeratedShapes.
input_shape = ct.EnumeratedShapes(shapes=[[1, 3, 640, 640],
[1, 3, 320, 320]],
default=[1, 3, 640, 640])
mlmodel = ct.convert(prog,
convert_to="mlprogram",
inputs=[ct.TensorType(name="a", shape=input_shape)],
compute_precision=ct.precision.FLOAT16,)
mlmodel.save("simple_test.mlpackage")
```

![Screenshot 2023-10-26 at 12 12 54](https://github.com/apple/coremltools/assets/88430451/c0ca3ab5-ee65-4896-a0ab-6d6f8a537b00)

Environment:

- coremltools version: 7.0
- macOS Ventura 13.3.1 (MacBook Pro M1 Pro)

Contributor guide

Open the contributing guide

Research direction

Start with the ct.convert() path for a MIL program, focusing on how inputs, TensorType, and EnumeratedShapes are handled for MLProgram conversion. Compare the generated MLPackage metadata with the supplied name and shapes; done means the renamed input and enumerated dimensions are visible in the converted model and Xcode Predictions tab.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.