TensorFlow ModuleNotFoundError: No module named 'official.nlp.bert' - python

I'm following the TenslorFlow TFlite basic text classification tutorial, but the third import line throws an error:
from tensorflow_examples.lite.model_maker.core.data_util import image_dataloader
ModuleNotFoundError: No module named 'official.nlp.bert'
I installed TensorFlow with pip install tensorflow and the tflite model maker with pip install -q tflite-model-maker.
I've also installed all the official models with pip install tf-models-official.
I can see the BERT model on the official models GitHub as well, so it should be in there.
How can I install the bert model?
Full error:
Traceback (most recent call last):
File "...\Downloads\tf_setup.py", line 6, in <module>
from tflite_model_maker import model_spec
File "...\AppData\Local\Programs\Python\Python310\lib\site-packages\tflite_model_maker\__init__.py", line 16, in <module>
from tensorflow_examples.lite.model_maker.core.data_util import image_dataloader
File "...\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow_examples\lite\model_maker\__init__.py", line 17, in <module>
from tensorflow_examples.lite.model_maker.core.data_util import text_dataloader
File "...\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow_examples\lite\model_maker\core\data_util\text_dataloader.py", line 31, in <module>
from tensorflow_examples.lite.model_maker.core.task import model_spec as ms
File "...\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow_examples\lite\model_maker\core\task\model_spec.py", line 37, in <module>
from official.nlp.bert import configs as bert_configs
ModuleNotFoundError: No module named 'official.nlp.bert'

What is your Python version? I encountered the exact same error while I was using Python 3.10. My tflite-model-maker was 0.1.x version despite I tried to install the latest one which is 0.4.0. I wanted to update it but I got more errors about "No matching distribution" for tflite-model-maker and tflite-support. After I installed and changed my Python interpreter (I'm using PyCharm) to Python 3.9.13 and reinstall tflite-model-maker, the error is gone.

Related

ModuleNotFoundError: No module named 'keras.api' error

I am trying to run a ".py" file on windows 10 with tensorflow version 2.8.0 and keras version 2.3.4, where I am calling libraries as follows
from tensorflow import keras
from keras.models import load_model
However, I am getting an error message "ModuleNotFoundError: No module named 'keras.api'"
as shown in the error log below.
DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
from distutils.log import debug
Using TensorFlow backend.
Traceback (most recent call last):
File "C:\Users\saniy\OneDrive\Derma-Project\app1.py", line 5, in <module>
from keras.models import load_model
File "C:\Python310\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Python310\lib\site-packages\keras\utils\__init__.py", line 26, in <module>
from .vis_utils import model_to_dot
File "C:\Python310\lib\site-packages\keras\utils\vis_utils.py", line 7, in <module>
from ..models import Model
File "C:\Python310\lib\site-packages\keras\models.py", line 12, in <module>
from .engine.training import Model
File "C:\Python310\lib\site-packages\keras\engine\__init__.py", line 8, in <module>
from .training import Model
File "C:\Python310\lib\site-packages\keras\engine\training.py", line 14, in <module>
from . import training_utils
File "C:\Python310\lib\site-packages\keras\engine\training_utils.py", line 17, in <module>
from .. import metrics as metrics_module
File "C:\Python310\lib\site-packages\keras\metrics.py", line 1850, in <module>
BaseMeanIoU = tf.keras.metrics.MeanIoU
File "C:\Python310\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 58, in __getattr__
module = self._load()
File "C:\Python310\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 41, in _load
module = importlib.import_module(self.__name__)
File "C:\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'keras.api'
I am wondering if you can help in this regard.
I am not sure how keras 2.3.4 is installed but minimum version of keras for tf 2.8 is 2.8; Just upgrade your keras version
pip install keras==2.8
Since version 2 of Tensorflow (TF), the Keras package comes installed alongside. Meaning that if you make pip install tensorflow, will install the latest TF version (2.8) and Keras 2.8. As the other answer suggests, I would guess you have some old Keras version installed on your computer.
My advise would be to create a fresh virtual environment,
python3 -m venv ENV_DIR
and then freshly install all the requirements that your .py file needs from scratch. This will hopefully solve your issue and it is also best practice not to mess with different Python packages versions throughout your projects.

Version conflict between cvxpy and numpy: "module compiled against API version 0xe but this version of numpy is 0xd"

I'm trying to upgrade some packages and to consolidate my requirements.txt for an existing python program in order to move it to a docker container.
This container will be based on the tensorflow docker container, this dictates some package versions I have to use. We work under windows and we want to be able to run the program locally on our machines (at least for some time). So I need to find a configuration that works in docker and on Windows 10.
Tensorflow 2.4.1 needs numpy~=1.19.2. When using numpy 1.20, pip complains that numpy 1.20 is an incompatible version.
But when using numpy~=1.19.2 I get the following error when importing cvxpy. pip installs all packages just fine:
RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
Traceback (most recent call last):
File "test.py", line 1, in <module>
import cvxpy
File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\__init__.py", line 18, in <module>
from cvxpy.atoms import *
File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\__init__.py", line 20, in <module>
from cvxpy.atoms.geo_mean import geo_mean
File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\geo_mean.py", line 20, in <module>
from cvxpy.utilities.power_tools import (fracify, decompose, approx_error, lower_bound,
File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\utilities\power_tools.py", line 18, in <module>
from cvxpy.atoms.affine.reshape import reshape
File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\affine\reshape.py", line 18, in <module>
from cvxpy.atoms.affine.hstack import hstack
File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\affine\hstack.py", line 18, in <module>
from cvxpy.atoms.affine.affine_atom import AffAtom
File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\atoms\affine\affine_atom.py", line 22, in <module>
from cvxpy.cvxcore.python import canonInterface
File "c:\Projekte\algo5\venv\lib\site-packages\cvxpy\cvxcore\python\__init__.py", line 3, in <module>
import _cvxcore
ImportError: numpy.core.multiarray failed to import
Steps to reproduce:
1.)
Create a new Python 3.8 venv under Windows 10 and activate it
2.) Install the following requirements.txt via pip install -r requirements.txt:
cvxpy
numpy~=1.19.2 # tensorflow 2.4.1 requires this version
3.) Execute the following test.py via python test.py
import cvxpy
if __name__ == '__main__':
pass
The same thing happens if I want to use tensorflow 2.3. In this case numpy~=1.18 is needed, the error is exactly the same.
Searching for the error finds very few hits which sadly did not help me.
What can I do to solve this?

