tensorflow / hub

A library for transfer learning by reusing parts of TensorFlow models.
https://tensorflow.org/hub
Apache License 2.0
3.49k stars 1.67k forks source link

(tensorflow_hub not imported in jupyter ) ImportError: cannot import name 'path_helpers' from 'tensorflow.python.saved_model' #885

Closed Dvdgg closed 1 year ago

Dvdgg commented 1 year ago

What happened?

hello, i'm trying to import tensorflow_hub and I can't. The issue seems to be unique (i have not found a similar issue). If someone may help me, please ?

It's like there is a problem between a file in "roaming" and the file of anaconda environment (base, in anaconda3 ) ? I don't understand the problem.

sincerely yours,

Relevant code

import tensorflow_hub as hub

Relevant log output

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[79], line 3
      1 #from absl import logging
----> 3 import tensorflow_hub as hub

File ~\anaconda3\lib\site-packages\tensorflow_hub\__init__.py:88
     76     raise ImportError(
     77         "\n\nThis version of tensorflow_hub requires tensorflow "
     78         "version >= {required}; Detected an installation of version {present}. "
   (...)
     82             required=required_tensorflow_version,
     83             present=tf.__version__))
     85 _ensure_tf_install()
---> 88 from tensorflow_hub.estimator import LatestModuleExporter
     89 from tensorflow_hub.estimator import register_module_for_export
     90 from tensorflow_hub.feature_column import image_embedding_column

File ~\anaconda3\lib\site-packages\tensorflow_hub\estimator.py:62
     55       raise ValueError(
     56           "There is already a module registered to be exported as %r"
     57           % export_name)
     58   tf.compat.v1.add_to_collection(_EXPORT_MODULES_COLLECTION,
     59                                  (export_name, module))
---> 62 class LatestModuleExporter(tf.compat.v1.estimator.Exporter):
     63   """Regularly exports registered modules into timestamped directories.
     64 
     65   Warning: Deprecated. This belongs to the hub.Module API and TF1 Hub format.
   (...)
     88   THIS FUNCTION IS DEPRECATED.
     89   """
     91   def __init__(self, name, serving_input_fn, exports_to_keep=5):

File ~\anaconda3\lib\site-packages\tensorflow\python\util\lazy_loader.py:58, in LazyLoader.__getattr__(self, item)
     57 def __getattr__(self, item):
---> 58   module = self._load()
     59   return getattr(module, item)

File ~\anaconda3\lib\site-packages\tensorflow\python\util\lazy_loader.py:41, in LazyLoader._load(self)
     39 """Load the module and insert it into the parent's globals."""
     40 # Import the target module and insert it into the parent's namespace
---> 41 module = importlib.import_module(self.__name__)
     42 self._parent_module_globals[self._local_name] = module
     44 # Emit a warning if one was specified

File ~\anaconda3\lib\importlib\__init__.py:126, in import_module(name, package)
    124             break
    125         level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\__init__.py:8
      3 """Public API for tf. namespace.
      4 """
      6 import sys as _sys
----> 8 from tensorflow_estimator._api.v1 import estimator
      9 from tensorflow.python.util import module_wrapper as _module_wrapper
     11 if not isinstance(_sys.modules[__name__], _module_wrapper.TFModuleWrapper):

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\_api\v1\estimator\__init__.py:8
      3 """Estimator: High level tools for working with models.
      4 """
      6 import sys as _sys
----> 8 from tensorflow_estimator._api.v1.estimator import experimental
      9 from tensorflow_estimator._api.v1.estimator import export
     10 from tensorflow_estimator._api.v1.estimator import inputs

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\_api\v1\estimator\experimental\__init__.py:8
      3 """Public API for tf.estimator.experimental namespace.
      4 """
      6 import sys as _sys
----> 8 from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
      9 from tensorflow_estimator.python.estimator.canned.kmeans import KMeansClustering as KMeans
     10 from tensorflow_estimator.python.estimator.canned.linear import LinearSDCA

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\python\estimator\canned\dnn.py:26
     24 from tensorflow.python.feature_column import feature_column_lib
     25 from tensorflow.python.framework import ops
