gpt4 book ai didi

Tensorflow:如何从训练的模型中预测单个图像?

转载 作者:行者123 更新时间:2023-11-30 09:17:46 25 4
gpt4 key购买 nike

我是 tensorflow 新手,我正在尝试构建一个图像分类器。我已成功创建模型,并尝试在恢复模型后预测单个图像。我已经阅读了各种教程( https://github.com/sankit1/cv-tricks.com/blob/master/Tensorflow-tutorials/tutorial-2-image-classifier/predict.py ),但我无法弄清楚代码中的 feed-dict 内容。加载保存的模型后,我陷入了预测功能。有人可以帮助我并告诉我从保存的模型加载所有变量后该怎么做吗?

这是训练函数,它返回参数并将它们保存在模型中。

def trainModel(train, test, learning_rate=0.0001, num_epochs=2, minibatch_size=32, graph_filename='costs'):
"""
Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.

Input:
train : training set
test : test set
learning_rate : learning rate
num_epochs : number of epochs
minibatch_size : size of minibatch
print_cost : True to print the cost every epoch

Returns:
parameters : parameters learnt by the model
"""

ops.reset_default_graph() #for rerunning the model without resetting tf vars

# input and output shapes
(n_x, m) = train.images.T.shape
n_y = train.labels.T.shape[0]

costs = [] #var for storing the costs for later use

# create placeholders
X, Y = placeholderCreator(n_x, n_y)

parameters = paramInitializer()

# Forward propagation
Z3 = forwardPropagation(X, parameters)
# Cost function
cost = costCalc(Z3, Y)
#Backpropagation using adam optimizer
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
# Initialize tf variables
init = tf.global_variables_initializer()
minibatch_size = 32
# Start session to compute Tensorflow graph
with tf.Session() as sess:
# Run initialization
sess.run(init)

for epoch in range(num_epochs): # Training loop
epoch_cost = 0.
num_minibatches = int(m / minibatch_size)
for i in range(num_minibatches):
minibatch_X, minibatch_Y = train.next_batch(minibatch_size) # Get next batch of training data and labels
_, minibatch_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X.T, Y: minibatch_Y.T}) # Execute optimizer and cost function
epoch_cost += minibatch_cost / num_minibatches # Update epoch cost

saver = tf.train.Saver()
# Save parameters
parameters = sess.run(parameters)
saver.save(sess, "~/trained-model.ckpt")
return parameters

这是我的预测函数,我试图预测图像。为了便于使用,我已将该图像转换为 MNIST 格式(predicting_data)。我加载保存的模型,对第三层的输出(最终输出)使用 softmax 函数。

def predict():

train = predicting_data.train
(n_x, m) = train.images.T.shape
n_y = train.labels.T.shape[0]
X, Y = placeholderCreator(n_x, n_y)
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph('~/trained-model.ckpt.meta')
new_saver.restore(sess, '~/trained-model.ckpt')
W1 = tf.get_default_graph().get_tensor_by_name('W1:0')
b1 = tf.get_default_graph().get_tensor_by_name('b1:0')
W2 = tf.get_default_graph().get_tensor_by_name('W2:0')
b2 = tf.get_default_graph().get_tensor_by_name('b2:0')
W3 = tf.get_default_graph().get_tensor_by_name('W3:0')
b3 = tf.get_default_graph().get_tensor_by_name('b3:0')
# forward propagation
Z1 = tf.add(tf.matmul(W1,X), b1)
A1 = tf.nn.relu(Z1)
Z2 = tf.add(tf.matmul(W2,A1), b2)
A2 = tf.nn.relu(Z2)
Z3 = tf.add(tf.matmul(W3,A2), b3)
y_pred = tf.nn.softmax(Z3) ####what to do after this????
cost = sess.run(y_pred, feed_dict={X: train.images.T})

提前谢谢您!

最佳答案

正如维杰在评论中所说:

您的预测部分不正确,您需要使用get_tensor_by_name()函数从保存的图形中获取输入和预测张量,然后在您的sess.run

如果你看this post ,它涵盖了类似的问题并有一些代码示例。

关于Tensorflow:如何从训练的模型中预测单个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50542021/

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