4

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.

Rubén
  • 34,714
  • 9
  • 70
  • 166
sydney
  • 83
  • 1
  • 2
  • 11

1 Answers1

6

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

Ali Cirik
  • 1,475
  • 13
  • 21
  • Hi Ali, could you elaborate what you mean by "replacing all `tensorflow.python.keras` imports..."? I'm not sure what to do. – F16Falcon Mar 02 '20 at 20:24
  • 1
    @F16Falcon basically find and replace every `tensorflow.python.keras` with `tensorflow.keras` in the ImageAI code that you installed with pip. – Ali Cirik Mar 03 '20 at 02:18
  • 1
    I changed all instances of tensorflow.python.keras to tensorflow.keras and now I have this error output: File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 329, in compile raise ValueError('`tf.compat.v1.keras` Optimizer (', optimizer, ') is ' ValueError: ('`tf.compat.v1.keras` Optimizer (', , ') is not supported when eager execution is enabled. Use a `tf.keras` Optimizer instead, or disable eager execution.') – sydney Mar 03 '20 at 16:58
  • 1
    @sydney I added new information to the answer. Looks like downgrading tensorflow to 1.* version fixes the issue – Ali Cirik Mar 06 '20 at 16:46