---> 26 from tensorflow_estimator.python.estimator import estimator
     27 from tensorflow_estimator.python.estimator.canned import head as head_lib
     28 from tensorflow_estimator.python.estimator.canned import optimizers

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\python\estimator\estimator.py:39
     37 from tensorflow.python.framework import ops
     38 from tensorflow.python.profiler import trace
---> 39 from tensorflow.python.saved_model import path_helpers
     40 from tensorflow.python.summary import summary
     41 from tensorflow.python.training import basic_session_run_hooks

ImportError: cannot import name 'path_helpers' from 'tensorflow.python.saved_model' (\anaconda3\lib\site-packages\tensorflow\python\saved_model\__init__.py)

tensorflow_hub Version

0.12.0 (latest stable release)

TensorFlow Version

other (please specify)

Other libraries

conda environment tensorflow 2.10.1 cudatoolkit 11.2.2 cudnn 8.1.0.77

system windows 10 famille version 22H2 / win-64 physical_device GPU_desc: "device: 0, name: NVIDIA GeForce GTX 1050, compute capability: 6.1"

version anaconda , python conda version : 23.3.1 conda-build version : 3.24.0 python version : 3.10.9.final.0 virtual packages : __archspec=1=x86_64 cuda=12.1=0 win=0=0

Python Version

3.x

OS

Windows

singhniraj08 commented 1 year ago

@Dvdgg,

This issue looks more like an environment error. I suspect the issue is that one Tensorflow is installed directly and other by anaconda environment which results in conflict. The suggested workaround would be to create a new conda environment, activate it and install Tensorflow and TF-Hub. Please let us know if this resolves the issue. Thanks.

Dvdgg commented 1 year ago

Hello singhniraj08, i tried to create a new environment with the same packages and the problem still remain ;

(new conda environment, with a new kernel for jupyter notebook)

is it possible to find and add a file called "path_helpers" to avoid this error, or a procedure to replace the folder in AppData / roaming, in order to avoid some conflicts or other types of errors ?

sincerely yours, David


ImportError Traceback (most recent call last) Cell In[3], line 3 1 #from absl import logging ----> 3 import tensorflow_hub as hub

File ~\anaconda3\envs\p6\lib\site-packages\tensorflow_hub__init__.py:88 76 raise ImportError( 77 "\n\nThis version of tensorflow_hub requires tensorflow " 78 "version >= {required}; Detected an installation of version {present}. " (...) 82 required=required_tensorflow_version, 83 present=tf.version)) 85 _ensure_tf_install() ---> 88 from tensorflow_hub.estimator import LatestModuleExporter 89 from tensorflow_hub.estimator import register_module_for_export 90 from tensorflow_hub.feature_column import image_embedding_column

File ~\anaconda3\envs\p6\lib\site-packages\tensorflow_hub\estimator.py:62 55 raise ValueError( 56 "There is already a module registered to be exported as %r" 57 % export_name) 58 tf.compat.v1.add_to_collection(_EXPORT_MODULES_COLLECTION, 59 (export_name, module)) ---> 62 class LatestModuleExporter(tf.compat.v1.estimator.Exporter): 63 """Regularly exports registered modules into timestamped directories. 64 65 Warning: Deprecated. This belongs to the hub.Module API and TF1 Hub format. (...) 88 THIS FUNCTION IS DEPRECATED. 89 """ 91 def init(self, name, serving_input_fn, exports_to_keep=5):

File ~\anaconda3\envs\p6\lib\site-packages\tensorflow\python\util\lazy_loader.py:58, in LazyLoader.getattr(self, item) 57 def getattr(self, item): ---> 58 module = self._load() 59 return getattr(module, item)

File ~\anaconda3\envs\p6\lib\site-packages\tensorflow\python\util\lazy_loader.py:41, in LazyLoader._load(self) 39 """Load the module and insert it into the parent's globals.""" 40 # Import the target module and insert it into the parent's namespace ---> 41 module = importlib.import_module(self.name) 42 self._parent_module_globals[self._local_name] = module 44 # Emit a warning if one was specified

