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+=1Apply 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();