Tensorflow 和 keras 模型在调用 load_model 时引发类型错误

问题描述

正在使用 covid 数据集并使用转移学习(DenseNet 预训练模型)制作神经网络模型并制作以下​​模型。

rb2D.veLocity = -jumpVector.normalized * shotRecoilPower;

评估有效并使用

获得约 93% 的准确率
import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.regularizers import l1_l2
model6 = Sequential()
model6.add(data_augmentation)
base_model = tf.keras.applications.DenseNet201(input_shape=(100,100,3),include_top=False,pooling='max',weights='imagenet')
model6.add(base_model)
model6.add(Batchnormalization())
model6.add(Dense(2048,activation='relu',kernel_regularizer=l1_l2(0.01)))
model6.add(Batchnormalization())
model6.add(Dense(1,activation='sigmoid'))
for layer in model6.layers:
  layer.trainable = True

model6.compile(loss='binary_crossentropy',optimizer=tf.keras.optimizers.Adam(lr=1e-4),metrics=['accuracy'])

stacked_img = np.stack((X_train,)*3,axis=-1)
history6 = model6.fit(stacked_img,y_train,validation_split=0.2,callbacks = [es_callback],epochs=10,verbose=1,validation_steps=22)

我可以使用以下代码在 Colab 中预测新图像

model6.evaluate(X_test_stacked,y_test)

我决定从 Colab 中取出模型,使用 model.save() 将其保存为 hdf5 文件并将其放入烧瓶应用程序中,但它不断引发错误

data = [] # initialize an empty numpy array
image_size = 100 # image size taken is 100 here. one can take other size too
img_path= '/content/Image_1.jpg'
img_array = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE) # converting the image to gray scale
img_array = cv2.resize(img_array,(image_size,image_size)) # resizing the image array
img_array = np.stack((img_array,axis=-1)
img_array = np.expand_dims(img_array,axis=0)
# img_array = np.array(img_array)
prediction = model6.predict(img_array)

在我调用 load_model 的那一行

TypeError: ('Keyword argument not understood:','fill_value')

...

from tensorflow.keras.models import load_model    
model = load_model('models/binary-covid-model-6.h5')

当我尝试在浏览器中访问它时。

解决方法

我现在意识到您不能将 load_model 用于在使用 Tensorflow 2.3.0 的环境中使用 TensorFlow 2.4.0 训练的模型,因为该环境适用于使用 2.3.0 训练的模型,可能是此 Tensorflow 版本中的错误或更新因此,在 Tensorflow 2.3.0 环境中使用 load_model() 调用时,使用 TensorFlow 2.4.0 训练的模型会出错。