我训练了5种不同的分类器,并保存到磁盘,就像这样:
for e in range(len(ensemble)): #..ensemble: -list of 5 models
save_file = "Model_" + str(e) + ".h5"
ensemble[e].save(save_file)
print("MODEL ", e, " SAVED!")
所以我有五个模型,每个分类器一个:
$ls
accuracy.txt dfObj.csv Model_0.h5 Model_1.h5 Model_2.h5
Model_3.h5 Model_4.h5 predicted_label.csv real_label.csv
我想在新的看不见的数据集中评估这些分类器,因此需要加载它们。所以我尝试了这个:
import os
from keras.models import load_model
ensemble = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.h5'):
model = load_model(file)
ensemble.append(model)
哪个会生成错误:
raise IOError("SavedModel file does not exist at: %s/{%s|%s}" %
OSError: SavedModel file does not exist at: Model_4.h5/{saved_model.pbtxt|saved_model.pb}
有人可以指出如何将这些模型重新加载以在看不见的数据集中进行评估吗?
答案 0 :(得分:2)
那是因为您从错误的路径加载
您从files
列表中迭代的文件是相对路径
有一个例子:
import os
model.save('/tmp/test.h5')
for root, dirs, files in os.walk('/tmp/'):
print(root) # root path 'tmp'
for file in files:
print(file) # relative path 'test.h5'
keras.models.load_model(file) # error relative path
从绝对路径加载有效:
keras.models.load_model('/tmp/test.h5')
这应该是加载绝对路径的正确方法:
# !/usr/bin/python3
import os
for root, dirs, files in os.walk(your_path, topdown = False):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
os.walk
here