Help with confusion matrix

I am modifying a script I download from https://github.com/pytorch/examples/blob/master/imagenet/main.py. I am adding code design to create a confusion matrix, but I keep getting the error message ValueError: Found input variables with inconsistent numbers of samples: [225, 1]

class_names = [‘covid’, ’ normal’, ’ pnuemonia’]
def validate(val_loader, model, criterion, args):
batch_time = AverageMeter(‘Time’, ‘:6.3f’)
losses = AverageMeter(‘Loss’, ‘:.4e’)
top1 = AverageMeter(‘Acc@1’, ‘:6.2f’)
top3 = AverageMeter(‘Acc@3’, ‘:6.2f’)
progress = ProgressMeter(
len(val_loader),
[batch_time, losses, top1, top3],
prefix='Test: ')

# switch to evaluate mode
model.eval()

with torch.no_grad():
    end = time.time()
    for i, (images, target) in enumerate(val_loader):
        if args.gpu is not None:
            images = images.cuda(args.gpu, non_blocking=True)
        if torch.cuda.is_available():
            target = target.cuda(args.gpu, non_blocking=True)

        # compute output
        output = model(images)
        loss = criterion(output, target)

        # measure accuracy and record loss
        acc1, acc3 = accuracy(output, target, topk=(1, 5))
        losses.update(loss.item(), images.size(0))
        top1.update(acc1[0], images.size(0))
        top3.update(acc3[0], images.size(0))

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % args.print_freq == 0:
            progress.display(i)

    # TODO: this should also be done with the ProgressMeter
    print(' * Acc@1 {top1.avg:.3f} Acc@3 {top3.avg:.3f}'
          .format(top1=top1, top3=top3))
target = target.cpu()
acc3 = acc3.cpu()
confusion_matrix(target.view(-1), acc3.view(-1))
    
return top1.avg

I code I wrote from scratch I did something like this:

with torch.no_grad():
for b, (pics, names) in enumerate(test_loader):
if torch.cuda.is_available():
pics = pics.cuda()
names = names.cuda()
b+=1

Apply the model

y_val = MobileNet(pics)
val_loss = criterion(y_val, names)

Tally the number of correct predictions

test_predicted = torch.max(y_val.data, 1)[1]
tst_corr += (test_predicted == names).sum()
test_correct.append(tst_corr)
Val_accuracy = tst_corr.item()*100/(test_batch)
names = names.cpu()
test_predicted = test_predicted.cpu()
arr = confusion_matrix(names.view(-1), test_predicted.view(-1))
df_cm = pd.DataFrame(arr, class_names, class_names)
plt.figure(figsize = (9,6))
sn.heatmap(df_cm, annot=True, fmt=“d”, cmap=‘BuGn’)
plt.xlabel(“prediction”)
plt.ylabel(“True label”)
plt.show();

It seems you are trying to pass an accuracy tensor to confusion_matrix, while it should expect the target and prediction tensors.
Could this be the issue?

Sounds like the shapes of your labels and predictions are not in alignment. I faced a similar problem while fitting a linear regression model . The problem in my case was, Number of rows in X was not equal to number of rows in y. In most case, x as your feature parameter and y as your predictor. But your feature parameter should not be 1D. So check the shape of x and if it is 1D, then convert it from 1D to 2D.

x.reshape(-1,1)