-
Notifications
You must be signed in to change notification settings - Fork 0
/
pred.py
42 lines (32 loc) · 1.09 KB
/
pred.py
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
from mydata_training import MyNN, load_model
import torch
from torch.nn.functional import softmax
import cv2
from random import randint
import os
def get_samples():
imgs = []
targets = []
for _ in range(10):
folder = os.listdir("./MyData/")
k = randint(0, len(folder) - 1)
folder = folder[k]
targets.append(int(folder))
file = os.listdir(os.path.join("./MyData", folder))
file = file[randint(0, len(file) - 1)]
path = f"./MyData/{folder}/{file}"
img = cv2.imread(path)
img = torch.from_numpy(img).mean(dim=2, dtype=torch.float).reshape((1,28,28))
imgs.append(img)
return torch.stack(imgs), targets
def evaluate(model, imgs, target):
output = model(imgs)
pred = output.argmax(dim=1)
print(f"Predicted: {pred}")
print(f"Actual : {torch.tensor(target)}")
if __name__ == "__main__":
model = MyNN()
model = load_model(model=model, path="./model_parameters/nn_mydata.pth.tar")
model.eval()
imgs, target = get_samples()
evaluate(model=model, imgs=imgs, target=target)