关于图像识别CNN预测应返回什么的一些说明

时间:2019-12-01 19:30:45

标签: python tensorflow conv-neural-network image-recognition

基于猫和狗识别的示例,我创建了一个带有tensorflow和keras的cnn,它应该识别4类图像:猫,汽车,人,花。当我做出预测时,模型会以[0. 1. 0. 0.]之类的列表进行响应,是正确的还是应该以0至1之间的一个值列表进行响应,而我必须取其中的最大值? 例如[0.2、0.1、0.5、0.8]

1 个答案:

答案 0 :(得分:0)

假设您已将softmax,log_softmax或Sigmoid用作最后一层,则下面是您的输出外观。我已经给出了计算背后的数学原理,并在代码段的注释中给出了一些解释。

%tensorflow_version 1.x
import tensorflow as tf
import numpy as np

# Assume below is the input to last layer
Input = tf.constant(np.array([[0.1, 0.3, 0.5, 1.5]]))

with tf.Session() as sess:
   # Computes softmax activations. softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), axis)
   # Output sums upto 1, as this is the probability
   print(tf.nn.softmax(Input).eval()) 

   # Computes log softmax activations.For each batch i and class j we have logsoftmax = logits - log(reduce_sum(exp(logits), axis))
   # Gives log(softmax(input)) as output which is log likelihood and value doesn't sum up to 1.
   print(tf.nn.log_softmax(Input).eval()) 

   # Computes sigmoid of x element-wise. Specifically, y = 1 / (1 + exp(-x)).
   print(tf.nn.sigmoid(Input).eval())  

输出-

[[0.12872618 0.15722651 0.1920369  0.52201041]]
[[-2.05006775 -1.85006775 -1.65006775 -0.65006775]]
[[0.52497919 0.57444252 0.62245933 0.81757448]]