Keras import error: cannot import np_utils - numpy

import keras
Runs in the following error. My python is installed with Anaconda and I have done pip install Tensorflow / Keras / Numpy
ImportError Traceback (most recent call
last)
<ipython-input-7-c74e2bd4ca71> in <module>()
----> 1 import keras
/Users/florianfontaine-papion/anaconda2/lib/python2.7/site-
packages/keras/__init__.py in <module>()
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
/Users/florianfontaine-papion/anaconda2/lib/python2.7/site-packages/keras/utils/__init__.py in <module>()
1 from __future__ import absolute_import
----> 2 from . import np_utils
3 from . import generic_utils
4 from . import data_utils
5 from . import io_utils
ImportError: cannot import name np_utils

First, check whether Keras is installed or not by running following command in terminal. If you are using python3, use python3 in place of python.
python -c 'import keras; print(keras.__version__)'
And if above command showing error do the following
Open terminal and type this
anaconda-navigator
Go to Environments tab present on left side
Select your destined environment
It will list all the packages installed. In first dropdown select ALL and in search bar type keras
It will list keras package. Select it and click apply at bottom
Done

Related

cannot import tensorflow_federated as tff

When I try to import tensorflow_federated I got this error, anyone has the idea why?
ImportError Traceback (most recent call last)
<ipython-input-6-a23308ec3f7c> in <module>()
3 import numpy as np
4 import tensorflow as tf
----> 5 import tensorflow_federated as tff
6
7 np.random.seed(0)
17 frames
/usr/local/lib/python3.7/dist-packages/keras/api/_v1/keras/mixed_precision/experimental/__init__.py in <module>()
6 import sys as _sys
7
----> 8 from keras.mixed_precision.loss_scale_optimizer import LossScaleOptimizerV1 as LossScaleOptimizer
9 from tensorflow.python.util import module_wrapper as _module_wrapper
10
ImportError: cannot import name 'LossScaleOptimizerV1' from 'keras.mixed_precision.loss_scale_optimizer' (/usr/local/lib/python3.7/dist-packages/keras/mixed_precision/loss_scale_optimizer.py)
Found the solution, I did this on Google Colab. I just uninstall keras version that was already installed by Colab and reinstall the latest version.

cannot import name 'get_config' from 'tensorflow.python.eager.context'?

I'm trying to follow this repo's tutorial on colabhttps://github.com/divamgupta/image-segmentation-keras
but I'm getting this error again and again
cannot import name 'get_config' from 'tensorflow.python.eager.context' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/context.py)
I googled it a lot so I found some solutions that solved same problems for them, like upgrade tensorflow version or using from tensorflow import keras instead of import keras
or add this code snippet
!pip install --upgrade tensorflow
!pip install --upgrade tensorflow-gpu
But none of them worked!
Full error codes are as follow
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-10-5c4963dc4fff> in <module>()
1 from tensorflow import keras
2 from tensorflow.keras.preprocessing import image
----> 3 from keras_segmentation.models.unet import vgg_unet
4
5 model = vgg_unet(n_classes=50 , input_height=320, input_width=640 )
3 frames
/usr/local/lib/python3.7/dist-packages/keras_segmentation/models/unet.py in <module>()
----> 1 from keras.models import *
2 from keras.layers import *
3
4 from .config import IMAGE_ORDERING
5 from .model_utils import get_segmentation_model
/usr/local/lib/python3.7/dist-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
/usr/local/lib/python3.7/dist-packages/keras/models.py in <module>()
17
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
/usr/local/lib/python3.7/dist-packages/keras/backend.py in <module>()
35 from tensorflow.python.distribute import distribute_coordinator as dc
36 from tensorflow.python.distribute import distribute_coordinator_context as dc_context
---> 37 from tensorflow.python.eager.context import get_config
38 from tensorflow.python.framework import config
39 from keras import backend_config
ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/context.py)
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
Full codes are as follow
!pip install keras-segmentation
!pip install keras==2.4.3
#!pip install tensorflow==2.4.1
!pip install tensorflow==2.5.0
!apt-get install -y libsm6 libxext6 libxrender-dev
!pip install opencv-python
!pip install --upgrade tensorflow
!pip install --upgrade tensorflow-gpu
! wget https://github.com/divamgupta/datasets/releases/download/seg/dataset1.zip && unzip dataset1.zip
from tensorflow import keras
from tensorflow.keras.preprocessing import image
from keras_segmentation.models.unet import vgg_unet
model = vgg_unet(n_classes=50 , input_height=320, input_width=640 )
model.train(
train_images = "dataset1/images_prepped_train/",
train_annotations = "dataset1/annotations_prepped_train/",
checkpoints_path = "/tmp/vgg_unet_1" , epochs=5
)
out = model.predict_segmentation(
inp="dataset1/images_prepped_test/0016E5_07965.png",
out_fname="/tmp/out.png"
)
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
plt.imshow(out)
from IPython.display import Image
Image('/tmp/out.png')
o = model.predict_segmentation(
inp="dataset1/images_prepped_test/0016E5_07965.png",
out_fname="/tmp/out.png" , overlay_img=True, show_legends=True,
class_names = [ "Sky", "Building", "Pole","Road","Pavement","Tree","SignSymbol", "Fence", "Car","Pedestrian", "Bicyclist"]
)
from IPython.display import Image
Image('/tmp/out.png')
I would appreciate any of your help
thanks in advance
From comments
It was just a matter of version
with tensorflow and keras. I looked into traceback tensorflow error
messages and opened it and changed import keras to from tensorflow import keras issue was resolved (Paraphrased from z2ouu).

