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.4k stars 635 forks source link

Cannot convert torchvision models that have multi-dimensional dictionary outputs (even with a wrapper). #1697

Open diegogranziol opened 1 year ago

diegogranziol commented 1 year ago

🐞Describing the bug

ValueError: tensor should have value of type ndarray, got <class 'numpy.float32'> instead

To Reproduce

class Wrapped(nn.Module): def init(self): super(Wrapped, self).init() self.model = model.eval() def forward(self, x): res = self.model(x)

Extract the tensor we want from the output dictionary

    x = res[0]['boxes']
    return x

new_model = Wrapped().eval() traced_model = torch.jit.trace(new_model,image) mlmodel = ct.convert(traced_model, inputs=[ct.TensorType(shape=[1,3, 400, 400])], source="pytorch", convert_to="mlprogram")


- If the model conversion succeeds, but there is a numerical mismatch in predictions, please include the code used for comparisons.

## System environment (please complete the following information):
 - coremltools version: 6.1
 - OS (e.g. MacOS version or Linux type): MacOS and Linux
 - Any other relevant version information (e.g. PyTorch or TensorFlow version): pytorch==1.12.1 torchvision==0.13.1 

## Additional context
- Add anything else about the problem here that you want to share.
TobyRoseman commented 1 year ago

With pytorch==1.12.1 and torchvision==0.13.1, I get an error when calling into torch.jit.trace before ct.convert is even called. I think this is an issue with torch or with this model. I don't think this is a coremltools error.