简体   繁体   English

TensorFlow 中图像分类模型的均方误差

[英]Mean Squared Error for image classification model in TensorFlow

I am trying to teach image classification model to define a number characteristic from an image.我正在尝试教图像分类模型从图像中定义数字特征。 I am sure that SparseCategoricalCrossentropy loss function doesn't work for me, as for training I need to penalize big differences more than small ones.我确信 SparseCategoricalCrossentropy 损失函数对我不起作用,至于训练,我需要惩罚大差异而不是小差异。 Ideally I would like to use Mean Squared Error loss function.理想情况下,我想使用均方误差损失函数。

I use TensorFlow tutorial to prepare the model - https://www.tensorflow.org/tutorials/images/classification .我使用 TensorFlow 教程来准备模型 - https://www.tensorflow.org/tutorials/images/classification

Class names are numbers for me, I tried the following options:类名对我来说是数字,我尝试了以下选项:

  • ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'] ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', ' 12']
  • ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'] ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', ' 12']

The only change I made against tutorial (except the dataset) is exchanging SparseCategoricalCrossentropy loss function to 'mean_squared_error'.我对教程所做的唯一更改(数据集除外)是将 SparseCategoricalCrossentropy 损失函数更改为“mean_squared_error”。

But the loss function clearly doesn't work for me.但是损失函数显然对我不起作用。 It returns values, that gets smaller with training, but accuracy is never more than 5%, and it even goes down as loss value becomes smaller.它返回的值随着训练而变小,但准确率永远不会超过 5%,甚至随着损失值变小而下降。 Results also do not make sense.结果也没有意义。 The data is fine, I can easily achieve 95% accuracy with SparseCategoricalCrossentropy loss function.数据很好,我可以使用 SparseCategoricalCrossentropy 损失函数轻松达到 95% 的准确率。 What am I missing?我错过了什么?

UPDATE: I think what I really need is a way to define regression problem in TensorFlow using images labeled with numbers.更新:我认为我真正需要的是一种使用标有数字的图像在 TensorFlow 中定义回归问题的方法。

Turns out it is quite easy to turn image classification problem into a regression problem.事实证明,很容易将图像分类问题转化为回归问题。 Against tutorial referenced in question I had to make the following changes:针对有问题引用的教程,我必须进行以下更改:

  1. Different dataset with numbers as 'classes' (folder names).以数字作为“类”(文件夹名称)的不同数据集。

  2. Changed loss function to Mean Squared Error or other loss function suitable for regression.将损失函数更改为均方误差或其他适合回归的损失函数。

  3. Made the last layer for model with just 1 neurone instead of number of classes (and without softmax):为只有 1 个神经元而不是类数(并且没有 softmax)的模型制作最后一层:

     ... layers.Dense(128, activation='relu'), layers.Dense(1) # changed from num_classes to 1
  4. Changed interpretation of prediction results:改变了对预测结果的解释:

     ... predictions = model.predict(img_array) # score = tf.nn.softmax(predictions[0]) # correct for classification, but not regression score = predictions.flatten()[0] # correct result for regression ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM