给定批处理RGB图像作为输入,shape=(batch_size,width,height,3)
和一个表示为one-hot,shape=(batch_size,width,height,n_classes)的多类目标
以及在最后一层具有softmax激活的模型(Unet,DeepLab)。
我在kera/tensorflow中寻找加权分类交叉熵损失函数。
fit_generator中的class_weight参数似乎不起作用,我在这里或在https://github.com/keras-team/keras/issues/2115中都找不到答案。
def weighted_categorical_crossentropy(weights):
# weights = [0.9,0.05,0.04,0.01]
def wcce(y_true, y_pred):
# y_true, y_pred shape is (batch_size, width, height, n_classes)
loos = ?...
return loss
return wcce发布于 2019-12-30 04:57:45
我将回答我的问题:
def weighted_categorical_crossentropy(weights):
# weights = [0.9,0.05,0.04,0.01]
def wcce(y_true, y_pred):
Kweights = K.constant(weights)
if not K.is_tensor(y_pred): y_pred = K.constant(y_pred)
y_true = K.cast(y_true, y_pred.dtype)
return K.categorical_crossentropy(y_true, y_pred) * K.sum(y_true * Kweights, axis=-1)
return wcce用法:
loss = weighted_categorical_crossentropy(weights)
optimizer = keras.optimizers.Adam(lr=0.01)
model.compile(optimizer=optimizer, loss=loss)发布于 2020-05-01 03:45:23
我使用的是广义骰子损失。在我的例子中,它比加权分类Crossentropy更好。我的实现是用PyTorch实现的,但是翻译起来应该相当容易。
class GeneralizedDiceLoss(nn.Module):
def __init__(self):
super(GeneralizedDiceLoss, self).__init__()
def forward(self, inp, targ):
inp = inp.contiguous().permute(0, 2, 3, 1)
targ = targ.contiguous().permute(0, 2, 3, 1)
w = torch.zeros((targ.shape[-1],))
w = 1. / (torch.sum(targ, (0, 1, 2))**2 + 1e-9)
numerator = targ * inp
numerator = w * torch.sum(numerator, (0, 1, 2))
numerator = torch.sum(numerator)
denominator = targ + inp
denominator = w * torch.sum(denominator, (0, 1, 2))
denominator = torch.sum(denominator)
dice = 2. * (numerator + 1e-9) / (denominator + 1e-9)
return 1. - dice发布于 2019-12-30 01:22:26
这个问题可能类似于:Unbalanced data and weighted cross entropy,它有一个公认的答案。
https://stackoverflow.com/questions/59520807
复制相似问题