Help with confusion matrix

username: gamebo5000
cooked: <p>I am modifying a script I download from <a href="https://github.com/pytorch/examples/blob/master/imagenet/main.py" rel="noopener nofollow ugc">https://github.com/pytorch/examples/blob/master/imagenet/main.py</a>. I am adding code design to create a confusion matrix, but I keep getting the error message <strong>ValueError: Found input variables with inconsistent numbers of samples: [225, 1]</strong></p>
<blockquote>
<p>class_names = [‘covid’, ’  normal’, ’ pnuemonia’]<br>
def validate(val_loader, model, criterion, args):<br>
batch_time = AverageMeter(‘Time’, ‘:6.3f’)<br>
losses = AverageMeter(‘Loss’, ‘:.4e’)<br>
top1 = AverageMeter(‘Acc@1’, ‘:6.2f’)<br>
top3 = AverageMeter(‘Acc@3’, ‘:6.2f’)<br>
progress = ProgressMeter(<br>
len(val_loader),<br>
[batch_time, losses, top1, top3],<br>
prefix='Test: ')</p>
<pre><code># 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
</code></pre>
</blockquote>
<p>I code I wrote from scratch I did something like this:</p>
<blockquote>
<p>with torch.no_grad():<br>
for b, (pics, names) in enumerate(test_loader):<br>
if torch.cuda.is_available():<br>
pics = pics.cuda()<br>
names = names.cuda()<br>
b+=1</p>
<h1>Apply the model</h1>
<p>y_val = MobileNet(pics)<br>
val_loss = criterion(y_val, names)</p>
<h1>Tally the number of correct predictions</h1>
<p>test_predicted = torch.max(y_val.data, 1)[1]<br>
tst_corr += (test_predicted == names).sum()<br>
test_correct.append(tst_corr)<br>
Val_accuracy = tst_corr.item()*100/(test_batch)<br>
names = names.cpu()<br>
test_predicted = test_predicted.cpu()<br>
arr = confusion_matrix(names.view(-1), test_predicted.view(-1))<br>
df_cm = pd.DataFrame(arr, class_names, class_names)<br>
plt.figure(figsize = (9,6))<br>
sn.heatmap(df_cm, annot=True, fmt=“d”, cmap=‘BuGn’)<br>
plt.xlabel(“prediction”)<br>
plt.ylabel(“True label”)<br>
plt.show();</p>
</blockquote>

username: ptrblck
cooked: <p>It seems you are trying to pass an accuracy tensor to <code>confusion_matrix</code>, while it should expect the target and prediction tensors.<br>
Could this be the issue?</p>

username: elonjigar
cooked: <p>Sounds like the shapes of your labels and predictions are not in alignment. I faced a similar problem while fitting a linear <a href="http://net-informations.com/ds/mla/default.htm" rel="noopener nofollow ugc">regression model</a> . 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.</p>
<p><code>x.reshape(-1,1)</code></p>