[英]Tensorflow training accuracy and loss different from evaluation of the same dataset
I try to train a Tensorflow model with two classes.我尝试用两个类训练 Tensorflow 模型。 My Trainingsdata is balanced (~11k images for both classes).
我的 Trainingsdata 是平衡的(两个类都有大约 11k 个图像)。 I am using Tranferlearning and try to continue on the InceptionV3 Model with the following code:
我正在使用 Transferlearning 并尝试使用以下代码继续使用 InceptionV3 模型:
BUFFER_SIZE = 1000
BATCH_SIZE = 32
def get_label(file_path, class_names):
# convert the path to a list of path components
parts = tf.strings.split(file_path, os.path.sep)
# The second to last is the class-directory
return parts[-2] == class_names
def parse_image(filename):
parts = tf.strings.split(filename, "\\")
label = get_label(filename, CLASS_NAMES)
image = tf.io.read_file(filename)
image = tf.image.decode_png(image, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, [299,299])/255.0
return image, label
datasetFilePath = "Path\To\BalancedData"
IMAGESIZE = 299
AUTOTUNE = tf.data.experimental.AUTOTUNE
datasetPath = pathlib.Path(datasetFilePath)
list_ds = tf.data.Dataset.list_files(str(datasetPath/"*/*"))
CLASS_NAMES = np.array([item.name for item in datasetPath.glob('*')])
# labeled_ds = list_ds.map(process_path)
images_ds = list_ds.map(parse_image, num_parallel_calls=AUTOTUNE)
images_ds = images_ds.shuffle(BATCH_SIZE)
dataset = images_ds.batch(BATCH_SIZE, drop_remainder=True)
for image_batch, label_batch in dataset.take(1):
pass
def build_and_compile_model():
base_model =tf.keras.applications.InceptionV3(include_top=False, weights = "imagenet", input_shape=(IMAGESIZE,IMAGESIZE,3))
feature_batch = base_model(image_batch)
print(feature_batch.shape)
base_model.trainable = False
x = base_model.output
x = tf.keras.layers.GlobalAveragePooling2D(name="avg_pool")(x)
x = tf.keras.layers.Dense(256, activation="relu")(x)
predictions = tf.keras.layers.Dense(2, activation="softmax")(x)
model = tf.keras.models.Model(inputs=base_model.input, outputs=predictions)
base_learning_rate = 0.00001
model.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate),
loss="categorical_crossentropy",
metrics=["accuracy"])
return model
multi_worker_model = build_and_compile_model()
tensorboard = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
history = multi_worker_model.fit(dataset, epochs=2, callbacks=[tensorboard])
=>Epoch 1/2
=>711/711 [==============================] - 176s 247ms/step - loss: 0.3309 - accuracy: 0.8945
=>Epoch 2/2
=>711/711 [==============================] - 166s 233ms/step - loss: 0.1410 - accuracy: 0.9632
predictions = multi_worker_model.evaluate(dataset)
=>711/711 [==============================] - 150s 212ms/step - loss: 0.7538 - accuracy: 0.4999
So all in all my model just predicts everything to be of class 1 and i dont understand why that is and mostly why it claims to have a training accuracy of 96%所以总而言之,我的模型只是预测一切都属于 1 类,我不明白为什么会这样,主要是为什么它声称具有 96% 的训练准确度
It might be a problem of the Data, but then again, i would expect a training accuracy of also ~50% as both training and evaluation happen on the same dataset.这可能是数据的问题,但话说回来,由于训练和评估都发生在同一数据集上,因此我希望训练准确度也能达到 50%。
Any help would be much appreciated and if you need more information let me know!任何帮助将不胜感激,如果您需要更多信息,请告诉我!
Ok, after a few more hours of reseach i might have the Problem.好的,经过几个小时的研究后,我可能会遇到问题。 InceptionV3 uses
InceptionV3 使用
def preprocess_input(x):
x /= 255.
x -= 0.5
x *= 2.
return x
as preprocess function.作为预处理函数。 And i am only doing the x/=255 part wich is not enough.
而且我只做 x/=255 部分还不够。 The difference of the accuracy might be because in the
.fit
function automatically does the preprocess, the .evaluate
on the other hand cant do that.准确性的差异可能是因为在
.fit
函数中自动进行预处理,而.evaluate
另一方面不能这样做。
I am not 100% sure that this is the correct answer, but its my best guess.我不是 100% 确定这是正确答案,但这是我最好的猜测。 And it also works with setting the
base_model.trainable = False
to base_model.trainable = True
.它也适用于将
base_model.trainable = False
设置为base_model.trainable = True
。 I think this allows the model to train with my own preprocessing and so the evaluation works also, because it gets my preprocessing.我认为这允许模型使用我自己的预处理进行训练,因此评估也有效,因为它得到了我的预处理。
I hope this might help someone with a similar problem.我希望这可以帮助有类似问题的人。 But i am still glad for any input from people who might give a better insight into the topic.
但我仍然很高兴人们提供任何可能对该主题有更好见解的意见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.