PixelLib icon indicating copy to clipboard operation
PixelLib copied to clipboard

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

Open purplebutterfly79 opened this issue 2 years ago • 9 comments

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)

purplebutterfly79 avatar Jun 24 '22 07:06 purplebutterfly79

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.

NiceboyWiseboy avatar Jun 24 '22 09:06 NiceboyWiseboy

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

naseemap47 avatar Jun 27 '22 05:06 naseemap47

Looks like tf ==2.6.0 is the goldilocks here.

asuhag avatar Aug 05 '22 16:08 asuhag

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.

KeryMg avatar Nov 17 '22 08:11 KeryMg

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

asuhag avatar Nov 17 '22 13:11 asuhag

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

tgahlaut avatar Mar 11 '23 05:03 tgahlaut

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')

yinyin-llll avatar Apr 02 '23 11:04 yinyin-llll

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'

nathzi1505 avatar May 02 '23 11:05 nathzi1505

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 🎉

valdas-v1 avatar Jul 09 '23 14:07 valdas-v1