AttributeError: 'Sequential' object has no attribute '_built'

AttributeError: 'Sequential' object has no attribute '_built'

我正在做迁移学习,我正在使用 VGG16 模型。我正在通过特征提取微调我的模型。然后我训练了我的最终模型并且我已经腌制了它的重量。 这是我的代码

def prediction(array):
    size = 224
    array = image_array(filename , size)
    print(array.shape)
    array = np.array(array , dtype = np.float64)
    array = np.reshape(array, (1,224,224,3))
    print(array.shape)
    final_array = preprocess_input(array)
    vgg16 = VGG16(weights='imagenet', include_top=False)
    features = vgg16.predict(final_array)
    image = features.reshape(features.shape[0] , -1)
    #return image
    loaded_model = pickle.load(open('vgg16.sav', 'rb'))
    #print(image.shape)
    array = np.asarray(array)
    y_predict = loaded_model.predict(array)

当我调用这个函数时,我遇到了错误 y_predict = loaded_model.predict(数组) 我得到

AttributeError: 'Sequential' object has no attribute '_built'

您不应该使用 picke.dump 将权重和负载保存为模型。而是使用提供的函数 model.save(filename)model.save_weights(filename) 分别保存模型或仅保存权重。在你的情况下你可以这样做:

vgg16.save('vgg16.h5')
# ...
loaded_model = keras.models.load_model('vgg16.h5')

您需要 h5py 包才能使用这些功能。