Skip to content

Try to use Matlab CNN Resnet50 pre-trained model to convert ONNX to use in Pytorch #52

Open
@icui4cu0118

Description

@icui4cu0118

I first use onnx2pytorch to convert onnx to pytorch, and then use it to use the test set to get the accuracy.
This is my part of code :

model = torch.load(" .pkl")
model.eval()
for epoch in range(1) :
    testing_loss = 0.0
    testing_correct = 0.0
    zpred, ztrue = [], []
    for test_inputs, test_labels in test_data :
        test_inputs, test_labels = test_inputs.to(device), test_labels.to(device)
        output = model(test_inputs)    #error poit
        loss = criterion(output, test_labels)
        _, pred = torch.max(output.data, 1)
        testing_loss += loss.item() 
        testing_correct += torch.sum(pred == test_labels.data)
        output = (torch.max(torch.exp(output), 1)[1]).data.cpu().numpy()
        zpred.extend(output)
        testlabel = test_labels.data.cpu().numpy()
        ztrue.extend(testlabel)
    test_loss = testing_loss / len(test_loader)
    test_acc = 100 * testing_correct.cpu().numpy() / len(test_loader)

    print('Epoch is : {}, Test Loss is : {:.4f} Test Accuracy is :{:.4f}%'.format(epoch + 1, test_loss, test_acc))

But I got this error :

sum() received an invalid combination of arguments - got (Tensor, Tensor), but expected one of :
 * (Tensor input, *, torch.dtype dtype)
 * (Tensor input, tuple of ints dim, bool keepdim, *, torch.dtype dtype, Tensor out)
 * (Tensor input, tuple of names dim, bool keepdim, *, torch.dtype dtype, Tensor out)

How can I change the second tensor to the expected attribute?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions