Upload loadmodel.py with huggingface_hub
Browse files- loadmodel.py +57 -0
loadmodel.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
from torch import nn
|
| 4 |
+
import torchvision
|
| 5 |
+
from torchvision import transforms, datasets
|
| 6 |
+
from torch.utils.data import DataLoader
|
| 7 |
+
from tqdm.auto import tqdm
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
test_transformer = transforms.Compose([
|
| 12 |
+
transforms.Resize((64,64)),
|
| 13 |
+
transforms.ToTensor()
|
| 14 |
+
])
|
| 15 |
+
class Tinyvgg(nn.Module):
|
| 16 |
+
def __init__(self):
|
| 17 |
+
super().__init__()
|
| 18 |
+
self.firstlayer = nn.Sequential(
|
| 19 |
+
nn.Conv2d(in_channels=3, out_channels=10, kernel_size=3, stride=1, padding=1),
|
| 20 |
+
nn.ReLU(),
|
| 21 |
+
nn.Conv2d(in_channels=10, out_channels=10, kernel_size=3, stride=1, padding=1),
|
| 22 |
+
nn.ReLU(),
|
| 23 |
+
nn.MaxPool2d(kernel_size=2, stride=2),
|
| 24 |
+
|
| 25 |
+
)
|
| 26 |
+
self.secondlayer = nn.Sequential(
|
| 27 |
+
nn.Conv2d(in_channels=10, out_channels=10, kernel_size=3, stride=1, padding=1),
|
| 28 |
+
nn.ReLU(),
|
| 29 |
+
nn.Conv2d(in_channels=10, out_channels=10, kernel_size=3, stride=1, padding=1),
|
| 30 |
+
nn.ReLU(),
|
| 31 |
+
nn.MaxPool2d(kernel_size=2, stride=2),
|
| 32 |
+
)
|
| 33 |
+
self.classifier = nn.Sequential(
|
| 34 |
+
nn.Flatten(),
|
| 35 |
+
nn.Linear(in_features=2560,out_features=2),
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
def forward(self, x):
|
| 39 |
+
return self.classifier(self.secondlayer(self.firstlayer(x)))
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
device = torch.device("cuda")
|
| 43 |
+
|
| 44 |
+
model12 = Tinyvgg()
|
| 45 |
+
model12 = model12.to(device)
|
| 46 |
+
|
| 47 |
+
model12.load_state_dict(torch.load("C:/pytorchprojesi/model12_weights.pth", map_location=device))
|
| 48 |
+
|
| 49 |
+
model12.eval()
|
| 50 |
+
with torch.inference_mode():
|
| 51 |
+
image_path = "C:/Users/ceyhu/Downloads/fe.png"
|
| 52 |
+
image = Image.open(image_path).convert('RGB') # PIL Image
|
| 53 |
+
image = test_transformer(image).unsqueeze(0).to(device)
|
| 54 |
+
output = model12(image)
|
| 55 |
+
prediction = torch.softmax(output,dim=1)
|
| 56 |
+
print(prediction)
|
| 57 |
+
|