【问题标题】:Tensor flow, making predictions using a trained networkTensorflow,使用经过训练的网络进行预测
【发布时间】:2017-02-07 04:43:56
【问题描述】:

所以我正在训练一个网络来对张量流中的图像进行分类。在我训练了网络之后,我开始尝试用它来对其他图像进行分类。目标是导入图像,将其提供给分类器并让它打印结果。不过,我在让那部分离开地面时遇到了一些麻烦。这是我到目前为止所拥有的。我发现有 tf.argmax(y,1) 会出错。我发现将其更改为 0 可以修复该错误。但是,我不相信它确实有效。我通过分类器扔了 2 张图像,尽管它们有很大的不同,但它们都得到了相同的类。这里只需要一些观点。这是有效的吗?或者这里有什么问题总是会为我提供相同的课程(在这种情况下,我尝试的两个图像都得到了 0 类)。

这甚至是在张量流中进行预测的正确方法吗?这只是我调试的高潮,不确定是否应该做。

from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
   X_train,X_validation,y_train,y_validation=train_test_split(X_train,y_train,   test_size=20,random_state=0)   
X_train, y_train = shuffle(X_train, y_train)




def LeNet(x):    
    # Arguments used for tf.truncated_normal, randomly defines variables 

for the weights and biases for each layer
    mu = 0
    sigma = 0.1

# SOLUTION: Layer 1: Convolutional. Input = 32x32x3. Output = 28x28x6.
conv1_W = tf.Variable(tf.truncated_normal(shape=(5, 5, 3, 6), mean = mu, stddev = sigma))
conv1_b = tf.Variable(tf.zeros(6))
conv1   = tf.nn.conv2d(x, conv1_W, strides=[1, 1, 1, 1], padding='VALID') + conv1_b

# SOLUTION: Activation.
conv1 = tf.nn.relu(conv1)

# SOLUTION: Pooling. Input = 28x28x6. Output = 14x14x6.
conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

# SOLUTION: Layer 2: Convolutional. Output = 10x10x16.
conv2_W = tf.Variable(tf.truncated_normal(shape=(5, 5, 6, 16), mean = mu, stddev = sigma))
conv2_b = tf.Variable(tf.zeros(16))
conv2   = tf.nn.conv2d(conv1, conv2_W, strides=[1, 1, 1, 1], padding='VALID') + conv2_b

# SOLUTION: Activation.
conv2 = tf.nn.relu(conv2)

# SOLUTION: Pooling. Input = 10x10x16. Output = 5x5x16.
conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

# SOLUTION: Flatten. Input = 5x5x16. Output = 400.
fc0   = flatten(conv2)

# SOLUTION: Layer 3: Fully Connected. Input = 400. Output = 120.
fc1_W = tf.Variable(tf.truncated_normal(shape=(400, 120), mean = mu, stddev = sigma))
fc1_b = tf.Variable(tf.zeros(120))
fc1   = tf.matmul(fc0, fc1_W) + fc1_b

# SOLUTION: Activation.
fc1    = tf.nn.relu(fc1)

# SOLUTION: Layer 4: Fully Connected. Input = 120. Output = 84.
fc2_W  = tf.Variable(tf.truncated_normal(shape=(120, 84), mean = mu, stddev = sigma))
fc2_b  = tf.Variable(tf.zeros(84))
fc2    = tf.matmul(fc1, fc2_W) + fc2_b

# SOLUTION: Activation.
fc2    = tf.nn.relu(fc2)

# SOLUTION: Layer 5: Fully Connected. Input = 84. Output = 43.
fc3_W  = tf.Variable(tf.truncated_normal(shape=(84, 43), mean = mu, stddev = sigma))
fc3_b  = tf.Variable(tf.zeros(43))
logits = tf.matmul(fc2, fc3_W) + fc3_b

return logits



import tensorflow as tf
 x = tf.placeholder(tf.float32, (None, 32, 32, 3))
 y = tf.placeholder(tf.int32, (None))
 one_hot_y = tf.one_hot(y, 43)
EPOCHS=10
BATCH_SIZE=128

rate = 0.001

logits = LeNet(x)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, one_hot_y)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
training_operation = optimizer.minimize(loss_operation)

correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()

def evaluate(X_data, y_data):
    num_examples = len(X_data)
    total_accuracy = 0
    sess = tf.get_default_session()
    for offset in range(0, num_examples, BATCH_SIZE):
        batch_x, batch_y = X_data[offset:offset+BATCH_SIZE], y_data[offset:offset+BATCH_SIZE]
        accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y})
        total_accuracy += (accuracy * len(batch_x))
    return total_accuracy / num_examples


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    num_examples = len(X_train)

    print("Training...")
    print()
    for i in range(EPOCHS):
        X_train, y_train = shuffle(X_train, y_train)
        for offset in range(0, num_examples, BATCH_SIZE):
            end = offset + BATCH_SIZE
            batch_x, batch_y = X_train[offset:end], y_train[offset:end]
            sess.run(training_operation, feed_dict={x: batch_x, y: batch_y})

        validation_accuracy = evaluate(X_validation, y_validation)
        print("EPOCH {} ...".format(i+1))
        print("Validation Accuracy = {:.3f}".format(validation_accuracy))
        print()

    saver.save(sess, './lenet')
    print("Model saved")


import cv2
image=cv2.imread('File path')
image=cv2.resize(image,(32,32)) #classifier takes 32X32 images 
image=np.array(image)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    saver3 = tf.train.import_meta_graph('./lenet.meta')
    saver3.restore(sess, "./lenet")
    pred = tf.nn.softmax(logits)
    predictions = sess.run(tf.argmax(y,0), feed_dict={x: image})
    print (predictions)

【问题讨论】:

  • pred 是怎么回事?它在任何地方使用吗?假设 y 有 logits 或 softmax 输出,你可以直接评估和打印(它肯定会根据图像而改变,标签可能只是巧合)。
  • 我尝试了这种方法 predictions = sess.run(pred, feed_dict={x: image}) 并得到了错误。 “您必须使用 dtype float 为占位符张量‘Placeholder_4’提供一个值”。我好像忘记删了。
  • 您是否以交互方式运行此程序(ipython/jupyter)?这通常会导致浮动占位符。如果没有,您是否声明了除x 之外的任何占位符?如果不看模型本身,我只能说些什么。
  • 供参考 y = tf.placeholder(tf.int32, (None)),所以我很确定我什么都没做。
  • 是的,它正在 jupyter 中运行。我会用整个模型修改帖子。

标签: python-3.x tensorflow deep-learning


【解决方案1】:

所以这里必须首先清除内核和输出。在某个地方,我的占位符搞混了,清理内核就解决了这个问题。然后我必须意识到这里真正要做的事情:我必须在我的新数据上调用softmax 函数。

像这样:

pred = tf.nn.softmax(logits)
classification = sess.run(pred, feed_dict={x: image_array}) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2020-04-18
    • 1970-01-01
    • 2020-07-04
    相关资源
    最近更新 更多