Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
nni
Commits
7fcd5172
Unverified
Commit
7fcd5172
authored
Feb 23, 2020
by
Cjkkkk
Committed by
GitHub
Feb 23, 2020
Browse files
update examples (#2082)
parent
585b5c53
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
109 additions
and
15 deletions
+109
-15
examples/model_compress/APoZ_torch_cifar10.py
examples/model_compress/APoZ_torch_cifar10.py
+1
-1
examples/model_compress/BNN_quantizer_cifar10.py
examples/model_compress/BNN_quantizer_cifar10.py
+1
-1
examples/model_compress/DoReFaQuantizer_torch_mnist.py
examples/model_compress/DoReFaQuantizer_torch_mnist.py
+89
-0
examples/model_compress/L1_torch_cifar10.py
examples/model_compress/L1_torch_cifar10.py
+1
-1
examples/model_compress/MeanActivation_torch_cifar10.py
examples/model_compress/MeanActivation_torch_cifar10.py
+5
-3
examples/model_compress/QAT_torch_quantizer.py
examples/model_compress/QAT_torch_quantizer.py
+2
-2
examples/model_compress/fpgm_torch_mnist.py
examples/model_compress/fpgm_torch_mnist.py
+3
-2
examples/model_compress/main_torch_pruner.py
examples/model_compress/main_torch_pruner.py
+1
-1
examples/model_compress/pruning_kd.py
examples/model_compress/pruning_kd.py
+1
-1
examples/model_compress/slim_torch_cifar10.py
examples/model_compress/slim_torch_cifar10.py
+5
-3
No files found.
examples/model_compress/APoZ_torch_cifar10.py
View file @
7fcd5172
...
...
@@ -41,7 +41,7 @@ def test(model, device, test_loader):
def
main
():
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
'
cuda
'
)
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
datasets
.
CIFAR10
(
'./data.cifar10'
,
train
=
True
,
download
=
True
,
transform
=
transforms
.
Compose
([
...
...
examples/model_compress/BNN_quantizer_cifar10.py
View file @
7fcd5172
...
...
@@ -105,7 +105,7 @@ def adjust_learning_rate(optimizer, epoch):
def
main
():
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
'
cuda
'
)
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
datasets
.
CIFAR10
(
'./data.cifar10'
,
train
=
True
,
download
=
True
,
transform
=
transforms
.
Compose
([
...
...
examples/model_compress/DoReFaQuantizer_torch_mnist.py
0 → 100644
View file @
7fcd5172
import
torch
import
torch.nn.functional
as
F
from
torchvision
import
datasets
,
transforms
from
nni.compression.torch
import
DoReFaQuantizer
class
Mnist
(
torch
.
nn
.
Module
):
def
__init__
(
self
):
super
().
__init__
()
self
.
conv1
=
torch
.
nn
.
Conv2d
(
1
,
20
,
5
,
1
)
self
.
conv2
=
torch
.
nn
.
Conv2d
(
20
,
50
,
5
,
1
)
self
.
fc1
=
torch
.
nn
.
Linear
(
4
*
4
*
50
,
500
)
self
.
fc2
=
torch
.
nn
.
Linear
(
500
,
10
)
self
.
relu1
=
torch
.
nn
.
ReLU6
()
self
.
relu2
=
torch
.
nn
.
ReLU6
()
self
.
relu3
=
torch
.
nn
.
ReLU6
()
def
forward
(
self
,
x
):
x
=
self
.
relu1
(
self
.
conv1
(
x
))
x
=
F
.
max_pool2d
(
x
,
2
,
2
)
x
=
self
.
relu2
(
self
.
conv2
(
x
))
x
=
F
.
max_pool2d
(
x
,
2
,
2
)
x
=
x
.
view
(
-
1
,
4
*
4
*
50
)
x
=
self
.
relu3
(
self
.
fc1
(
x
))
x
=
self
.
fc2
(
x
)
return
F
.
log_softmax
(
x
,
dim
=
1
)
def
train
(
model
,
quantizer
,
device
,
train_loader
,
optimizer
):
model
.
train
()
for
batch_idx
,
(
data
,
target
)
in
enumerate
(
train_loader
):
data
,
target
=
data
.
to
(
device
),
target
.
to
(
device
)
optimizer
.
zero_grad
()
output
=
model
(
data
)
loss
=
F
.
nll_loss
(
output
,
target
)
loss
.
backward
()
optimizer
.
step
()
if
batch_idx
%
100
==
0
:
print
(
'{:2.0f}% Loss {}'
.
format
(
100
*
batch_idx
/
len
(
train_loader
),
loss
.
item
()))
def
test
(
model
,
device
,
test_loader
):
model
.
eval
()
test_loss
=
0
correct
=
0
with
torch
.
no_grad
():
for
data
,
target
in
test_loader
:
data
,
target
=
data
.
to
(
device
),
target
.
to
(
device
)
output
=
model
(
data
)
test_loss
+=
F
.
nll_loss
(
output
,
target
,
reduction
=
'sum'
).
item
()
pred
=
output
.
argmax
(
dim
=
1
,
keepdim
=
True
)
correct
+=
pred
.
eq
(
target
.
view_as
(
pred
)).
sum
().
item
()
test_loss
/=
len
(
test_loader
.
dataset
)
print
(
'Loss: {} Accuracy: {}%)
\n
'
.
format
(
test_loss
,
100
*
correct
/
len
(
test_loader
.
dataset
)))
def
main
():
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
"cuda"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
trans
=
transforms
.
Compose
([
transforms
.
ToTensor
(),
transforms
.
Normalize
((
0.1307
,),
(
0.3081
,))])
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
datasets
.
MNIST
(
'data'
,
train
=
True
,
download
=
True
,
transform
=
trans
),
batch_size
=
64
,
shuffle
=
True
)
test_loader
=
torch
.
utils
.
data
.
DataLoader
(
datasets
.
MNIST
(
'data'
,
train
=
False
,
transform
=
trans
),
batch_size
=
1000
,
shuffle
=
True
)
model
=
Mnist
()
model
=
model
.
to
(
device
)
configure_list
=
[{
'quant_types'
:
[
'weight'
],
'quant_bits'
:
{
'weight'
:
8
,
},
# you can just use `int` here because all `quan_types` share same bits length, see config for `ReLu6` below.
'op_types'
:[
'Conv2d'
,
'Linear'
]
}]
quantizer
=
DoReFaQuantizer
(
model
,
configure_list
)
quantizer
.
compress
()
optimizer
=
torch
.
optim
.
SGD
(
model
.
parameters
(),
lr
=
0.001
,
momentum
=
0.5
)
for
epoch
in
range
(
10
):
print
(
'# Epoch {} #'
.
format
(
epoch
))
train
(
model
,
quantizer
,
device
,
train_loader
,
optimizer
)
test
(
model
,
device
,
test_loader
)
if
__name__
==
'__main__'
:
main
()
\ No newline at end of file
examples/model_compress/L1_torch_cifar10.py
View file @
7fcd5172
...
...
@@ -41,7 +41,7 @@ def test(model, device, test_loader):
def
main
():
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
'
cuda
'
)
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
datasets
.
CIFAR10
(
'./data.cifar10'
,
train
=
True
,
download
=
True
,
transform
=
transforms
.
Compose
([
...
...
examples/model_compress/MeanActivation_torch_cifar10.py
View file @
7fcd5172
import
math
import
os
import
argparse
import
torch
import
torch.nn
as
nn
...
...
@@ -48,7 +49,7 @@ def main():
args
=
parser
.
parse_args
()
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
'
cuda
'
)
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
datasets
.
CIFAR10
(
'./data.cifar10'
,
train
=
True
,
download
=
True
,
transform
=
transforms
.
Compose
([
...
...
@@ -79,10 +80,11 @@ def main():
test
(
model
,
device
,
test_loader
)
lr_scheduler
.
step
(
epoch
)
torch
.
save
(
model
.
state_dict
(),
'vgg16_cifar10.pth'
)
else
:
assert
os
.
path
.
isfile
(
'vgg16_cifar10.pth'
),
"can not find checkpoint 'vgg16_cifar10.pth'"
model
.
load_state_dict
(
torch
.
load
(
'vgg16_cifar10.pth'
))
# Test base model accuracy
print
(
'='
*
10
+
'Test on the original model'
+
'='
*
10
)
model
.
load_state_dict
(
torch
.
load
(
'vgg16_cifar10.pth'
))
test
(
model
,
device
,
test_loader
)
# top1 = 93.51%
...
...
examples/model_compress/QAT_torch_quantizer.py
View file @
7fcd5172
...
...
@@ -56,7 +56,7 @@ def test(model, device, test_loader):
def
main
():
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
'
cpu
'
)
device
=
torch
.
device
(
"cuda"
if
torch
.
cuda
.
is_available
()
else
"
cpu
"
)
trans
=
transforms
.
Compose
([
transforms
.
ToTensor
(),
transforms
.
Normalize
((
0.1307
,),
(
0.3081
,))])
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
...
...
@@ -67,7 +67,6 @@ def main():
batch_size
=
1000
,
shuffle
=
True
)
model
=
Mnist
()
'''you can change this to DoReFaQuantizer to implement it
DoReFaQuantizer(configure_list).compress(model)
'''
...
...
@@ -86,6 +85,7 @@ def main():
quantizer
=
QAT_Quantizer
(
model
,
configure_list
)
quantizer
.
compress
()
model
.
to
(
device
)
optimizer
=
torch
.
optim
.
SGD
(
model
.
parameters
(),
lr
=
0.01
,
momentum
=
0.5
)
for
epoch
in
range
(
10
):
print
(
'# Epoch {} #'
.
format
(
epoch
))
...
...
examples/model_compress/fpgm_torch_mnist.py
View file @
7fcd5172
...
...
@@ -72,7 +72,7 @@ def test(model, device, test_loader):
def
main
():
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
'
cpu
'
)
device
=
torch
.
device
(
"cuda"
if
torch
.
cuda
.
is_available
()
else
"
cpu
"
)
trans
=
transforms
.
Compose
([
transforms
.
ToTensor
(),
transforms
.
Normalize
((
0.1307
,),
(
0.3081
,))])
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
...
...
@@ -83,6 +83,7 @@ def main():
batch_size
=
1000
,
shuffle
=
True
)
model
=
Mnist
()
model
.
to
(
device
)
model
.
print_conv_filter_sparsity
()
configure_list
=
[{
...
...
@@ -92,7 +93,7 @@ def main():
pruner
=
FPGMPruner
(
model
,
configure_list
)
pruner
.
compress
()
model
.
to
(
device
)
optimizer
=
torch
.
optim
.
SGD
(
model
.
parameters
(),
lr
=
0.01
,
momentum
=
0.5
)
for
epoch
in
range
(
10
):
pruner
.
update_epoch
(
epoch
)
...
...
examples/model_compress/main_torch_pruner.py
View file @
7fcd5172
...
...
@@ -55,7 +55,7 @@ def test(model, device, test_loader):
def
main
():
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
'
cuda
'
)
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
trans
=
transforms
.
Compose
([
transforms
.
ToTensor
(),
transforms
.
Normalize
((
0.1307
,),
(
0.3081
,))])
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
...
...
examples/model_compress/pruning_kd.py
View file @
7fcd5172
...
...
@@ -49,7 +49,7 @@ def test(model, device, test_loader):
def
main
():
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
'
cuda
'
)
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
datasets
.
CIFAR10
(
'./data.cifar10'
,
train
=
True
,
download
=
True
,
transform
=
transforms
.
Compose
([
...
...
examples/model_compress/slim_torch_cifar10.py
View file @
7fcd5172
import
math
import
os
import
argparse
import
torch
import
torch.nn
as
nn
...
...
@@ -57,7 +58,7 @@ def main():
args
=
parser
.
parse_args
()
torch
.
manual_seed
(
0
)
device
=
torch
.
device
(
'
cuda
'
)
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
datasets
.
CIFAR10
(
'./data.cifar10'
,
train
=
True
,
download
=
True
,
transform
=
transforms
.
Compose
([
...
...
@@ -90,10 +91,11 @@ def main():
train
(
model
,
device
,
train_loader
,
optimizer
,
True
)
test
(
model
,
device
,
test_loader
)
torch
.
save
(
model
.
state_dict
(),
'vgg19_cifar10.pth'
)
else
:
assert
os
.
path
.
isfile
(
'vgg19_cifar10.pth'
),
"can not find checkpoint 'vgg19_cifar10.pth'"
model
.
load_state_dict
(
torch
.
load
(
'vgg19_cifar10.pth'
))
# Test base model accuracy
print
(
'='
*
10
+
'Test the original model'
+
'='
*
10
)
model
.
load_state_dict
(
torch
.
load
(
'vgg19_cifar10.pth'
))
test
(
model
,
device
,
test_loader
)
# top1 = 93.60%
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment