onnx-coreml icon indicating copy to clipboard operation
onnx-coreml copied to clipboard

non-classifier case in coremltools4.0+

Open Guanbin-Huang opened this issue 4 years ago • 28 comments

❓Question

Is it possible to convert a pytorch model withtout classification layer into mlmodel? I don't know how to do it because in the example, I am asked to specify the label.

System Information

  • If applicable

Guanbin-Huang avatar Jun 29 '20 19:06 Guanbin-Huang

Just don't provide the classifier_config argument to the ct.convert call, as shown here

aseemw avatar Jun 29 '20 20:06 aseemw

Hi, have you ever encountered this situation? image One day ago, I convert my pytorch model to onnx. It works well. But now I use your tool to do the conversion directly. It seems not good.

Guanbin-Huang avatar Jun 29 '20 20:06 Guanbin-Huang

This is the complete error. TypeError: Input strides has type <class 'coremltools.converters.mil.mil.types.type_tensor.tensor..tensor'> not compatible with expected type IntTensorInputType

Guanbin-Huang avatar Jun 29 '20 20:06 Guanbin-Huang

it works well until the 7-th part image

Guanbin-Huang avatar Jun 29 '20 20:06 Guanbin-Huang

I run the whole thing on colab.

Guanbin-Huang avatar Jun 29 '20 20:06 Guanbin-Huang

Hi, have you ever encountered this situation? [image: image] https://user-images.githubusercontent.com/30463291/86051128-d90cd880-ba4c-11ea-88aa-c3038dafa5d6.png One day ago, I convert my pytorch model to onnx. It works well. But now I use your tool to do the conversion directly. It seems not good.

This is the complete error. TypeError: Input strides has type <class 'coremltools.converters.mil.mil.types.type_tensor.tensor..tensor'> not compatible with expected type IntTensorInputType

it works well until the 7-th part [image: image] https://user-images.githubusercontent.com/30463291/86051706-be872f00-ba4d-11ea-9354-b908148d270e.png

On Mon, 29 Jun 2020 at 21:03, Aseem Wadhwa [email protected] wrote:

Just don't provide the classifier_config argument to the ct.convert call, as shown here https://coremltools.readme.io/docs/unified-conversion-api#conversion-from-pytorch

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/581#issuecomment-651329773, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHINKO6DHUZADM3JUWXVDR3RZDXRPANCNFSM4OLRJ5WA .

Guanbin-Huang avatar Jun 29 '20 21:06 Guanbin-Huang

@huangguanbin can you please provide the pytorch model, which can reproduce this error? Otherwise its hard to debug the error.

aseemw avatar Jun 29 '20 22:06 aseemw

no problem. I will do it right now. Give me a second.

On Mon, Jun 29, 2020 at 11:23 PM Aseem Wadhwa [email protected] wrote:

@huangguanbin https://github.com/huangguanbin can you please provide the pytorch model, which can reproduce this error? Otherwise its hard to debug the error.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/581#issuecomment-651401396, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHINKO26V5E5WAGMSM5RQ23RZEH63ANCNFSM4OLRJ5WA .

Guanbin-Huang avatar Jun 29 '20 22:06 Guanbin-Huang

https://colab.research.google.com/drive/1hfv_Qbl6ZHDbXq_HB9bo3tV89KM7Skoq?usp=sharing This is the notebook where you can reproduce the bug. And you can download the pytorch model by clicking the shared link. It contains a zip file. https://drive.google.com/file/d/1etvQPPGlr4YxBEsCgOeduicErYN18BLg/view?usp=sharing All you need to do is just go through the notebook. Make sure the zip file is in the /content folder. The checkpoint(aka the pytorch model itself) is in the /AlignedReID-master/util/log if you need to access it.

On Mon, Jun 29, 2020 at 11:24 PM 黄冠斌 [email protected] wrote:

no problem. I will do it right now. Give me a second.

On Mon, Jun 29, 2020 at 11:23 PM Aseem Wadhwa [email protected] wrote:

@huangguanbin https://github.com/huangguanbin can you please provide the pytorch model, which can reproduce this error? Otherwise its hard to debug the error.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/581#issuecomment-651401396, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHINKO26V5E5WAGMSM5RQ23RZEH63ANCNFSM4OLRJ5WA .

