我正在使用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]
这是不同的。
在thread中解释了为h5模型添加激活功能的原因 (但也许我错了)
我在做什么错? 我会适当的帮助。 谢谢!