-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
65 lines (49 loc) · 2.44 KB
/
test.py
File metadata and controls
65 lines (49 loc) · 2.44 KB
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 torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import torch.nn.utils.prune as prune
from network_params import get_prune_params, print_sparsity
from models import *
def evaluate_performance(path):
device="cuda" if torch.cuda.is_available() else "cpu"
criterion = nn.CrossEntropyLoss()
model = ResNet18()
model = model.to(device)
checkpoint = torch.load(path)
if device == 'cuda':
model = torch.nn.DataParallel(model)
cudnn.benchmark =True
model.load_state_dict(checkpoint["net"])
device="cuda" if torch.cuda.is_available() else "cpu"
model.eval()
test_loss = 0
correct = 0
total = 0
criterion = nn.CrossEntropyLoss()
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(testloader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
return( str(100. * correct/total))
if __name__ == '__main__':
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),])
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=256, shuffle=False, num_workers=2)
print("Accuracy for no pruning model :" + evaluate_performance("./checkpoint/ckpt.pth"))
print("Accuracy for one-shot pruning 90% sparsity :"+evaluate_performance("./checkpoint/ckpt_prune_one_shot_90.pth"))
print("Accuracy for one-shot pruning 75% sparsity :"+evaluate_performance("./checkpoint/ckpt_prune_one_shot_75.pth") )
print("Accuracy for one-shot pruning 50% sparsity :" +evaluate_performance("./checkpoint/ckpt_prune_one_shot_90.pth") )
print("Accuracy for iterative pruning 90% sparsity :"+ evaluate_performance("./checkpoint/ckpt_prune_iterative_90.pth") )
print("Accuracy for iterative pruning 75% sparsity :" +evaluate_performance("./checkpoint/ckpt_prune_iterative_75.pth") )
print("Accuracy for iterative pruning 50% sparsity :" + evaluate_performance("./checkpoint/ckpt_prune_iterative_50.pth") )