【问题标题】:Classify single image based on trained tensorflow model基于训练好的张量流模型对单张图像进行分类
【发布时间】:2023-03-10 15:21:01
【问题描述】:

我正在使用卷积神经网络对 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

【问题讨论】:

  • 这个问题的答案很大程度上取决于convolutional_neural_network()中的内容
  • 您好!感谢帮助,我添加了我的卷积网络的代码!

标签: python tensorflow deep-learning classification conv-neural-network


【解决方案1】:

现在您似乎只有一个线性回归输出层(或 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)

【讨论】:

    【解决方案2】:

    取决于模型最后一层使用的激活层的类型。我假设 softmax,因为有 3 个标签要预测。基于这些值,我认为网络无法以高确定性预测标签,因此值较低。如果您只想知道图像的标签,请像这样使用 numpy 库的 argmax 函数

    import numpy as np
    c=np.argmax(c)
    print(c)
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 2020-02-29
      • 2017-09-22
      • 2020-09-14
      • 2021-12-24
      • 2023-03-12
      相关资源
      最近更新 更多