ayoolaolafenwa / PixelLib

Visit PixelLib's official documentation https://pixellib.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

purplebutterfly79 opened this issue · 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)

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.

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

Looks like tf ==2.6.0 is the goldilocks here.

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 which python version are you working with? From the documentation, I gather you need py 3.5-3.7

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

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

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'

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 🎉