在这里输入代码import torch
import torchvision.datasets as dataset
import torchvision.transforms as transforms
import torch.utils.data as data_utils
# net
class CNN(torch.nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv = torch.nn.Sequential(
torch.nn.Conv2d(1, 32, kernel_size=5, padding=2),
torch.nn.BatchNorm2d(32),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2)
)
self.fc = torch.nn.Linear(14 * 14 * 32, 10)
def forward(self, x):
out = self.conv(x)
out = out.view(out.size()[0], -1)
out = self.fc(out)
return out
# data
test_data = dataset.MNIST(root="mnist",
train=False,
transform=transforms.ToTensor(),
download=True)
# batchsize
test_loader = data_utils.DataLoader(dataset=test_data,
batch_size=64,
shuffle=True)
cnn = torch.load("model/model1.pkl")
# test
loss_test = 0
accuracy = 0
for i, (images, labels) in enumerate(test_loader):
outputs = cnn(images)
_, pred = outputs.max(1)
accuracy += (pred == labels).sum().item()
accuracy = accuracy / len(test_data)
print(accuracy)
D:\ana\envs\PyTorch\python.exe D:/pytorch/inference.py
Traceback (most recent call last):
File “D:/pytorch/inference.py”, line 41, in
outputs = cnn(images)
TypeError: ‘collections.OrderedDict’ object is not callable
进程已结束,退出代码为 1
出现了这个错误是怎么回事