Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Python 3.8, Tensorflow 2.5.0 and keras 2.3.1 and I am trying to make a model, but I get error from keras.

This is my code :

import cv2
import os
import numpy as np
from keras.layers import Conv2D,Dropout, Flatten, Dense,MaxPooling2D, MaxPool2D
import keras.layers.normalization
#from tensorflow.keras.layers import Conv2D,Dropout, Flatten, Dense,MaxPooling2D, MaxPool2D
from keras_preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from keras.models import Sequential
import pandas as pd
import random
from tensorflow.python.keras.utils.np_utils import to_categorical

count = 0
images = []
classNo = []
labelFile = 'signnames.csv'
classes = 43
testRatio = 0.2  # if 1000 images split will 200 for testing
validationRatio = 0.2  # if 1000 images 20% from remaining 800 will be 160 for valid
path_current = os.getcwd()
imageDim = (32, 32, 3)

####IMPORTING THE IMAGES FROM TRAIN FOLDER

for j in range(classes):
    path = os.path.join(path_current, 'train', str(j))
    imagesList = os.listdir(path)
    for i in imagesList:
        image = cv2.imread(path + '\\' + i)
        imageResized = cv2.resize(image, (32, 32))
        imageResized = np.array(imageResized)
        images.append(imageResized)
        classNo.append(count)
    count += 1

images = np.array(images)
classNo = np.array(classNo)

print(images.shape, classNo.shape)

##### Split Data - make the train
X_train, X_test, y_train, y_test = train_test_split(images, classNo, test_size=testRatio)
X_train, X_validation, y_train, y_validation = train_test_split(X_train, y_train, test_size=validationRatio)


#####processing all the images from train, test, validation
X_train = np.array(list(map(preprocessing, X_train)))  # for all the images
X_validation = np.array(list(map(preprocessing, X_validation)))
X_test = np.array(list(map(preprocessing, X_test)))
cv2.imshow("GrayScale Images", X_train[random.randint(0, len(X_train) - 1)])  # just to verify the tain
# cv2.waitKey(5000)


##### add a depth of 1 - for better lines
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1)
X_validation = X_validation.reshape(X_validation.shape[0], X_validation.shape[1], X_validation.shape[2], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1)

####augmentation of images : to make from some images more images, making it more generic, creating various similar images
dataGen = ImageDataGenerator(width_shift_range=0.1,  # 10%
                             height_shift_range=0.1,
                             zoom_range=0.2,
                             shear_range=0.1,  # distorted along an axis(aplecata)
                             rotation_range=10)  # degrees

dataGen.fit(X_train)
batches = dataGen.flow(X_train, y_train, batch_size=20)  # generate 20 images when it s called
X_batch, y_batch = next(batches)

#######from label to one encoding(making matrix with 0 and 1 based on classes number)
y_test = to_categorical(y_test, classes)
y_train = to_categorical(y_train, classes)
y_validation = to_categorical(y_validation, classes)


###########convolution neural network model
def myModel():
    nodesNr = 500
    filterNr = 60  ##to dont remove pixels based on filter size
    filterSize = (5, 5)  ##the kernel that move around the image to get the features
    # making padding
    filterSize2 = (3, 3)
    poolSize = (
    2, 2)  # for more generalize, to reduce overfitting(when detail and noise in training and go to negative result)

    model = Sequential();
    model.add(Conv2D(filterNr, filterSize, activation='relu', input_shape=X_train.shape[1:]))
    model.add(Conv2D(filterNr, filterSize, activation='relu'))
    model.add(MaxPooling2D(pool_size=poolSize))

    model.add(Conv2D(filterNr // 2, filterSize2, activation='relu'))
    model.add(Conv2D(filterNr // 2, filterSize2, activation='relu'))
    model.add(MaxPool2D(pool_size=poolSize))
    model.add(Dropout(0.5))

    model.add(Flatten())
    model.add(Dense(nodesNr, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(classes, activation='softmax'))  # output layer

    model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy'])
    return model


####TRAIN

model = myModel()
print(model.summary())

model.save('traffic_classifier.h5')

I am using PyCharm and I get error from first keras import, at line 8.

There are the errors:

Using TensorFlow backend.
2021-05-15 20:43:16.281415: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
Traceback (most recent call last):
  File "E:/FACULTATE ANUL 3 SEMESTRUL 2/Procesarea Imaginilor/proiect/main.py", line 8, in <module>
    from keras.layers import Conv2D,Dropout, Flatten, Dense,MaxPooling2D, MaxPool2D
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\__init__.py", line 3, in <module>
    from . import utils
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
    from . import conv_utils
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\backend\__init__.py", line 1, in <module>
    from .load_backend import epsilon
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\backend\load_backend.py", line 90, in <module>
    from .tensorflow_backend import *
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\__init__.py", line 48, in <module>
    from tensorflow.python import keras
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\__init__.py", line 25, in <module>
    from tensorflow.python.keras import models
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\models.py", line 20, in <module>
    from tensorflow.python.keras import metrics as metrics_module
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\metrics.py", line 37, in <module>
    from tensorflow.python.keras import activations
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\activations.py", line 18, in <module>
    from tensorflow.python.keras.layers import advanced_activations
  File "C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\layers\__init__.py", line 146, in <module>
    from tensorflow.python.keras.layers.normalization import LayerNormalization
ImportError: cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization' (C:\Users\My-Pc\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\layers\normalization\__init__.py)
like image 310
Felician-Nicu Herman Avatar asked May 15 '21 17:05

Felician-Nicu Herman


6 Answers

The package has been renamed. This import worked for me.

from keras.layers.normalization import layer_normalization
like image 64
Ali Raza Avatar answered Oct 24 '22 02:10

Ali Raza


I had the same error with Python 3.8, Tensorflow 2.5.0 and keras 2.3.1. Gone through numerous solutions from all sources. What fixed it for me was to downgrade Python to 3.7. For some reason it seems Keras LayerNormalization is incompatible with Python 3.8 locally on my computer, even though I was unable to replicate the problem over Colab.

If you use Anaconda, you could create a new environment just for Tensorflow. Here is what worked for me:

conda create -n tensorflow_env tensorflow
conda activate tensorflow_env

which installed Python 3.7.10 and Tensorflow 2.0.0. You can then upgrade Tensorflow to 2.5.0.

like image 35
Wendy Avatar answered Oct 24 '22 01:10

Wendy


Try importing your modules through the Tensorflow repository instead of the Keras repository.

For example:

from tensorflow.keras.models import Sequential
like image 34
Son.Dre Avatar answered Oct 24 '22 02:10

Son.Dre


it seems to be a combination of versions mismatch between python/tensorflow/keras. Here is the versions that worked for me python 3.8.6/tensorflow==2.5.0/keras==2.4.3 and got rid of the layer_normalization error

like image 30
Thesane Avatar answered Oct 24 '22 01:10

Thesane


Might not be 100% related to the original question, but have landed here trying to solve this on MacBook Pro M1 in a conda environment

Joining bits and pieces from multiple places my ultimate fix was:

pip uninstall -y tensorflow keras tf-nightly keras-nightly

python -m pip install tensorflow-macos

like image 26
Luke_V Avatar answered Oct 24 '22 02:10

Luke_V


if you want to install tensorflow in your base environment use pip install tensorflow==2.2.0 --user command .

like image 27
Kumar Deep Dhar Avatar answered Oct 24 '22 01:10

Kumar Deep Dhar