python解释的keras模型预测输出

plicqrtu  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(201)

我已经拟合了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%左右
在这一点上,我要通过模型运行一个训练示例,看看输出是什么样子的。我相信你会使用预测函数来做这件事。我不得不对培训示例数据进行一些修改(这可能是我在这里遇到的问题,还有其他帖子,但没有定论)
我认为结果是合理的

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
包含最大数字的输出的标签和索引之间存在Map。
所以我决定对我画的数字进行一些测试。



所以看起来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

我用经过训练的模型重新塑造数据以对其进行评估的方法正确吗?
我在代码中所做的加载灰度png的所有数据处理是否合法?
如果1和2都是真的,那么对于模型的明显缺陷有什么解释呢?该模型在我的第六个训练阶段结束时在mnist评估集上的剪辑率为95%,但在我的(尽管有限)评估集上的剪辑率仅为33%?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题