ayoolaolafenwa / PixelLib

Visit PixelLib's official documentation https://pixellib.readthedocs.io/en/latest/
MIT License
1.05k stars 264 forks source link

ImportError: cannot import name 'BatchNormalization' from 'tensorflow.python.keras.layers' #152

Open purplebutterfly79 opened 2 years ago

purplebutterfly79 commented 2 years ago

this issue comes up in Google colab

from pixellib.semantic import semantic_segmentation the above line gives the following error

ImportError Traceback (most recent call last)

in () ----> 1 from pixellib.semantic import semantic_segmentation 1 frames /usr/local/lib/python3.7/dist-packages/pixellib/deeplab.py in () 13 from tensorflow.python.keras.layers import Add 14 from tensorflow.python.keras.layers import Dropout ---> 15 from tensorflow.python.keras.layers import BatchNormalization 16 from tensorflow.python.keras.layers import Conv2D 17 from tensorflow.python.keras.layers import DepthwiseConv2D ImportError: cannot import name 'BatchNormalization' from 'tensorflow.python.keras.layers' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/__init__.py)
NiceboyWiseboy commented 2 years ago

Go to Pixellib folder -> semantic -> deeplab.py in your installed location of Pixellib and replace this line. Replace this from tensorflow.python.keras.layers import BatchNormalization with from keras.layers.normalization.batch_normalization import BatchNormalization.

It'll solve your issue.

naseemap47 commented 2 years ago

I fixed using installing Tensorflow==2.6.0 and Keras==2.6.0

!pip3 install tensorflow==2.6.0 !pip3 install keras==2.6.0

asuhag commented 2 years ago

Looks like tf ==2.6.0 is the goldilocks here.

KeryMg commented 1 year ago

I fixed using installing Tensorflow==2.6.0 and Keras==2.6.0

!pip3 install tensorflow==2.6.0 !pip3 install keras==2.6.0

i still can't solve it after installing these two.

asuhag commented 1 year ago

@KeryMg which python version are you working with? From the documentation, I gather you need py 3.5-3.7

tgahlaut commented 1 year ago

Go to Pixellib folder -> semantic -> deeplab.py in your installed location of Pixellib and replace this line. Replace this from tensorflow.python.keras.layers import BatchNormalization with from keras.layers.normalization.batch_normalization import BatchNormalization.

It'll solve your issue.

right

yinyin-llll commented 1 year ago

Go to Pixellib folder -> semantic -> deeplab.py in your installed location of Pixellib and replace this line. Replace this from tensorflow.python.keras.layers import BatchNormalization with from keras.layers.normalization.batch_normalization import BatchNormalization.

It'll solve your issue.

there is question in the next cell:segment=semantic_segmentation()and segment.load_ade20k_model('./weights/deeplabv3_xception65_ade20k.h5')

p3jitnath commented 1 year ago

Used this fix in Google Colab and I found to work pretty well.

import os
import fileinput

# Define the path to the file that needs to be modified
FILE_PATH = "/usr/local/lib/python3.10/dist-packages/pixellib/semantic/deeplab.py"

# Define the old and new strings that need to be replaced
OLD_STRING = "tensorflow.python.keras"
NEW_STRING = "tensorflow.keras"

# Use fileinput to replace the old string with the new string in the file
for line in fileinput.input(FILE_PATH, inplace=True):
    print(line.replace(OLD_STRING, NEW_STRING), end='')

# Define the old and new strings that need to be replaced
# This handles model loading errors
OLD_STRING = "tensorflow.keras.utils.layer_utils import get_source_inputs"
NEW_STRING = "tensorflow.python.keras.utils.layer_utils import get_source_inputs"

# Use fileinput to replace the old string with the new string in the file
for line in fileinput.input(FILE_PATH, inplace=True):
    print(line.replace(OLD_STRING, NEW_STRING), end='')

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
valdas-v1 commented 1 year ago

Used this fix in Google Colab and I found to work pretty well.

import os
import fileinput

# Define the path to the file that needs to be modified
FILE_PATH = "/usr/local/lib/python3.10/dist-packages/pixellib/semantic/deeplab.py"

# Define the old and new strings that need to be replaced
OLD_STRING = "tensorflow.python.keras"
NEW_STRING = "tensorflow.keras"

# Use fileinput to replace the old string with the new string in the file
for line in fileinput.input(FILE_PATH, inplace=True):
    print(line.replace(OLD_STRING, NEW_STRING), end='')

# Define the old and new strings that need to be replaced
# This handles model loading errors
OLD_STRING = "tensorflow.keras.utils.layer_utils import get_source_inputs"
NEW_STRING = "tensorflow.python.keras.utils.layer_utils import get_source_inputs"

# Use fileinput to replace the old string with the new string in the file
for line in fileinput.input(FILE_PATH, inplace=True):
    print(line.replace(OLD_STRING, NEW_STRING), end='')

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

This worked on Colab with Python 3.10 🎉