from tensorflow import keras
from segmentation_models import PSPNet
While running this, I am facing the error below:
AttributeError Traceback (most recent call last)
<ipython-input-14-fbd9360b4944> in <module>()
1
2 from tensorflow import keras
----> 3 from segmentation_models import PSPNet
3 frames
/usr/local/lib/python3.6/dist-packages/segmentation_models/__init__.py in <module>()
96 _framework = os.environ.get('SM_FRAMEWORK', _DEFAULT_KERAS_FRAMEWORK)
97 try:
---> 98 set_framework(_framework)
99 except ImportError:
100 other = _TF_KERAS_FRAMEWORK_NAME if _framework == _KERAS_FRAMEWORK_NAME else _KERAS_FRAMEWORK_NAME
/usr/local/lib/python3.6/dist-packages/segmentation_models/__init__.py in set_framework(name)
66 if name == _KERAS_FRAMEWORK_NAME:
67 import keras
---> 68 import efficientnet.keras # init custom objects
69 elif name == _TF_KERAS_FRAMEWORK_NAME:
70 from tensorflow import keras
/usr/local/lib/python3.6/dist-packages/efficientnet/keras.py in <module>()
15 preprocess_input = inject_keras_modules(model.preprocess_input)
16
---> 17 init_keras_custom_objects()
/usr/local/lib/python3.6/dist-packages/efficientnet/__init__.py in init_keras_custom_objects()
69 }
70
---> 71 keras.utils.generic_utils.get_custom_objects().update(custom_objects)
72
73
AttributeError: module 'keras.utils' has no attribute 'generic_utils'
I installed segmentation-models library using pip as the provided instruction (link)
I would be appreciated if someone can help me with how to fix it. I simply copy the code from the instruction, and all I found on the net was just the same. Should it be something wrong with the installation?
Please guide me through this!:)
You are facing this issue because you are using Tensorflow version >= 2.2. To fix this problem either you have to use Tensorflow 2.1/2.0 or Tensorflow 1.x (i.e 1.15.2)
Please follow below steps to perform Image segmentation using Segmentation models using TF 2.1.
!pip install q tensorflow==2.1
!pip install segmentation-models
import tensorflow as tf
from segmentation_models import PSPNet
#Instantiate PSPNet model
model = PSPNet()
#Display model summary
model.summary()
Related
TypeError Traceback (most recent call last)
in
5 import numpy as np
6 import tensorflow as tf
----> 7 import tensorflow_federated as tff
14 frames
/usr/lib/python3.8/typing.py in _type_check(arg, msg, is_argument)
147 return arg
148 if not callable(arg):
--> 149 raise TypeError(f"{msg} Got {arg!r:.100}.")
150 return arg
151
TypeError: Callable[[arg, ...], result]: each arg must be a type. Got Ellipsis.
I'm getting this error while trying to import tensorflow_federated in Colab.
I have tried installing lower compatible versions of tensorflow and tensorflow_federated, but nothing works. Can someone else also faced this problem? and Do anyone know how to fix it?
I get the following error when attempting to create a TB callback.
I am using PlaidML in a Keras sequential model in a Jupiter notebook in MacOS 10.15.7 with
Keras version 2.2.4, &
TensorFlow 2.4.1
import plaidml.keras
plaidml.keras.install_backend()
import os
os.environ['KERAS_BACKEND'] = 'plaidml.keras.backend'
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import numpy as np
import pathlib
import PIL
import PIL.Image
# tensorflow for TensorBoard
import tensorflow
logs_dir = '/Users/x/TensorFlowFolder/logs'
from keras.callbacks import TensorBoard
callbacks_list = [
keras.callbacks.EarlyStopping(
monitor='val_loss',
patience=2,
),
keras.callbacks.ModelCheckpoint(
'best_modelFour.h5',
monitor='val_loss',
verbose=1,
save_best_only=True
),
keras.callbacks.TensorBoard(
log_dir=logs_dir,
histogram_freq=0,
write_graph=True, write_images=True
)
]
The Error chain is
> ModuleNotFoundError Traceback (most recent call
> last) ~/opt/anaconda3/lib/python3.8/site-packages/keras/callbacks.py
> in __init__(self, log_dir, histogram_freq, batch_size, write_graph,
> write_grads, write_images, embeddings_freq, embeddings_layer_names,
> embeddings_metadata, embeddings_data, update_freq)
> 744 import tensorflow as tf
> --> 745 from tensorflow.contrib.tensorboard.plugins import projector
> 746 except ImportError:
>
> ModuleNotFoundError: No module named 'tensorflow.contrib'
>
> During handling of the above exception, another exception occurred:
>
> ImportError Traceback (most recent call
> last) <ipython-input-4-93b8c44b3056> in <module>
> 15 save_best_only=True
> 16 ),
> ---> 17 keras.callbacks.TensorBoard(
> 18 log_dir=logs_dir,
> 19 histogram_freq=0,
>
> ~/opt/anaconda3/lib/python3.8/site-packages/keras/callbacks.py in
> __init__(self, log_dir, histogram_freq, batch_size, write_graph, write_grads, write_images, embeddings_freq, embeddings_layer_names,
> embeddings_metadata, embeddings_data, update_freq)
> 745 from tensorflow.contrib.tensorboard.plugins import projector
> 746 except ImportError:
> --> 747 raise ImportError('You need the TensorFlow module installed to '
> 748 'use TensorBoard.')
> 749
>
> ImportError: You need the TensorFlow module installed to use
> TensorBoard.
TensorBoard is apparently trying to get the module Projector from TensorFlow.contrib. TensorFlow.contrib has apparently been removed from TensorFlow for quite some time. The second error appears to be spurious since TF is successfully installed. I imagine that if this really broke TensorBoard there would be a big squawk, so what am I doing wrong?
Keras 2.2 does not support TensorFlow 2. Excerpt from the release of the 2.2.5:
Keras 2.2.5 is the last release of Keras that implements the 2.2.* API. It is the last release to only support TensorFlow 1 (as well as Theano and CNTK).
Downgrade your TensorFlow version to the 1.15.
I have been running my DL models on colab well, until they upgraded the system last Friday.
I get this error:
AttributeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _get_default_graph()
65 try:
---> 66 return tf.get_default_graph()
67 except AttributeError:
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
During handling of the above exception, another exception occurred:
RuntimeError Traceback (most recent call last)
8 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _get_default_graph()
67 except AttributeError:
68 raise RuntimeError(
---> 69 'It looks like you are trying to use '
70 'a version of multi-backend Keras that '
71 'does not support TensorFlow 2.0. We recommend '
RuntimeError: It looks like you are trying to use a version of multi-backend Keras that does not support TensorFlow 2.0. We recommend using tf.keras, or alternatively, downgrading to TensorFlow 1.14.
I am currently running Python 3.7.6 version on my Mac(Mojave).
I am running TensorFlow 2.0.0 version on my machine.
I too had the same issue while using the Google Colab.
So what I did was, imported all the modules from the tensorflow.keras modules, instead of using the traditional method of calling via keras only.
eg. instead of from keras.models import Sequential
use
from tensorflow.keras import Sequential
And,
from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout use
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dropout, Dense
So, my new code looks like this. My new code.
Try using from tensorflow.keras.models import Model
And make sure that all the Keras modules imported are of the same version.
Don't use
from tensorflow.keras.models import Model
and
from Keras import layers,
while building NN in the same notebook else it keeps on throwing some errors.
This solution helped me.
I am following a tutorial for TensorFlow and Keras.
When I run the following syntax:
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))
(This is not full stacktrace as StackOverflow wont let me saying it has code only)
I get following error:
--------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-49-30ebc8793948> in <module>()
5 from keras.layers import Dense
6
----> 7 model.add(Dense(units=64, activation='relu', input_dim=100))
--> 164 layer(x)
165 set_inputs = True
166 else:
/usr/local/lib/python3.7/site-packages/keras/initializers.py in __call__(self, shape, dtype)
self._message_listener.Modified()
UnboundLocalError: local variable 'self' referenced before assignment
Could this be because of python 3.7?
Yes, I had to uninstall and reinstall Python 3.6. Python 3.7 looks like it introduces unexpected behavior. Thankfully, my issue is now resolved.
I had the same issue. The problem seemed to be with python 3.7. Instead of uninstalling 3.7 and installing 3.6 I downloaded anaconda, created a virtual environment for 3.6 along with all the other dependancies.
I've created a very simple Sequential Linear Regression model using Keras 2.0.4 (TensorFlow 1.1.0 backend) and my coremltools (0.6.3) export is failing with this error message:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () ----> 1 coreml_model = coremltools.converters.keras.convert(model, input_names="input", output_names="output") /Users/Jacopo/anaconda/envs/KerasTensorFlowCoreML/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.pyc in convert(model, input_names, output_names, image_input_names, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, predicted_probabilities_output) 489 predicted_probabilities_output = predicted_probabilities_output) 490 elif _HAS_KERAS2_TF: --> 491 from . import _keras2_converter 492 return _keras2_converter._convert(model = model, 493 input_names = input_names, /Users/Jacopo/anaconda/envs/KerasTensorFlowCoreML/lib/python2.7/site-packages/coremltools/converters/keras/_keras2_converter.py in () 66 _keras.layers.wrappers.TimeDistributed:_layers2.default_skip, 67 ---> 68 _keras.applications.mobilenet.DepthwiseConv2D:_layers2.convert_convolution, 69 70 } AttributeError: 'module' object has no attribute 'mobilenet'
I'm using Python 2.7 on macOS
As said this is a very simple Linear Regression and the module has no image input at all.
Any hint ?
Thanks, Jacopo
Updating Keras to 2.0.6 worked for me...
coremltools works when keras uses tensorflow, not theano, as its backend.
you can change keras' default backend at $HOME/.keras/keras.json, and changing to "backend": "tensorflow".