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!
Related
I have added following libraries and half part of the code is executing. In second half I get this error.
Libraries added:
#importing libraries
import keras
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
from tensorflow.keras.utils import img_to_array
Error:
AttributeError: module 'keras.preprocessing.image' has no attribute 'img_to_array'
I was following this code and changes the libraries too still can't resolve the issue.
https://www.analyticsvidhya.com/blog/2021/06/k-means-clustering-and-transfer-learning-for-image-classification/
It has now moved to tf.keras.utils.img_to_array. See the docs
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?
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.
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.
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.