AttributeError: module 'tensorflow' has no attribute 'compat'

Since my CPU doesn't support AVX instructions and I want to install a newer version of Tensorflow (>= 2.0), I installed a tensorflow wheel for my Python version from https://github.com/fo40225/tensorflow-windows-wheel using pip
However, when trying to import tensorflow, I get the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\__init__.py", line 98, in <module>
from tensorflow_core import *
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\__init__.py", line 45, in <module>
from . _api.v2 import compat
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\_api\v2\compat\__init__.py", line 23, in <module>
from . import v1
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\_api\v2\compat\v1\__init__.py", line 664, in <module>
from tensorflow_estimator.python.estimator.api._v1 import estimator
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\__init__.py", line 10, in <module>
from tensorflow_estimator._api.v1 import estimator
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\_api\v1\estimator\__init__.py", line 10, in <module>
from tensorflow_estimator._api.v1.estimator import experimental
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\_api\v1\estimator\experimental\__init__.py", line 10, in <module>
from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\canned\dnn.py", line 33, in <module>
from tensorflow_estimator.python.estimator import estimator
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 53, in <module>
from tensorflow_estimator.python.estimator import util as estimator_util
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\util.py", line 75, in <module>
class _DatasetInitializerHook(tf.compat.v1.train.SessionRunHook):
AttributeError: module 'tensorflow' has no attribute 'compat'
I have searched, but not found anybody who encountered the same issue using tensorflow wheel (just trying to install tensorflow via "pip install tensorflow" isn't going to work for me, as I will run into the ImportError: DLL load failed-error).
It seems tensorflow is not installed in your system in the proper way which causes this error.
You need to install tensorflow using any of the below command:
pip install tensorflow
pip install tensorflow-gpu # to install tensorflow for gpu support
pip install tensorflow==2.4.0 # to install a specific tensorflow version
For Anaconda environment in jupyter notebook, use below code:
conda install tensorflow
conda install tensorflow-gpu # to install tensorflow for gpu support
Please follow this link which has step wise instructions to install tensorflow in your system.

ImportError: cannot import name 'GceClusterResolver'

I am following the guide for installing TensorFlow Slim, and I faced this error.
ImportError: cannot import name 'GceClusterResolver'
I searched online and found some questions on Stack Overflow regarding "import error cannot import name xxx", but none of them is the same as mine. I've tried their solutions too but they didn't work.
This is the complete error message. Any help is greatly appreciated.
python -c "import tensorflow.contrib.slim as slim; eval = slim.evaluation.evaluate_once"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/anaconda3/lib/python3.6/site-packages/tensorflow/contrib/__init__.py", line 31, in <module>
from tensorflow.contrib import cluster_resolver
File "/anaconda3/lib/python3.6/site-packages/tensorflow/contrib/cluster_resolver/__init__.py", line 26, in <module>
from tensorflow.python.distribute.cluster_resolver.gce_cluster_resolver import GceClusterResolver
ImportError: cannot import name 'GceClusterResolver'
I found out that i was using tflearn with tensorflow 2 but tflearn is not compatible with tensorflow 2. I just falled back to a tensorflow 1 version.
# uninstall both libs
pip uninstall tensorflow
pip uninstall tflearn
# install the compatible version
pip install tflearn
pip install tensorflow==1.15

ImportError while running python program

I have tried all the import methods and upgrading the libraries but still I'm unable to get over this error. I have downloaded and installed all the nltk and corpus data programmatically and it is working in the python shell but i'm getting error.
Traceback (most recent call last):
File "spark.py", line 7, in <module>
from textblob_aptagger import PerceptronTagger
File "/usr/local/lib/python2.7/dist-packages/textblob_aptagger/__init__.py", line 8, in <module>
from textblob_aptagger.taggers import PerceptronTagger
File "/usr/local/lib/python2.7/dist-packages/textblob_aptagger/taggers.py", line 10, in <module>
from textblob.packages import nltk
ImportError: No module named packages
Here's a pastebin to my code and imports..
Same error has been posted on github here. Use this instead to install textblob:
$ pip install -U git+https://github.com/sloria/textblob-aptagger.git#dev
Also, you should change from text.blob import TextBlob as tbto from textblob...
Works for me..

Categories