Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always get the correct 0. #3

Open
sxzy opened this issue Oct 25, 2018 · 2 comments
Open

Always get the correct 0. #3

sxzy opened this issue Oct 25, 2018 · 2 comments

Comments

@sxzy
Copy link

sxzy commented Oct 25, 2018

HI I have tried to implement your code in my experiment on UCF101.
but I did't get any improvement and always get the zero correct.
It is a little strange.
my code
``
def train_1epoch(self):
print('==> Epoch:[{0}/{1}][training stage]'.format(self.epoch, self.nb_epochs))
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter() #switch to train mode
self.model.train()
end = time.time()
train_loss = AverageMeter()
total = 0
correct = 0
# mini-batch training
progress = tqdm(self.train_loader)
for i, (data_dict,label) in enumerate(progress):

       # measure data loading time
        data_time.update(time.time() - end)
       # generate mixed inputs, two one-hot label vectors and mixing coefficient 
        # transfer the label into one-hot Encoder

        # label = torch.zeros(label.shape[0], 101).scatter_(1, label.reshape(-1, 1), 1).cuda()
        # print(label.shape[0])
        label = label.cuda()
        # compute output
        output = Variable(torch.zeros(len(data_dict['img1']),101).float()).cuda()
        # print(len(data_dict['img1'])

        for i in range(len(data_dict)):
            key = 'img'+str(i)
            input_var = (data_dict[key]).cuda()
            # generate mixed inputs, two one-hot label vectors and mixing coefficient 
            input_var, label_a, label_b, lam = mixup_data(input_var, label, args.alpha, True)
            input_var, label_a, label_b = Variable(input_var), Variable(label_a), Variable(label_b)
            output += self.model(input_var)

        criterion = self.criterion
        loss = mixup_criterion(criterion, output, label_a, label_b, lam)
        # print(label_a.argmax(dim=1).data,label_b.argmax(dim=1).data)
        # loss = loss_func(criterion, output)
        # print(args.alpha, lam)
        # compute gradient and do SGD step
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

        # measure accuracy and record loss
        # train_loss += loss.data[0]
        train_loss.update(loss.data[0], data_dict[key].size(0))
        # print(loss.data[0])
        _, predicted = torch.max(output.data, 1)
        total += label.size(0)
        # print(label.size(0))
        correct += lam * predicted.eq(label_a.data).cpu().sum() + (1 - lam) * predicted.eq(label_b.data).cpu().sum()
        # print(predicted.eq(label_a.argmax(dim=1).data).cpu().sum())
        # print(label_a.data)
        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()
    
    info = {'Epoch':[self.epoch],
            'Batch Time':[round(batch_time.avg,3)],
            'Data Time':[round(data_time.avg,3)],
            'Loss':[round(train_loss.avg,5)],
            'correct':[round(correct,4)],
            'Prec@1':[round(correct/total,4)],
            'Prec@5':[round(correct/total,4)],
            'lr': self.optimizer.param_groups[0]['lr'],
	        'weight-decay': args.decay
            }
    record_info(info, 'record/spatial/rgb_train.csv','train')

``
I did't know where is wrong....

@staceycy
Copy link

staceycy commented Nov 4, 2018

@sxzy I met the same problem.

I changed the line:
'correct += lam * predicted.eq(label_a.data).cpu().sum() + (1 - lam) * predicted.eq(label_b.data).cpu().sum()'
to:
'correct += lam * predicted.eq(targets_a.data).cpu().sum().item() + (1 - lam) * predicted.eq(targets_b.data).cpu().sum().item()'.

Then the accuracy becomes normal.
However, i don't know about the reason since I am new to Pytorch,

@sxzy
Copy link
Author

sxzy commented Nov 5, 2018

@sxzy I met the same problem.

I changed the line:
'correct += lam * predicted.eq(label_a.data).cpu().sum() + (1 - lam) * predicted.eq(label_b.data).cpu().sum()'
to:
'correct += lam * predicted.eq(targets_a.data).cpu().sum().item() + (1 - lam) * predicted.eq(targets_b.data).cpu().sum().item()'.

Then the accuracy becomes normal.
However, i don't know about the reason since I am new to Pytorch,
Ok. I see . I will try your method later.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants