我有一个深度 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
应该是一个 one-hot 向量。但请注意,仅在定义变量时才将其归零。之后,它被分配给不同索引处的1
。 while
循环的后续迭代更新相同的 b
数组,因此它最终在批处理的后续行中包含多个 1
。 cross-entropy loss期望有效的概率分布,因此对于您的数据,其输出变得完全没有意义:
Each row
labels[i]
must be a valid probability distribution.
总结:您的数据处理方式过于复杂,因此很容易出错。尝试更简单地组织输入文件,以便可以将其读入 numpy 数组(或 pandas 数据帧)并馈送到 session 中。
关于python - Tensorflow 神经网络预测始终相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47638633/