TensorFlow 2.5 update in Colab breaks keras code - python

I have been running several deep learning models in google colab using Keras.
My codes worked perfectly but suddenly, today, a lot of errors appeared reporting problemas with the imports. Pasting one here.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-3-745bb0894037> in <module>()
19 from keras.preprocessing.image import ImageDataGenerator
20 from keras.utils import np_utils
---> 21 from keras_contrib.applications.densenet import DenseNetFCN
22 from keras_contrib.losses.jaccard import jaccard_distance
1 frames
/usr/local/lib/python3.7/dist-packages/keras_contrib/applications/densenet.py in <module>()
66 from keras.layers import BatchNormalization
67 from keras.regularizers import l2
---> 68 from keras.utils.layer_utils import convert_all_kernels_in_model
69 from keras.utils.data_utils import get_file
70 from keras.engine.topology import get_source_inputs
ImportError: cannot import name 'convert_all_kernels_in_model' from 'keras.utils.layer_utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/layer_utils.py)

so I am answering my question for anyone who might have trouble with it.
They updated Tensorflow and Keras in Colab to 2.5 versions, and some functions are not migrated (i.e. convert_all_kernels_in_model).
For me, the solution was to paste the code of the NN architecture and comment the line with that function as I was not using it.
I know it is not the best solution but it got me through what I needed.

The final release of TensorFlow version 2.5 requires a nightly dev build of keras that seems to be the source of these issues.
https://github.com/tensorflow/tensorflow/blob/v2.5.0/tensorflow/tools/pip_package/setup.py#L107
The relevant TensorFlow issue to report your experience is:
https://github.com/tensorflow/tensorflow/issues/49823

Related

import error of ArcFace using python in jupyternotebbok

When I am importing the package ArcFace.
from arcface.metrics import ArcFace
It is showning the import error. Like this,
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-fe2759a1802e> in <module>
15 from keras.layers.convolutional import MaxPooling2D
16 from keras.layers.core import Activation, Flatten, Dropout, Dense
---> 17 from arcface.metrics import ArcFace
18 from keras.layers import Input
19 from keras import backend as K
ModuleNotFoundError: No module named 'arcface'
I have searched for importing it for Anaconda and pip too. I have not found any results. Please help me to solve this.
This is part of project keras-arcface but it is not part of keras so you have to install/copy it separatelly.
If you put it in your project in subfolder arcface then it should work.
You can run ArcFace within deepface. The framework is mainly based on keras and tensorflow. It handles model building and downloading pre-trained weights in the background. Besides, it covers face recognition pipeline stages includign detect, align.
#!pip install deepface
from deepface import DeepFace
resp = DeepFace.verify("img1.jpg", "img2.jpg", model_name = 'ArcFace')

Cannot import 'swish' in 'tensorflow.python.keras.activations'

I am running a jupyter notebook from an Anaconda Prompt (Anaconda 3), and I am trying to use tensorflow keras.
I am trying to run the import statement:
from tensorflow.keras.models import Sequential
I get the following error:
ImportError Traceback (most recent call last)
<ipython-input-1-d23f18c08372> in <module>
4 from keras.models import Model
5
----> 6 from tensorflow.keras.models import Sequential
7
8 from tensorflow.keras.layers import Dense, Activation, Dropout
~\Anaconda3\lib\site-packages\tensorflow\keras\__init__.py in <module>
12 import sys as _sys
13
---> 14 from . import activations
15 from . import applications
16 from . import backend
~\Anaconda3\lib\site-packages\tensorflow\keras\activations\__init__.py in <module>
21 from tensorflow.python.keras.activations import softplus
22 from tensorflow.python.keras.activations import softsign
---> 23 from tensorflow.python.keras.activations import swish
24 from tensorflow.python.keras.activations import tanh
25
ImportError: cannot import name 'swish' from 'tensorflow.python.keras.activations' (C:\Users\FlamePrinz\Anaconda3\lib\site-packages\tensorflow\python\keras\activations.py)
Try this way
from keras.utils.generic_utils import get_custom_objects
from keras.layers import Activation
get_custom_objects().update({'swish': Activation(swish)})
swish function needs to be passed into activation class to actually build it.
And then you can do
model.add(Dense(64, activation = "swish"))
You can download swish activation from pypi. Try the following:
pip install swish-activation
import swish_package
from swish_package import swish
This is an older question, but I ended up taking Dr. Snoopy's suggestion uninstalling and reinstalling TensorFlow which solved my issue.

