from keras.backend.tensorflow_backend import set_session - python

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.

Related

AttributeError: module 'keras.preprocessing.image' has no attribute 'img_to_array'

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 installed tensorflow successfully, but when i tried to import tensorflow the error was shown like this : can any one help me for this ? thank you

File "c:\users\w\appdata\local\programs\python\python39\lib\site-packages\tensorflow_api\v2_internal_\distribute\interim_init_.py", line 8, in
from tensorflow.python.distribute.merge_call_interim import maybe_merge_call
ModuleNotFoundError: No module named 'tensorflow.python.distribute.merge_call_interim'

No module named 'keras.backend.tensorflow_backend'

I am trying to learn from a python script on semantic search that uses sentence transformer-BERT, While compiling this python script (semantic_search.py) file I encountered the following error:
Traceback (most recent call last):
File "semantic_search.py", line 6, in <module>
import keras.backend.tensorflow_backend as tb
ModuleNotFoundError: No module named 'keras'
Kindly suggest possible source of error and corrections.
This is a problem with versions of keras and tensorflow. It sounds like this tutorial was written before keras became a part of tensorflow. For more info on why this happened and what version you should use, see this article.
Probably what you'd need is to change any imports in the script that start with keras to tensorflow.keras, if you have tensorflow version 2.0 and above. So for that line specifically, you probably want:
import tensorflow.keras.backend as tb

from protos import string_int_label_map_pb2. ModuleNotFoundError: No module named 'protos'?

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

object detection using tensorflow by own classifier

When I try training my train.py for object detection using the tensorflow/models repository using the code
python train.py --logtostderr --train_dir=training_dir/ --pipeline_config_path=training/faster_rcnn_inception_resnet_v2_atrous_pets.config
I am unable to run this command.
I have tried including all the files within object_detection as well tried to remove the object_detection. In the from statement, but it didn't work.
import functools
import json
import os
import tensorflow as tf
from object_detection.builders import dataset_builder
from object_detection.builders import graph_rewriter_builder
from object_detection.builders import model_builder
from object_detection.legacy import trainer
from object_detection.utils import config_util
Traceback (most recent call last):
File "C:/Users/varsh/Documents/models/research/object_detection/train.py", line 49, in <module>
from object_detection.builders import dataset_builder
ModuleNotFoundError: No module named 'object_detection'
I've managed to solve this problem in my system (windows 10). The solution is not very straight forward but:
1) First u need to clone Tensorflow Object Detection API repository https://github.com/tensorflow/models.
2) Follow the installation provided in https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md
3) In the step 2, you are required to compile protobuf library, so download the protobuf compiler at https://github.com/google/protobuf/releases/latest (at the time of this writing (3.5.1), there's a bug in protoc which may or may not related to the Windows environment, my solution is use the protoc v 3.4.0)
4) Append the PYTHONPATH environment variable with the directory of /research/ and /research/slim (dont forget to add the PYTHONPATH to Path if you haven't done so.
5) No more ModuleNotFoundError: No module named 'object_detection'

Categories