ImportError: cannot import name convert_all_kernels_in_model - python

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

Related

When importing keras on my Raspberry Pi 4 I get the same error

I have installed Tensorflow and all its libraries following various websites and my terminal actually tells me they are installed and have been upgraded to their last versions, but somehow when I try to run the code in Python it keeps giving me the same error. In case you were wondering the Python code itself works fine on my laptop.
The error code says as it follows:
tensorflow/core/platform/hadoop/hadoop_file_system.cc:132] HadoopFileSystem load error: libhdfs.so: cannot open shared object file: No such file or directory
Traceback (most recent call last):
File "/media/pi/8F3B-E71C/TRachscope/livefeed_tensorflow_model.py", line 5, in <module>
from keras import models
File "/home/pi/.local/lib/python3.7/site-packages/keras/__init__.py", line 25, in <module>
from keras import models
File "/home/pi/.local/lib/python3.7/site-packages/keras/models.py", line 19, in <module>
from keras import backend
File "/home/pi/.local/lib/python3.7/site-packages/keras/backend.py", line 39, in <module>
from tensorflow.python.eager.context import get_config
ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' (/home/pi/.local/lib/python3.7/site-packages/tensorflow_core/python/eager/context.py)**

ImportError: cannot import name 'device_spec' from 'tensorflow.python.framework'

When i try to run
python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config
command this error pops out.
(tensorflow1.13)
C:\tensorflow1\models\research\object_detection>python train.py
--logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config Traceback (most recent call last): File "train.py", line 51, in
from object_detection.builders import dataset_builder File "C:\tensorflow1\models\research\object_detection\builders\dataset_builder.py",
line 33, in
from object_detection.builders import decoder_builder File "C:\tensorflow1\models\research\object_detection\builders\decoder_builder.py",
line 25, in
from object_detection.data_decoders import tf_example_decoder File
"C:\tensorflow1\models\research\object_detection\data_decoders\tf_example_decoder.py",
line 28, in
from tf_slim import tfexample_decoder as slim_example_decoder File
"C:\Users\user\anaconda3\envs\tensorflow1\lib\site-packages\tf_slim_init_.py",
line 25, in
from tf_slim.layers import * File "C:\Users\user\anaconda3\envs\tensorflow1\lib\site-packages\tf_slim\layers_init_.py",
line 25, in
from tf_slim.layers.layers import * File "C:\Users\user\anaconda3\envs\tensorflow1\lib\site-packages\tf_slim\layers\layers.py",
line 30, in
from tf_slim.ops import variables File "C:\Users\user\anaconda3\envs\tensorflow1\lib\site-packages\tf_slim\ops\variables.py",
line 27, in
from tensorflow.python.framework import device_spec as tf_device ImportError: cannot import name 'device_spec'
EDIT -> The TF OD API now supports TF2
FRCNN is one of the supported models. Soooooooo, update your TF Models version and you should be good to go :)
You can check out the updated TF OD API here.
Do you know what version of the Object Detection API you are using?
You probably have one of the versions which targets TF2. Looks like you are training FRCNN which isn't supported on TF2 yet - hence the issue!
You can find an older version of the TF Models API here. Try release v1.13 which targets TF 1.13.
The OD API will probably be migrated to TF2 in future but don't hold your breath.

How to install or make working keras_retinanet.utils.gpu module?

I am trying to train M-RCNN using Keras and RetinaNet as in the Keras founder's example. I installed retinanet package but this module isn't found. May be it is related to that my TF doesn't use GPU? How to check that?
from keras_retinanet.utils.gpu import setup_gpu
Traceback (most recent call last):
File "<ipython-input-2-1f3ae263a7c6>", line 1, in <module>
from keras_retinanet.utils.gpu import setup_gpu
ModuleNotFoundError: No module named 'keras_retinanet.utils.gpu'
You probably need to install some submodules. The beginning of this code may help you https://www.kaggle.com/labintsevai/keras-retinanet-submission

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'

Why am I getting Tensorflow Serving module import error?

I am trying to use TensorFlow Serving. I installed TensorFlow serving with these instructions.
When I attempted use this line in my python code
from tensorflow_serving.session_bundle import exporter
I got this problem
>>> from tensorflow_serving.session_bundle import exporter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named tensorflow_serving.session_bundle
Why am I getting this problem? Am I missing something to build TensorFlow to include this module?
P.S.: Hello World TensorFlow application is working fine in my setup.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
After spending countless hours, I managed to find the solution.
When I changed the line
from tensorflow_serving.session_bundle import exporter
to
from tensorflow.contrib.session_bundle import exporter
It seems TF developers decided to change their session_bundle package location in the source tree.

Categories