我想知道如何使用TensorFlow处理图像分割中未标记的图像部分.例如,我的输入是高度*宽度*通道的图像.标签尺寸高度*宽度太大,每个像素都有一个标签.
图像的某些部分是注释的,其他部分则没有.我希望这些部分对梯度计算没有任何影响.此外,我对网络预测这个"无效"标签不感兴趣.
这有标签或功能吗?目前我正在使用tf.nn.sparse_softmax_cross_entropy_with_logits.
python neural-network image-segmentation deep-learning tensorflow
我正在使用tensorflow进行语义分割.在计算像素损失时如何告诉tensorflow忽略特定标签?
我在这篇文章中读到,对于图像分类,可以将标签设置为-1,它将被忽略.如果这是真的,给定标签张量,我如何修改我的标签,以便将某些值更改为-1?
在Matlab中它将是这样的:
ignore_label = 255
myLabelTensor(myLabelTensor == ignore_label) = -1
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在TF中这样做?
一些背景信息:
这是标签的加载方式:
label_contents = tf.read_file(input_queue[1])
label = tf.image.decode_png(label_contents, channels=1)
Run Code Online (Sandbox Code Playgroud)
这就是目前计算损失的方式:
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)
Run Code Online (Sandbox Code Playgroud)
同
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 …Run Code Online (Sandbox Code Playgroud) 我正在恢复这个 github问题,因为我相信它是有效的并且需要解释。tf.keras 有一个掩码层,其文档如下:
对于输入张量中的每个时间步长(张量中的维度#1),如果该时间步长的输入张量中的所有值都等于 mask_value,则该时间步长将在所有下游层中被屏蔽(跳过)(只要它们支持掩蔽)。
如果任何下游层不支持掩码但收到这样的输入掩码,则会引发异常。
# create padded zeros and change two valid entries.
inputs = np.zeros([1,5])
inputs[0,1] = 0.5
inputs[0,2] = 0.1
inputs = tf.Variable(inputs)
masked_inputs = tf.keras.layers.Masking(mask_value=0.0)(inputs)
with_masking = tf.keras.layers.Softmax()(masked_inputs)
without_masking = tf.keras.layers.Softmax()(inputs)
Run Code Online (Sandbox Code Playgroud)
两个结果几乎相同
with_masking
<tf.Tensor: shape=(1, 5), dtype=float32, numpy=
array([[0.1737954 , 0.28654018, 0.19207363, 0.1737954 , 0.1737954 ]],
dtype=float32)>
without_masking
<tf.Tensor: shape=(1, 5), dtype=float64, numpy=array([[0.1737954 , 0.28654017, 0.19207362, 0.1737954 , 0.1737954 ]])>
Run Code Online (Sandbox Code Playgroud)
我希望只对有效条目进行 softmax 处理,类似于
#Assign one large value
inputs = np.zeros([1,2])
inputs[0,0] = …Run Code Online (Sandbox Code Playgroud)