Paddle 交叉熵类别加权

a6b3iqyw  于 2021-12-07  发布在  Java
关注(0)|答案(11)|浏览(244)

类别数有20个

input大小 [8, 20, 512, 512]

label大小 [8, 1, 512, 512]

weight 是 [20]

结果就会报错:

File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 884, incall
outputs = self.forward(*inputs,**kwargs)
File "work/FocalLoss.py", line 27, in forward
logpt = - F.cross_entropy(input, target, weight=weight, axis =1,ignore_index = self.ignore_index)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/functional/loss.py", line 1222, in cross_entropy
weight_gather = core.ops.gather_nd(weight, label) #trans to sample
ValueError: (InvalidArgument) Input(Index).shape[-1] should be no greater than Input(X).rank
[Hint: Expected index_dims[index_dims_size - 1] <= x_dims_size, but received index_dims[index_dims_size - 1]:512 > x_dims_size:1.] (at /paddle/paddle/fluid/operators/gather_nd_op.cc:47)
[Hint: If you need C++ stacktraces for debugging, please set FLAGS_call_stack_level=2.]
[operator < gather_nd > error]

这意思,难道weight要 [20, 512]嘛??
谢谢!

bfrts1fy

bfrts1fy1#

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

hl0ma9xz

hl0ma9xz2#

Loss函数代码:
`
import paddle
import paddle.nn as nn
import paddle.nn.functional as F

class FocalLoss2d(nn.Layer):
definit(self, gamma=2, weight=None, size_average=None, ignore_index=-100,
reduce=None, reduction='mean', balance_param=0.25):
super(FocalLoss2d, self).init()
self.gamma = gamma
self.weight = weight
self.size_average = size_average
self.ignore_index = ignore_index
self.balance_param = balance_param

def forward(self, input, target):
    # inputs and targets are assumed to be BatchxClasses
    # assert len(input.shape) == len(target.shape)
    assert input.shape[0] == target.shape[0]
    # assert input.size(1) == target.size(1)

    weight = self.weight

    # compute the negative likelyhood
    logpt = - F.cross_entropy(input, target, weight=weight, axis =1,ignore_index = self.ignore_index)
    pt = paddle.exp(logpt)

    # compute the loss
    focal_loss = -((1 - pt)**self.gamma) * logpt
    #balanced_focal_loss = self.balance_param * focal_loss

    return focal_loss

`

iih3973s

iih3973s3#

weight = paddle.to_tensor([0.007, 1 , 1, 1, 1, 1, 1,
1, 1, 1 , 1, 1, 1, 1,
1, 1, 1 , 1, 1, 1 , 1.99999 ])
criterion_CE = FL.FocalLoss2d(weight = weight)

0x6upsns

0x6upsns4#

`ifname== 'main':

img1_path = 'work/HRSCD/train/im1'
img2_path = 'work/HRSCD/train/im2/'
label1_path = 'work/HRSCD/train/label1/'
label2_path = 'work/HRSCD/train/label2/'
label_path = 'work/HRSCD/train/label/'

dataset = MutliTask_CDDataset_HRSCD(img1_path, img2_path,
                              label1_path, label2_path, label_path, aug=1)

# hyper-parameters

size =8
epochs = 60
lr = .01
num_class = 21

train_loader = DataLoader(dataset, batch_size=size, shuffle=True, num_workers=4,use_shared_memory=False)

net = Model_CA.PCF_Unet_CD(5,21)

data = np.array([0.01,1,1,1,1,
0.5,2,2,2,2,2,2.5,2,2,1,2.5,1,1,1,2.5,3])

# weight = paddle.to_tensor([0.007, 1 , 1, 1, 1, 1, 1,

# 1, 1, 1 , 1, 1, 1, 1,

# 1, 1, 1 , 1, 1, 1 , 1.99999  ])

weight = paddle.to_tensor(data)
print(weight.shape)

criterion_CE1 = FL.FocalLoss2d(ignore_index=100)
criterion_CE2 = paddle.nn.loss.CrossEntropyLoss(weight=weight,reduction='mean',axis=1)
evaluate = eval.ConfusionMatrix(num_classes=num_class, streaming=True)
scheduler = optim.lr.ReduceOnPlateau(learning_rate=lr, factor=0.1, patience=5, verbose=True)

SGD = True
if SGD == False:
    optimizer = optim.Adam(parameters=net.parameters(),
                           learning_rate=scheduler, weight_decay=0.0001)
else:
    optimizer = optim.Momentum(parameters=net.parameters(), learning_rate=scheduler,
                               momentum=0.9, weight_decay=0.0001)

# record loss and acc

log_file = io.open("work/log_files_HRSCD/log_lr_0.01_PCF_CA.txt", "w")

print('Start training...')
for epoch in range(0, epochs):

    train_loss = 0
    val_loss = 0
    train_IoU_fg = 0
    val_IoU_fg = 0
    train_mIoU = 0
    val_mIoU = 0
    train_kappa = 0
    val_kappa = 0
    train_IoU = 0

    time_start = time.time()
    net.train()
    for i_batch, (img1, img2, label1, label2, label) in enumerate(train_loader):

        label1 = paddle.cast(label1, dtype='int64')
        label2 = paddle.cast(label2, dtype='int64')
        label = paddle.cast(label, dtype='int64')

        y = paddle.to_tensor(101)
        label1=paddle.where(label1!=0,label1,y)
        label2=paddle.where(label2!=0,label2,y)

        label1 = label1-1
        label2 = label2-1

        print(label.shape)

        output1, output2, output = net(img1, img2)
        #output1 shape: 8 5 512 512
        #output2 shape: 8 5 512 512
        #output shape: 8 21 512 512

        loss2 = criterion_CE1(output1, label1)
        loss3 = criterion_CE1(output2, label2)

        # error happens
        loss1 = criterion_CE2(output, label)`
