-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluator.py
65 lines (53 loc) · 1.99 KB
/
evaluator.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
import torch
import torch.nn as nn
import torchvision.models as models
'''===============================================================
1. Title:
DLP spring 2021 Lab7 classifier
2. Purpose:
For computing the classification accruacy.
3. Details:
The model is based on ResNet18 with only chaning the
last linear layer. The model is trained on iclevr dataset
with 1 to 5 objects and the resolution is the upsampled
64x64 images from 32x32 images.
It will capture the top k highest accuracy indexes on generated
images and compare them with ground truth labels.
4. How to use
You should call eval(images, labels) and to get total accuracy.
images shape: (batch_size, 3, 64, 64)
labels shape: (batch_size, 24) where labels are one-hot vectors
e.g. [[1,1,0,...,0],[0,1,1,0,...],...]
==============================================================='''
class evaluation_model():
def __init__(self,device):
#modify the path to your own path
checkpoint = torch.load('./checkpoints/classifier_weight.pth')
self.resnet18 = models.resnet18(pretrained=False)
self.resnet18.fc = nn.Sequential(
nn.Linear(512,24),
nn.Sigmoid()
)
self.resnet18.load_state_dict(checkpoint['model'])
self.resnet18 = self.resnet18.to(device)
self.resnet18.eval()
self.classnum = 24
def compute_acc(self, out, onehot_labels):
batch_size = out.size(0)
acc = 0
total = 0
for i in range(batch_size):
k = int(onehot_labels[i].sum().item())
total += k
outv, outi = out[i].topk(k)
lv, li = onehot_labels[i].topk(k)
for j in outi:
if j in li:
acc += 1
return acc / total
def eval(self, images, labels):
with torch.no_grad():
#your image shape should be (batch, 3, 64, 64)
out = self.resnet18(images)
acc = self.compute_acc(out.cpu(), labels.cpu())
return acc