File ~\anaconda3\envs\p6\lib\importlib__init__.py:126, in import_module(name, package) 124 break 125 level += 1 --> 126 return _bootstrap._gcd_import(name[level:], package, level)

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\python\estimator\api__init__.py:8 3 """Public API for tf. namespace. 4 """ 6 import sys as _sys ----> 8 from tensorflow_estimator.python.estimator.api._v1 import estimator 9 from tensorflow.python.util import module_wrapper as _module_wrapper 11 if not isinstance(_sys.modules[name], _module_wrapper.TFModuleWrapper):

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\python\estimator\api_v1\estimator__init__.py:8 3 """Estimator: High level tools for working with models. 4 """ 6 import sys as _sys ----> 8 from tensorflow_estimator.python.estimator.api._v1.estimator import experimental 9 from tensorflow_estimator.python.estimator.api._v1.estimator import export 10 from tensorflow_estimator.python.estimator.api._v1.estimator import inputs

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\python\estimator\api_v1\estimator\experimental__init__.py:8 3 """Public API for tf.estimator.experimental namespace. 4 """ 6 import sys as _sys ----> 8 from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder 9 from tensorflow_estimator.python.estimator.canned.kmeans import KMeansClustering as KMeans 10 from tensorflow_estimator.python.estimator.canned.linear import LinearSDCA

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\python\estimator\canned\dnn.py:26 24 from tensorflow.python.feature_column import feature_column_lib 25 from tensorflow.python.framework import ops ---> 26 from tensorflow_estimator.python.estimator import estimator 27 from tensorflow_estimator.python.estimator.canned import head as head_lib 28 from tensorflow_estimator.python.estimator.canned import optimizers

File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow_estimator\python\estimator\estimator.py:39 37 from tensorflow.python.framework import ops 38 from tensorflow.python.profiler import trace ---> 39 from tensorflow.python.saved_model import path_helpers 40 from tensorflow.python.summary import summary 41 from tensorflow.python.training import basic_session_run_hooks

ImportError: cannot import name 'path_helpers' from 'tensorflow.python.saved_model' (C:\Users\raoul\anaconda3\envs\p6\lib\site-packages\tensorflow\python\saved_model__init__.py)

singhniraj08 commented 1 year ago

@Dvdgg,

I was unable to replicate this issue. Please find attached the below screenshot. TF-Hub works with Tensorflow 2.12 and Python 3.10. I would suggest you to update the Tensorflow packages and make sure multiple Tensorflow packages are not installed in your machine using pip and conda. Thanks.

image

Dvdgg commented 1 year ago

Hello singhniraj08, thanks , i have passed many days to search a solution ; i have removed conda, python, the files. Remove and reinstall the driver, cudnn, cuda...

finally, i have tested this :

thanks to this tutorial , it worked

Dvdgg commented 1 year ago

windows 10 famille version 22H2 / win-64 NVIDIA GeForce GTX 1050

with this kind of environment, it worked :

Name Version Build Channel