7kqas0il

7kqas0il5#

label的shape都是 8 1 512 512

pieyvz9o

pieyvz9o6#

Traceback (most recent call last):
File "work/PCF_CA/train_PCF_CA_HRSCD.py", line 95, in
loss1 = criterion_CE2(output, label)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 884, incall
outputs = self.forward(*inputs,**kwargs)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/layer/loss.py", line 249, in forward
name=self.name)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/functional/loss.py", line 1222, in cross_entropy
weight_gather = core.ops.gather_nd(weight, label) #trans to sample
ValueError: (InvalidArgument) Input(Index).shape[-1] should be no greater than Input(X).rank
[Hint: Expected index_dims[index_dims_size - 1] <= x_dims_size, but received index_dims[index_dims_size - 1]:512 > x_dims_size:1.] (at /paddle/paddle/fluid/operators/gather_nd_op.cc:47)
[Hint: If you need C++ stacktraces for debugging, please set FLAGS_call_stack_level=2.]
[operator < gather_nd > error]

y4ekin9u

y4ekin9u7#

错误信息nn.loss.CrossEntropyLoss(训练代码那里),加权会报错。 自定义的Focal loss和官方交叉熵都会因为加权报错

mv1qrgav

mv1qrgav8#

环境配置(AI Studio)
PaddlePaddle版本:2.0.0rc(显示的这个版本)
Python版本:3.7

2、BUG复现步骤(必要时给出截图):

import paddle
import numpy as np

np.random.seed(123)

pp_ce = paddle.nn.functional.cross_entropy

pp_to_tensor = paddle.to_tensor

# shape [batchsize,classes,rows,cols]

# logits 2 4 2 2

# labels 2 1 2 2

# weight 4

logits = np.random.rand(2,4,2,2).astype("float32")
labels = np.array([[[0,1],[2,3]],[[0,1],[2,3]]]).astype("int64")
labels = np.expand_dims(labels,1)

weight = np.array([0.5,0.5,1,2]).astype("int64")

print('logits.shape: ',logits.shape)
print('labels.shape: ',labels.shape)
print('weight.shape: ',weight.shape)

pp_loss = pp_ce(pp_to_tensor(logits), pp_to_tensor(labels), axis=1)
print('CE_Loss is: ',pp_loss.numpy())

pp_loss = pp_ce(pp_to_tensor(logits), pp_to_tensor(labels), axis=1,weight=pp_to_tensor(weight))

print('weighted CE_Loss is: ',pp_loss.numpy())

3、期望结果
都能计算loss输出:
CE_Loss: 某个数字
weight_CE_Loss: 另一个数字

4、实际结果
CE_Loss is: [1.3867134]
Traceback (most recent call last):
File "work/script.py", line 51, in
pp_loss = pp_ce(pp_to_tensor(logits), pp_to_tensor(labels), axis=1,weight=pp_to_tensor(weight))
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/functional/loss.py", line 1222, in cross_entropy
weight_gather = core.ops.gather_nd(weight, label) #trans to sample
ValueError: (InvalidArgument) Input(Index).shape[-1] should be no greater than Input(X).rank
[Hint: Expected index_dims[index_dims_size - 1] <= x_dims_size, but received index_dims[index_dims_size - 1]:2 > x_dims_size:1.] (at /paddle/paddle/fluid/operators/gather_nd_op.cc:47)
[Hint: If you need C++ stacktraces for debugging, please set FLAGS_call_stack_level=2.]
[operator < gather_nd > error]

无法计算加权交叉熵。

rjzwgtxy

rjzwgtxy9#

问题已经收到,我们会尽快处理,谢谢

kgsdhlau

kgsdhlau10#

您好,这个问题在2.0正式版里面似乎还存在。或者有别的计算加权交叉熵的方式嘛?

nr9pn0ug

nr9pn0ug11#

已建立相关卡片,有专门同学在处理中。 @cjt222 @程军

相关问题