'predict_generator'返回的值大于1且小于0 [英] 'predict_generator' return values over then 1 and less then 0

查看:231
本文介绍了'predict_generator'返回的值大于1且小于0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Autokeras训练我的模型,然后在fit_final之后我将其另存为纯keras h5文件.

I used Autokeras to train my model, after then fit_final I save it as pure keras h5 file.

我的模特:

from autokeras import ImageClassifier
from autokeras.image.image_supervised import load_image_dataset

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')

我也有一个predict.py代码,但是它给我错误的值

I also have a predict.py code but it gives me wrong values

from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import glob
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix

# dimensions of our images
img_width, img_height = 128, 128

# load the model we saved
model = 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]]

例如.

我在做什么错了?

推荐答案

在Mickey向我提出有关激活功能的建议之后,我在github

After Mickey adviced me about the activation function, I found this thread on github here

此线程帮助我找出了这些代码行:

this thread helped me to figure out those lines of code:

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

这篇关于'predict_generator'返回的值大于1且小于0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