【问题标题】:Tensorflow neural network prediction is always the sameTensorFlow 神经网络预测总是一样的
【发布时间】:2018-05-18 05:30:36
【问题描述】:

我有一个深度 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”标签?

【问题讨论】:

  • 将至少部分数据添加到 repo,以便复制
  • 我已将一些数据添加到 repo 中。谢谢。

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


【解决方案1】:

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

以下是脚本的摘录:

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

每一行labels[i] 必须是一个有效的概率分布。

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

【讨论】:

    【解决方案2】:

    由于您使用的是 ReLU,一种可能是您遇到了 Dying ReLU 问题;你可以通过切换到像leaky ReLUs 这样的东西来解决这个问题。

    除此之外,您的模型非常深刻和复杂;为了确保它正常工作,您可能需要大幅缩减它,对其进行测试以查看它是否可以为您提供合理的结果,然后分阶段添加。

    无论如何,您的模型似乎过于复杂,无法解决问题。为每个单独的像素生成标签的模型应该非常简单,因为给定像素的标签可能只取决于附近的像素,而且可能不是很复杂。

    【讨论】:

    • 我正在复制研究论文中的架构,因此我对模型本身有些信心。我认为我的问题来自我存储、训练和测试模型的方式。
    • 你能附上论文的链接吗?我很好奇他们为什么要为每个像素输出一个标签。
    • 我已将其添加到存储库中。是否有可能模型训练速度太慢以至于我需要 300 多张图像来训练它?
    猜你喜欢
    • 2018-05-23
    • 2017-05-20
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    相关资源
    最近更新 更多