【发布时间】:2017-06-16 18:16:08
【问题描述】:
我正在使用 tensorflow 进行语义分割。在计算像素损失时,如何告诉 tensorflow 忽略特定标签?
我读过in this post,对于图像分类,可以将标签设置为-1,它将被忽略。如果这是真的,给定标签张量,我如何修改我的标签,以便将某些值更改为 -1?
在 Matlab 中是这样的:
ignore_label = 255
myLabelTensor(myLabelTensor == ignore_label) = -1
但我不知道如何在 TF 中做到这一点?
一些背景信息:
这是标签的加载方式:
label_contents = tf.read_file(input_queue[1])
label = tf.image.decode_png(label_contents, channels=1)
这是当前计算损失的方式:
raw_output = net.layers['fc1_voc12']
prediction = tf.reshape(raw_output, [-1, n_classes])
label_proc = prepare_label(label_batch, tf.pack(raw_output.get_shape()[1:3]),n_classes)
gt = tf.reshape(label_proc, [-1, n_classes])
# Pixel-wise softmax loss.
loss = tf.nn.softmax_cross_entropy_with_logits(prediction, gt)
reduced_loss = tf.reduce_mean(loss)
与
def prepare_label(input_batch, new_size, n_classes):
"""Resize masks and perform one-hot encoding.
Args:
input_batch: input tensor of shape [batch_size H W 1].
new_size: a tensor with new height and width.
Returns:
Outputs a tensor of shape [batch_size h w 21]
with last dimension comprised of 0's and 1's only.
"""
with tf.name_scope('label_encode'):
input_batch = tf.image.resize_nearest_neighbor(input_batch, new_size) # as labels are integer numbers, need to use NN interp.
input_batch = tf.squeeze(input_batch, squeeze_dims=[3]) # reducing the channel dimension.
input_batch = tf.one_hot(input_batch, depth=n_classes)
return input_batch
我正在使用 tensorflow-deeplab-resnet model,它使用 caffe-tensorflow 将 Caffe 中实现的 Resnet 模型传输到 tensorflow。
【问题讨论】:
标签: tensorflow