Skip to main content
 首页 » 编程设计

python之Tensorflow神经网络预测总是一样的

2024年08月19日33wayfarer

我有一个深度 CNN,可以为 3d 图像中的每个像素预测“0”和“2”之间的标签。我在每个像素都标记为“1”的图像上训练了模型。因此,在测试模型时,我认为每个预测都应该是“1”。相反,该模型仅预测“0”。

这里是整个模型的存储库:https://github.com/dhasl002/Research-DeepLearning .

由于代码将近 300 行,下面我将只包含相关代码。

 x = tf.placeholder(tf.float32, shape=[None, 7168]) 
 y_ = tf.placeholder(tf.float32, shape=[None, 7168, 3]) 
 
 W_final = weight_variable([7168,7168,3]) 
 b_final = bias_variable([7168,3]) 
 
 #"final" is the result of the many convolutions 
 final_conv = tf.tensordot(final, W_final, axes=[[1], [1]]) + b_final 
 
 cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=final_conv)) 
 train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 
 correct_prediction = tf.equal(tf.argmax(final_conv, 2), tf.argmax(y_, 2)) 
 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
 
 #a is a threshold associate with each pixel, b is the label of each pixel 
 a = np.zeros((1,7168),dtype = float) 
 b = np.zeros((1,7168, 3), dtype = float) 
 
 #this is a little simplified for clarity of reader 
 #TRAINING 
 for line in inputFile: 
   thresh, label = line.strip().split(",") 
   a[0][it] = thresh 
   b[0][it][label] = 1 
 train_step.run(feed_dict={x: a, y_: b, keep_prob: .5}) 
 
 #TESTING 
 for line in inputFile: 
   thresh, label = line.strip().split(",") 
   a[0][it] = thresh 
   b[0][it][label] = 1 
 temp = sess.run(tf.argmax(final_conv,2), feed_dict={x: a}) 

我相信最后一行的“temp”应该包含正确的预测(7168 个标签 - 每个像素一个)。 为什么“temp”实际上是在仅带有“1”标签的图像上训练时总是产生全“0”标签?

请您参考如下方法:

您提供的数据不仅包含 1 标签,偶尔也包含 2(您可以浏览文本文件或简单地打印 label 值来查看)。这不仅与您训练常量函数的想法相矛盾,而且还破坏了单热编码,从而破坏了整个算法。

这是您脚本的摘录:

a = np.zeros((1,N*M*P),dtype = float) 
b = np.zeros((1,N*M*P, 3), dtype = float) 
[...] 
 
with tf.Session() as sess: 
   sess.run(tf.global_variables_initializer()) 
   parent = "..." 
   with open(parent) as inf1: 
     next(inf1) 
     for line5 in inf1: 
       line1, maxNum = line5.strip().split(",") 
       path = "..." 
       num = 0 
       while num < maxNum: 
         it = 0 
         with open(path + str(num) + ".txt") as inf: 
           next(inf) 
           num = num + 1 
           for line in inf: 
             [...] 
             a[0][it] = thresh 
             b[0][it][label] = 1 
             it = it + 1 

查看您的代码,b 应该是一个单热向量。但请注意,它仅在定义变量时才归零。之后,它被分配给不同索引处的 1。 while 循环的后续迭代更新相同的 b 数组,因此它最终在批处理的后面的行中包含多个 1。 cross-entropy loss期望有效的概率分布,因此对于您的数据,其输出变得完全没有意义:

Each row labels[i] must be a valid probability distribution.

总结:您处理数据的方式过于复杂,因此容易出错。尝试更简单地组织您的输入文件,以便可以将其读入 numpy 数组(或 pandas 数据帧)并提供给 session 。