import torch import torchvision from PIL import Image from torch import nn image_path = '../g.jpg' image = Image.open(image_path) print(image) transform = torchvision.transforms.Compose([torchvision.transforms.Resize((32, 32)), torchvision.transforms.ToTensor()]) image = transform(image) # 搭建神经网络 class li(nn.Module): def __init__(self): super(li, self).__init__() self.model=nn.Sequential( nn.Conv2d(3,32,5,1,2), nn.MaxPool2d(2), nn.Conv2d(32,32,5,1,2) ,nn.MaxPool2d(2) ,nn.Conv2d(32,64,5,1,2) ,nn.MaxPool2d(2) ,nn.Flatten() ,nn.Linear(64*4*4,64) ,nn.Linear(64,10) ) def forward(self,x): x=self.model(x) return x Li=li() model = torch.load(r'E:\python存储\pytorch\小土堆\li19.pth',map_location='cpu') image=torch.reshape(image,(1,3,32,32)) # 如果有dropout就写,良好代码习惯 # model.eval() with torch.no_grad(): output=model(image) print(output) a=output.argmax(1) print(output.argmax(1)) d=a.item() c={0:'airplane',1:'mobile',2:'bird',3:'cat',4:'der',5:'dog',6:'frog',7:'horse',8:'shp',9:'truck'} print(c[d])
卷积神经网络测试代码-cifar10
最新推荐文章于 2023-11-29 10:32:00 发布