-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_model.py
71 lines (63 loc) · 1.99 KB
/
test_model.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import torch
import numpy as np
import torch.nn as nn
from torch.nn import functional as F
from torch.utils.data import Dataset, DataLoader
import torchvision
import os
from torchvision.io import read_image
from face_mask_DataLoader import faceMaskAccSet, faceMaskTestSet, faceMaskDataSet
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
#load the model
model = torchvision.models.resnet18()
n = model.fc.in_features
model.fc = nn.Linear(n, 1)
model.load_state_dict(torch.load("weights/modelTR.pth"))
model = model.to(device)
model.eval()
data_train = faceMaskDataSet()
dataloader_train = DataLoader(data_train, batch_size=1,\
shuffle=False, num_workers=6)
data_test = faceMaskTestSet()
dataloader_test = DataLoader(data_test, batch_size=1,\
shuffle=False, num_workers=6)
data_val = faceMaskAccSet()
dataloader_val = DataLoader(data_val, batch_size=1,\
shuffle=False, num_workers=6)
with torch.no_grad():
n_correct = 0
n_total = 0
for x,y in dataloader_test:
image = x.to(device)
label = y.to(device)
output = model(image)
pred = 1 if output >= 0.5 else 0
n_total += 1
n_correct += (pred == label).item()
acc = 100.0 * n_correct / n_total
print('test accuracy = ',acc)
with torch.no_grad():
n_correct = 0
n_total = 0
for x, y in dataloader_train:
image = x.to(device)
label = y.to(device)
output = model(image)
pred = 1 if output >= 0.5 else 0
n_total += 1
n_correct += (pred == label).item()
acc = 100.0 * n_correct / n_total
print('train accuracy = ', acc)
with torch.no_grad():
n_correct = 0
n_total = 0
for x, y in dataloader_val:
image = x.to(device)
label = y.to(device)
output = model(image)
pred = 1 if output >= 0.5 else 0
n_total += 1
n_correct += (pred == label).item()
acc = 100.0 * n_correct / n_total
print('val accuracy = ', acc)