File size: 1,871 Bytes
fbf6d53 |
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 51 52 53 54 55 56 57 58 |
import torch
from torch import nn
import torchvision
from torchvision import transforms, datasets
from torch.utils.data import DataLoader
from tqdm.auto import tqdm
from pathlib import Path
from PIL import Image
test_transformer = transforms.Compose([
transforms.Resize((64,64)),
transforms.ToTensor()
])
class Tinyvgg(nn.Module):
def __init__(self):
super().__init__()
self.firstlayer = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=10, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=10, out_channels=10, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.secondlayer = nn.Sequential(
nn.Conv2d(in_channels=10, out_channels=10, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=10, out_channels=10, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(in_features=2560,out_features=2),
)
def forward(self, x):
return self.classifier(self.secondlayer(self.firstlayer(x)))
device = torch.device("cuda")
model12 = Tinyvgg()
model12 = model12.to(device)
model12.load_state_dict(torch.load("C:/pytorchprojesi/model12_weights.pth", map_location=device))
model12.eval()
with torch.inference_mode():
image_path = "C:/Users/ceyhu/Downloads/fe.png"
image = Image.open(image_path).convert('RGB') # PIL Image
image = test_transformer(image).unsqueeze(0).to(device)
output = model12(image)
prediction = torch.softmax(output,dim=1)
print(prediction)
|