【问题标题】:Extracting probabilities from all classes in Tensorflow from CNN从 CNN 中提取 Tensorflow 中所有类的概率
【发布时间】:2018-04-04 16:30:01
【问题描述】:

我有一个训练有素的 CNN,它可以生成正确的预测来确定预期的类别,但是我希望将每个类别的概率作为一个列表查看。我试图添加一个 softmax 函数,但我收到一个关于负维度的错误。我已经阅读了这个问题的多个版本,并且看到了似乎有多种解决方案,但是,似乎一种尺寸并不适合所有人,虽然我尝试了各种带有 eval 和 run 语句的解决方案;它们似乎都不适用于此模型。我使用的代码是:

import tensorflow as tf
import numpy as np
import csv
SpeciesID = {'None':0}

def extract_data(filename, CSV_Origin_File):
    print("extracting data...")

    NUM_LABELS = 113 
    NUM_FEATURES = 10816


    labels = []
    fvecs = []
    rowCount = 0


    for IDline in open(CSV_Origin_File):
        IDrow = IDline.split(',')
        INkey = IDrow[6]
        INvalue = IDrow[9]
        SpeciesID.update({INkey:INvalue})

    #iterate over the rows, split the label from the features
    #convert the labels to integers and features to floats

    for line in open(filename):
        rowCount = rowCount + 1
        row = line.split(',')
        labels.append(row[0])#(int(row[7]))

        for x in row [0:10816]: #was 3-10820
            #print("fvecs>>>", x)
            fvecs.append(float(x))

    #convert the array of float arrasy into a numpy float matrix
    fvecs_np = np.matrix(fvecs).astype(np.float32)

    #convert the array of int lables inta a numpy array
    labels_np = np.array(labels).astype(dtype=np.uint8)

    #convert the int numpy array into a one-hot matrix
    labels_onehot = (np.arange(NUM_LABELS) == labels_np[:, None]).astype(np.float32)
    print("arrays converted")
    return fvecs_np, labels_onehot

# Create some wrappers for simplicity
def conv2d(x, W, b, strides=1): #Layer 1 : Convolutional layer
    # Conv2D wrapper, with bias and relu activation
    #print("conv2d")
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') # Strides are the tensors...list of integers.  Tensors=data
    x = tf.nn.bias_add(x, b)  #bias is the tuning knob
    return tf.nn.relu(x) #rectified linear unit (activation function)


def maxpool2d(x, k=2): #Layer 2 : Takes samples from the image. (This is a 4D tensor)
    #print("maxpool2d")
    # MaxPool2D wrapper
    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
                          padding='SAME')


def conv_net(x, wc1, wc2, wd1, Wout, bc1, bc2, bd1, Bout, dropout):
    #print("conv_net setup")
    # Reshape input picture
    x = tf.reshape(x, shape=[-1, 104, 104, 1])  #-->52x52 , -->26x26x64

    # Convolution Layer
    conv1 = conv2d(x, wc1, bc1) #defined above already
    # Max Pooling (down-sampling)
    conv1 = maxpool2d(conv1, k=2)
    #print(conv1.get_shape)

    # Convolution Layer
    conv2 = conv2d(conv1, wc2, bc2)  #wc2 and bc2 are just placeholders...could actually skip this layer...maybe
    # Max Pooling (down-sampling)
    conv2 = maxpool2d(conv2, k=2)
    #print(conv2.get_shape)

    # Fully connected layer
    # Reshape conv2 output to fit fully connected layer input
    fc1 = tf.reshape(conv2, [-1, wd1.get_shape().as_list()[0]])
    fc1 = tf.add(tf.matmul(fc1, wd1), bd1)
    fc1 = tf.nn.relu(fc1) #activation function for the NN
    # Apply Dropout
    #fc1 = tf.nn.dropout(fc1, dropout) #NO DROPOUT FOR TESTING!!!

    # Output, class prediction

    out = tf.add(tf.matmul(fc1, Wout), Bout)
    #print(out, "OUTPUT_________$$$")
    return out


