加载模型后 Keras 精度低 [英] Keras giving low accuracy after loading model

查看:33
本文介绍了加载模型后 Keras 精度低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一个非常奇怪的情况.在训练卷积网络后,我在验证数据上获得了大约 95% 的准确率.我保存模型.稍后我恢复模型并在相同的验证数据集上运行验证.这次我几乎没有得到 10% 的准确率.我已阅读文档,但似乎没有任何帮助.我做错了什么吗?

I am seeing a very strange situation. After training a convolutional network I get about 95% accuracy on the validation data. I save the model. Later I restore the model and run validation on the same validation data set. This time I barely get 10% accuracy. I have read the documentation but nothing seems to help. Is there something I am doing wrong?

def build_model_mnist(image_width, image_height, image_depth):
  model = keras.Sequential()
  model.add(keras.layers.Conv2D(5, (3, 3), activation='relu', input_shape=(image_width, image_height, image_depth)))
  model.add(keras.layers.MaxPooling2D((2, 2)))
  model.add(keras.layers.Conv2D(10, (3, 3), activation='relu'))
  model.add(keras.layers.MaxPooling2D((2, 2)))
  model.add(keras.layers.Conv2D(10, (3, 3), activation='relu'))

  model.add(keras.layers.Flatten())
  model.add(keras.layers.Dense(64, activation='relu'))
  model.add(keras.layers.Dense(10, activation='softmax'))

  model.compile(optimizer='adam',
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])
  
  return model

def train_mnist():
  model = build_model_mnist(image_width=train_images.shape[1], 
                    image_height=train_images.shape[2], 
                    image_depth=train_images.shape[3])
  # Start training              
  h = model.fit(train_images, train_labels, batch_size=500, epochs=5)

  model.save("minist")

  # Evaluate the model
  test_loss, test_acc = model.evaluate(test_images, test_labels)

  print("Accuracy:", test_acc)

train_mnist()

以上将显示 95% 的准确率.但是下面的代码显示了 10% 的准确率.

The above will show 95% accuracy. But the code below shows 10% accuracy.

def evaluate_mnist():
  # Load the model
  model = keras.models.load_model("minist")

  # Evaluate the model
  test_loss, test_acc = model.evaluate(test_images, test_labels)

  print("Accuracy:", test_acc)

evaluate_mnist()

如果我只保存和恢复权重,那么一切都很好.在下面的代码中,我们只保存权重.稍后我们使用代码重新创建模型架构并恢复权重.这种方法产生了正确的准确性.

If I save and restore just the weights then things work fine. In the code below we are saving the weights only. Later we recreate the model architecture using code and restore the weights. This approach produces the correct accuracy.

def train_mnist():
  #Create the network model
  model = build_model_mnist(image_width=train_images.shape[1], 
                    image_height=train_images.shape[2], 
                    image_depth=train_images.shape[3])
  # Start training              
  h = model.fit(train_images, train_labels, batch_size=500, epochs=5)

  # Evaluate the model
  test_loss, test_acc = model.evaluate(test_images, test_labels)

  print("Accuracy:", test_acc)

  model.save_weights("minist-weights")

train_mnist()

def evaluate_mnist():
  # Re-create the model architecture
  model = build_model_mnist(image_width=train_images.shape[1], 
                    image_height=train_images.shape[2], 
                    image_depth=train_images.shape[3])

  model.load_weights("minist-weights")
  
  # Evaluate the model
  test_loss, test_acc = model.evaluate(test_images, test_labels)

  print("Accuracy:", test_acc)

evaluate_mnist()

推荐答案

我在 tf 2.3.0 中遇到了类似的问题.

I had a similar problem in tf 2.3.0.

这个问题解释了通用术语准确性"的问题;使用 sparse_categorical_crossentropy 时的度量.在模型重新加载时,它关联了错误的准确度指标.解决方案是明确告诉 keras 使用正确的指标,而不是让它推断出正确的指标(其中存在错误),即使用 metrics='sparse_categorical_accuracy' 进行编译.

This issue explains the problem with the generic term "accuracy" metric when using sparse_categorical_crossentropy. On model reloading it associates the wrong accuracy metric. The solution is to explicitly tell keras to use the correct metric instead of letting it infer what is the correct one (bug in it) i.e. compile with metrics='sparse_categorical_accuracy'.

我最初使用 metrics='accuracy' 作为训练阶段的指标,发现只有在重新加载后重新编译模型才能恢复预期的性能.

I was using metrics='accuracy' initially as metric in training phase and discovered that only by recompiling the model after reloading it gave back the expected performance.

这篇关于加载模型后 Keras 精度低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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