keras import assertion error in new virtual env

After installing the newest Keras and TF in a virtual env on a Win10 machine I keep having an assertion error
AssertionError Traceback (most recent call last)
<ipython-input-6-88d96843a926> in <module>()
----> 1 import keras
~\AppData\Local\Continuum\Anaconda3\envs\aind-vui\lib\site-packages\keras\__init__.py in <module>()
1 from __future__ import absolute_import
2
----> 3 from . import activations
4 from . import applications
5 from . import backend
~\AppData\Local\Continuum\Anaconda3\envs\aind-vui\lib\site-packages\keras\activations.py in <module>()
2 import six
3 import warnings
----> 4 from . import backend as K
5 from .utils.generic_utils import deserialize_keras_object
6 from .engine import Layer
~\AppData\Local\Continuum\Anaconda3\envs\aind-vui\lib\site-packages\keras\backend\__init__.py in <module>()
69 if 'KERAS_BACKEND' in os.environ:
70 _backend = os.environ['KERAS_BACKEND']
---> 71 assert _backend in {'theano', 'tensorflow', 'cntk'}
72 _BACKEND = _backend
73
AssertionError:
tensorflow itself imports fine. I also have keras working perfectly fine in my main working env and the keras.json is properly pointing to tensorflow.
pip list shows that Keras is installed in the env.
python -c "from keras import backend"
Using TensorFlow backend.
shows the proper message.
frustratingly
$ python
>>> import keras
>>> quit()
works, but doing the same thing in a jupyternotebook does not
Do
$ export KERAS_BACKEND=tensorflow
and re-run your program.
Apparently you defined it, but chose something outside of those 3 values.

Keras: Cannot Import Name np_utils [duplicate]

This question already has answers here:
ImportError: cannot import name np_utils
(16 answers)
Closed 5 years ago.
I'm using Python 2.7 and a Jupyter notebook to do some basic machine learning. I'm following along with this tutorial:
http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/
I'm simply trying to import different things from Keras so I can run the tutorial. Specifically, I do this:
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
and it gets stuck at the first import, giving me a traceback of this:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-28-aae66d0fdaee> in <module>()
----> 1 from keras.models import Sequential
2 from keras.layers import Dense
3 from keras.wrappers.scikit_learn import KerasRegressor
4 from sklearn.model_selection import cross_val_score
5 from sklearn.model_selection import KFold
/Users/newscred/anaconda/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/newscred/anaconda/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
I've Googled around but can't seem to find out why I'm running into this problem / how to fix. Any ideas?
Thanks!
That tutorial was written on June 9th, 2016. Keras 2 was released in March 2017. Try installing the old version, using pip install keras==1.2.2.
Hi this is how it worked for me.(I am using conda)
i created a virtualenv first with conda and then install tensorflow,theano and also future. make sure numpy is updated as well...
steps to follow
conda install numpy
conda install future
conda install -c anaconda theano
conda install keras
now once all this is done. you can open jupyter from the same virtualenv or spyder(i was using spyder) or ipython notebook. this will definitely work.
import numpy in your python script before you import anything from Keras. I was facing the same issue, importing numpy before importing numpy utilities ( np_utils ) solved the issue.
try
pip install --upgrade --user keras
duplicate. Solution here
ImportError: cannot import name np_utils

Import skflow on a Windows machine

I am trying to import skflow on my Windows PC. I have already install and used Anaconda on Python (3.5). I have no trouble to use tensorflow but when I want to use skflow I get the error:
ImportError Traceback (most recent call last)
<ipython-input-1-04faecc7c0de> in <module>()
8 import tensorflow as tf
9 from tensorflow.contrib import learn
---> 10 from tensorflow.contrib import skflow
ImportError: cannot import name 'skflow'
Does anyone know how to fix this?
Thanks in advance!
Please try this:
>>> import tensorflow as tf
>>> from tensorflow.contrib import learn as skflow
FYI, skflow is not any special package/module/... It's an alias for tensorflow.contrib.learn which is in 'sklearn' style.

Categories

Resources