‘predict_generator’ 返回值大于1且小于0

我使用Autokeras训练了我的模型,训练完成后,我将其保存为纯Keras的h5文件。

我的模型:

from autokeras import ImageClassifierfrom autokeras.image.image_supervised import load_image_datasetif __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')

我还有一个predict.py代码,但它返回了错误的值

from keras.models import load_modelfrom keras.preprocessing import imageimport numpy as npimport globfrom keras.preprocessing.image import ImageDataGeneratorfrom sklearn.metrics import confusion_matrix# dimensions of our imagesimg_width, img_height = 128, 128# load the model we savedmodel = load_model('model.h5')#model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])datagen = ImageDataGenerator(rescale=1./255)generator = datagen.flow_from_directory(        'data/test',        target_size=(img_width, img_height),        batch_size=1,        class_mode=None,  # only data, no labels        shuffle=False)  # keep data in same order as labels#filenames = datagen.filenames#nb_samples = len(filenames)probabilities = model.predict_generator(generator, 4)

实际结果:

[[-2.0996048  1.862035 ] [-1.4634153  1.2710633] [-1.4367918  1.4041075] [-1.3242773  1.2946494]]

期望的结果应类似于以下格式:

[[0  0.51234 ] [1  0.67847] [1  0.92324] [1  0.32333]]

例如。

我做错了什么?


回答:

在@人名建议我关于激活函数后,我在GitHub上找到了这个讨论这里

这个讨论帮助我理解了以下几行代码:

keras_model = load_model('model.h5')x = keras_model.outputx = Activation('softmax', name='activation_add')(x)new_model = Model(keras_model.input, x)

Related Posts

Flatten and back keras

我正在尝试使用自编码器获取简单向量中的值 这是我的代码…

如何按索引访问PyTorch模型参数

如果我的网络有10层,包括偏置项,如何仅通过索引访问第…

Python中多元逻辑回归显示错误

我试图使用逻辑回归进行预测,并使用Python和skl…

在MACOS上安装NLTK

我在我的2015款Mac Pro上尝试安装NLTK,操…

如何在R中将通过RFE选择的变量插入到机器学习模型中?

我想使用递归特征消除方法来选择最重要的特征,然后将这些…

CountVectorizer 错误:ValueError: setting an array element with a sequence

我有一个包含144条学生反馈的数据集,其中有72条正面…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注