import torchvision
from PIL import Image
from torch import nn
import torch
from torch import nn
class Tudui(nn.Module):
def __init__(self):
super().__init__()
self.seq1=nn.Sequential(
nn.Conv2d(in_channels=3,out_channels=32,kernel_size=5,stride=1,padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32,out_channels=32,kernel_size=5,stride=1,padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32,out_channels=64,kernel_size=5,stride=1,padding=2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(64*4*4,64),
nn.Linear(64,10)
)
def forward(self,x):
x=self.seq1(x)
return x
device=torch.device("cuda")
model=torch.load("tudui_0.pth")
print(model)
img_path="./dog.jpg"
img_dog=Image.open(img_path)
img_dog=img_dog.convert("RGB")
transform=torchvision.transforms.Compose([torchvision.transforms.Resize((32,32)),
torchvision.transforms.ToTensor()])
img_dog=transform(img_dog)
img_dog=torch.reshape(img_dog,(1,3,32,32))
img_dog=img_dog.to(device)
model.eval()
with torch.no_grad():
output=model(img_dog)
print(output)
print(output.argmax(1))