Can't import plot_model from keras.utils - python

I'm trying to visualize a neural network model with plot_model,
but importing plot_model fails with an error message i don't understand.
My code:
# %% setup
import os
import tensorflow as tf
tf_model_target_path = os.path.join(os.getcwd(), 'tfModelDir')
model = tf.keras.models.load_model(tf_model_target_path)
# %% visualize model: save to image file
from keras.utils import plot_model
plot_model(model, to_file='model.jpg')
the line
from keras.utils import plot_model
throws:
Exception has occurred: AttributeError
module 'tensorflow.compat.v2.__internal__' has no attribute 'dispatch'
File "{...}\kerasVisMinError.py", line 9, in <module>
from keras.utils import plot_model
When i run the code from VSCode's IPython interactive shell, i get a more verbose error message:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
{...}\kerasVisMinError.py in <module>
9 # %% visualize model: save to image file
----> 10 from keras.utils import plot_model
11 #from keras.utils.vis_utils import plot_model
12 #plot_model(model, to_file='model.jpg')
13
~\AppData\Roaming\Python\Python38\site-packages\keras\__init__.py in <module>
18 [keras.io](https://keras.io).
19 """
---> 20 from keras import distribute
21 from keras import models
22 from keras.engine.input_layer import Input
~\AppData\Roaming\Python\Python38\site-packages\keras\distribute\__init__.py in <module>
16
17
---> 18 from keras.distribute import sidecar_evaluator
~\AppData\Roaming\Python\Python38\site-packages\keras\distribute\sidecar_evaluator.py in <module>
20 from tensorflow.python.platform import tf_logging as logging
21 from tensorflow.python.util import deprecation
---> 22 from keras.optimizers.optimizer_experimental import (
...
---> 33 #tf.__internal__.dispatch.add_dispatch_support
34 def epsilon():
35 """Returns the value of the fuzz factor used in numeric expressions.
AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'dispatch'
im using VS Code.
my python version is 3.8.3
my packages for keras, etc:
tensorflow 2.4.1
keras 2.10.0
Keras-Preprocessing 1.1.2
keras-utils 1.0.13
I found a similar
question on SO that has no answer yet
I tried replacing the import line with from keras.utils.vis_utils import plot_model as suggested here, but i get the same error message
I created a google colab. There i'm able to import plot_model and then visualise my model
I tried to downgrade tensorflow to 2.3.1 as suggested
here, but now i get this error:
AttributeError Traceback (most recent call last)
c:\Users\Miki\Documents\progi_peldak\pythonTensorflow\kerasVisMinErrorForSO\kerasVisMinError.py in <module>
14 print(tf.version.VERSION)
15 #from keras.utils import plot_model
----> 16 from keras.utils.vis_utils import plot_model
17 #plot_model(model, to_file='model.jpg')
~\AppData\Roaming\Python\Python38\site-packages\keras\__init__.py in <module>
18 [keras.io](https://keras.io).
19 """
---> 20 from keras import distribute
21 from keras import models
22 from keras.engine.input_layer import Input
~\AppData\Roaming\Python\Python38\site-packages\keras\distribute\__init__.py in <module>
16
17
---> 18 from keras.distribute import sidecar_evaluator
~\AppData\Roaming\Python\Python38\site-packages\keras\distribute\sidecar_evaluator.py in <module>
20 from tensorflow.python.platform import tf_logging as logging
21 from tensorflow.python.util import deprecation
---> 22 from keras.optimizers.optimizer_experimental import (
23 optimizer as optimizer_experimental,
...
---> 33 #tf.__internal__.dispatch.add_dispatch_support
34 def epsilon():
35 """Returns the value of the fuzz factor used in numeric expressions.
AttributeError: module 'tensorflow.compat.v2' has no attribute '__internal__'

There are Tensorflow and Keras version mismatched in your system. You need to upgrade the tensorflow version to TF 2.10 as you have installed Keras version 2.10.
!pip install tensorflow==2.10
import tensorflow as tf
tf.__version__
Now import plot_model api by providing a complete alias name:
from tensorflow.keras.utils import plot_model

Related

Cannot import tensorflow after a week of no use, no changes was made

What I've did:
uninstall and pip install tensorflow-macos
uninstall pip install tensorflow-metal
import tensorflow as tf
The expected result is no error.
However, I got an error after importing tensorflow.
TypeError Traceback (most recent call last)
Cell In[1], line 1
----> 1 import tensorflow as tf
3 # from keras import datasets, layers, models
4 # import matplotlib.pyplot as plt
File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tensorflow/__init__.py:37
34 import sys as _sys
35 import typing as _typing
---> 37 from tensorflow.python.tools import module_util as _module_util
38 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
40 # Make sure code inside the TensorFlow codebase can use tf2.enabled() at import.
File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tensorflow/python/__init__.py:45
43 from tensorflow.python import distribute
44 # from tensorflow.python import keras
---> 45 from tensorflow.python.feature_column import feature_column_lib as feature_column
46 # from tensorflow.python.layers import layers
47 from tensorflow.python.module import module
File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tensorflow/python/feature_column/feature_column_lib.py:18
15 """FeatureColumns: tools for ingesting and representing features."""
17 # pylint: disable=unused-import,line-too-long,wildcard-import,g-bad-import-order
---> 18 from tensorflow.python.feature_column.feature_column import *
...
(...)
3248 :returns: The PKCS12 object
3249 """
TypeError: deprecated() got an unexpected keyword argument 'name'

Error importing binary_weighted_focal_crossentropy from keras backend: Cannot import name

---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [20], in <cell line: 1>()
----> 1 from tensorflow.keras import layers
2 import keras.backend as backend
3 import tensorflow as tf
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\_v2\keras\__init__.py:13, in <module>
10 import sys as _sys
12 from keras import __version__
---> 13 from keras.api._v2.keras import __internal__
14 from keras.api._v2.keras import activations
15 from keras.api._v2.keras import applications
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\__init__.py:8, in <module>
3 """Public API for tf. namespace.
4 """
6 import sys as _sys
----> 8 from keras.api import keras
9 from tensorflow.python.util import module_wrapper as _module_wrapper
11 if not isinstance(_sys.modules[__name__], _module_wrapper.TFModuleWrapper):
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\keras\__init__.py:16, in <module>
14 from keras.api.keras import activations
15 from keras.api.keras import applications
---> 16 from keras.api.keras import backend
17 from keras.api.keras import callbacks
18 from keras.api.keras import constraints
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\keras\backend\__init__.py:24, in <module>
22 from keras.backend import binary_crossentropy
23 from keras.backend import binary_focal_crossentropy
---> 24 from keras.backend import binary_weighted_focal_crossentropy
25 from keras.backend import cast
26 from keras.backend import cast_to_floatx
ImportError: cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend' (D:\Users\DGong\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\backend.py)
I can't figure out why I'm getting an import error here. I've uninstalled and reinstalled tensorflow and keras many times, as well as used both pip and pip3. I checked the file and nothing seems to be different between the way binary_focal_crossentropy and binary_weighted_focal_crossentropy is exported. But the former seems to import without error.
You can import and use tf.keras.metrics.binary_focal_crossentropy by importing the metrics library below.
Also, Instead of pip installing each package(TensorFlow, Keras) separately, the recommended approach is to install Keras as part of the TensorFlow installation. When you install TensorFlow >2.0, Keras will be automatically installed, as well.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.metrics import binary_focal_crossentropy
You can check more about binary_focal_crossentropy in the given link.

AlreadyExistsError :Another metric with the same name already exists . should i delete whole keras folder /uninstall keras?

from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
**this is error that shows **
should i delete keras folder in python ?
AlreadyExistsError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_4132/4279258018.py in <module>
1 from sklearn.model_selection import train_test_split
----> 2 from tensorflow.keras.utils import to_categorical
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\api\_v2\keras\__init__.py in <module>
6 import sys as _sys
7
----> 8 from keras import __version__
9 from keras.api._v2.keras import __internal__
10 from keras.api._v2.keras import activations
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\__init__.py in <module>
23
24 # See b/110718070#comment18 for more details about this import.
---> 25 from keras import models
26
27 from keras.engine.input_layer import Input
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\models.py in <module>
18 import tensorflow.compat.v2 as tf
19 from keras import backend
---> 20 from keras import metrics as metrics_module
21 from keras import optimizer_v1
22 from keras.engine import functional
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\metrics.py in <module>
24
25 import numpy as np
---> 26 from keras import activations
27 from keras import backend
28 from keras.engine import base_layer
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\activations.py in <module>
18
19 from keras import backend
---> 20 from keras.layers import advanced_activations
21 from keras.utils.generic_utils import deserialize_keras_object
22 from keras.utils.generic_utils import serialize_keras_object
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\layers\__init__.py in <module>
21
22 # Generic layers.
---> 23 from keras.engine.input_layer import Input
24 from keras.engine.input_layer import InputLayer
25 from keras.engine.input_spec import InputSpec
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\input_layer.py in <module>
19 from keras import backend
20 from keras.distribute import distributed_training_utils
---> 21 from keras.engine import base_layer
22 from keras.engine import keras_tensor
23 from keras.engine import node as node_module
AlreadyExistsError: Another metric with the same name already exists.
I deleted and uninstalled everything related to tensorflow , keras and deleted all the files in APPdata and reinstalled everything. And it solved my problem .
I think, you installed both keras and tensoflow, due to conflict, My solution is uninstall keras.
The answer is post in github/keras/issues/15579
It is a problem with keras 2.7. Downgrade to keras 2.6 solves the issue.

Tensorflow import error AttributeError: 'module' object has no attribute 'Exporter'

When I try to import tensorflow hub, I get the following error saying that 'module' object has no attribute 'Exporter'.
AttributeErrorTraceback (most recent call last)
<ipython-input-1-31fb71834c8c> in <module>()
1 # Install TF-Hub.
2 import tensorflow as tf
----> 3 import tensorflow_hub as hub
4 import matplotlib.pyplot as plt
5 import numpy as np
/usr/local/lib/python2.7/dist-packages/tensorflow_hub/__init__.py in <module>()
24 import tensorflow as tf
25
---> 26 from tensorflow_hub.estimator import LatestModuleExporter
27 from tensorflow_hub.estimator import register_module_for_export
28 from tensorflow_hub.feature_column import image_embedding_column
/usr/local/lib/python2.7/dist-packages/tensorflow_hub/estimator.py in <module>()
59
60
---> 61 class LatestModuleExporter(tf.estimator.Exporter):
62 """Regularly exports registered modules into timestamped directories.
63
AttributeError: 'module' object has no attribute 'Exporter'
Its due to the old version of tensorflow. It should be >1.7

Getting the following error, "ImportError: cannot import name 'rnn_cell_impl' " on tensorfow V 1.4.0

~/anaconda3/lib/python3.6/site-packages/tflearn/layers/recurrent.py in <module>()
12 # Fix for TF 1.1.0 and under
13 from tensorflow.contrib.rnn.python.ops.core_rnn import static_rnn as _rnn, static_bidirectional_rnn as _brnn
---> 14 from tensorflow.python.ops.rnn import rnn_cell_impl as _rnn_cell, dynamic_rnn as _drnn
15 from tensorflow.contrib.rnn.python.ops import core_rnn_cell
16
ImportError: cannot import name 'rnn_cell_impl'
Getting the following error. TF version 1.4 .
Post your code also. Try following way,
from tensorflow.python.ops import rnn_cell_impl

Categories

Resources