Tensorflow 全卷积网络,训练误差
很抱歉我英语不好 我正在尝试使用TensorFlow构建自己的完全卷积网络。 但是我很难用自己的图像数据训练这个模型,而MNIST数据工作正常 这是我的FCN型号代码:(不使用预培训或预构建的型号) 加载MNIST数据 卷积层 小结:[Conv ReLU Pool]-[Conv ReLU Pool]-[Conv ReLU]-[Dropout]-[Conv ReLU] 定义损耗、精度 培训模式 损失:0.784(约) 准确率:94.8%(约) 问题是,用MNIST数据训练这个模型效果很好,但用我自己的数据,损失总是相同的(0.6319),输出层总是0 除了第三个卷积层的滤波器大小外,与代码没有区别。此筛选器大小和由以前的池层压缩的输入大小必须具有相同的宽度和高度。这就是为什么该层中的过滤器大小为[7,7] 我的模型怎么了 两种情况(MNIST,我自己的数据)之间唯一不同的代码是: 占位符 我自己的数据有(128 x 64 x 1),标签是“眼睛”,“非眼睛”Tensorflow 全卷积网络,训练误差,tensorflow,Tensorflow,很抱歉我英语不好 我正在尝试使用TensorFlow构建自己的完全卷积网络。 但是我很难用自己的图像数据训练这个模型,而MNIST数据工作正常 这是我的FCN型号代码:(不使用预培训或预构建的型号) 加载MNIST数据 卷积层 小结:[Conv ReLU Pool]-[Conv ReLU Pool]-[Conv ReLU]-[Dropout]-[Conv ReLU] 定义损耗、精度 培训模式 损失:0.784(约) 准确率:94.8%(约) 问题是,用MNIST数据训练这个模型效果很好,但用我自
images = tf.placeholder(tf.float32, [None, 128, 64, 1])
labels = tf.placeholder(tf.int32, [None, 2])
第三卷积层
进料(批)
这里的“input_data”是同一目录中的另一个python文件,“get_batch(TRAINING_file,10)”是返回批处理数据的函数。代码是:
def get_input_queue(txtfile_name):
images = []
labels = []
for line in open(txtfile_name, 'r'): # Here txt file has data's path, label, label number
cols = re.split(',|\n', line)
labels.append(int(cols[2]))
images.append(tf.image.decode_jpeg(tf.read_file(cols[0]), channels = 1))
input_queue = tf.train.slice_input_producer([images, labels], shuffle = True)
return input_queue
def get_batch(txtfile_name, batch_size):
input_queue = get_input_queue(txtfile_name)
image = input_queue[0]
label = input_queue[1]
image = tf.reshape(image, [128, 64, 1])
batch_image, batch_label = tf.train.batch([image, label], batch_size)
batch_label_one_hot = tf.one_hot(tf.to_int64(batch_label), 2, on_value=1.0, off_value=0.0)
return batch_image, batch_label_one_hot
似乎没有任何问题……:(请帮帮我!!您的MNIST模型的准确度是多少?如果您发布代码会很有帮助。您是否使用经过培训的模型来评估您自己数据的输出 提出了建立卷积模型的一般性建议。 根据文章,以下是模型建议:- 输入->[[CONV->RELU]*N->POOL?]*M->[FC->RELU]*K->FC 在合用之前拥有一层以上的CONV->RELU对可以提高学习复杂功能的能力。尝试使用N=2而不是1 其他一些建议:
image=tf.重塑(图像[28,28,1])
mean=np.mean(img,dtype='float32')
std=np.std(img,dtype='float32',ddof=1)
如果标准<1e-4:std=1。
img=(img-平均值)/std
您的输入缩放是否适当?。JPEG在[0-255]范围内,需要缩放到[-1-1]。您可以尝试:
image = tf.reshape(image, [128, 64, 1])
image = tf.scalar_mul((1.0/255), image)
image = tf.subtract(image, 0.5)
image = tf.multiply(image, 2.0)
尝试缩放输入(-1到1),并将权重的
stddev
设置为较低的值,如0.01。输出始终为零
指向死ReLUs。“缩放输入(-1到1)”是什么意思?嗯……我试图上传损失和精度图。它说我需要声誉(?)上传图表..无论如何,当我使用MNIST进行培训时,准确率为94%。我将尽快发布整个代码!是的,可能是因为您是新用户。是的,当然会发布代码。由于您总是获得相同的输出0,我强烈怀疑您的筛选值从未更改,您可以通过对sa进行统计来检查y W1(如每次迭代后的最大值和最小值)。我还建议您在重塑图像时,将其缩小为image=tf。重塑(图像[28,28,1])非常感谢!这非常有帮助:)谢谢!我了解到图像有时需要标准化。它解决了你面临的问题吗?嗯。。在我这样做之后,损失和准确度似乎得到了正确的计算,但最终准确度变成198,甚至超过200。但是,损失问题(始终为0.6139)得到了解决:不要介意!它起作用了。非常感谢你。现在我意识到数据和预处理对于训练模型绝对重要。
prediction = tf.squeeze(LAST_RELU)
# Because FCN returns (1 x 1 x class_num) in training
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, labels))
# First arg is 'logits=' and the other one is 'labels='
optimizer = tf.train.AdamOptimizer(0.001)
train = optimizer.minimize(loss)
label_max = tf.argmax(labels, 1)
pred_max = tf.argmax(prediction, 1)
correct_pred = tf.equal(pred_max, label_max)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(10000):
image_batch, label_batch = mnist.train.next_batch(100)
sess.run(train, feed_dict={images: image_batch, labels: label_batch, keep_prob: 0.8})
if i % 10 == 0:
tr = sess.run([loss, accuracy], feed_dict={images: image_batch, labels: label_batch, keep_prob: 1.0})
print("Step %d, Loss %g, Accuracy %g" % (i, tr[0], tr[1]))
images = tf.placeholder(tf.float32, [None, 128, 64, 1])
labels = tf.placeholder(tf.int32, [None, 2])
W3 = tf.Variable(tf.truncated_normal([32, 16, 8, 16], stddev = 0.1))
image_data, label_data = input_data.get_batch(TRAINING_FILE, 10)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
for i in range(10000):
image_batch, label_batch = sess.run([image_data, label_data])
sess.run(train, feed_dict={images: image_batch, labels: label_batch, keep_prob: 0.8})
if i % 10 == 0: ... # Validation part is almost same, too...
coord.request_stop()
coord.join(threads)
def get_input_queue(txtfile_name):
images = []
labels = []
for line in open(txtfile_name, 'r'): # Here txt file has data's path, label, label number
cols = re.split(',|\n', line)
labels.append(int(cols[2]))
images.append(tf.image.decode_jpeg(tf.read_file(cols[0]), channels = 1))
input_queue = tf.train.slice_input_producer([images, labels], shuffle = True)
return input_queue
def get_batch(txtfile_name, batch_size):
input_queue = get_input_queue(txtfile_name)
image = input_queue[0]
label = input_queue[1]
image = tf.reshape(image, [128, 64, 1])
batch_image, batch_label = tf.train.batch([image, label], batch_size)
batch_label_one_hot = tf.one_hot(tf.to_int64(batch_label), 2, on_value=1.0, off_value=0.0)
return batch_image, batch_label_one_hot
img = np.dot(np.array(img, dtype='float32'), [[0.2989],[0.5870],[0.1140]])
mean = np.mean(img, dtype='float32')
std = np.std(img, dtype='float32', ddof=1)
if std < 1e-4: std = 1.
img = (img - mean) / std
image = tf.reshape(image, [128, 64, 1])
image = tf.scalar_mul((1.0/255), image)
image = tf.subtract(image, 0.5)
image = tf.multiply(image, 2.0)