Guanbin-Huang avatar Jun 29 '20 22:06 Guanbin-Huang

If you encountered any issue to run my code, plz let me know ASAP. I will reply to you on time.

Guanbin-Huang avatar Jun 29 '20 22:06 Guanbin-Huang

The colab is not accessible...

aseemw avatar Jun 29 '20 22:06 aseemw

Is the zip accessible "?

On Mon, 29 Jun 2020 at 23:52, Aseem Wadhwa [email protected] wrote:

The colab is not accessible...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/581#issuecomment-651409968, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHINKOZJCTOK4TH6Z3O5AVLRZELKBANCNFSM4OLRJ5WA .

Guanbin-Huang avatar Jun 29 '20 22:06 Guanbin-Huang

https://colab.research.google.com/drive/1hfv_Qbl6ZHDbXq_HB9bo3tV89KM7Skoq?usp=sharing try this one.

On Mon, Jun 29, 2020 at 11:52 PM 黄冠斌 [email protected] wrote:

Is the zip accessible "?

On Mon, 29 Jun 2020 at 23:52, Aseem Wadhwa [email protected] wrote:

The colab is not accessible...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/581#issuecomment-651409968, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHINKOZJCTOK4TH6Z3O5AVLRZELKBANCNFSM4OLRJ5WA .

Guanbin-Huang avatar Jun 29 '20 22:06 Guanbin-Huang

if it is not accessible again, copy the code below onto colab.

!unzip AlignedReID-master.zip !pip install coremltools==4.0b1

%cd AlignedReID-master/

import torch from util.FeatureExtractor import FeatureExtractor from torchvision import transforms from IPython import embed import models from scipy.spatial.distance import cosine, euclidean from util.utils import * from sklearn.preprocessing import normalize

def pool2d(tensor, type= 'max'): sz = tensor.size() if type == 'max': x = torch.nn.functional.max_pool2d(tensor, kernel_size=(int(sz[2]/8 ), sz[3])) if type == 'mean': x = torch.nn.functional.mean_pool2d(tensor, kernel_size=(int(sz[2]/8 ), sz[3])) x = x[0].cpu().data.numpy() x = np.transpose(x,(2,1,0))[0] return x

if name == 'main': os.environ['CUDA_VISIBLE_DEVICES'] = "0" use_gpu = torch.cuda.is_available() if torch.cuda.is_available(): map_location = lambda storage, loc: storage.cuda() else: map_location = 'cpu'

model = models.init_model(name='resnet50', num_classes=751, loss={ 'softmax', 'metric'}, use_gpu=use_gpu,aligned=True)

checkpoint = torch.load( "/content/AlignedReID-master/util/log/checkpoint_ep300.pth.tar" , map_location=map_location) model.load_state_dict(checkpoint['state_dict'])

model.eval()

Trace with random data

example_input = torch.rand(1, 3, 256,128) traced_model = torch.jit.trace(model, example_input)

traced_model.save("reid.pt") import coremltools as ct

Convert the saved PyTorch model to Core ML

mlmodel = ct.convert("reid.pt", inputs=[ct.TensorType(shape=(1, 3, 256, 128))])

On Mon, Jun 29, 2020 at 11:54 PM 黄冠斌 [email protected] wrote:

https://colab.research.google.com/drive/1hfv_Qbl6ZHDbXq_HB9bo3tV89KM7Skoq?usp=sharing try this one.

On Mon, Jun 29, 2020 at 11:52 PM 黄冠斌 [email protected] wrote:

Is the zip accessible "?

On Mon, 29 Jun 2020 at 23:52, Aseem Wadhwa [email protected] wrote:

The colab is not accessible...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/581#issuecomment-651409968, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHINKOZJCTOK4TH6Z3O5AVLRZELKBANCNFSM4OLRJ5WA .

Guanbin-Huang avatar Jun 29 '20 22:06 Guanbin-Huang

I changed the link of zip file by mistake. If you want to download it again. use this one. https://drive.google.com/file/d/1ufh5kndXMdWfKPRT85dDtEwhZ1MXnPg7/view?usp=sharing

