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

Hi there :wave:,

I have trouble importing the BatchNormalization class for my image classification project. I am currently learning about CNNs with the Cifar10 dataset and Kaggle. I would like to get some indications on how to properly import BatchNormalization.

Here is my command:
from tensorflow.python.keras.layers import BatchNormalization

But when I run my code, I get the following issue:
ImportError: cannot import name 'BatchNormalization' from 'tensorflow.python.keras.layers'

I already tried importing from ‘tensorflow.keras.layers’ without the ‘python’ specification but it does not work with my version of tensorflow (v2.16.1). Also, when I check the path to the ‘layer’ folder, it is located in tensorflow.python.keras so I don’t think ‘python’ is the issue here. Then, when I check the .py files in the ‘layers’ folder, there is no BatchNormalization class.

I also tried importing it directly from Keras (v3.3.3):
from keras.src.layers.normalization.batch_normalization import BatchNormalization

The import works but when try to build my model I get this issue:
TypeError: The added layer must be an instance of class Layer.

I guess it is because my model is created with Tensorflow, then I try to add a Keras Layer. Here is the complete file:
`#!/usr/bin/env python3
“”" CNN model “”"

from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
from tensorflow.python.keras.metrics import Precision, Recall
from tensorflow.python.keras.callbacks import EarlyStopping
from keras.src.legacy.preprocessing.image import ImageDataGenerator

from tensorflow.python.keras.layers import BatchNormalization < — does not exist

from keras.src.layers.normalization.batch_normalization import BatchNormalization # < — import OK

INPUT_SHAPE = (32, 32, 3)
KERNEL_SIZE = (3, 3)
CNN = Sequential()

Convolutional layer

CNN.add(Conv2D(filters=32, kernel_size=KERNEL_SIZE, input_shape=INPUT_SHAPE,
activation=‘relu’, padding=‘same’))
CNN.add(BatchNormalization()) # < — Raises an error (TypeError)
CNN.add(Conv2D(filters=32, kernel_size=KERNEL_SIZE, input_shape=INPUT_SHAPE,
activation=‘relu’, padding=‘same’))
CNN.add(BatchNormalization())

Pooling layer

CNN.add(MaxPool2D(pool_size=(2, 2)))

Dropout layers

CNN.add(Dropout(0.25))

Repeat the operations with more filters

CNN.add(Conv2D(filters=64, kernel_size=KERNEL_SIZE, input_shape=INPUT_SHAPE,
activation=‘relu’, padding=‘same’))
CNN.add(BatchNormalization())
CNN.add(Conv2D(filters=64, kernel_size=KERNEL_SIZE, input_shape=INPUT_SHAPE,
activation=‘relu’, padding=‘same’))
CNN.add(BatchNormalization())
CNN.add(MaxPool2D(pool_size=(2, 2)))
CNN.add(Dropout(0.25))

CNN.add(Conv2D(filters=128, kernel_size=KERNEL_SIZE, input_shape=INPUT_SHAPE,
activation=‘relu’, padding=‘same’))
CNN.add(BatchNormalization())
CNN.add(Conv2D(filters=128, kernel_size=KERNEL_SIZE, input_shape=INPUT_SHAPE,
activation=‘relu’, padding=‘same’))
CNN.add(BatchNormalization())
CNN.add(MaxPool2D(pool_size=(2, 2)))
CNN.add(Dropout(0.25))

Flatten arrays

CNN.add(Flatten())

Output layer

CNN.add(Dense(128, activation=‘relu’))
CNN.add(Dropout(0.25))
CNN.add(Dense(10, activation=‘softmax’))

Metrics

METRICS = [
‘accuracy’,
Precision(name=‘precision’),
Recall(name=‘recall’)
]

Compile the model

CNN.compile(loss=‘categorical_crossentropy’, optimizer=‘adam’, metrics=METRICS)
`

ImportError: DLL load failed while importing_pywrap_tf2: The operation completed successfully.

Traceback:

File "C:\Users\Dell\PycharmProjects\pythonProject.venv\Lib\site-packages\stre exec (code, module.dict)

File “C:\Users\Dell\PycharmProjects\pythonProject\app\main.py”, line 6, in <mo

import tensorflow as tf

File "C:\Users\Dell\PycharmProjects\pythonProject.venv\Lib\site-packages\tens from tensorflow.python import tf2 as tf2

File "C:\Users\Dell\PycharmProjects\pythonProject.venv\Lib\site-packages\tens from tensorflow.python.platform import_pywrap_tf2