-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathclassification_evaluation.py
79 lines (67 loc) · 3.27 KB
/
classification_evaluation.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
72
73
74
75
76
77
78
79
'''
This code is used to evaluate the classification accuracy of the trained model.
You should at least guarantee this code can run without any error on validation set.
And whether this code can run is the most important factor for grading.
We provide the remaining code, all you should do are, and you can't modify the remaining code:
1. Replace the random classifier with your trained model.(line 64-68)
2. modify the get_label function to get the predicted label.(line 18-24)(just like Leetcode solutions)
'''
from torchvision import datasets, transforms
from utils import *
from model import *
from dataset import *
from tqdm import tqdm
from pprint import pprint
import argparse
NUM_CLASSES = len(my_bidict)
# Write your code here
# And get the predicted label, which is a tensor of shape (batch_size,)
# Begin of your code
def get_label(model, model_input, device):
answer = model(model_input, device)
return answer
# End of your code
def classifier(model, data_loader, device):
model.eval()
acc_tracker = ratio_tracker()
for batch_idx, item in enumerate(tqdm(data_loader)):
model_input, categories = item
model_input = model_input.to(device)
original_label = [my_bidict[item] for item in categories]
original_label = torch.tensor(original_label, dtype=torch.int64).to(device)
answer = get_label(model, model_input, device)
correct_num = torch.sum(answer == original_label)
acc_tracker.update(correct_num.item(), model_input.shape[0])
return acc_tracker.get_ratio()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--data_dir', type=str,
default='data', help='Location for the dataset')
parser.add_argument('-b', '--batch_size', type=int,
default=32, help='Batch size for inference')
parser.add_argument('-m', '--mode', type=str,
default='validation', help='Mode for the dataset')
args = parser.parse_args()
pprint(args.__dict__)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
kwargs = {'num_workers':0, 'pin_memory':True, 'drop_last':False}
ds_transforms = transforms.Compose([transforms.Resize((32, 32)), rescaling])
dataloader = torch.utils.data.DataLoader(CPEN455Dataset(root_dir=args.data_dir,
mode = args.mode,
transform=ds_transforms),
batch_size=args.batch_size,
shuffle=True,
**kwargs)
#Write your code here
#You should replace the random classifier with your trained model
#Begin of your code
model = random_classifier(NUM_CLASSES)
#End of your code
model = model.to(device)
#Attention: the path of the model is fixed to 'models/conditional_pixelcnn.pth'
#You should save your model to this path
model.load_state_dict(torch.load('models/conditional_pixelcnn.pth'))
model.eval()
print('model parameters loaded')
acc = classifier(model = model, data_loader = dataloader, device = device)
print(f"Accuracy: {acc}")