我使用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_true 和 y_pred 的张量有关,我不知道要为它们使用哪个其他张量。我会寻求任何帮助,谢谢!
答案 0 :(得分: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