Autokeras导出的模型表现不理想 [英] Autokeras exported model performs not as expected
问题描述
我刚刚在python3.6上安装了autokeras.修复一些错误后,它可以很好地工作,并且我可以使用我的数据集训练模型. 训练后,我得到一个模型,精度为0.991,损失为0.06.借助预测功能,它几乎可以毫无故障地对我的测试数据集进行分类. 但是,当我导出并将其另存为keras模型时,该模型的性能确实很差.它将所有内容错误地分类,并且预测函数仅返回随机的小数,而不返回预期类的编号. 我认为我只是缺少一些命令,因为图像分类器可以很好地工作. 任何帮助或提示都将非常好.
I just installed autokeras on python3.6. After some bug fixing it works well and I can train models with my dataset. After training i get a model with an accuracy of 0.991 and loss of 0.06. With the predict-function it classifies my test dataset almost without any fault. But when I export and save it as a keras model the model performs really bad. It classifies everything wrongly and the predict-function returns just random decimals and not the number of the expected class. I think I am just missing some commands since the image classifier works well. Any help or tips would be very nice.
我基本上按照 https://autokeras.com/start/上的示例进行操作.我将数据预处理为形状(n,150、150、3),范围在0到1之间.每个图像的代码如下:
I basically followed the example on https://autokeras.com/start/. I preprocessed my data to be in shape (n, 150, 150, 3) and range between 0 and 1. The code for each image is the following:
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的列表.然后,我使用网站上的代码:
I also tried with 50x50 shape for the images because autokeras seems to work better with smaller shapes. My labels are a list of length n. Then I use the code from the website:
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)
它返回了一个非常好的模型,具有很高的精度和很小的损失.我无法显示输出的屏幕截图,因为我目前在家里,而我的笔记本电脑太虚弱,无法重现此问题.当我使用以下命令时:
It returns a very good model with high accuracy and small loss. I can't show you the screenshots of the output because I am currently at home and my laptop is too weak to reproduce this problem. When I use the following command:
clf.predict(example_img)
我还得到了图像中对象数量正确的结果. 当我尝试使用他们网站上给出的命令导出模型时,我的问题就开始了:
I also get a correct result with the number of object in the image. My problem starts when I try to export the model with the commands given on their website:
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:在保存文件中找不到训练配置:模型尚未编译.请手动编译. warnings.warn('在保存文件中找不到训练配置:'"
"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: '"
我搜索了它,但人们说忽略它. 但是,当我尝试在此加载的模型上使用预测时,我总是会得到一些随机十进制值的数组,这些值通常为负数.分类始终是错误的,因为此数组中的最大值是针对错误的类.
I searched for it but people said to ignore it. But when I try to use predict with this loaded model I always get an array of some random decimal values which are often negative. The classification is always wrong since the highest value in this array is for a wrong class.
当我再次使用ML计算机在办公室时,我将使用更多屏幕截图和详细信息来编辑此文本.
I will edit this text with more screenshots and details when I am at the office again with my ML computer.
当我训练模型时,这是最好的模型:
Edit 2: When I train the model this is the best model :
+--------------------------------------------------------------------------+
| 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并尝试了预报功能:
After that I used final_fit and tried the predict_function:
>>> clf.predict(test_images)
array([ 0., 0., 0., ..., 12., 12., 12.])
这是预期的输出.评估也给出了良好的结果:
This is the expected output. Evaluation gives also good results:
>>> y = clf.evaluate(test_images, test_labels)
>>> print(y)
0.9969230769230769
然后我将模型导出为keras模型并再次加载:
Then I export the model as a keras model and load it again:
>>> 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时,它将返回错误的结果:
Everything does fine and I get no error. But when I use the predict_function now it returns wrong results:
>>> 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给我一个数组.不应该一样吗?几乎所有的预测都是错误的.当我评估模型时,这是输出:
So clf.predict gives me one number and model.predict give me an array. Shouldn't it be the same? Almost all predictions are wrong. When I evaluate the model this is the output:
>>> model.evaluate(test_images, test_labels1)
2600/2600 [==============================] - 1s 569us/step
[5.251570468682509, 0.10115384615384615]
与clf评估相比,这是完全糟糕的,我不知道为什么.
It is totally bad in comparison to the clf evaluation and I don't know why.
推荐答案
在新版本(0.3.6)中,看来
In the new version, (0.3.6), it seems
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模型,但是我们可以获得相同的结果.
is removed. For the moment you can use "autokeras.utils.pickle_to_file" for saving and "autokeras.utils.pickle_from_file" to load the model. This is not a usual keras model, however we can get same results.
这篇关于Autokeras导出的模型表现不理想的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!