首页 > 解决方案 > 基于训练好的张量流模型对单张图像进行分类

问题描述

我正在使用卷积神经网络对 3 个标签中的图像进行分类。我做了所有的训练和测试,得到了 60% 的准确率。然后,我保存了这个模型,我想加载一个图像并将其分类到其中一个标签中。我正在使用的代码:

X_new = process_data() # That's my input image after some processing
pred = convolutional_neural_network(x) # That's my CNN

with tf.Session() as sess:
    # Here I restore the trained model
    saver = tf.train.import_meta_graph('modelo.meta')
    saver.restore(sess, 'modelo')
    print('Model loaded')

    sess.run(tf.initialize_all_variables())

    # Here I'm trying to predict the label of my image
    c = sess.run(pred, feed_dict={x: X_new})

    print(c)

当我打印c时,它会返回如下内容:

[[ 1.5495030e+07 -2.3345528e+08 -1.5847101e+08]]

但我不知道这意味着什么以及我应该如何处理它。无论如何,我想做的是获取图像属于某个标签的百分比。如果有人可以帮助我,我将不胜感激!我是 tensorflow 的新手,我遇到了困难。

太感谢了!

编辑:

卷积神经网络方法:

def convolutional_neural_network(x):
    weights = {'W_conv1': tf.Variable(tf.random_normal([3, 3, 3, 1, 32])), 
               'W_conv2': tf.Variable(tf.random_normal([3, 3, 3, 32, 64])),
               'W_fc': tf.Variable(tf.random_normal([54080, 1024])), 
               'out': tf.Variable(tf.random_normal([1024, n_classes]))}

    biases = {'b_conv1': tf.Variable(tf.random_normal([32])),
              'b_conv2': tf.Variable(tf.random_normal([64])),
              'b_fc': tf.Variable(tf.random_normal([1024])),
              'out': tf.Variable(tf.random_normal([n_classes]))}

    x = tf.reshape(x, shape=[-1, IMG_PX_SIZE, IMG_PX_SIZE, HM_SLICES, 1])

    conv1 = tf.nn.relu(conv3d(x, weights['W_conv1']) + biases['b_conv1'])
    conv1 = maxpool3d(conv1)

    conv2 = tf.nn.relu(conv3d(conv1, weights['W_conv2']) + biases['b_conv2'])
    conv2 = maxpool3d(conv2)

    fc = tf.reshape(conv2, [-1, 54080])
    fc = tf.nn.relu(tf.matmul(fc, weights['W_fc']) + biases['b_fc'])
    fc = tf.nn.dropout(fc, keep_rate)

    output = tf.matmul(fc, weights['out']) + biases['out']

    return output

标签: pythontensorflowdeep-learningclassificationconv-neural-network

解决方案


现在你似乎只有一个线性回归输出层(或 logits 层),所以很难解释输出。我们还需要查看您的损失函数以了解您所看到的输出。

但是,为了分类到 3 个标签之一,您通常希望使用 softmax 层终止网络,然后输出将可解释为每个标签的概率(如果需要百分比,则乘以 100)。

   probabilities = tf.nn.softmax(output)

您仍然可以使用带有 softmax 交叉熵损失的输出/logits 层和您的 grounth-truth 'y' 值来训练您的分类器。

losses = tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y)

推荐阅读