首页 > 解决方案 > 评估 AutoKeras 模型给出不同的结果,然后与纯 Keras 评估相同的模型(h5 文件)

问题描述

我正在使用 AutoKeras 训练我的数据集,然后将其保存为 AutoKeras 文件和 Keras 文件(h5)。

我的问题是评估这两个模型会给我不同的结果。

这是训练数据集和保存模型的代码:

if __name__ == '__main__':
    x_test, y_test = load_image_dataset(csv_file_path="test/label.csv", images_path="test")
    print(x_test.shape)
    print(y_test.shape)

    x_train, y_train = load_image_dataset(csv_file_path="train/label.csv", images_path="train")
    print(x_train.shape)
    print(y_train.shape)

    clf = ImageClassifier(path="~/automodels/", verbose=True)
    clf.fit(x_train, y_train, time_limit= 1 * 10 * 60)
    clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)
    y = clf.evaluate(x_test, y_test)
    print(y)

    clf.export_autokeras_model('my_autokeras_model.h5ak')
    clf.export_keras_model('my_model.h5')

这是加载模型并对其进行评估的代码:

from keras.models import load_model
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Input, LSTM, Bidirectional, Conv1D, Activation
from keras.models import Sequential, Model
from autokeras.image.image_supervised import load_image_dataset
from sklearn.metrics import confusion_matrix
import numpy as np
from keras.utils import to_categorical
from autokeras.utils import pickle_from_file

x_test, y_test = load_image_dataset(csv_file_path="test/label.csv", images_path="test")
print(x_test.shape)
print(y_test.shape)

model = pickle_from_file('my_autokeras_model.h5ak')
results = model.evaluate(x_test, y_test)
print(results)

keras_model = load_model('model.h5')
x = keras_model.output
x = Activation('softmax', name='activation_add')(x)
new_model = Model(keras_model.input, x)
new_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
y_test = to_categorical(y_test)
score = new_model.evaluate(x_test, y_test)
print(score)

但这就是输出:

0.7238095238095238
[8.135800362768627, 0.49523809523809526]

这是不同的。

这个线程解释了为h5模型添加激活函数的原因 (但也许我弄错了)

我究竟做错了什么 ?我会给予任何帮助。谢谢你!

标签: machine-learningkerasdeep-learningauto-keras

解决方案


推荐阅读