【问题标题】:Trouble with evaluating pre-trained model评估预训练模型的问题
【发布时间】:2025-12-21 01:30:10
【问题描述】:

我有一个微调版本的 inceptionV3 模型,我想在新数据集上进行测试。但是,我收到错误No model found in config file.

这是我的代码,

from tensorflow import keras
model = keras.models.load_model('/home/saved_model/CNN_inceptionv3.h5')

CLASS_1_data = '/home/spectrograms/data/c1'

def label_img(img):
    word_label = img[:5]
    if img[1] == '1':
      return [1,0]
    elif img[1] == '3':
      return [0,1]

def create_data(data,loc): #loads data into a list
    for img in tqdm(os.listdir(loc)):
        label = label_img(img)
        path = os.path.join(loc,img)
        img = Image.open(path) 
        img = ImageOps.grayscale(img) 
        # w,h = img.size
        # img = img.resize((w//3,h//3))
        data.append([np.array(img),np.array(label)])
    return data

def make_X_and_Y(set): #split data into numpy arrays of inputs and outputs
  set_X,set_Y = [],[]
  n = len(set)
  for i in range(n):
    set_X.append(set[i][0])
    set_Y.append(set[i][1])
  return np.array(set_X),np.array(set_Y)

data = []
data = create_data(data,CLASS_1_data)
data = np.array(data)

X_data,Y_data = make_X_and_Y(data)

X_data = X_data.astype('float32')

X_data /= 255 

results = model.evaluate(X-data, Y_data, batch_size=5)

这里的错误是什么?如何更正它并测试我的模型?

【问题讨论】:

  • 你是只保存权重还是只保存模型? model.save 还是 model.save_weights?
  • 我使用了model.save_weights

标签: tensorflow machine-learning keras deep-learning conv-neural-network


【解决方案1】:

要加载模型,您必须使用 model.save,以防您只想加载 wieghts model.save_weights。 在您的情况下使用model.save,它将上传模型。 让我知道。 或者您可以从 json 加载模型。

model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)

你有权重所以...加载 json 并创建模型

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)

将权重加载到新模型中

loaded_model.load_weights("model.h5")
print("Loaded model from disk")

告诉我!

【讨论】:

  • 我在训练后使用了model.save_weights。所以这意味着我只保存了重量对吗?据我了解,我现在有两个选择,1 - 重新构建模型并加载保存的权重并将它们用于我的数据,2 - 再次训练模型并使用 model.save() 保存它。我说的对吗?
  • 或者不是从 h5 文件而是从 json 加载模型。检查我的答案的编辑!
  • 不小心丢失了模型的架构。所以现在唯一的办法就是再次制作模型并训练并保存模型和权重对吗?
  • 或从头开始构建模型并加载权重或如您所说是正确的
  • 有消息了吗??
最近更新 更多