gpt4 book ai didi

keras - Autokeras 导出的模型表现不如预期

转载 作者:行者123 更新时间:2023-12-01 05:51:50 25 4
gpt4 key购买 nike

我刚刚在 python3.6 上安装了 autokeras。经过一些错误修复后,它运行良好,我可以用我的数据集训练模型。
训练后,我得到一个准确度为 0.991 和损失为 0.06 的模型。使用预测功能,它几乎没有任何错误地对我的测试数据集进行分类。
但是当我将其导出并保存为 keras 模型时,该模型的表现非常糟糕。它错误地对所有内容进行分类,并且预测函数仅返回随机小数而不是预期类的数量。
我想我只是缺少一些命令,因为图像分类器运行良好。
任何帮助或提示都会非常好。

编辑:
我基本上遵循了 https://autokeras.com/start/ 上的示例.我对数据进行了预处理,使其形状为 (n, 150, 150, 3) 并且范围在 0 和 1 之间。每个图像的代码如下:

img='/home/example_image.png'
x=image.img_to_array(image.load_img(img, target_size=(150, 150)))
x=x.reshape(1, 150, 150, 3)
x = x.astype('float32') / 255

我还尝试使用 50x50 形状的图像,因为 autokeras 似乎更适合较小的形状。我的标签是一个长度为 n 的列表。然后我使用网站上的代码:
clf = ImageClassifier(verbose=True)
clf.fit(x_train, y_train, time_limit=12 * 60 * 60)
clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)
y = clf.evaluate(x_test, y_test)
print(y)

它返回了一个非常好的模型,精度高,损失小。我无法向您展示输出的屏幕截图,因为我目前在家,而我的笔记本电脑太弱,无法重现此问题。当我使用以下命令时:
clf.predict(example_img)

我也得到了图像中对象数量的正确结果。
当我尝试使用他们网站上给出的命令导出模型时,我的问题就开始了:
from autokeras import ImageClassifier
clf = ImageClassifier(verbose=True, augment=False)
clf.load_searcher().load_best_model().produce_keras_model().save('my_model.h5')

之后我加载我的模型。
import keras
from keras.models import load_model
model = load_model('my_model.h5')

它给了我一些错误:

"UserWarning: No training configuration found in save file: the model was not compiled. Compile it manually. warnings.warn('No training configuration found in save file: '"



我搜索了它,但人们说忽略它。
但是当我尝试对这个加载的模型使用 predict 时,我总是得到一些随机十进制值的数组,这些值通常是负数。分类总是错误的,因为这个数组中的最高值是错误的类。

当我再次使用我的 ML 计算机在办公室时,我将使用更多屏幕截图和详细信息编辑此文本。

编辑2:
当我训练模型时,这是最好的模型:
+--------------------------------------------------------------------------+
| Father Model ID | Added Operation |
+--------------------------------------------------------------------------+
| | ('to_add_skip_model', 1, 5) |
| | ('to_wider_model', 1, 64) |
| | ('to_wider_model', 5, 64) |
| 5 | ('to_conv_deeper_model', 9, 3) |
| | ('to_conv_deeper_model', 23, 3) |
| | ('to_concat_skip_model', 5, 9) |
+--------------------------------------------------------------------------+

Saving model.
+--------------------------------------------------------------------------+
| Model ID | Loss | Metric Value |
+--------------------------------------------------------------------------+
| 6 | 0.014135746611282229 | 1.0 |
+--------------------------------------------------------------------------+

之后我使用了 final_fit 并尝试了 predict_function:
>>> clf.predict(test_images)
array([ 0., 0., 0., ..., 12., 12., 12.])

这是预期的输出。评估也给出了很好的结果:
>>> y = clf.evaluate(test_images, test_labels)
>>> print(y)
0.9969230769230769

然后我将模型导出为 keras 模型并再次加载它:
>>> from keras import models
>>> clf.load_searcher().load_best_model().produce_keras_model().save('keras_best_model.h5')
>>> model = models.load_model('keras_best_model.h5')

一切都很好,我没有收到任何错误。但是当我现在使用 predict_function 时它返回错误的结果:
>>> model.predict(test_images[0].reshape(1, 50, 50, 3))
array([[ 2.5287893, -2.2281592, -2.8172228, 1.1171696, -5.8477755,
-3.1250796, 1.4904132, 1.1068834, -4.982565 , -1.6350467,
-4.3806715, -2.7464929, -6.1051216]], dtype=float32)

所以 clf.predict 给我一个数字,model.predict 给我一个数组。不应该是一样的吗?几乎所有的预测都是错误的。当我评估模型时,这是输出:
>>> model.evaluate(test_images, test_labels1)
2600/2600 [==============================] - 1s 569us/step
[5.251570468682509, 0.10115384615384615]

与 clf 评估相比,这是完全糟糕的,我不知道为什么。

最佳答案

在新版本(0.3.6)中,似乎

clf.load_searcher().load_best_model().produce_keras_model().save('keras_best_model.h5') 

已移除。目前你可以使用“autokeras.utils.pickle_to_file”来保存和“autokeras.utils.pickle_from_file”来加载模型。这不是通常的 keras 模型,但是我们可以得到相同的结果。

关于keras - Autokeras 导出的模型表现不如预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53229283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com