Skip to content

Bug: TFHUB_CACHE_DIR environment variable causes model loading failure when set to Unicode-containing path #936

@the-vindicar

Description

@the-vindicar

What happened?

I'm trying to set cache directory programmatically, from within the script.
Sample code attached.
If resulting value of CACHE contains Unicode characters, it causes load() call to fail with the following message:

tensorflow.python.framework.errors_impl.FailedPreconditionError: D:\path_with_unicode_characters_in_it\TFHUB_CACHE is not a directory

However, load() works just fine if CACHE contains an ASCII-only path.

Environment info:
OS: Windows 10 HOME 22H2 build 19045.6456
Python: 3.13.5
Package versions: tensorflow==2.20.0, tensorflow-hub==0.16.1, both installed via pip

Relevant code

import sys
import os
from pathlib import Path

APP_PATH = Path(sys.argv[0]).parent.resolve()
CACHE = APP_PATH / 'TFHUB_CACHE'
CACHE.mkdir(parents=True, exist_ok=True)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ['TFHUB_DOWNLOAD_PROGRESS'] = '1'
os.environ['TFHUB_CACHE_DIR'] = str(CACHE)

import tensorflow as tf
import tensorflow_hub as hub

print('Loading...')
hub_handle = 'https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2'
hub_module = hub.load(hub_handle)

Relevant log output

C:\Program Files\Python313\Lib\site-packages\keras\src\export\tf2onnx_lib.py:8: FutureWarning: In the future `np.object` will be defined as the corresponding NumPy scalar.
  if not hasattr(np, "object"):
C:\Program Files\Python313\Lib\site-packages\tensorflow_hub\__init__.py:61: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
  from pkg_resources import parse_version
WARNING:tensorflow:From C:\Program Files\Python313\Lib\site-packages\tf_keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.

Loading...
WARNING:tensorflow:From C:\Program Files\Python313\Lib\site-packages\tensorflow_hub\resolver.py:120: The name tf.gfile.MakeDirs is deprecated. Please use tf.io.gfile.makedirs instead.

WARNING:tensorflow:From C:\Program Files\Python313\Lib\site-packages\tensorflow_hub\resolver.py:120: The name tf.gfile.MakeDirs is deprecated. Please use tf.io.gfile.makedirs instead.

Traceback (most recent call last):
  File "D:\Dropbox\Документы\Лекции\Технологии компьютерного зрения\Labs\lab9_5.py", line 20, in <module>
    hub_module = hub.load(hub_handle)
  File "C:\Program Files\Python313\Lib\site-packages\tensorflow_hub\module_v2.py", line 100, in load
    module_path = resolve(handle)
  File "C:\Program Files\Python313\Lib\site-packages\tensorflow_hub\module_v2.py", line 55, in resolve
    return registry.resolver(handle)
           ~~~~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Program Files\Python313\Lib\site-packages\tensorflow_hub\registry.py", line 49, in __call__
    return impl(*args, **kwargs)
  File "C:\Program Files\Python313\Lib\site-packages\tensorflow_hub\compressed_module_resolver.py", line 61, in __call__
    module_dir = _module_dir(handle)
  File "C:\Program Files\Python313\Lib\site-packages\tensorflow_hub\compressed_module_resolver.py", line 36, in _module_dir
    return resolver.create_local_module_dir(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        cache_dir,
        ^^^^^^^^^^
        hashlib.sha1(handle.encode("utf8")).hexdigest())
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python313\Lib\site-packages\tensorflow_hub\resolver.py", line 120, in create_local_module_dir
    tf.compat.v1.gfile.MakeDirs(cache_dir)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "C:\Program Files\Python313\Lib\site-packages\tensorflow\python\lib\io\file_io.py", line 498, in recursive_create_dir
    recursive_create_dir_v2(dirname)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "C:\Program Files\Python313\Lib\site-packages\tensorflow\python\lib\io\file_io.py", line 513, in recursive_create_dir_v2
    _pywrap_file_io.RecursivelyCreateDir(compat.path_to_bytes(path))
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tensorflow.python.framework.errors_impl.FailedPreconditionError: D:\path_with_unicode_characters_in_it\TFHUB_CACHE is not a directory

tensorflow_hub Version

other (please specify)

TensorFlow Version

other (please specify)

Other libraries

tensorboard==2.20.0
tensorboard-data-server==0.7.2
tensorflow==2.20.0
tensorflow-datasets==4.9.9
tensorflow-hub==0.16.1
tensorflow-metadata==1.17.2

Python Version

3.x

OS

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions