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
Related
I am trying to run a code using keras. The program uses from keras.backend.tensorflow_backend import set_session and i am getting an underhanded Exception thats says No module named 'keras.backend.tensorflow_backend'; 'keras.backend' is not a package with the following error code File "c:/Users/phili/Desktop/python-projects/test.py", line 15, in <module> from keras.backend.tensorflow_backend import set_session ModuleNotFoundError: No module named 'keras.backend.tensorflow_backend'; 'keras.backend' is not a package
I am using python 3.7 Keras 2.4.3 and tensorflow 2.2.0, is there any solution to the problem i can provide the whole code if needed
Thanks in advance
I guess you are importing it incorrectly.
The command to import set_session, for Tensorflow 2.3 (latest version) is shown below.
from tensorflow.compat.v1.keras.backend import set_session
Please find this Colab for working code.
Please refer this Tensorflow Documentation for more information.
So I am new to Keras and Tensorflow. I'm trying to import the img_to_array function and for some reason it is not importing it. I read the documentation it says from PIL import Image and after that use tf.keras.preprocessing.image.img_to_array(img) and that doesn't work either. I am using anaconda.
Here's the code:
from keras.preprocessing.image import img_to_array
And here's the error:
AttributeError: module 'tensorflow' has no attribute 'name_scope'
I am trying to implement the code for number plate detection. I am working with tensorflow 2.1. The code i was working on was compatible with tensorflow 1.x, i made the necessary changes. But the following piece of code gives trouble:
import logging
import tensorflow as tf
from google.protobuf import text_format
from protos import string_int_label_map_pb2
...
The following error comes:
File "C:/Users/hp/Downloads/Compressed/models1/utils/label_map_util.py", line 24, in <module>
from protos import string_int_label_map_pb2
ModuleNotFoundError: No module named 'protos'
I have tried all the implementations that are available on internet but it is not working. Can anyone guide me what is the way forward? Thanks.
i solve this problem by coping the file in protos folder in the folder where string_int_label_map_pb2 is to enable import
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.
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