def TestModels(InputFile, RefFile):
        fvecs_np, labels_onehot = extract_data(InputFile, RefFile)

        print('RESTORING NN MODEL')

        weights = {}
        biases = {}
        sess=tf.Session()  
        init = tf.global_variables_initializer()

        #Load meta graph and restore weights

        ModelID = "CNN_PICORNA_UCONN2-1000.meta" 
        print("RESTORING:::", ModelID)

        saver = tf.train.import_meta_graph(ModelID)

        saver.restore(sess,tf.train.latest_checkpoint('./'))

        graph = tf.get_default_graph()
        x = graph.get_tensor_by_name("x:0")
        y = graph.get_tensor_by_name("y:0")
        keep_prob = tf.placeholder(tf.float32) 
        y_ = tf.placeholder("float", shape=[None, 113])

        wc1 = graph.get_tensor_by_name("wc1:0")
        wc2 = graph.get_tensor_by_name("wc2:0")
        wd1 = graph.get_tensor_by_name("wd1:0")
        Wout = graph.get_tensor_by_name("Wout:0")

        bc1 = graph.get_tensor_by_name("bc1:0")
        bc2 = graph.get_tensor_by_name("bc2:0")
        bd1 = graph.get_tensor_by_name("bd1:0")
        Bout = graph.get_tensor_by_name("Bout:0")

        weights = {wc1, wc2, wd1, Wout}
        biases = {bc1, bc2, bd1, Bout}

        pred = conv_net(x, wc1, wc2, wd1, Wout, bc1, bc2, bd1, Bout, keep_prob)

        prediction=tf.argmax(pred,1)
        best = sess.run([prediction],feed_dict={x: fvecs_np})

        probstesting = tf.nn.softmax(y,1)

        print("NEXTArgmax") 

        predY = prediction.eval(feed_dict={x: fvecs_np}, session=sess)

        PredSTR = str(predY[0])

        ProbBest = sess.run([probstesting], feed_dict={x: fvecs_np})

        print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
        print("Predicted::: ", predY,  (list(SpeciesID.keys())[list(SpeciesID.values()).index(PredSTR)]))
        print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")

TestModels("/users/rwbarrettemac/desktop/NN_DB/VirPlotOUT_TESTX.csv", "/users/rwbarrettemac/Picorna_NN/PicornaALL_INv2.csv")

虽然我在运行测试样本时得到了预期类的正确预测,但在尝试获取概率时出现以下错误,该错误是从行中抛出的:

ProbBest = sess.run([probstesting], feed_dict={x: fvecs_np})

它返回的错误语句是:

2017-10-23 17:53:36.855270: W tensorflow/core/framework/op_kernel.cc:1148]    Invalid argument: Shape [-1,113] has negative dimensions
2017-10-23 17:53:36.855314: E tensorflow/core/common_runtime/executor.cc:644]  Executor failed to create kernel. Invalid argument: Shape [-1,113] has negative  dimensions
      [[Node: y = Placeholder[dtype=DT_FLOAT, shape=[?,113],  _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
2017-10-23 17:53:36.855424: W tensorflow/core/framework/op_kernel.cc:1148]   Invalid argument: Shape [-1,113] has negative dimensions
2017-10-23 17:53:36.855441: E tensorflow/core/common_runtime/executor.cc:644]  Executor failed to create kernel. Invalid argument: Shape [-1,113] has negative  dimensions
     [[Node: y = Placeholder[dtype=DT_FLOAT, shape=[?,113],   _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Traceback (most recent call last):

...lots more of the same....

InvalidArgumentError (see above for traceback): Shape [-1,113] has negative dimensions
     [[Node: y = Placeholder[dtype=DT_FLOAT, shape=[?,113],   _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

(tensorflow) aapnypi2vtrbarr:~ rwbarrettemac$

我也尝试了以下方法,但没有成功:

probabilities=pred
print "probabilities", probabilities.eval(feed_dict={x: mnist.test.images},      session=sess)

归根结底,下面的示例是我为该网络设想的输出。假设我走错了路,有人可以建议正确的代码来获得输出,该输出会产生类似于下面示例的数据,其中包含类和概率。谢谢。

[class] probability

[1] 0.0050
[2] 0.0124
[3] 0.8260
[4] 0.0001
 .
 .
 .
[113] 0.0008

【问题讨论】:

    标签: python-3.x tensorflow probability conv-neural-network


    【解决方案1】:

    所以我已经找到了部分解决方案。

    我添加了一行:

    y_ = tf.nn.softmax(tf.matmul(fc1,Wout) + Bout)
    

    到 conv_net(...) 函数,然后将以下行添加到 TestModels(...) 函数:

    ProbOUT = probabilities.eval(feed_dict={x: fvecs_np}, session=sess)
    

    遍历 ProbOUT,得到以下输出:

    0.0.....Cardiovirus A[51]
    0.0.....Cardiovirus B[52]
    0.0.....Foot-and-mouth disease virus[53]
    0.0.....Equine rhinitis A virus[54]
    0.0.....Bovine rhinitis B virus[55]
    0.0.....Bovine rhinovirus 1[56]
    0.0.....Teschovirus A[57]
    0.0.....Ljungan virus[58]
    1.0.....Human parechovirus[59]
    0.0.....Aichivirus A[60]
    0.0.....Kobuvirus sheep/TB3/HUN/2009[61]
    0.0.....Mouse kobuvirus M-5/USA/2010[62]
    

    这很好!但为什么值是 1 或 0?我认为通过 softmax 传递它我应该得到更精确的概率。这看起来像是通过 argmax 传递的。我错过了什么吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 2019-06-06
      • 2015-01-13
      • 1970-01-01
      • 2017-09-16
      • 2016-12-17
      • 2019-04-21
      相关资源
      最近更新 更多