Dropbox ModuleNotFoundError: No module named 'stone'

Im trying to import Dropbox with the line:
import dropbox
on Google Colab but im getting the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-26-fac67f6fa745> in <module>()
28 import math
29 import io
---> 30 import dropbox
31 from keras.models import Sequential
32 from keras.layers import Dense
2 frames
/usr/local/lib/python3.6/dist-packages/dropbox/auth.py in <module>()
5 # pylint: skip-file
6 from __future__ import unicode_literals
----> 7 from stone.backends.python_rsrc import stone_base as bb
8 from stone.backends.python_rsrc import stone_validators as bv
9
ModuleNotFoundError: No module named 'stone'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
It was working perfectly normal a day ago
The problem is that a new release of Dropbox API was out today and it has that glitch. Just used the version before today by using
pip install dropbox==10.10.0

pip install PyRQA in Google colab

Hope someone can help me, when I try to install and run PyRQA in colab in the GPU runtime and after run the next code:
pip install PyRQA
from pyrqa.time_series import TimeSeries
from pyrqa.settings import Settings
from pyrqa.analysis_type import Classic
from pyrqa.neighbourhood import FixedRadius
from pyrqa.metric import EuclideanMetric
from pyrqa.computation import RQAComputation
from pyrqa.computation import RPComputation
from pyrqa.image_generator import ImageGenerator
give me the next error.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-6c56a517e983> in <module>()
4 from pyrqa.neighbourhood import FixedRadius
5 from pyrqa.metric import EuclideanMetric
----> 6 from pyrqa.computation import RQAComputation
7 from pyrqa.computation import RPComputation
8 from pyrqa.image_generator import ImageGenerator
6 frames
/usr/local/lib/python3.6/dist-packages/pyopencl/tools.py in <module>()
35 from decorator import decorator
36 from pytools import memoize, memoize_method
---> 37 from pyopencl._cl import bitlog2 # noqa: F401
38 from pytools.persistent_dict import KeyBuilder as KeyBuilderBase
39
ImportError: /usr/lib64-nvidia/libOpenCL.so.1: version `OPENCL_2.1' not found (required by /usr/local/lib/python3.6/dist-packages/pyopencl/_cl.cpython-36m-x86_64-linux-gnu.so)
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
Some days ago it works fine, but after yesterday it doesn't work anymore

Keras Import Error Tensorflow backend

---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in <module>()
4 try:
----> 5 from tensorflow.python.ops import ctc_ops as ctc
6 except ImportError:
ImportError: cannot import name 'ctc_ops'
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
<ipython-input-10-c74e2bd4ca71> in <module>()
----> 1 import keras
/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/__init__.py in <module>()
1 from __future__ import absolute_import
----> 2 from . import backend
3 from . import datasets
4 from . import engine
5 from . import layers
/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/backend/__init__.py in <module>()
67 elif _BACKEND == 'tensorflow':
68 sys.stderr.write('Using TensorFlow backend.\n')
---> 69 from .tensorflow_backend import *
70 else:
71 raise Exception('Unknown backend: ' + str(_BACKEND))
/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in <module>()
5 from tensorflow.python.ops import ctc_ops as ctc
6 except ImportError:
----> 7 import tensorflow.contrib.ctc as ctc
8
9 import numpy as np
ImportError: No module named 'tensorflow.contrib.ctc'
Seems like is an problem with Keras and not Tensorflow according to this post enter link description here
I was wondering if this has been fixed yet?
This is a problem between Keras and tensorflow. You should change this fix to use something like
if hasattr(tf.contrib, 'ctc'):
ctc = tf.contrib.ctc # old version
else:
ctc = tf.nn # New official version.
The current "official" way to access the ctc operations are in tf.nn, for instance, tf.nn.ctc_loss.
If keras is giving an import problem and you are facing "no module named keras" even if you have installed it.
Just upgrade your pip by:
python -m pip install –upgrade pip
Re-install upgraded keras and tensorflow by:
pip install keras
pip install tensorflow
Hope it will solve the problem. If not, try upgrading your conda (Anaconda) and then do steps 1 to 3 above again.