Error in importing Keras sequential model - python

I have installed keras followed by tensorflow. When I execute keras sequential model, I get an error message stating that
from keras.models import sequential
Using TensorFlow backend.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'sequential'

Changing my comment to the answer.
Try Sequential with capital S

The problem was the lower case s in Sequential when you imported it. It should have been - from keras.models import Sequential.However, as a side note, you can use the code from tensorflow.keras import Sequential, using the tensorflow keras api 😊. If you are using colab, you can write %tensorflow_version 2.x at the beginning of your notebook to use version 2 of tensorflow

Related

ImportError: cannot import name convert_all_kernels_in_model

I am trying to use ImageAI’s model training to train an AI model.
This is the code:
from imageai.Prediction.Custom import ModelTraining
model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
model_trainer.setDataDirectory("idenprof")
model_trainer.trainModel(num_objects=2, num_experiments=3, enhance_data=True,
batch_size=32, show_network_summary=True)
This is the error I get when running:
Traceback (most recent call last):
File ".../FirstTraining.py", line 1, in <module>
from imageai.Prediction.Custom import ModelTraining
File ".../lib/python2.7/site-packages/imageai/Prediction/Custom/__init__.py", line 4, in <module>
from ..DenseNet.densenet import DenseNetImageNet121
File ".../PycharmProjects/bonez/venv/lib/python2.7/site-packages/imageai/Prediction/DenseNet/densenet.py", line 21, in <module>
from tensorflow.python.keras.utils import convert_all_kernels_in_model
ImportError: cannot import name convert_all_kernels_in_model
I have searched all over but I could not find the same issue or a way to solve the problem. I have the following dependencies installed: Tensorflow, OpenCV, Keras, and ImageAI.
Update: It turns out ImageAI does not support Tensorflow 2 yet. This issue does not happen with following tensorflow version: pip install tensorflow==1.15.2
I had the same issue and resolved it by replacing all tensorflow.python.keras imports with tensorflow.keras in ImageAI library. After this, from imageai.Prediction.Custom import ModelTraining import works fine.
If you want to follow, there is an open issue for problem in ImageAI: https://github.com/OlafenwaMoses/ImageAI/issues/494

Python module has no attribute error when importing with an 'as'

I want to import Keras from the Tensorflow library. So I did the following thing :
import tensorflow.python.keras as keras
But this import throws the error :
AttributeError: module 'tensorflow' has no attribute 'python'
It appears that both the following import work correctly :
import tensorflow.python.keras
from tensorflow.python import keras
For me, import tensorflow.python.keras as keras and from tensorflow.python import keras are identical but it look like it's not true. What is the difference between these two imports ?
You should try something like this:
import tensorflow as tf
import tensorflow
from tensorflow import keras
from keras.layers import Dense
# ...
The first line helps you use the tensorflow module with the tf short name (asname).
For the asname you can check this thread.

Keras model to tensorflow.keras

I have a model that I made with Keras (using Tensorflow as backend).
Now I want to use the Keras inside the Tensorflow release.
However, replacing this line
from keras.engine.topology import get_source_inputs
with this line
from tensorflow.keras.engine.topology import get_source_inputs
does not work. It says
ImportError: No module named 'tensorflow.keras.engine'
where can I find the get_source_inputs function in Tensorflow?
Updating Keras to version 2.2.4 seems to do the trick for me!
You can then import the get_source_inputs function in one of the following ways:
from keras.utils import get_source_inputs
from keras.utils.layer_utils import get_source_inputs
or if you are not using Keras as an external library but the TF version of it, then you can import the function with:
from tensorflow.keras.utils import get_source_inputs
Hope this helps!

Import tensorflow issue on Python 3.6

When I try to import tensorflow in my Python scripts, I have some weird results. For instance:
import tensorflow
from keras.datasets import imdb
gives me
ModuleNotFoundError
Traceback (most recent call last) <ipython-input-12-25cf0f878919> in <module>()
1 import tensorflow
----> 2 from keras.datasets import imdb
ModuleNotFoundError: No module named 'keras'
If I try:
import tensorflow as tf
from tf.keras.datasets import imdb
I get :
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-9-bd3db3d3567b> in <module>()
1 import tensorflow as tf
----> 2 from tf.keras.datasets import imdb
ModuleNotFoundError: No module named 'tf'
But, if I use :
from tensorflow.keras.datasets import imdb
it works.
I've been googling this for a full hour now, and I still don't understand what I'm doing wrong in the first two scripts.
Thanks
You haven't specified how and where you installed tensorflow, so I could be wrong, but:
(a) it appears that keras is installed with tensorflow, but not in a location that is in the default Python path (hence, you cannot do from keras.datasets import imdb).
(b) this combination:
import tensorflow as tf
from tf.keras.datasets import imdb
is invalid, because from x import y searches for x as a module and not as a symbol in your code's globals (and tf is NOT a module name but a global variable, import tensorflow as tf imports tensorflow and sets tf to point to the module object).
Therefore (unless you fix your install or your PYTHONPATH to make keras visible as a module), you should use this to import keras (or specific symbols from it):
# either this, to access keras.*, e.g., keras.datasets.imdb
import tensorflow.keras as keras
# or this, as you've done in your example
from tensorflow.keras.datasets import imdb

Tensorflow Modules in Jupiter Notebooks (using OSX)

I am having trouble getting tensorflow to work using Jupiter notebooks. I am a complete noob so please keep responses as simple as possible and apologies if this is trivial.
I run the following code in the notebook:
import tensorflow as tf
from tensorflow.python.framework import ops
And get this error message:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-15-509396287076> in <module>()
1
2 import tensorflow as tf
----> 3 from tensorflow.python.framework import ops
4 from tf_utils import load_dataset, random_mini_batches, convert_to_one_hot, predict
5
ModuleNotFoundError: No module named 'tensorflow.python'
(I originally struggled to get the "import tensorflow as tf" line to work but have resolved that. The code runs fine without the second line...)
Thanks for your help!

Categories