absl-py 0.15.0 pypi_0 pypi astunparse 1.6.3 pypi_0 pypi attrs 23.1.0 pypi_0 pypi backcall 0.2.0 pypi_0 pypi ca-certificates 2023.5.7 h56e8100_0 conda-forge cachetools 4.2.4 pypi_0 pypi certifi 2023.5.7 pypi_0 pypi charset-normalizer 3.1.0 pypi_0 pypi clang 5.0 pypi_0 pypi click 8.1.3 pypi_0 pypi colorama 0.4.6 pypi_0 pypi cudatoolkit 11.2.2 h933977f_10 conda-forge cudnn 8.1.0.77 h3e0f4f4_0 conda-forge cycler 0.11.0 pypi_0 pypi cython 0.29.23 pypi_0 pypi debugpy 1.6.7 pypi_0 pypi decorator 5.1.1 pypi_0 pypi dill 0.3.6 pypi_0 pypi entrypoints 0.4 pypi_0 pypi filelock 3.12.0 pypi_0 pypi flatbuffers 1.12 pypi_0 pypi fsspec 2023.5.0 pypi_0 pypi gast 0.4.0 pypi_0 pypi gensim 4.1.2 pypi_0 pypi google-auth 1.35.0 pypi_0 pypi google-auth-oauthlib 0.4.6 pypi_0 pypi google-pasta 0.2.0 pypi_0 pypi grpcio 1.54.2 pypi_0 pypi h5py 3.1.0 pypi_0 pypi huggingface-hub 0.14.1 pypi_0 pypi idna 3.4 pypi_0 pypi importlib-metadata 6.6.0 pypi_0 pypi ipykernel 6.4.2 pypi_0 pypi ipython 7.34.0 pypi_0 pypi ipython-genutils 0.2.0 pypi_0 pypi jedi 0.18.2 pypi_0 pypi jinja2 3.0.2 pypi_0 pypi joblib 1.2.0 pypi_0 pypi jsonschema 4.17.3 pypi_0 pypi jupyter-client 7.4.9 pypi_0 pypi jupyter-core 5.3.0 pypi_0 pypi keras 2.6.0 pypi_0 pypi keras-preprocessing 1.1.2 pypi_0 pypi kiwisolver 1.4.4 pypi_0 pypi markdown 3.4.3 pypi_0 pypi markupsafe 2.1.2 pypi_0 pypi matplotlib 3.4.3 pypi_0 pypi matplotlib-inline 0.1.6 pypi_0 pypi nbformat 5.1.3 pypi_0 pypi nest-asyncio 1.5.6 pypi_0 pypi nltk 3.6.5 pypi_0 pypi numpy 1.19.5 pypi_0 pypi oauthlib 3.2.2 pypi_0 pypi opencv-python 4.5.4.58 pypi_0 pypi openssl 1.1.1q h8ffe710_0 conda-forge opt-einsum 3.3.0 pypi_0 pypi packaging 23.1 pypi_0 pypi pandas 1.3.4 pypi_0 pypi parso 0.8.3 pypi_0 pypi pickleshare 0.7.5 pypi_0 pypi pillow 9.5.0 pypi_0 pypi pip 23.0.1 py39haa95532_0 platformdirs 3.5.1 pypi_0 pypi plotly 5.3.1 pypi_0 pypi prompt-toolkit 3.0.38 pypi_0 pypi protobuf 3.19.6 pypi_0 pypi pyasn1 0.5.0 pypi_0 pypi pyasn1-modules 0.3.0 pypi_0 pypi pygments 2.15.1 pypi_0 pypi pyparsing 3.0.9 pypi_0 pypi pyrsistent 0.19.3 pypi_0 pypi python 3.9.0 h6244533_2 python-dateutil 2.8.2 pypi_0 pypi pytz 2023.3 pypi_0 pypi pywin32 306 pypi_0 pypi pyyaml 6.0 pypi_0 pypi pyzmq 25.0.2 pypi_0 pypi regex 2023.5.5 pypi_0 pypi requests 2.30.0 pypi_0 pypi requests-oauthlib 1.3.1 pypi_0 pypi rsa 4.9 pypi_0 pypi sacremoses 0.0.53 pypi_0 pypi scikit-learn 1.2.0 pypi_0 pypi scipy 1.10.1 pypi_0 pypi seaborn 0.11.2 pypi_0 pypi setuptools 66.0.0 py39haa95532_0 six 1.15.0 pypi_0 pypi smart-open 6.3.0 pypi_0 pypi sqlite 3.41.2 h2bbff1b_0 tenacity 8.2.2 pypi_0 pypi tensorboard 2.6.0 pypi_0 pypi tensorboard-data-server 0.6.1 pypi_0 pypi tensorboard-plugin-wit 1.8.1 pypi_0 pypi tensorflow 2.6.5 pypi_0 pypi tensorflow-estimator 2.6.0 pypi_0 pypi tensorflow-hub 0.12.0 pypi_0 pypi termcolor 1.1.0 pypi_0 pypi threadpoolctl 3.1.0 pypi_0 pypi tokenizers 0.10.3 pypi_0 pypi tornado 6.3.2 pypi_0 pypi tqdm 4.65.0 pypi_0 pypi traitlets 5.9.0 pypi_0 pypi transformers 4.12.2 pypi_0 pypi typing-extensions 3.10.0.2 pypi_0 pypi tzdata 2023c h04d1e81_0 urllib3 2.0.2 pypi_0 pypi vc 14.2 h21ff451_1 vs2015_runtime 14.27.29016 h5e58377_2 wcwidth 0.2.6 pypi_0 pypi werkzeug 2.3.4 pypi_0 pypi wheel 0.38.4 py39haa95532_0 wrapt 1.12.1 pypi_0 pypi zipp 3.15.0 pypi_0 pypi

