alibi-detect
alibi-detect copied to clipboard
imdb reviews example: cannot serialize detector
Hello,
I am trying to follow the example here
However, I am facing an error:
at
save_detector(cd, filepath)
I get
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/alibi_detect/saving/saving.py:67, in save_detector(detector, filepath, legacy)
66 if isinstance(detector, ConfigurableDetector) and not legacy:
---> 67 _save_detector_config(detector, filepath)
69 # Otherwise, save via the previous meta and state_dict approach
70 else:
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/alibi_detect/saving/saving.py:160, in _save_detector_config(detector, filepath)
159 logger.info('Saving the preprocess_fn function.')
--> 160 preprocess_cfg = _save_preprocess_config(preprocess_fn, backend, cfg['input_shape'], filepath)
161 cfg['preprocess_fn'] = preprocess_cfg
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/alibi_detect/saving/saving.py:282, in _save_preprocess_config(preprocess_fn, backend, input_shape, filepath)
281 elif callable(v):
--> 282 src, _ = _serialize_object(v, filepath, local_path)
283 kwargs.update({k: src})
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/alibi_detect/saving/saving.py:338, in _serialize_object(obj, base_path, local_path)
337 with open(filepath.with_suffix('.dill'), 'wb') as f:
--> 338 dill.dump(obj, f)
339 src = str(local_path.with_suffix('.dill'))
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/dill/_dill.py:235, in dump(obj, file, protocol, byref, fmode, recurse, **kwds)
234 _kwds.update(dict(byref=byref, fmode=fmode, recurse=recurse))
--> 235 Pickler(file, protocol, **_kwds).dump(obj)
236 return
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/dill/_dill.py:394, in Pickler.dump(self, obj)
393 logger.trace_setup(self)
--> 394 StockPickler.dump(self, obj)
File /usr/lib/python3.8/pickle.py:487, in _Pickler.dump(self, obj)
486 self.framer.start_framing()
--> 487 self.save(obj)
488 self.write(STOP)
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/dill/_dill.py:388, in Pickler.save(self, obj, save_persistent_id)
387 raise PicklingError(msg)
--> 388 StockPickler.save(self, obj, save_persistent_id)
File /usr/lib/python3.8/pickle.py:578, in _Pickler.save(self, obj, save_persistent_id)
577 if reduce is not None:
--> 578 rv = reduce(self.proto)
579 else:
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/engine/training.py:367, in Model.__reduce__(self)
364 if self.built:
365 return (
366 pickle_utils.deserialize_model_from_bytecode,
--> 367 (pickle_utils.serialize_model_as_bytecode(self),),
368 )
369 else:
370 # SavedModel (and hence serialize_model_as_bytecode) only support
371 # built models, but if the model is not built,
(...)
375 # the superclass hierarchy to get an implementation of __reduce__
376 # that can pickle this Model as a plain Python object.
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/saving/pickle_utils.py:73, in serialize_model_as_bytecode(model)
72 except Exception as e:
---> 73 raise e
74 else:
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/saving/pickle_utils.py:69, in serialize_model_as_bytecode(model)
68 filepath = os.path.join(temp_dir, "model.keras")
---> 69 saving_lib.save_model(model, filepath)
70 with open(filepath, "rb") as f:
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/saving/experimental/saving_lib.py:117, in save_model(model, filepath)
115 _SAVING_V3_ENABLED.value = True
--> 117 serialized_model_dict = serialize_keras_object(model)
118 config_json = json.dumps(serialized_model_dict)
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/saving/experimental/serialization_lib.py:116, in serialize_keras_object(obj)
112 registered_name = None
113 return {
114 "module": module,
115 "class_name": class_name,
--> 116 "config": _get_class_or_fn_config(obj),
117 "registered_name": registered_name,
118 }
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/saving/experimental/serialization_lib.py:141, in _get_class_or_fn_config(obj)
137 raise TypeError(
138 f"The `get_config()` method of {obj} should return "
139 f"a dict. It returned: {config}"
140 )
--> 141 return serialize_dict(config)
142 else:
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/saving/experimental/serialization_lib.py:151, in serialize_dict(obj)
150 def serialize_dict(obj):
--> 151 return {key: serialize_keras_object(value) for key, value in obj.items()}
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/saving/experimental/serialization_lib.py:151, in <dictcomp>(.0)
150 def serialize_dict(obj):
--> 151 return {key: serialize_keras_object(value) for key, value in obj.items()}
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/saving/experimental/serialization_lib.py:116, in serialize_keras_object(obj)
112 registered_name = None
113 return {
114 "module": module,
115 "class_name": class_name,
--> 116 "config": _get_class_or_fn_config(obj),
117 "registered_name": registered_name,
118 }
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/keras/saving/experimental/serialization_lib.py:143, in _get_class_or_fn_config(obj)
142 else:
--> 143 raise TypeError(
144 f"Cannot serialize object {obj} of type {type(obj)}. "
145 "To be serializable, "
146 "a class must implement the `get_config()` method."
147 )
TypeError: Cannot serialize object {'input_ids': TensorShape([5, 100]), 'token_type_ids': TensorShape([5, 100]), 'attention_mask': TensorShape([5, 100])} of type <class 'transformers.tokenization_utils_base.BatchEncoding'>. To be serializable, a class must implement the `get_config()` method.
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
Cell In[17], line 2
1 filepath = 'my_path' # change to directory where detector is saved
----> 2 save_detector(cd, filepath)
File ~/envs/tmp-mlflow-whylogs/lib/python3.8/site-packages/alibi_detect/saving/saving.py:77, in save_detector(detector, filepath, legacy)
75 orig_files = set(filepath.iterdir())
76 _cleanup_filepath(orig_files, filepath)
---> 77 raise RuntimeError(f'Saving failed. The save directory {filepath} has been cleaned.') from error
79 logger.info('finished saving.')
RuntimeError: Saving failed. The save directory my_path has been cleaned.
System and packages
Ubuntu Python 3.8.15 alibi-detect==0.10.4 transformers==4.25.1
full packages:
huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...
To disable this warning, you can either:
- Avoid using `tokenizers` before the fork if possible
- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)
absl-py==1.3.0
adal==1.2.7
aiofiles==22.1.0
aiohttp==3.8.3
aiokafka==0.8.0
aiosignal==1.3.1
alembic==1.8.1
alibi-detect==0.10.4
anyio==3.6.2
argcomplete==2.0.0
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
arrow==1.2.3
asttokens==2.2.1
astunparse==1.6.3
async-timeout==4.0.2
attrs==22.1.0
azure-common==1.1.28
azure-core==1.26.1
azure-graphrbac==0.61.1
azure-mgmt-authorization==3.0.0
azure-mgmt-containerregistry==10.0.0
azure-mgmt-core==1.3.2
azure-mgmt-keyvault==10.1.0
azure-mgmt-resource==21.2.1
azure-mgmt-storage==20.1.0
azureml-core==1.48.0
Babel==2.11.0
backcall==0.2.0
backports.tempfile==1.0
backports.weakref==1.0.post1
bcrypt==4.0.1
beautifulsoup4==4.11.1
bleach==5.0.1
boto3==1.26.24
botocore==1.29.24
Brotli==1.0.9
cachetools==5.2.0
catalogue==2.0.8
certifi==2022.9.24
cffi==1.15.1
charset-normalizer==2.1.1
click==8.1.3
cloudpickle==2.2.0
contextlib2==21.6.0
contourpy==1.0.6
cryptography==38.0.4
cycler==0.11.0
databricks-cli==0.17.3
debugpy==1.6.4
decorator==5.1.1
defusedxml==0.7.1
dill==0.3.6
distlib==0.3.6
docker==6.0.1
entrypoints==0.4
executing==1.2.0
fastapi==0.88.0
fastjsonschema==2.16.2
filelock==3.8.2
Flask==2.2.2
flatbuffers==22.12.6
fonttools==4.38.0
fqdn==1.5.1
frozenlist==1.3.3
gast==0.4.0
gevent==22.10.2
geventhttpclient==2.0.2
gitdb==4.0.10
GitPython==3.1.29
google-api-core==2.11.0
google-auth==2.15.0
google-auth-oauthlib==0.4.6
google-cloud-core==2.3.2
google-cloud-storage==2.7.0
google-crc32c==1.5.0
google-pasta==0.2.0
google-resumable-media==2.4.0
googleapis-common-protos==1.57.0
greenlet==2.0.1
grpcio==1.51.1
gunicorn==20.1.0
h11==0.14.0
h5py==3.7.0
huggingface-hub==0.11.1
humanfriendly==10.0
idna==3.4
imageio==2.22.4
importlib-metadata==5.1.0
importlib-resources==5.10.1
ipykernel==6.17.1
ipython==8.7.0
ipython-genutils==0.2.0
isodate==0.6.1
isoduration==20.11.0
itsdangerous==2.1.2
jedi==0.18.2
jeepney==0.8.0
Jinja2==3.1.2
jmespath==1.0.1
joblib==1.2.0
json5==0.9.10
jsonpickle==2.2.0
jsonpointer==2.3
jsonschema==4.17.3
jupyter-events==0.5.0
jupyter_client==7.4.8
jupyter_core==5.1.0
jupyter_server==2.0.0
jupyter_server_terminals==0.4.2
jupyterlab==3.5.1
jupyterlab-pygments==0.2.2
jupyterlab_server==2.16.3
kafka-python==2.0.2
keras==2.11.0
kiwisolver==1.4.4
knack==0.10.1
kubernetes==25.3.0
libclang==14.0.6
llvmlite==0.38.1
Mako==1.2.4
Markdown==3.4.1
MarkupSafe==2.1.1
marshmallow==3.19.0
matplotlib==3.6.2
matplotlib-inline==0.1.6
mistune==2.0.4
mlflow==2.0.1
mlserver==1.2.0
mlserver-mlflow==1.2.0
msal==1.20.0
msal-extensions==1.0.0
msrest==0.7.1
msrestazure==0.6.4
multidict==6.0.3
nbclassic==0.4.8
nbclient==0.7.2
nbconvert==7.2.6
nbformat==5.7.0
ndg-httpsclient==0.5.1
nest-asyncio==1.5.6
networkx==2.8.8
nlp==0.4.0
notebook==6.5.2
notebook_shim==0.2.2
numba==0.55.2
numpy==1.22.0
oauthlib==3.2.2
opencv-python==4.6.0.66
opt-einsum==3.3.0
orjson==3.8.3
packaging==21.3
pandas==1.5.2
pandocfilters==1.5.0
paramiko==2.12.0
parso==0.8.3
pathspec==0.10.2
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.3.0
pkginfo==1.9.2
pkgutil_resolve_name==1.3.10
platformdirs==2.6.0
portalocker==2.6.0
prometheus-client==0.15.0
prometheus-flask-exporter==0.21.0
prompt-toolkit==3.0.36
protobuf==3.19.6
psutil==5.9.4
ptyprocess==0.7.0
pure-eval==0.2.2
puremagic==1.14
py-grpc-prometheus==0.7.0
pyarrow==10.0.1
pyasn1==0.4.8
pyasn1-modules==0.2.8
pybars3==0.9.7
pycparser==2.21
pydantic==1.10.2
Pygments==2.13.0
PyJWT==2.6.0
PyMeta3==0.5.1
PyNaCl==1.5.0
pyOpenSSL==22.1.0
pyparsing==3.0.9
pyrsistent==0.19.2
pysftp==0.2.9
PySocks==1.7.1
python-dateutil==2.8.2
python-dotenv==0.21.0
python-json-logger==2.0.4
python-rapidjson==1.9
pytz==2022.6
PyWavelets==1.4.1
PyYAML==6.0
pyzmq==24.0.1
querystring-parser==1.2.4
regex==2022.10.31
requests==2.28.1
requests-auth-aws-sigv4==0.7
requests-oauthlib==1.3.1
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rsa==4.9
s3transfer==0.6.0
scikit-image==0.19.3
scikit-learn==1.1.3
scipy==1.9.3
seaborn==0.12.1
SecretStorage==3.3.3
Send2Trash==1.8.0
shap==0.41.0
six==1.16.0
slicer==0.0.7
smart-open==6.2.0
smmap==5.0.0
sniffio==1.3.0
soupsieve==2.3.2.post1
SQLAlchemy==1.4.44
sqlparse==0.4.3
stack-data==0.6.2
starlette==0.22.0
starlette-exporter==0.14.0
tabulate==0.9.0
tensorboard==2.11.0
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.1
tensorflow==2.11.0
tensorflow-estimator==2.11.0
tensorflow-io-gcs-filesystem==0.28.0
termcolor==2.1.1
terminado==0.17.1
threadpoolctl==3.1.0
tifffile==2022.10.10
tinycss2==1.2.1
tokenizers==0.13.2
toml==0.10.2
tomli==2.0.1
tornado==6.2
tqdm==4.64.1
traitlets==5.6.0
transformers==4.25.1
tritonclient==2.28.0
typing_extensions==4.4.0
uri-template==1.2.0
urllib3==1.26.13
uvicorn==0.20.0
uvloop==0.17.0
virtualenv==20.17.1
wcwidth==0.2.5
webcolors==1.12
webencodings==0.5.1
websocket-client==1.4.2
Werkzeug==2.2.2
whylabs-client==0.3.0
whylabs-datasketches==2.2.0b1
whylogs==0.7.10
whylogs-sketching==3.4.1.dev3
wrapt==1.14.1
xxhash==3.1.0
yarl==1.8.2
zipp==3.11.0
zope.event==4.5.0
zope.interface==5.5.2
Hi @cristianmtr, thank you for opening this. I haven't yet been able to reproduce this on my end. Please could you give some more info on how you installed alibi-detect
? I ask because v0.10.4 only supports tensorflow < 2.10
, but I noticed you are using tensorflow == 2.11.0
.
Also, if you are able to, to help us with reproducing, would you be able to share the output of print(preprocess_fn)
please?