Coremltools: Can not use -1 for batch in shape

Created on 26 Aug 2020  路  8Comments  路  Source: apple/coremltools

馃悶Describe the bug

When I used version 4.0b2 I was able to use -1 for batch in shape.
Now with version 4.0b3 it shows me error - Dimension cannot be None of -1. Use ct.RangeDim for runtime determined dimension. Dim 0: -1 See https://coremltools.readme.io/docs/flexible-inputs

Trace

import coremltools as ct
shape = ct.Shape(shape=(-1,224,224,3), default=(1,224,224,3))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/coremltools/converters/mil/input_types.py", line 300, in __init__
    raise ValueError(msg.format(idx, s))
ValueError: Dimension cannot be None of -1. Use ct.RangeDim for runtime determined dimension. Dim 0: -1 See https://coremltools.readme.io/docs/flexible-inputs

To Reproduce

  • If a python script can reproduce the error, please paste the code snippet
import coremltools as ct
shape = ct.Shape(shape=(-1,224,224,3), default=(1,224,224,3))
  • If applicable, please attach the source model
  • If it is a model conversion issue and the conversion succeeds, however, if there is a numerical mismatch between the original and the coreml model, please paste script used for comparison.

System environment (please complete the following information):

  • coremltools version (e.g., 3.0b5):4.0b3
  • OS (e.g., MacOS, Linux): Linux
  • macOS version (if applicable):
  • XCode version (if applicable):
  • How you install python (anaconda, virtualenv, system): pip install
  • python version (e.g. 3.7): 3.7
  • any other relevant information: Tensorflow 2.2.0

    • e.g. keras version if using keras conversion etc.

Additional context

Add any other context about the problem here.

bug

All 8 comments

Have you tried something like ct.Shape(shape=(ct.RangeDim(1, 32),224,224,3)) ?
Per examples in https://coremltools.readme.io/docs/flexible-inputs
In any case, the documentation might need an update if '-'1 is retired in favor of RangeDim()

Yes, RangeDim works. But I need the batch to be -1 (any batch size). And it worked in 4.0b2.

Looks like None or -1 are not allowed. Related PR https://github.com/apple/coremltools/pull/858

Docs should be updated: https://coremltools.readme.io/docs/flexible-inputs
It still says

A -1 denoted in shape acts like None in Tensorflow's placeholder. It allows arbitrary size of input in that dimension.

@apivovarov The API for the coremltools 4.0b3 is changed,
now you need to use ct.RangeDim() instead if -1.
Please reopen the issue if it doesn't fix your problem.

Nice! The doc has been updated for the new API:
https://coremltools.readme.io/docs/flexible-inputs.
Let us know if you get further question.

Thank you for looking into it.
I still see a couple places in the documentation which might be misleading

Case 1

It's a natural choice of using -1 for all flexible shaped dimension. However, it is recommended to use EnumeratedShapes for best performance

Not it is not a recommendation any more. -1 is not allowed.

Case 2

shape = ct.Shape(shape=(1, -1, 1024), default=(1, 50, 1024))

It is going to fail

@apivovarov I did a pass on the docs for this page. Hopefully it's more accurate now. Thanks for the feedback!

Was this page helpful?
0 / 5 - 0 ratings