machine-learning - 属性错误: module 'keras.layers.normalization' has no attribute 'BatchNormalization'

标签 machine-learning keras coreml coremltools

我正在尝试将迁移学习 Keras 模型转换为 Core ml。我需要这个核心 ml 文件具有 class_labels 才能将模型识别为分类器。每当我尝试调用 ct.converters.keras.convert()我收到错误 AttributeError: module 'keras.layers.normalization' has no attribute 'BatchNormalization'

我想知道如何添加 class_labels 以及如何使用我的模型调用此函数。或者只是创建具有字典(字符串 → double )输出的分类器的可能方法。任何帮助将不胜感激。

下面是我的模型训练代码(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])

下面是我尝试的转换代码(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')

我在 ct.converters.keras.convert() 末尾收到错误 AttributeError: module 'keras.layers.normalization' has no attribute 'BatchNormalization'函数在最后一行。

这是错误消息的图像。 /image/CFtFR.png

任何帮助都会很棒。

最佳答案

尝试 ct.converters.convert(...),因为 ct.converters.keras.convert() 用于旧的 Keras 版本,它是 TensorFlow 1.x 的包装器。

关于machine-learning - 属性错误: module 'keras.layers.normalization' has no attribute 'BatchNormalization' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70623782/

相关文章:

c++ - 如何在不调用 Matlab 的情况下从 C++ 应用经过训练的 Matlab 神经网络?

machine-learning - 我可以使用 `tf.nn.dropout`来实现DropConnect吗?

python - 在转换后的 tflite 模型上调用 `allocate_tensors()` 时出现运行时错误

python - 我的 GAN 实现没有获得完全的 GPU 利用率

machine-learning - 如何在 Keras 中对批量大小应用均值/平均池化以获得整个批量的单个输出?

python - 通过 LSTM (Core ML) 处理序列

swift - 结合 CoreML 和 ARKit

amazon-web-services - 适用于tensorflow和pymysql的Sagemaker内核

machine-learning - 近端梯度下降的 l1_regularization_strength 和 l2_regularization_strength 的定义

ios - ARKit 能否将特定表面检测为平面?