openvinotoolkit / anomalib

An anomaly detection library comprising state-of-the-art algorithms and features such as experiment management, hyper-parameter optimization, and edge inference.
https://anomalib.readthedocs.io/en/latest/
Apache License 2.0
3.68k stars 653 forks source link

[Bug]: WinCLIP export pytorch model issue #2202

Open junxnone opened 2 months ago

junxnone commented 2 months ago

Describe the bug

WinCLIP cannot export pytorch model

Dataset

MVTec

Model

Other (please specify in the field below)

Steps to reproduce the behavior

from anomalib.data import MVTec
from anomalib.models import WinClip
from anomalib.engine import Engine
from anomalib.deploy import ExportType

datamodule = MVTec()
model = WinClip()

engine = Engine()
engine.fit(datamodule=datamodule, model=model)
engine.export(model, export_type=ExportType.TORCH)

OS information

OS information:

Expected behavior

export pytorch model and inference correctly

Screenshots

No response

Pip/GitHub

pip

What version/branch did you use?

main

Configuration YAML

none

Logs

File "/media/xxx/test_winclip/test.py", line 44, in <module>
    engine.export(model, export_type=ExportType.TORCH)
  File "/home/xxx/anomalib/src/anomalib/engine/engine.py", line 944, in export
    exported_model_path = model.to_torch(
  File "/home/xxx/anomalib/src/anomalib/models/components/base/export_mixin.py", line 89, in to_torch
    torch.save(
  File "/home/xxx/winclip_env/lib/python3.10/site-packages/torch/serialization.py", line 628, in save
    _save(obj, opened_zipfile, pickle_module, pickle_protocol, _disable_byteorder_record)
  File "/home/xxx/winclip_env/lib/python3.10/site-packages/torch/serialization.py", line 840, in _save
    pickler.dump(obj)
AttributeError: Can't pickle local object 'WinClipModel.encode_image.<locals>.get_feature_map.<locals>.hook'


### Code of Conduct

- [X] I agree to follow this project's Code of Conduct
pzqian commented 1 month ago

hit the same issue - did you find a workaround?