-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathweight_share.py
33 lines (25 loc) · 977 Bytes
/
weight_share.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
import argparse
import os
import torch
from net.models import LeNet
from net.quantization import apply_weight_sharing
import util
parser = argparse.ArgumentParser(description='This program quantizes weight by using weight sharing')
parser.add_argument('model', type=str, help='path to saved pruned model')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--output', default='saves/model_after_weight_sharing.ptmodel', type=str,
help='path to model output')
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
# Define the model
model = torch.load(args.model)
print('accuracy before weight sharing')
util.test(model, use_cuda)
# Weight sharing
apply_weight_sharing(model)
print('accuacy after weight sharing')
util.test(model, use_cuda)
# Save the new model
os.makedirs('saves', exist_ok=True)
torch.save(model, args.output)