-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathcompute_metrics.py
54 lines (39 loc) · 1.48 KB
/
compute_metrics.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
import json
from argparse import ArgumentParser
import torch
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR10
from torchvision.models import resnet18
from hparams import config
def main(args):
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
])
test_dataset = CIFAR10(root='CIFAR10/test',
train=False,
transform=transform,
download=False,
)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=config["batch_size"])
device = torch.device("cuda")
model = resnet18(pretrained=False, num_classes=10)
model.load_state_dict(torch.load("model.pt"))
model.to(device)
correct = 0.0
for test_images, test_labels in test_loader:
test_images = test_images.to(device)
test_labels = test_labels.to(device)
with torch.inference_mode():
outputs = model(test_images)
preds = torch.argmax(outputs, 1)
correct += (preds == test_labels).sum()
accuracy = correct / len(test_dataset)
with open("final_metrics.json", "w+") as f:
json.dump({"accuracy": accuracy.item()}, f)
print("\n", file=f)
if __name__ == '__main__':
parser = ArgumentParser()
args = parser.parse_args()
main(args)