1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import torchvision.transforms from PIL import Image import torch from torch import nn
image_path = "./image/dog.png"
image = Image.open(image_path) image = image.convert("RGB") transform = torchvision.transforms.Compose([ torchvision.transforms.Resize((32, 32)), torchvision.transforms.ToTensor(), ]) image = transform(image)
class Classification10Class(nn.Module): def __init__(self): super(Classification10Class, self).__init__() self.module = 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(kernel_size=2), nn.Flatten(), nn.Linear(in_features=64 * 4 * 4, out_features=64), nn.Linear(in_features=64, out_features=10), )
def forward(self, x): x = self.module(x) return x
model = torch.load("./models_cifar/classification_gpu_29.pth", map_location=torch.device('cpu'))
image = torch.reshape(image, (1, 3, 32, 32))
model.eval() with torch.no_grad(): outputs = model(image)
print(outputs) print(outputs.argmax(axis=1))
|