apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.44k stars 643 forks source link

Concatenation support PyTorch -> CoreML #1040

Open MarkoKostiv opened 3 years ago

MarkoKostiv commented 3 years ago

🐞Describe the bug

Unexpected behavior with 0 size tensors concatenation from PyTorch

To Reproduce

model = ct.convert(prog)

Make a prediction with CoreML

prediction = model.predict({ 'x': np.random.rand(0, 100, 100).astype(np.float32), 'y': np.random.rand(0, 100, 120).astype(np.float32) })

prediction



Running MIL optimization passes: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 17/17 [00:00<00:00, 7773.16 passes/s]
Translating MIL ==> MLModel Ops: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 3/3 [00:00<00:00, 6594.82 ops/s]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-121-ed4d7bc6db2d> in <module>
     10 prediction = model.predict({
     11   'x': np.random.rand(0, 100, 100).astype(np.float32),
---> 12   'y': np.random.rand(0, 100, 120).astype(np.float32)
     13 })
     14 

~/.pyenv/versions/3.7.0/envs/twilio-test/lib/python3.7/site-packages/coremltools/models/model.py in predict(self, data, useCPUOnly, **kwargs)
    365             else:
    366                 if self._framework_error:
--> 367                     raise self._framework_error
    368                 else:
    369                     raise Exception(

~/.pyenv/versions/3.7.0/envs/twilio-test/lib/python3.7/site-packages/coremltools/models/model.py in _get_proxy_and_spec(filename, use_cpu_only)
    111 
    112         try:
--> 113             return (_MLModelProxy(filename, use_cpu_only), specification, None)
    114         except RuntimeError as e:
    115             _warnings.warn(

RuntimeError: Error compiling model: "compiler error:  Blob with zero size found: x".

## System environment (please complete the following information):
 - coremltools version  '4.0b4':
 - torch version 1.6.0
TobyRoseman commented 3 years ago

This is still an issue with coremltools 5.0.

I'm not sure this is worth "supporting". What is the purpose of handling a tensor where the first dimension has zero length?

MarkoKostiv commented 3 years ago

We include detection model post-processing (thresholding, masking) into the CoreML graph, and the number of detected objects can be zero. PyTorch pipeline successfully outputs an empty tensor