image

Dvdgg commented 1 year ago

but now....

i have another problem:

keras VGG16() doesn't worked when i use the pre-trained model and apply the line model.predict() ; the kernel died.

i feel the tears on my skin...no message, just the kernel is died when the code line is applied

Dvdgg commented 1 year ago

from tensorflow.keras.models import Model, Sequential from tensorflow.keras.applications.vgg16 import VGG16

base_model = VGG16(weights='imagenet', include_top=True)

from tensorflow.keras.preprocessing.image import load_img, img_to_array from keras.preprocessing import image from tensorflow.keras.applications.vgg16 import preprocess_input

images_features = [] i=0 for image_file in dataFull_vf["image_path"] : if i%20 == 0 : print(i) i +=1 imge = load_img(image_file, target_size=(224, 224)) imge_b = image.img_to_array(imge) # Convertit une instance d'image PIL en un tableau Numpy

imge_c = np.expand_dims(imge_b, axis=0) # augmente le nombre de dimension du tableau numpy représentant l'image

imge_d = preprocess_input(imge_c)

prepared_images_np = np.array(imge_d)

#features = base_model.predict(prepared_images_np) #----> fait mourir le kernel
#images_features.append(features) 
Dvdgg commented 1 year ago

it was just a test, in order to see if something happen and then try to understand, improve...

but i don't know what to do, and my version of tensorflow is adapted for windows 10..if i upgrade, the risk is to lose the detection and possibility to use my gpu...i don't want that

singhniraj08 commented 1 year ago

@Dvdgg,

I am able to make predictions using VGG16. Can you try the similar code in your environment and see if that works. Ref: gist

I can understand getting a perfect environment to run your code is frustrating. However, I have a suggestion to install linux with WSL2 on your windows and there you can make a conda environment and try to install latest release of tensorflow. Ref: Install TensorFlow with GPU Acceleration Simultaneously for Windows and WSL Linux (2) Let us know if this works for you. Thank you!

Dvdgg commented 1 year ago

Hello singhniraj08,

thanks for your answer, and for the example.

unfortunately, the problem still remind on my config , as you can see below : kernel die when we call the keras model vgg16 to predict ;

image

if i have enough time, i will try the alternative with wsl linux but i have never do this before.

If i remember what i have seen on this kind of solution, it's necessary to use docker , isn't it ?

sincerely,

Dvdgg commented 1 year ago

just in case, i have also tested this :

https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#install-windows image

https://stackoverflow.com/questions/44110799/kernel-died-restarting-whenever-training-a-model/72623784#72623784 image

not worked with my version of tensorflow (in the list of packages i have posted in this in this chat, there is 4 days ago

sincerely, Dav

singhniraj08 commented 1 year ago

@Dvdgg, This issue seems to be with your environment and Tensorflow rather than TF-Hub. I would request you to close this issue and follow this issue with Tensorflow repo by creating new issue. Thanks.

google-ml-butler[bot] commented 1 year ago

Are you satisfied with the resolution of your issue? Yes No