AttributeError: module 'keras.layers.normalization' has no attribute 'BatchNormalization'

Viewed 923

I am trying to convert a transfer learning Keras model into Core ml. I need this core ml file to have class_labels in order for the model to be recognized as a classifier. Whenever I attempt to call ct.converters.keras.convert() I am given the error AttributeError: module 'keras.layers.normalization' has no attribute 'BatchNormalization'.

I was wondering how to add the class_labels and just how to call this function with my model. Or just a possible way to create a classifier with Dictionary (String → Double) output. Any help would be appreciated.

Below is my model training code(google colab)

base_model = InceptionV3(
                     include_top=False,
                     weights='imagenet',
                     input_shape=(224,224,3))
  
base_model.trainable=False
  
model = tf.keras.Sequential([ 
        base_model,
        tf.keras.layers.Dropout(0.1),
        tf.keras.layers.GlobalAveragePooling2D(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(12, activation='softmax')
    ])

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

early = tf.keras.callbacks.EarlyStopping( patience=10,
                                          min_delta=0.001,
                                          restore_best_weights=True)

checkpoint = tf.keras.callbacks.ModelCheckpoint("/content/drive/My Drive/Colab_Notebooks/Models/plant_classifier_10.h5",monitor='val_accuracy', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', save_freq='epoch')

batch_size=32
STEP_SIZE_TRAIN = training_set.n//training_set.batch_size
STEP_SIZE_VALID = test_set.n//test_set.batch_size
  
history = model.fit(training_set,
                    steps_per_epoch=STEP_SIZE_TRAIN,
                    validation_data=test_set,
                    validation_steps=STEP_SIZE_VALID,
                    epochs=10,
                    callbacks=[early, checkpoint])

Below is my attempted conversion code (google colab)

model = keras.models.load_model('/content/drive/MyDrive/Colab_Notebooks/Models/plant_classifier.h5')

core_modelB = ct.converters.keras.convert(model, 
                                                  input_names="image",
                                                  image_input_names="image",
                                                  class_labels=['Black-grass', 'Charlock', 'Cleavers', 'Common Chickweed', 'Common wheat', 'Fat Hen', 'Loose Silky-bent', 'Maize', 'Scentless Mayweed', 'Shepherd’s Purse', 'Small-flowered Cranesbill', 'Sugar beet'],
                                                  output_names=("labelProbability"))

core_model.save('Plant_Classifier_New.mlmodel')

I get the error AttributeError: module 'keras.layers.normalization' has no attribute 'BatchNormalization' at the end of the ct.converters.keras.convert() function on the last line.

here is an image of the error message. https://i.stack.imgur.com/CFtFR.png

Any help would be amazing.

1 Answers

Try ct.converters.convert(...), because ct.converters.keras.convert() used for old Keras version which is wrapper around TensorFlow 1.x.

Related