enet分割_Enet、refinenet多类别的语义分割的loss

这篇博客探讨了在Pytorch中进行语义分割时,使用CrossEntropyLoss和LogSoftmax+NLLLoss2d两种计算损失的方法。通过ENet模型展示了如何实现多类别语义分割,并且提到了损失函数的选择对于多类别问题的重要性。同时,还提及了NLLLoss2d在忽略特定标签时的应用。
摘要由CSDN通过智能技术生成

1、语义分割的loss再pytorch里有两个方式计算,其效果是一样的。

A、使用CrossEntropyLoss():

# Intialize ENet

model = ENet(num_classes).to(device)

# Check if the network architecture is correct

print(model)

# We are going to use the CrossEntropyLoss loss function as it's most

# frequentely used in classification problems with multiple classes which

# fits the problem. This criterion combines LogSoftMax and NLLLoss.

#这个损失函数适用于于多类别的语义分割也,其损失也可以换成logSoftMax跟NLLLoss的组合。

criterion = nn.CrossEntropyLoss(weight=class_weights)

# Forward propagation

outputs = self.model(inputs)

# Loss computation

loss = self.criterion(outputs, labels)

# Backpropagation

self.optim.zero_grad()

loss.backward()

self.optim.step()

B、使用LogSoftmax跟 NLLLoss2d计算损失:

## Criterion ##这个NLLLoss2d限制已经改用NLLLoss()

segm_crit = nn.NLLLoss2d(ignore_index=args.ignore_label).cuda()

# Compute output

output = segmenter(input_var)

output = nn.functional.interpolate(output, size=target_var.size()[1:], mode='bilinear', align_corners=False)

soft_output = nn.LogSoftmax()(output)

# Compute loss and backpropagate

loss = segm_crit(soft_output, target_var)

optim_enc.zero_grad()

optim_dec.zero_grad()

loss.backward()

optim_enc.step()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值