failed to import keras with CNN model - python

from tensorflow.keras.models import Sequential
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers import MaxPool2D
from keras.layers.core import Activation, Flatten, Dropout, Dense
from tensorflow.keras import backend as K
import keras
the code can't read convolution, Batchnormaliztion, maxpool2D and etc.
I got this error:
No module named 'module 'tensorflow' has no attribute 'get_default_graph''

If the error No module named 'module 'tensorflow' most probably you need to install it correctly.
If the error is module 'tensorflow' has no attribute 'get_default_graph':
first you need to unify the imports either use keras or tensorflow.keras
If the problem still exists try from tensorflow.python.keras import backend as k

I use tensorflow version 2.2.0. And it works, but instead:
from keras.layers import MaxPool2D
I use:
from keras.layers import MaxPooling2D

I think you might have a problem with your Tensorflow module.
Run the following code and see if it outputs you details about your Tensorflow Module:
pip show tensorflow
If this shows you details about your TensorFlow module, then you have installed it correctly.
I would suggest you use Anaconda for module management.

Related

ImportError: cannot import name 'keras_ctcmodel' from 'keras'

I'm trying to run a Connectionist Temporal Classification Model (Automatic Speech Recognition), and below is the beginning code I got from the example.py in https://github.com/cyprienruffino/CTCModel/blob/master/example.py:
import os
!pip install keras
import keras
!pip install tensorflow
import tensorflow
from keras.layers import TimeDistributed, Activation, Dense, Input, Bidirectional, LSTM, Masking, GaussianNoise
from tensorflow.keras.optimizers import Adam
from keras import keras_ctcmodel
from keras_ctcmodel.CTCModel import CTCModel as CTCModel
import pickle
from keras.preprocessing import sequence
from tensorflow.python.keras.utils.generic_utils import Progbar
import numpy as np
from six.moves.urllib.request import urlretrieve
However, I keep getting an import error on
from keras_ctcmodel.CTCModel import CTCModel as CTCModel
I faced this issue with importing Adam as well until I added tensorflow. Is this a bug in Keras?

ImportError: cannot import name 'image_dataset_from_directory' from 'keras.preprocessing'

I used anaconda in ubuntu-20.04 operating systems.
Keras version is: 2.4.3
Tensorflow version: 2.2.0
I imported below packages,
import os
import keras
import numpy as np
import tensorflow as tf
from keras.models import Model
import matplotlib.pyplot as plt
from keras.optimizers import Adam
from keras.applications import MobileNet
from sklearn.metrics import confusion_matrix
from keras.layers.core import Dense, Activation
from keras.metrics import categorical_crossentropy
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.mobilenet import preprocess_input
from keras.preprocessing import image_dataset_from_directory //Here I faced error
Error is also below, please concern here,
ImportError Traceback (most recent call last)
<ipython-input-8-477695c122c5> in <module>
13 from keras.preprocessing.image import ImageDataGenerator
14 from keras.applications.mobilenet import preprocess_input
---> 15 from keras.preprocessing import image_dataset_from_directory
ImportError: cannot import name 'image_dataset_from_directory' from 'keras.preprocessing' (/home/imdadul/anaconda3/lib/python3.8/site-packages/keras/preprocessing/__init__.py)
Note: Please concern my problem and help me for solving this error.
First off, I cannot find any keras implementation of image_dataset_from_directory, but instead only the tensorflow.keras implementation, imported by writing: from tensorflow.keras.preprocessing import image_dataset_from_directory . In the keras-documentation, they refer to use tensorflow.keras as well.
However, since you are using Tensorflow 2.2.0, I am afraid that will not be possible either, as explained here. I would recommend installing Tensorflow 2.3.0, and it should work.

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!

keras - cannot import name Conv2D

I recently got the deep learning docker from https://github.com/floydhub/dl-docker running and while trying out the tutorials, received an error when importing the keras layers module.
from __future__ import print_function
import keras
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-13-3a12c6f32fcf> in <module>()
5 from keras.models import Sequential
6 from keras.layers import Dense, Dropout, Activation, Flatten
----> 7 from keras.layers import Conv2D, MaxPooling2D
ImportError: cannot import name Conv2D
I am running with ubuntu 14.04, python version 2.7.6 on the ipython notebook and the following versions of the deep learning libraries on docker.
ARG THEANO_VERSION=rel-0.8.2
ARG TENSORFLOW_VERSION=0.12.1
ARG TENSORFLOW_ARCH=cpu
ARG KERAS_VERSION=1.2.0
ARG LASAGNE_VERSION=v0.1
ARG TORCH_VERSION=latest
ARG CAFFE_VERSION=master
Im not sure if the problem lies with the version because it seems that there no related issues on the github thread.
Try this: from keras.layers.convolutional import Conv2D
Importing changed with the new keras. Are you sure you are not using keras >= 2?
NOTE:
With tensorflow 2.0 keras is included. You can now import the layer with:
from tensorflow.keras.layers import Conv2D
Following packages which are very essential for CNN (Convolutional Neural Networks) are reorganized into different packages
from keras.layers.convolutional import Conv2D
from keras.layers import Dense
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Flatten
Whenever you get an import error always google the name for the package and the library it is associated for example google "Keras Convolution2D". It will direct you to the keras documentation. That will easily give away the path to import.
For Keras 1.2.0 (the current one on floydhub as of print(keras.__version__)) use these imports for Conv2D (which you use) and Conv2DTranspose (used in the Keras examples):
from keras.layers import Convolution2D as Conv2D
from keras.layers.convolutional import Deconv2D as Conv2DTranspose

Categories