解释 Keras 模型预测输出

时间:2021-07-10 13:01:44

标签: python tensorflow keras neural-network data-science

我已经拟合了 mnist 数字 Keras/TF 示例。

digits_mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128,activation='relu'),
  tf.keras.layers.Dense(10)
])
model.compile(
    optimizer=tf.keras.optimizers.Adam(0.001),
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)

model.fit(
    x=train_images,
    y=train_labels,
    epochs=6,
    validation_data=(test_images, test_labels),
)

稀疏分类准确率达到约 94.5%

此时我要通过模型运行其中一个训练示例,以查看输出是什么样的。我相信你会使用 predict 函数来做到这一点。我不得不对训练示例数据进行一些重塑(这可能是我在这里遇到的问题,还有其他帖子但没有定论)

我认为结果是合理的

image_in = train_images[0][ np.newaxis, :, : ] # reshape
predict = model.predict(image_in)
print(predict, train_labels[0])

image_in2 = train_images[1][ np.newaxis, :, : ] # reshape
predict = model.predict(image_in2)
print(predict, train_labels[1])

image_in3 = train_images[2][ np.newaxis, :, : ] # reshape
predict = model.predict(image_in3)
print(predict, train_labels[2])

image_in4 = train_images[3][ np.newaxis, :, : ] # reshape
predict = model.predict(image_in4)
print(predict, train_labels[3])
<块引用>

[[-15.103473 20.778965 -9.244939 62.400173 -23.793236
72.29711 -2.7528331 12.732147 37.075775 36.81269 ]] 5

[[ -1.3534731 -24.39009 -14.5208435 -20.452188 -16.758095 -12.028614 -13.0093565 -9.06416 -11.541512 -14.997495 ]] 0

[[-9.685611 18.384281 13.8173685 -0.23191524 37.27173
18.273088 -1.4883347 26.91457 11.042679 25.099646 ]] 4

[[ 11.550052 37.031742 -0.43448153 2.1549647 6.6804423 1.829277 11.534891 4.703198 1.562077 -14.293095 ]] 1

标签和包含最大数字的输出索引之间存在映射。

所以我决定对我画的数字进行一些测试。

enter image description here

enter image description here

enter image description here

所以看起来MNIST是黑底白字的,所以我在加载图片的时候做了一点转换

image_file = Image.open('mysix.png')
image_file = ImageOps.grayscale(image_file)
mysix = np.invert(np.array(image_file))
image_in = mysix[ np.newaxis, :, : ] # reshape
predict = model.predict(image_in)
print(predict)
cv2.imwrite("real_test.png", mysix)

结果不那么令人信服

  • 此为 6 [不正确]
<块引用>

[[-11.062315 -3.6117797 -12.970709 -3.692216 -20.52597
6.8898406 -6.7844076 -4.1480203 -8.589685 -8.556881 ]]

  • 这是三个[正确]
<块引用>

[[-30.695564 -23.397968 -21.212194 24.455023 -31.399946
10.118337 -82.92692 -10.150092 -5.8821173 -12.108372 ]]

  • 这如果为七个[不正确]
<块引用>

[[ 1.2403618 4.0243044 9.859227 9.83745 -6.681723 2.4680052
-7.4165597 6.6975245 3.355576 -9.518949 ]]

  1. 我重塑数据以使用经过训练的模型对其进行评估的方式是否正确?
  2. 我在代码中为加载灰度 PNG 所做的所有数据处理是否合法?
  3. 如果 1 和 2 都为真,那么对于在我的第 6 个训练时期结束时在 mnist 评估集上以 95% 的剪辑工作但在我的(尽管是有限)评估集?

1 个答案:

答案 0 :(得分:0)

我需要卷积神经网络,类似于 41 分钟 https://www.youtube.com/watch?v=AjtX1N_VT9E 这段出色的 MIT 视频中显示的内容(解决 Frightera 上面提到的这一点)

enter image description here

model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(28,28,1), data_format="channels_last"),
  tf.keras.layers.MaxPooling2D(2,2),
  tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
  tf.keras.layers.MaxPooling2D(2,2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(1024,activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')
])

我可能需要做更多的评估,但是超过 6 个 epochs 网络达到了 99.63% 的稀疏分类准确率,明显优于之前的实现,也正确地对我的三个手绘数字进行了分类。