【问题标题】:Why is the restored TensorFlow model, made with Keras, giving me 0 predictions?为什么用 Keras 制作的恢复的 TensorFlow 模型给了我 0 个预测?
【发布时间】:2018-11-23 07:57:06
【问题描述】:

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

这是分类器:

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 的张量有关,我不知道为它们使用哪个其他张量.我会给予任何帮助,谢谢!

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    代码中的问题是,您应该只为图表提供 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
    

    【讨论】:

    • 我收到以下错误:ValueError: Cannot feed value of shape (1, 784, 1) for Tensor 'dense_1_input:0', which has shape '(?, 784)' 这就是为什么我将其更改为 cmets 所说的。
    • @MarcoLugo 我刚刚意识到,您使用了密集层。请参阅我的更新答案。
    猜你喜欢
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多