Search code examples
pythontensorflowkerasdeep-learning

Tensorflow InceptionV4 ImportError: cannot import name 'tensor' from 'tensorflow.python.framework'


from inceptionV4 import *

I'm using tensorflow InceptionV4 but got this error message. Tensorflow-inceptionV4

 ImportError
cannot import name 'tensor' from 'tensorflow.python.framework' (C:\Users\zxc\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\__init__.py)
  File "C:\Labbb\inception\model_tensorflow_official.py", line 28, in <module>
    import tf_slim as slim
  File "C:\Labbb\inception\main.py", line 14, in <module>
    from model_tensorflow_official import *
ImportError: cannot import name 'tensor' from 'tensorflow.python.framework' (C:\Users\zxc\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\__init__.py)
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow.compat.v1 as tf
import tf_slim as slim

from nets import inception_utils

I tried tensorflow2.5 + keras2.5 & tensorflow2.2 +keras2.3.1, they are not work

and in previously error message detection that I dont have "inception_utils" file in tf_slim\nets

In tf-slim , there is no inception_utils too


Solution

  • So, when tf-models-official or slim is installed, the tf_slim package is installed as well (https://github.com/google-research/tf-slim/tree/master). Here, the tf_slim.layers.utils module was updated to import 'tensor' from 'tensorflow.python.framework'. However, the 'tensor' module is only present starting from tensorflow==2.13. You can see this by comparing the different branches here: https://github.com/tensorflow/tensorflow/tree/r2.13/tensorflow/python/framework

    I made it work by installing tensorflow==2.13:

    1. pip install tensorflow==2.13

    2. tf-models-official==2.13 (possibly not necessary)

    3. cd /path/to/clone/repo

    4. git clone https://github.com/tensorflow/models/ (as described here https://github.com/tensorflow/models/tree/master/research/slim)

    in my python script:

    import sys
    sys.path.append("/path/to/clone/repo/models/research/slim")
    from nets.inception_v4 import *