anomalib icon indicating copy to clipboard operation
anomalib copied to clipboard

export patchcore to onnx error

Open mrljwlm opened this issue 2 years ago • 3 comments

RuntimeError: Exporting the operator cdist to ONNX opset version 11 is not supported

mrljwlm avatar Jul 18 '22 06:07 mrljwlm

As state in the error, ONNX does not yet support cdist, see

https://github.com/onnx/onnx/issues/2442

ORippler avatar Jul 18 '22 09:07 ORippler

You could replace the torch.cdist by a simple implementation of the euclidian distance like this one: https://discuss.pytorch.org/t/understanding-cdist-function/76296/13

def my_cdist(x1, x2):
    x1_norm = x1.pow(2).sum(dim=-1, keepdim=True)
    x2_norm = x2.pow(2).sum(dim=-1, keepdim=True)
    res = torch.addmm(x2_norm.transpose(-2, -1), x1, x2.transpose(-2, -1), alpha=-2).add_(x1_norm)
    res = res.clamp_min_(1e-30).sqrt_()
    return res

I guess the operators are all supported in onnx.

or maybe even just: https://discuss.pytorch.org/t/efficient-distance-matrix-computation/9065/5

alexriedel1 avatar Jul 21 '22 08:07 alexriedel1

Just a quick update from my side:

support for torch.cdist was added in PyTorch 1.12 by partitioning/refactorign cdist via supported ops in onnx (see this PR, so export to onnx should work and does work.

However, the exports of all models that use gaussian blur from kornia fails in 1.12, see here.

Side note: You won't be able to convert patchcore for application on VPUs though, see here

ORippler avatar Aug 02 '22 06:08 ORippler

@ORippler Thanks for the info here! I tried to install pytorch 1.12 with anomalib 0.35. But pytorch version was automatically degraded to 1.11. I suspect the reason why is because that newest pytorch version is only compatible with torchvision 0.13 for now according to this page.

torch torchvision python
main / nightly main / nightly >=3.7, <=3.10
1.11.0 0.12.0 >=3.7, <=3.10
1.10.2 0.11.3 >=3.6, <=3.9

anomalib has currently torchvision>=0.9.1,<=0.12.0 as dependency set in requirements/base.txt Because of this, I can't upgrade to pytorch 1.12. Is there any plan to cover torchvision 0.13.x (latest is 0.13.1 as of today) in dependency so that we can use torch 1.12 for onnx conversion?

fujikosu avatar Aug 18 '22 13:08 fujikosu

I just overrode those requirements manually, and this seemed to work out locally (at least for my testings with EfficientNets + onnx export of patchcore). So you could always do that. But if the test suite passes, we could just up the pinned version in the requirements.

Edit: NVM, torch 1.12 seems to break some components of anomalib, see #518

ORippler avatar Aug 30 '22 06:08 ORippler

Thanks @alexriedel1 and @ORippler for looking into this error. I am closing this issue as dependency for lightning has been increased to 0.13.0. onnx conversion should work fine with those using torch 1.12. But for those using lower version of torch, refer to @alexriedel1 's solution.

ashwinvaidya17 avatar Sep 22 '22 13:09 ashwinvaidya17