Guanbin-Huang avatar Jun 29 '20 23:06 Guanbin-Huang

I would appreciate it if you can also check how precise the conversion is. Because some say conversion leads to loss of precision. That means given same input matrix but get different output matrix. THANK YOU!!

On Mon, Jun 29, 2020 at 11:55 PM 黄冠斌 [email protected] wrote:

if it is not accessible again, copy the code below onto colab.

!unzip AlignedReID-master.zip !pip install coremltools==4.0b1

%cd AlignedReID-master/

import torch from util.FeatureExtractor import FeatureExtractor from torchvision import transforms from IPython import embed import models from scipy.spatial.distance import cosine, euclidean from util.utils import * from sklearn.preprocessing import normalize

def pool2d(tensor, type= 'max'): sz = tensor.size() if type == 'max': x = torch.nn.functional.max_pool2d(tensor, kernel_size=(int(sz[2]/8 ), sz[3])) if type == 'mean': x = torch.nn.functional.mean_pool2d(tensor, kernel_size=(int(sz[2]/8 ), sz[3])) x = x[0].cpu().data.numpy() x = np.transpose(x,(2,1,0))[0] return x

if name == 'main': os.environ['CUDA_VISIBLE_DEVICES'] = "0" use_gpu = torch.cuda.is_available() if torch.cuda.is_available(): map_location = lambda storage, loc: storage.cuda() else: map_location = 'cpu'

model = models.init_model(name='resnet50', num_classes=751, loss={ 'softmax', 'metric'}, use_gpu=use_gpu,aligned=True)

checkpoint = torch.load( "/content/AlignedReID-master/util/log/checkpoint_ep300.pth.tar" , map_location=map_location) model.load_state_dict(checkpoint['state_dict'])

model.eval()

Trace with random data

example_input = torch.rand(1, 3, 256,128) traced_model = torch.jit.trace(model, example_input)

traced_model.save("reid.pt") import coremltools as ct

Convert the saved PyTorch model to Core ML

mlmodel = ct.convert("reid.pt", inputs=[ct.TensorType(shape=(1, 3, 256, 128))])

On Mon, Jun 29, 2020 at 11:54 PM 黄冠斌 [email protected] wrote:

https://colab.research.google.com/drive/1hfv_Qbl6ZHDbXq_HB9bo3tV89KM7Skoq?usp=sharing try this one.

On Mon, Jun 29, 2020 at 11:52 PM 黄冠斌 [email protected] wrote:

Is the zip accessible "?

On Mon, 29 Jun 2020 at 23:52, Aseem Wadhwa [email protected] wrote:

The colab is not accessible...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/581#issuecomment-651409968, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHINKOZJCTOK4TH6Z3O5AVLRZELKBANCNFSM4OLRJ5WA .

Guanbin-Huang avatar Jun 30 '20 00:06 Guanbin-Huang

Any progress on this?

On Tue, 30 Jun 2020 at 01:13, 黄冠斌 [email protected] wrote:

I would appreciate it if you can also check how precise the conversion is. Because some say conversion leads to loss of precision. That means given same input matrix but get different output matrix. THANK YOU!!

On Mon, Jun 29, 2020 at 11:55 PM 黄冠斌 [email protected] wrote:

if it is not accessible again, copy the code below onto colab.

!unzip AlignedReID-master.zip !pip install coremltools==4.0b1

%cd AlignedReID-master/

import torch from util.FeatureExtractor import FeatureExtractor from torchvision import transforms from IPython import embed import models from scipy.spatial.distance import cosine, euclidean from util.utils import * from sklearn.preprocessing import normalize

def pool2d(tensor, type= 'max'): sz = tensor.size() if type == 'max': x = torch.nn.functional.max_pool2d(tensor, kernel_size=(int(sz[2]/8 ), sz[3])) if type == 'mean': x = torch.nn.functional.mean_pool2d(tensor, kernel_size=(int(sz[2]/ 8), sz[3])) x = x[0].cpu().data.numpy() x = np.transpose(x,(2,1,0))[0] return x

if name == 'main': os.environ['CUDA_VISIBLE_DEVICES'] = "0" use_gpu = torch.cuda.is_available() if torch.cuda.is_available(): map_location = lambda storage, loc: storage.cuda() else: map_location = 'cpu'

