summerlvsong / Aggregation-Cross-Entropy

Aggregation Cross-Entropy for Sequence Recognition. CVPR 2019.
303 stars 60 forks source link

It doesn't work on my data, why don't you provide a pretrained model? #20

Open LJXLJXLJX opened 4 years ago

LJXLJXLJX commented 4 years ago

I pretrained a model using ctcloss and it works well. Then I loaded the weights and continued to train with the aceloss. The losses seemed to be coming down, but the test results were terrible, almost all wrong.

Here is my implementation of ACELoss.

device = torch.device("cuda:" + cfg.TRAIN.GPU_ID if torch.cuda.is_available() else "cpu")
class ACELoss(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input_, target, target_lens):
        w, bs, num_class = input_.size()
        aggragetions = torch.zeros(bs, cfg.ARCH.NUM_CLASS)
        for i in range(bs):
            idx = 0
            for j in range(target_lens[i]):
                aggragetions[i][target[idx]] += 1
                idx += 1
            aggragetions[i][0] = w - target_lens[i]
        target = aggragetions.to(device)

        input_ = input_ + 1e-10
        input_ = torch.sum(input_, 0)
        input_ = input_ / w
        target = target / w

        loss = (-torch.sum(torch.log(input_) * target)) / bs
        return loss
MiTeng0215 commented 4 years ago

@LJXLJXLJX Have you solved it?

MiTeng0215 commented 4 years ago

I pretrained a model using ctcloss and it works well. Then I loaded the weights and continued to train with the aceloss. The losses seemed to be coming down, but the test results were terrible, almost all wrong.

Here is my implementation of ACELoss.

device = torch.device("cuda:" + cfg.TRAIN.GPU_ID if torch.cuda.is_available() else "cpu")
class ACELoss(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input_, target, target_lens):
        w, bs, num_class = input_.size()
        aggragetions = torch.zeros(bs, cfg.ARCH.NUM_CLASS)
        for i in range(bs):
            idx = 0
            for j in range(target_lens[i]):
                aggragetions[i][target[idx]] += 1
                idx += 1
            aggragetions[i][0] = w - target_lens[i]
        target = aggragetions.to(device)

        input_ = input_ + 1e-10
        input_ = torch.sum(input_, 0)
        input_ = input_ / w
        target = target / w

        loss = (-torch.sum(torch.log(input_) * target)) / bs
        return loss

your idxseems wrong

LJXLJXLJX commented 4 years ago

I pretrained a model using ctcloss and it works well. Then I loaded the weights and continued to train with the aceloss. The losses seemed to be coming down, but the test results were terrible, almost all wrong. Here is my implementation of ACELoss.

device = torch.device("cuda:" + cfg.TRAIN.GPU_ID if torch.cuda.is_available() else "cpu")
class ACELoss(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input_, target, target_lens):
        w, bs, num_class = input_.size()
        aggragetions = torch.zeros(bs, cfg.ARCH.NUM_CLASS)
        for i in range(bs):
            idx = 0
            for j in range(target_lens[i]):
                aggragetions[i][target[idx]] += 1
                idx += 1
            aggragetions[i][0] = w - target_lens[i]
        target = aggragetions.to(device)

        input_ = input_ + 1e-10
        input_ = torch.sum(input_, 0)
        input_ = input_ / w
        target = target / w

        loss = (-torch.sum(torch.log(input_) * target)) / bs
        return loss

your idxseems wrong

Hello, I checked it, and I think it is not wrong.

Hubert2102 commented 4 years ago

+1,all the test results is "blank"---------

zhangfengyo commented 3 years ago

I pretrained a model using ctcloss and it works well. Then I loaded the weights and continued to train with the aceloss. The losses seemed to be coming down, but the test results were terrible, almost all wrong.

Here is my implementation of ACELoss.

device = torch.device("cuda:" + cfg.TRAIN.GPU_ID if torch.cuda.is_available() else "cpu")
class ACELoss(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input_, target, target_lens):
        w, bs, num_class = input_.size()
        aggragetions = torch.zeros(bs, cfg.ARCH.NUM_CLASS)
        for i in range(bs):
            idx = 0
            for j in range(target_lens[i]):
                aggragetions[i][target[idx]] += 1
                idx += 1
            aggragetions[i][0] = w - target_lens[i]
        target = aggragetions.to(device)

        input_ = input_ + 1e-10
        input_ = torch.sum(input_, 0)
        input_ = input_ / w
        target = target / w

        loss = (-torch.sum(torch.log(input_) * target)) / bs
        return loss

what is the difference between cfg.ARCH.NUM_CLASS and num_class? whether contain the blank?