This question already has answers here:
ImportError: cannot import name 'abs'
(9 answers)
Closed 4 years ago.
My code is, simply, executing in pycharm:
import tensorflow as tf
I get the following messages:
Traceback (most recent call last):
File "/Users/peterwills/PyCharmProjects/Kaggle/Salt_pretrain/pretrain_model.py", line 1, in <module>
import tensorflow as tf
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/__init__.py", line 28, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/__init__.py", line 81, in <module>
from tensorflow.python import keras
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/keras/__init__.py", line 24, in <module>
from tensorflow.python.keras import activations
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/keras/activations/__init__.py", line 22, in <module>
from tensorflow.python.keras._impl.keras.activations import elu
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/__init__.py", line 21, in <module>
from tensorflow.python.keras._impl.keras import activations
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/activations.py", line 23, in <module>
from tensorflow.python.keras._impl.keras import backend as K
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/backend.py", line 38, in <module>
from tensorflow.python.layers import base as tf_base_layers
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 25, in <module>
from tensorflow.python.keras.engine import base_layer
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/keras/engine/__init__.py", line 21, in <module>
from tensorflow.python.keras.engine.base_layer import InputSpec
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 33, in <module>
from tensorflow.python.keras import backend
File "/Users/peterwills/PyCharmProjects/anaconda3/envs/May2018_1/lib/python3.6/site-packages/tensorflow/python/keras/backend/__init__.py", line 22, in <module>
from tensorflow.python.keras._impl.keras.backend import abs
ImportError: cannot import name 'abs'
My tensor flow version is 1.9.0.
My Keras version is: 2.2.2
...cboth in anaconda
I have tried uninstalling tensor flow and protobuf, as suggested elsewhere, to no avail.
Can anyone advise on this?
I solved this by making a new virtual environment in anaconda and re-installing tensor flow (and a bunch of other unrelated stuff). A bit much, I agree but "only way to be sure".
Related
I'm just starting to learn how to use the ML models in the Tensorflow Object Detection API on Google Colab and so I'm trying to follow the documentation very carefully. After installing the libraries and packages, I'm testing the installation using the command from https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html:
python object_detection/builders/model_builder_tf2_test.py
But I get the following error:
Traceback (most recent call last):
File "object_detection/builders/model_builder_tf2_test.py", line 24, in <module>
from object_detection.builders import model_builder
File "/usr/local/lib/python3.7/dist-packages/object_detection/builders/model_builder.py", line 37, in <module>
from object_detection.meta_architectures import deepmac_meta_arch
File "/usr/local/lib/python3.7/dist-packages/object_detection/meta_architectures/deepmac_meta_arch.py", line 20, in <module>
from object_detection.models.keras_models import resnet_v1
File "/usr/local/lib/python3.7/dist-packages/object_detection/models/keras_models/resnet_v1.py", line 28, in <module>
from keras.applications import resnet # pylint:disable=g-import-not-at-top
File "/usr/local/lib/python3.7/dist-packages/keras/__init__.py", line 25, in <module>
from keras import models
File "/usr/local/lib/python3.7/dist-packages/keras/models.py", line 20, in <module>
from keras import metrics as metrics_module
File "/usr/local/lib/python3.7/dist-packages/keras/metrics.py", line 27, in <module>
from keras import activations
File "/usr/local/lib/python3.7/dist-packages/keras/activations.py", line 20, in <module>
from keras.layers import advanced_activations
File "/usr/local/lib/python3.7/dist-packages/keras/layers/__init__.py", line 93, in <module>
from keras.layers.core import Masking
File "/usr/local/lib/python3.7/dist-packages/keras/layers/core/__init__.py", line 20, in <module>
from keras.layers.core.dropout import Dropout
File "/usr/local/lib/python3.7/dist-packages/keras/layers/core/dropout.py", line 26, in <module>
class Dropout(base_layer.BaseRandomLayer):
AttributeError: module 'keras.engine.base_layer' has no attribute 'BaseRandomLayer'
Even after importing keras version 2.0, it still not working, what I can do to solve this issue, thanks
Solved it: tensorflow version 2.5.0 is not compatible with the code provided. I had to install 2.8.0 so that it will work
I've installed tensorflow and keras in a virtual environment, but when I try to run from tensorflow.keras.models import load_model I get the following error:
from tensorflow.python.framework.constant_op import constant
ImportError: cannot import name 'constant' from partially initialized module 'tensorflow.python.framework.constant_op' (most likely due to a circular import) (/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py)
I'm running tensorflow version 2.4.0 and Keras 2.4.3 and python 3.8.7
I've uninstalled and reinstalled both tensorflow and keras, but still no chance! Any idea how I can find what's causing this issue?
P.S. This is the full error message:
Traceback (most recent call last):
File "code.py", line 4, in <module>
from tensorflow.keras.models import load_model
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/__init__.py", line 46, in <module>
from tensorflow.python import data
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/__init__.py", line 25, in <module>
from tensorflow.python.data import experimental
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/experimental/__init__.py", line 96, in <module>
from tensorflow.python.data.experimental import service
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/experimental/service/__init__.py", line 140, in <module>
from tensorflow.python.data.experimental.ops.data_service_ops import distribute
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/experimental/ops/data_service_ops.py", line 25, in <module>
from tensorflow.python.data.experimental.ops import compression_ops
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/experimental/ops/compression_ops.py", line 20, in <module>
from tensorflow.python.data.util import structure
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/util/structure.py", line 26, in <module>
from tensorflow.python.data.util import nest
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/data/util/nest.py", line 41, in <module>
from tensorflow.python.framework import sparse_tensor as _sparse_tensor
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/sparse_tensor.py", line 29, in <module>
from tensorflow.python.framework import constant_op
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 29, in <module>
from tensorflow.python.eager import execute
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/eager/execute.py", line 28, in <module>
from tensorflow.python.framework import ops
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 63, in <module>
from tensorflow.python.platform import app
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/platform/app.py", line 23, in <module>
from absl.app import run as _run
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/absl/app.py", line 35, in <module>
import pdb
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pdb.py", line 77, in <module>
import code
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/code.py", line 4, in <module>
from tensorflow.keras.models import load_model
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/keras/__init__.py", line 14, in <module>
from . import activations
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/keras/activations/__init__.py", line 10, in <module>
from tensorflow.python.keras.activations import deserialize
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/keras/__init__.py", line 27, in <module>
from tensorflow.python.keras import models
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/keras/models.py", line 23, in <module>
from tensorflow.python.keras import backend as K
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/keras/backend.py", line 37, in <module>
from tensorflow.python.client import session as session_module
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 40, in <module>
from tensorflow.python.ops import session_ops
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/ops/session_ops.py", line 30, in <module>
from tensorflow.python.ops import array_ops
File "/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 35, in <module>
from tensorflow.python.framework.constant_op import constant
ImportError: cannot import name 'constant' from partially initialized module 'tensorflow.python.framework.constant_op' (most likely due to a circular import) (/Users/Gabriel/Development/Projects/Keras-FaceNet_demo/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py)
From Comments
code.py
is already defined in python packages. Hence after renaming code.py
file to something other name has resolved the problem (paraphrased
from Macattack).
While running yolo detection on webcam, I came across this error
ImportError: cannot import name 'export_saved_model' from 'tensorflow.python.keras.saving.saved_model'
and its indicated on this line
from yolov3_tf2.models import YoloV3, YoloV3Tiny
I saw this similar question but it is of no use for me.
I did tried this..
conda uninstall -y tensorflow
pip uninstall tensorflow
pip uninstall keras
and then
conda create -n tf tensorflow
conda activate tf
and still the error persists..
Here is the full traceback call..
Traceback (most recent call last):
File "d:/Github/cloned repo/Object-Detection-API/load_weights.py", line 4, in <module>
from yolov3_tf2.models import YoloV3, YoloV3Tiny
File "d:\Github\cloned repo\Object-Detection-API\yolov3_tf2\models.py", line 5, in <module>
from tensorflow.keras import Model
File "D:\Anaconda\lib\site-packages\tensorflow\keras\__init__.py", line 14, in <module>
from . import activations
File "D:\Anaconda\lib\site-packages\tensorflow\keras\activations\__init__.py", line 10, in <module>
from tensorflow.python.keras.activations import deserialize
File "D:\Anaconda\lib\site-packages\tensorflow\python\__init__.py", line 83, in <module>
from tensorflow.python import keras
File "D:\Anaconda\lib\site-packages\tensorflow\python\keras\__init__.py", line 24, in <module>
from tensorflow.python.keras import activations
File "D:\Anaconda\lib\site-packages\tensorflow\python\keras\activations.py", line 24, in <module>
from tensorflow.python.keras.utils.generic_utils import deserialize_keras_object
File "D:\Anaconda\lib\site-packages\tensorflow\python\keras\utils\__init__.py", line 39, in <module>
from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
File "D:\Anaconda\lib\site-packages\tensorflow\python\keras\utils\multi_gpu_utils.py", line 22, in <module>
from tensorflow.python.keras.engine.training import Model
File "D:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py", line 43, in <module>
from tensorflow.python.keras.engine.network import Network
File "D:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\network.py", line 40, in <module>
from tensorflow.python.keras.saving import hdf5_format
File "D:\Anaconda\lib\site-packages\tensorflow\python\keras\saving\__init__.py", line 31, in <module>
from tensorflow.python.keras.saving.saved_model import export_saved_model
ImportError: cannot import name 'export_saved_model' from 'tensorflow.python.keras.saving.saved_model' (D:\Anaconda\lib\site-packages\tensorflow\python\keras\saving\saved_model\__init__.py)
Help me solve this error..
Thanks in advance.
I am trying to use keras.models. Here is the code I have in a python script
import keras.models
Running it gives me this error:
Using TensorFlow backend.
Traceback (most recent call last):
File "test.py", line 1, in <module>
import keras.models
File "/usr/local/lib/python3.7/site-packages/keras/__init__.py", line 3, in <module>
from . import utils
File "/usr/local/lib/python3.7/site-packages/keras/utils/__init__.py", line 6, in <module>
from . import conv_utils
File "/usr/local/lib/python3.7/site-packages/keras/utils/conv_utils.py", line 9, in <module>
from .. import backend as K
File "/usr/local/lib/python3.7/site-packages/keras/backend/__init__.py", line 1, in <module>
from .load_backend import epsilon
File "/usr/local/lib/python3.7/site-packages/keras/backend/load_backend.py", line 90, in <module>
from .tensorflow_backend import *
File "/usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 5, in <module>
import tensorflow as tf
File "/usr/local/lib/python3.7/site-packages/tensorflow/__init__.py", line 101, in <module>
from tensorflow_core import *
File "/usr/local/lib/python3.7/site-packages/tensorflow_core/__init__.py", line 40, in <module>
from tensorflow.python.tools import module_util as _module_util
ModuleNotFoundError: No module named 'tensorflow.python.tools'; 'tensorflow.python' is not a package
I have tried uninstalling and reinstalling keras as well as installing keras.models seperately however in both cases I still get this error. Why is this? Thanks
The problem is with your Tensorflow, re-install that.
I had implemented a neural net architecture in keras using tensor flow as a backend.
Which is working fine in last month but when I tried to run the code again it gives a series of errors. I tried to uninstall my packages installed and reinstall it but it never worked for me. I also tried the same combination with theano as a backend but it still gives me error.
I am attaching the traceback of the hope I could get a better solution for my question.
Using TensorFlow backend.
Traceback (most recent call last):
File "DeepNeuralNetwork.py", line 9, in <module>
from keras.models import Sequential
File "/usr/local/lib/python2.7/dist-packages/keras/__init__.py", line 2, in <module>
from . import backend
File "/usr/local/lib/python2.7/dist-packages/keras/backend/__init__.py", line 69, in <module>
from .tensorflow_backend import *
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 1, in <module>
import tensorflow as tf
File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 23, in <module>
from tensorflow.python import *
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 65, in <module>
import tensorflow.contrib as contrib
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/__init__.py", line 28, in <module>
from tensorflow.contrib import learn
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/__init__.py", line 72, in <module>
from tensorflow.contrib.learn.python.learn import *
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/__init__.py", line 23, in <module>
from tensorflow.contrib.learn.python.learn import *
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/__init__.py", line 25, in <module>
from tensorflow.contrib.learn.python.learn import estimators
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/__init__.py", line 23, in <module>
from tensorflow.contrib.learn.python.learn.estimators.autoencoder import TensorFlowDNNAutoencoder
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/autoencoder.py", line 25, in <module>
from tensorflow.contrib.learn.python.learn.estimators.base import TensorFlowBaseTransformer
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/base.py", line 34, in <module>
from tensorflow.contrib.learn.python.learn.estimators import estimator
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 38, in <module>
from tensorflow.contrib.learn.python.learn.io import data_feeder
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/io/__init__.py", line 22, in <module>
from tensorflow.contrib.learn.python.learn.io.dask_io import extract_dask_data
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/io/dask_io.py", line 26, in <module>
import dask.dataframe as dd
File "/usr/local/lib/python2.7/dist-packages/dask/dataframe/__init__.py", line 5, in <module>
from .io import (from_array, from_pandas, from_bcolz,
File "/usr/local/lib/python2.7/dist-packages/dask/dataframe/io/__init__.py", line 6, in <module>
from .csv import read_csv, to_csv, read_table
File "/usr/local/lib/python2.7/dist-packages/dask/dataframe/io/csv.py", line 147, in <module>
CPU_COUNT = psutil.cpu_count()
AttributeError: 'module' object has no attribute 'cpu_count'
As you can see from the last lines of the backtrace:
File "/usr/local/lib/python2.7/dist-packages/dask/dataframe/io/csv.py", line 147, in <module>
CPU_COUNT = psutil.cpu_count()
AttributeError: 'module' object has no attribute 'cpu_count'
the error is due to dask trying to call the attribute cpu_count of the module psutil.
This is probably due to some wrong version of psutil installed, and should be solved by simply upgrading or reinstalling it.