model = models.init_model(name='resnet50', num_classes=751, loss={ 'softmax', 'metric'}, use_gpu=use_gpu,aligned=True)

checkpoint = torch.load( "/content/AlignedReID-master/util/log/checkpoint_ep300.pth.tar" , map_location=map_location) model.load_state_dict(checkpoint['state_dict'])

model.eval()

Trace with random data

example_input = torch.rand(1, 3, 256,128) traced_model = torch.jit.trace(model, example_input)

traced_model.save("reid.pt") import coremltools as ct

Convert the saved PyTorch model to Core ML

mlmodel = ct.convert("reid.pt", inputs=[ct.TensorType(shape=(1, 3, 256, 128))])

On Mon, Jun 29, 2020 at 11:54 PM 黄冠斌 [email protected] wrote:

https://colab.research.google.com/drive/1hfv_Qbl6ZHDbXq_HB9bo3tV89KM7Skoq?usp=sharing try this one.

On Mon, Jun 29, 2020 at 11:52 PM 黄冠斌 [email protected] wrote:

Is the zip accessible "?

On Mon, 29 Jun 2020 at 23:52, Aseem Wadhwa [email protected] wrote:

The colab is not accessible...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/581#issuecomment-651409968, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHINKOZJCTOK4TH6Z3O5AVLRZELKBANCNFSM4OLRJ5WA .

Guanbin-Huang avatar Jun 30 '20 14:06 Guanbin-Huang

I had the same issue (TypeError: Input strides has type <class 'coremltools.converters.mil.mil.types.type_tensor.tensor..tensor'> not compatible with expected type IntTensorInputType). I am wondering if it is the issue related compatibility between coreML tensor and torch Tensor?

JialeHu avatar Jun 30 '20 21:06 JialeHu

any progress?

Guanbin-Huang avatar Jul 02 '20 09:07 Guanbin-Huang

i had same issue: TypeError: Input strides has type <class 'coremltools.converters.mil.mil.types.type_tensor.tensor..tensor'> not compatible with expected type IntTensorInputType

quangphap208 avatar Jul 05 '20 04:07 quangphap208

Seems like the error message for type mismatch of input tensors is not very descriptive. @quangphap208 @JialeHu Which models are you hitting this error on? Since type mismatch can happen in any model for different reasons. Some of the them might be bugs but others might be genuine errors. Therefore, this needs to investigated on a model basis.

DawerG avatar Jul 05 '20 16:07 DawerG

@huangguanbin Can you please re-convert your model after incorporating this following change?

Replace this line strides = inputs[2] in https://github.com/apple/coremltools/blob/e87f1d0d6ca1dc9cd812f14f777ead0bba70bf43/coremltools/converters/mil/frontend/torch/ops.py#L1421

with following code snippet

    strides = inputs[2]
    if strides.op.op_type == "const"  and (not strides.val):
        strides = mb.const(val=kernel_sizes.val, name=strides.name)

DawerG avatar Jul 05 '20 18:07 DawerG

Since this issue is not related to ONNX. For PyTorch related issues in the future, please use coremltools repository https://github.com/apple/coremltools.

DawerG avatar Jul 05 '20 18:07 DawerG

@huangguanbin This PR https://github.com/apple/coremltools/pull/769 fixes the reported issue. Please verify if it solves your problem.

DawerG avatar Jul 08 '20 20:07 DawerG

@huangguanbin This PR apple/coremltools#769 fixes the reported issue. Please verify if it solves your problem.

This solves the issue for me. Thanks a lot.

JialeHu avatar Jul 13 '20 01:07 JialeHu

strides = inputs[2]
if strides.op.op_type == "const"  and (not strides.val):
    strides = mb.const(val=kernel_sizes.val, name=strides.name)

I tried what you said, but it is not working yet.

Guanbin-Huang avatar Jul 14 '20 03:07 Guanbin-Huang

I checked the latest notification. But I'm not sure what i should modify.

Guanbin-Huang avatar Jul 14 '20 03:07 Guanbin-Huang

@JialeHu

Guanbin-Huang avatar Jul 14 '20 03:07 Guanbin-Huang