模型在相同生成器上的model.fit和model.evaluate中的准确率值不同。

huangapple go评论42阅读模式
英文:

Different values of accuracy in model.fit and model.evaluate on the same generator

问题

使用tensorflow中的model.fitmodel.evaluate在相同的生成器上会得到不同的准确性。这是我的示例代码:

import tensorflow as tf
from nChannelGenerator import nChannelGenerator
from databaseSplitter import automaticPath
from keras.preprocessing.image import ImageDataGenerator

nClasses = 5

height = 200
width = 200
batch_size = 32

model_1 = tf.keras.Sequential([
    tf.keras.layers.Rescaling(scale=1./255, input_shape=[None, None, 3]),
    tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.GlobalMaxPooling2D(),
    tf.keras.layers.Dense(nClasses, activation="softmax"),
])

model_1.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), metrics=["accuracy"], loss="categorical_crossentropy")

datagen = ImageDataGenerator(rescale=1.)
data = datagen.flow_from_directory("database_prova")
model_2.fit(data, epochs=5)
print("这是评估步骤")
model_2.evaluate(data)

然后这是输出:

找到 1302 张属于 5 类的图像。
Epoch 1/5
41/41 [==============================] - 6s 84ms/step - loss: 1.5366 - accuracy: 0.2888
Epoch 2/5
41/41 [==============================] - 3s 64ms/step - loss: 1.3420 - accuracy: 0.4117
Epoch 3/5
41/41 [==============================] - 3s 63ms/step - loss: 0.9565 - accuracy: 0.5799
Epoch 4/5
41/41 [==============================] - 3s 64ms/step - loss: 0.7061 - accuracy: 0.6920
Epoch 5/5
41/41 [==============================] - 3s 64ms/step - loss: 0.6357 - accuracy: 0.7143
这是评估步骤
41/41 [==============================] - 1s 26ms/step - loss: 0.5574 - accuracy: 0.7957

最后一个训练步骤和评估之间的准确性和损失都不同。正如您可以看到的,我这里没有批量标准化,因此我期望得到相同的值。我已经看过之前的回答,但我无法弄清楚问题出在哪里。

英文:

I obtain a different accuracy on the same generator by using the model.fit and model.evaluate in tensorflow. This is my sample code

import tensorflow as tf
from nChannelGenerator import nChannelGenerator
from databaseSplitter import automaticPath
from keras.preprocessing.image import ImageDataGenerator

nClasses = 5

height = 200
width = 200
batch_size = 32


    model_1 = tf.keras.Sequential([
    tf.keras.layers.Rescaling(scale = 1./255, input_shape=[None, None, 3]),
    tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.GlobalMaxPooling2D(),
    tf.keras.layers.Dense(nClasses, activation = "softmax"),
])

model_1.compile(optimizer=tf.keras.optimizers.Adam(learning_rate = 0.001), metrics=["accuracy"], loss = "categorical_crossentropy")

datagen = ImageDataGenerator(rescale=1.)
data = datagen.flow_from_directory("database_prova")
model_2.fit(data, epochs = 5)
print("This is the evaluation step")
model_2.evaluate(data)

Then this is the output

Found 1302 images belonging to 5 classes.
Epoch 1/5
41/41 [==============================] - 6s 84ms/step - loss: 1.5366 - accuracy: 0.2888
Epoch 2/5
41/41 [==============================] - 3s 64ms/step - loss: 1.3420 - accuracy: 0.4117
Epoch 3/5
41/41 [==============================] - 3s 63ms/step - loss: 0.9565 - accuracy: 0.5799
Epoch 4/5
41/41 [==============================] - 3s 64ms/step - loss: 0.7061 - accuracy: 0.6920
Epoch 5/5
41/41 [==============================] - 3s 64ms/step - loss: 0.6357 - accuracy: 0.7143
This is the evaluation step
41/41 [==============================] - 1s 26ms/step - loss: 0.5574 - accuracy: 0.7957

Both the accuracies and the losses are different between the last training step and the evaluation. I have no batch normalization here as you can see, I then expected the same values. I have seen a previous response, but I cannot figure out where is the problem.

答案1

得分: 1

在最后一个训练周期中报告的准确率是整个周期内的累积准确率。因为你正在进行小批量梯度下降,所以在训练的第一个批次和最后一个批次使用的模型权重是不同的。

如果你在每个周期进行验证,然后再进行一次评估,你会看到相同的数字。

英文:

The accuracy reported during the last training epoch is the accumulated accuracy during the epoch. Because you're doing mini batch gradient descent, the weights of the model used for the first batch are different than for the last batch during training.

If you were validating at each epoch, and then doing one more evaluation, you would see the same number.

答案2

得分: 0

为了清晰起见,我将在这里写下如何检查正确的结果。正如@Lescurel所说,如果在每个时代都进行验证,将会产生应该的相同结果。在我的原始代码中,适合线可以替换为:

model_2.fit(data, epochs=5, validation_data=data)
英文:

For the sake of clarity, I will write here how to check the correct result. As said by @Lescurel, if one validates at each epoch, the same result will be delivered as it should be. The fit line in my original code can be replaced by:

model_2.fit(data, epochs = 5, validation_data = data)

huangapple
  • 本文由 发表于 2023年6月19日 20:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定