gpt4 book ai didi

python - 为什么使用 Keras 制作的恢复后的 TensorFlow 模型给出的预测为 0?

转载 作者:行者123 更新时间:2023-12-01 08:42:37 24 4
gpt4 key购买 nike

我在 Keras 中使用 MNIST 数字数据创建了一个分类器,我尝试仅使用 Tensorflow 保存和恢复模型,但我的所有 10 个类都为零。

这是分类器:

from keras import models
from keras import layers
from keras.datasets import mnist
from keras.layers.core import K
import tensorflow as tf

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(10, activation='softmax'))

network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])

train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype('float32')/255

test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32')/255

from keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

network.fit(train_images, train_labels, epochs=5, batch_size=128)

test_loss, test_acc = network.evaluate(test_images, test_labels)
print('\n\ntest_acc: ', test_acc)
print('\n' + network.input.op.name)
print('\n' + network.output.op.name)

sess = K.get_session()
saver = tf.train.Saver()
saver.save(sess, './digit-model')

运行脚本后的结果:

test_acc:  0.9799

dense_1_input

dense_2/Softmax

这是我传递自己的图像的脚本,将其大小调整为 28 * 28 并将其提供给保存的模型:

import tensorflow as tf
import numpy as np
import os
import cv2

# First, pass the path of the image
dir_path = os.path.dirname(os.path.realpath(__file__))
image_path = './3.png' # sys.argv[1]
filename = dir_path + '/' + image_path
image_size = 28
num_channels = 1
images = []

# Reading the image using OpenCV
image = cv2.imread(filename)

# Resizing the image to our desired size and pre-processing will be done exactly as done
# during training
image = cv2.resize(image, (image_size, image_size), 0, 0, cv2.INTER_LINEAR)

if num_channels == 1:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = np.reshape(image, (image.shape[0], image.shape[1], 1))

images.append(image)
images = np.array(images, dtype=np.uint8)
images = images.astype('float32')
images = np.multiply(images, 1.0 / 255.0)

# The input to the network is of shape [None image_size image_size num_channels].
# Hence we reshape
x_batch = images.reshape(1, image_size * image_size)

# Let us restore the saved model
sess = tf.Session()

# Step-1: Recreate the network graph. At this step only graph is created.
saver = tf.train.import_meta_graph('./digit-model.meta')

# Step-2: Now let's load the weights saved using the restore method
saver.restore(sess, tf.train.latest_checkpoint('./'))

# Accessing the default graph which we have restored
graph = tf.get_default_graph()

# for op in graph.get_operations():
# print(str(op.name))

# Now, let's get hold of the op that we can be processed to get the output.
# In the original network y_pred is the tensor that is the prediction of the network
y_pred = graph.get_tensor_by_name("dense_2/Softmax:0")

# Let's feed the images to the input placeholders
x = graph.get_tensor_by_name("dense_1_input:0")
y_true = graph.get_tensor_by_name("dense_2/Softmax:0")
y_test_images = np.zeros((1, 10))

# Creating the feed_dict that is required to be fed to calculate y_pred
feed_dict_testing = {x: x_batch, y_true: y_test_images}
result = sess.run(y_pred, feed_dict=feed_dict_testing)

for r in result:
for i in r:
print(i)

这些是我得到的结果:

0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0

我猜这与我使用的 y_truey_pred 张量有关,我不知道要为它们使用哪个其他张量。我会感谢任何帮助,谢谢!

最佳答案

代码中的问题是,您应该只为图表提供x_batch。所以解决方案是改变原来的:

feed_dict_testing = {x: x_batch, y_true: y_test_images

  feed_dict_testing = {x: x_batch}

我们根本不需要这条线: y_true = graph.get_tensor_by_name("dense_2/Softmax:0")实际上我们不需要y_true变量,我们只需要训练阶段true标签。

您也可以更改行:

images = np.array(images, dtype=np.uint8)
images = images.astype('float32')

images = np.array(images, dtype=np.float32)

因此,您赢得了一行并节省了一些额外的计算。

经过我们的努力,我们对任意图像都得到了一些合理的结果。

INFO:tensorflow:Restoring parameters from ./digit-model
3.4569392e-29
6.898592e-28
1.0
2.9526584e-16
0.0
7.1692116e-14
2.0023208e-12
1.9861456e-14
2.7171926e-23
1.3212834e-28

关于python - 为什么使用 Keras 制作的恢复后的 TensorFlow 模型给出的预测为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53437710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com