Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
nni
Commits
8314d6ee
Commit
8314d6ee
authored
Sep 07, 2018
by
Deshui Yu
Committed by
fishyds
Sep 07, 2018
Browse files
Merge from dogfood branch to master
parent
98530fd2
Changes
103
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
1595 additions
and
0 deletions
+1595
-0
examples/trials/pytorch_cifar10/config.yml
examples/trials/pytorch_cifar10/config.yml
+20
-0
examples/trials/pytorch_cifar10/main.py
examples/trials/pytorch_cifar10/main.py
+193
-0
examples/trials/pytorch_cifar10/models/__init__.py
examples/trials/pytorch_cifar10/models/__init__.py
+11
-0
examples/trials/pytorch_cifar10/models/densenet.py
examples/trials/pytorch_cifar10/models/densenet.py
+107
-0
examples/trials/pytorch_cifar10/models/dpn.py
examples/trials/pytorch_cifar10/models/dpn.py
+98
-0
examples/trials/pytorch_cifar10/models/googlenet.py
examples/trials/pytorch_cifar10/models/googlenet.py
+107
-0
examples/trials/pytorch_cifar10/models/lenet.py
examples/trials/pytorch_cifar10/models/lenet.py
+23
-0
examples/trials/pytorch_cifar10/models/mobilenet.py
examples/trials/pytorch_cifar10/models/mobilenet.py
+61
-0
examples/trials/pytorch_cifar10/models/mobilenetv2.py
examples/trials/pytorch_cifar10/models/mobilenetv2.py
+86
-0
examples/trials/pytorch_cifar10/models/pnasnet.py
examples/trials/pytorch_cifar10/models/pnasnet.py
+125
-0
examples/trials/pytorch_cifar10/models/preact_resnet.py
examples/trials/pytorch_cifar10/models/preact_resnet.py
+118
-0
examples/trials/pytorch_cifar10/models/resnet.py
examples/trials/pytorch_cifar10/models/resnet.py
+121
-0
examples/trials/pytorch_cifar10/models/resnext.py
examples/trials/pytorch_cifar10/models/resnext.py
+95
-0
examples/trials/pytorch_cifar10/models/senet.py
examples/trials/pytorch_cifar10/models/senet.py
+121
-0
examples/trials/pytorch_cifar10/models/shufflenet.py
examples/trials/pytorch_cifar10/models/shufflenet.py
+109
-0
examples/trials/pytorch_cifar10/models/vgg.py
examples/trials/pytorch_cifar10/models/vgg.py
+47
-0
examples/trials/pytorch_cifar10/requirement.txt
examples/trials/pytorch_cifar10/requirement.txt
+6
-0
examples/trials/pytorch_cifar10/search_space.json
examples/trials/pytorch_cifar10/search_space.json
+5
-0
examples/trials/pytorch_cifar10/utils.py
examples/trials/pytorch_cifar10/utils.py
+127
-0
examples/tuners/ga_customer_tuner/README.md
examples/tuners/ga_customer_tuner/README.md
+15
-0
No files found.
examples/trials/pytorch_cifar10/config.yml
0 → 100644
View file @
8314d6ee
authorName
:
default
experimentName
:
example_pytorch_cifar10
trialConcurrency
:
1
maxExecDuration
:
100h
maxTrialNum
:
1
#choice: local, remote
trainingServicePlatform
:
local
searchSpacePath
:
~/nni/examples/trials/pytorch_cifar10/search_space.json
#choice: true, false
useAnnotation
:
false
tuner
:
#choice: TPE, Random, Anneal, Evolution
builtinTunerName
:
TPE
classArgs
:
#choice: maximize, minimize
optimize_mode
:
maximize
trial
:
command
:
python3 main.py
codeDir
:
~/nni/examples/trials/pytorch_cifar10
gpuNum
:
1
examples/trials/pytorch_cifar10/main.py
0 → 100644
View file @
8314d6ee
'''Train CIFAR10 with PyTorch.'''
from
__future__
import
print_function
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
os
import
argparse
import
logging
from
models
import
*
from
utils
import
progress_bar
import
nni
_logger
=
logging
.
getLogger
(
"cifar10_pytorch_automl"
)
trainloader
=
None
testloader
=
None
net
=
None
criterion
=
None
optimizer
=
None
device
=
'cuda'
if
torch
.
cuda
.
is_available
()
else
'cpu'
best_acc
=
0.0
# best test accuracy
start_epoch
=
0
# start from epoch 0 or last checkpoint epoch
def
prepare
(
args
):
global
trainloader
global
testloader
global
net
global
criterion
global
optimizer
# Data
print
(
'==> Preparing data..'
)
transform_train
=
transforms
.
Compose
([
transforms
.
RandomCrop
(
32
,
padding
=
4
),
transforms
.
RandomHorizontalFlip
(),
transforms
.
ToTensor
(),
transforms
.
Normalize
((
0.4914
,
0.4822
,
0.4465
),
(
0.2023
,
0.1994
,
0.2010
)),
])
transform_test
=
transforms
.
Compose
([
transforms
.
ToTensor
(),
transforms
.
Normalize
((
0.4914
,
0.4822
,
0.4465
),
(
0.2023
,
0.1994
,
0.2010
)),
])
trainset
=
torchvision
.
datasets
.
CIFAR10
(
root
=
'./data'
,
train
=
True
,
download
=
True
,
transform
=
transform_train
)
trainloader
=
torch
.
utils
.
data
.
DataLoader
(
trainset
,
batch_size
=
128
,
shuffle
=
True
,
num_workers
=
2
)
testset
=
torchvision
.
datasets
.
CIFAR10
(
root
=
'./data'
,
train
=
False
,
download
=
True
,
transform
=
transform_test
)
testloader
=
torch
.
utils
.
data
.
DataLoader
(
testset
,
batch_size
=
100
,
shuffle
=
False
,
num_workers
=
2
)
#classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# Model
print
(
'==> Building model..'
)
if
args
[
'model'
]
==
'vgg'
:
net
=
VGG
(
'VGG19'
)
if
args
[
'model'
]
==
'resnet18'
:
net
=
ResNet18
()
if
args
[
'model'
]
==
'googlenet'
:
net
=
GoogLeNet
()
if
args
[
'model'
]
==
'densenet121'
:
net
=
DenseNet121
()
if
args
[
'model'
]
==
'mobilenet'
:
net
=
MobileNet
()
if
args
[
'model'
]
==
'dpn92'
:
net
=
DPN92
()
if
args
[
'model'
]
==
'shufflenetg2'
:
net
=
ShuffleNetG2
()
if
args
[
'model'
]
==
'senet18'
:
net
=
SENet18
()
net
=
net
.
to
(
device
)
if
device
==
'cuda'
:
net
=
torch
.
nn
.
DataParallel
(
net
)
cudnn
.
benchmark
=
True
criterion
=
nn
.
CrossEntropyLoss
()
#optimizer = optim.SGD(net.parameters(), lr=args['lr'], momentum=0.9, weight_decay=5e-4)
if
args
[
'optimizer'
]
==
'SGD'
:
optimizer
=
optim
.
SGD
(
net
.
parameters
(),
lr
=
args
[
'lr'
],
momentum
=
0.9
,
weight_decay
=
5e-4
)
if
args
[
'optimizer'
]
==
'Adadelta'
:
optimizer
=
optim
.
Adadelta
(
net
.
parameters
(),
lr
=
args
[
'lr'
])
if
args
[
'optimizer'
]
==
'Adagrad'
:
optimizer
=
optim
.
Adagrad
(
net
.
parameters
(),
lr
=
args
[
'lr'
])
if
args
[
'optimizer'
]
==
'Adam'
:
optimizer
=
optim
.
Adam
(
net
.
parameters
(),
lr
=
args
[
'lr'
])
if
args
[
'optimizer'
]
==
'Adamax'
:
optimizer
=
optim
.
Adam
(
net
.
parameters
(),
lr
=
args
[
'lr'
])
# Training
def
train
(
epoch
):
global
trainloader
global
testloader
global
net
global
criterion
global
optimizer
print
(
'
\n
Epoch: %d'
%
epoch
)
net
.
train
()
train_loss
=
0
correct
=
0
total
=
0
for
batch_idx
,
(
inputs
,
targets
)
in
enumerate
(
trainloader
):
inputs
,
targets
=
inputs
.
to
(
device
),
targets
.
to
(
device
)
optimizer
.
zero_grad
()
outputs
=
net
(
inputs
)
loss
=
criterion
(
outputs
,
targets
)
loss
.
backward
()
optimizer
.
step
()
train_loss
+=
loss
.
item
()
_
,
predicted
=
outputs
.
max
(
1
)
total
+=
targets
.
size
(
0
)
correct
+=
predicted
.
eq
(
targets
).
sum
().
item
()
acc
=
100.
*
correct
/
total
progress_bar
(
batch_idx
,
len
(
trainloader
),
'Loss: %.3f | Acc: %.3f%% (%d/%d)'
%
(
train_loss
/
(
batch_idx
+
1
),
100.
*
correct
/
total
,
correct
,
total
))
def
test
(
epoch
):
global
best_acc
global
trainloader
global
testloader
global
net
global
criterion
global
optimizer
net
.
eval
()
test_loss
=
0
correct
=
0
total
=
0
with
torch
.
no_grad
():
for
batch_idx
,
(
inputs
,
targets
)
in
enumerate
(
testloader
):
inputs
,
targets
=
inputs
.
to
(
device
),
targets
.
to
(
device
)
outputs
=
net
(
inputs
)
loss
=
criterion
(
outputs
,
targets
)
test_loss
+=
loss
.
item
()
_
,
predicted
=
outputs
.
max
(
1
)
total
+=
targets
.
size
(
0
)
correct
+=
predicted
.
eq
(
targets
).
sum
().
item
()
acc
=
100.
*
correct
/
total
progress_bar
(
batch_idx
,
len
(
testloader
),
'Loss: %.3f | Acc: %.3f%% (%d/%d)'
%
(
test_loss
/
(
batch_idx
+
1
),
100.
*
correct
/
total
,
correct
,
total
))
# Save checkpoint.
acc
=
100.
*
correct
/
total
if
acc
>
best_acc
:
print
(
'Saving..'
)
state
=
{
'net'
:
net
.
state_dict
(),
'acc'
:
acc
,
'epoch'
:
epoch
,
}
if
not
os
.
path
.
isdir
(
'checkpoint'
):
os
.
mkdir
(
'checkpoint'
)
torch
.
save
(
state
,
'./checkpoint/ckpt.t7'
)
best_acc
=
acc
return
acc
,
best_acc
if
__name__
==
'__main__'
:
try
:
RCV_CONFIG
=
nni
.
get_parameters
()
#RCV_CONFIG = {'lr': 0.1, 'optimizer': 'Adam', 'model':'senet18'}
_logger
.
debug
(
RCV_CONFIG
)
prepare
(
RCV_CONFIG
)
acc
=
0.0
best_acc
=
0.0
for
epoch
in
range
(
start_epoch
,
start_epoch
+
200
):
train
(
epoch
)
acc
,
best_acc
=
test
(
epoch
)
nni
.
report_intermediate_result
(
acc
)
nni
.
report_final_result
(
best_acc
)
except
Exception
as
exception
:
_logger
.
exception
(
exception
)
raise
examples/trials/pytorch_cifar10/models/__init__.py
0 → 100644
View file @
8314d6ee
from
.vgg
import
*
from
.densenet
import
*
from
.dpn
import
*
from
.googlenet
import
*
from
.lenet
import
*
from
.mobilenet
import
*
from
.pnasnet
import
*
from
.resnet
import
*
from
.senet
import
*
from
.shufflenet
import
*
examples/trials/pytorch_cifar10/models/densenet.py
0 → 100644
View file @
8314d6ee
'''DenseNet in PyTorch.'''
import
math
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
Bottleneck
(
nn
.
Module
):
def
__init__
(
self
,
in_planes
,
growth_rate
):
super
(
Bottleneck
,
self
).
__init__
()
self
.
bn1
=
nn
.
BatchNorm2d
(
in_planes
)
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
4
*
growth_rate
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
4
*
growth_rate
)
self
.
conv2
=
nn
.
Conv2d
(
4
*
growth_rate
,
growth_rate
,
kernel_size
=
3
,
padding
=
1
,
bias
=
False
)
def
forward
(
self
,
x
):
out
=
self
.
conv1
(
F
.
relu
(
self
.
bn1
(
x
)))
out
=
self
.
conv2
(
F
.
relu
(
self
.
bn2
(
out
)))
out
=
torch
.
cat
([
out
,
x
],
1
)
return
out
class
Transition
(
nn
.
Module
):
def
__init__
(
self
,
in_planes
,
out_planes
):
super
(
Transition
,
self
).
__init__
()
self
.
bn
=
nn
.
BatchNorm2d
(
in_planes
)
self
.
conv
=
nn
.
Conv2d
(
in_planes
,
out_planes
,
kernel_size
=
1
,
bias
=
False
)
def
forward
(
self
,
x
):
out
=
self
.
conv
(
F
.
relu
(
self
.
bn
(
x
)))
out
=
F
.
avg_pool2d
(
out
,
2
)
return
out
class
DenseNet
(
nn
.
Module
):
def
__init__
(
self
,
block
,
nblocks
,
growth_rate
=
12
,
reduction
=
0.5
,
num_classes
=
10
):
super
(
DenseNet
,
self
).
__init__
()
self
.
growth_rate
=
growth_rate
num_planes
=
2
*
growth_rate
self
.
conv1
=
nn
.
Conv2d
(
3
,
num_planes
,
kernel_size
=
3
,
padding
=
1
,
bias
=
False
)
self
.
dense1
=
self
.
_make_dense_layers
(
block
,
num_planes
,
nblocks
[
0
])
num_planes
+=
nblocks
[
0
]
*
growth_rate
out_planes
=
int
(
math
.
floor
(
num_planes
*
reduction
))
self
.
trans1
=
Transition
(
num_planes
,
out_planes
)
num_planes
=
out_planes
self
.
dense2
=
self
.
_make_dense_layers
(
block
,
num_planes
,
nblocks
[
1
])
num_planes
+=
nblocks
[
1
]
*
growth_rate
out_planes
=
int
(
math
.
floor
(
num_planes
*
reduction
))
self
.
trans2
=
Transition
(
num_planes
,
out_planes
)
num_planes
=
out_planes
self
.
dense3
=
self
.
_make_dense_layers
(
block
,
num_planes
,
nblocks
[
2
])
num_planes
+=
nblocks
[
2
]
*
growth_rate
out_planes
=
int
(
math
.
floor
(
num_planes
*
reduction
))
self
.
trans3
=
Transition
(
num_planes
,
out_planes
)
num_planes
=
out_planes
self
.
dense4
=
self
.
_make_dense_layers
(
block
,
num_planes
,
nblocks
[
3
])
num_planes
+=
nblocks
[
3
]
*
growth_rate
self
.
bn
=
nn
.
BatchNorm2d
(
num_planes
)
self
.
linear
=
nn
.
Linear
(
num_planes
,
num_classes
)
def
_make_dense_layers
(
self
,
block
,
in_planes
,
nblock
):
layers
=
[]
for
i
in
range
(
nblock
):
layers
.
append
(
block
(
in_planes
,
self
.
growth_rate
))
in_planes
+=
self
.
growth_rate
return
nn
.
Sequential
(
*
layers
)
def
forward
(
self
,
x
):
out
=
self
.
conv1
(
x
)
out
=
self
.
trans1
(
self
.
dense1
(
out
))
out
=
self
.
trans2
(
self
.
dense2
(
out
))
out
=
self
.
trans3
(
self
.
dense3
(
out
))
out
=
self
.
dense4
(
out
)
out
=
F
.
avg_pool2d
(
F
.
relu
(
self
.
bn
(
out
)),
4
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
DenseNet121
():
return
DenseNet
(
Bottleneck
,
[
6
,
12
,
24
,
16
],
growth_rate
=
32
)
def
DenseNet169
():
return
DenseNet
(
Bottleneck
,
[
6
,
12
,
32
,
32
],
growth_rate
=
32
)
def
DenseNet201
():
return
DenseNet
(
Bottleneck
,
[
6
,
12
,
48
,
32
],
growth_rate
=
32
)
def
DenseNet161
():
return
DenseNet
(
Bottleneck
,
[
6
,
12
,
36
,
24
],
growth_rate
=
48
)
def
densenet_cifar
():
return
DenseNet
(
Bottleneck
,
[
6
,
12
,
24
,
16
],
growth_rate
=
12
)
def
test
():
net
=
densenet_cifar
()
x
=
torch
.
randn
(
1
,
3
,
32
,
32
)
y
=
net
(
x
)
print
(
y
)
# test()
examples/trials/pytorch_cifar10/models/dpn.py
0 → 100644
View file @
8314d6ee
'''Dual Path Networks in PyTorch.'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
Bottleneck
(
nn
.
Module
):
def
__init__
(
self
,
last_planes
,
in_planes
,
out_planes
,
dense_depth
,
stride
,
first_layer
):
super
(
Bottleneck
,
self
).
__init__
()
self
.
out_planes
=
out_planes
self
.
dense_depth
=
dense_depth
self
.
conv1
=
nn
.
Conv2d
(
last_planes
,
in_planes
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
in_planes
)
self
.
conv2
=
nn
.
Conv2d
(
in_planes
,
in_planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
groups
=
32
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
in_planes
)
self
.
conv3
=
nn
.
Conv2d
(
in_planes
,
out_planes
+
dense_depth
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn3
=
nn
.
BatchNorm2d
(
out_planes
+
dense_depth
)
self
.
shortcut
=
nn
.
Sequential
()
if
first_layer
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
Conv2d
(
last_planes
,
out_planes
+
dense_depth
,
kernel_size
=
1
,
stride
=
stride
,
bias
=
False
),
nn
.
BatchNorm2d
(
out_planes
+
dense_depth
)
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
F
.
relu
(
self
.
bn2
(
self
.
conv2
(
out
)))
out
=
self
.
bn3
(
self
.
conv3
(
out
))
x
=
self
.
shortcut
(
x
)
d
=
self
.
out_planes
out
=
torch
.
cat
([
x
[:,:
d
,:,:]
+
out
[:,:
d
,:,:],
x
[:,
d
:,:,:],
out
[:,
d
:,:,:]],
1
)
out
=
F
.
relu
(
out
)
return
out
class
DPN
(
nn
.
Module
):
def
__init__
(
self
,
cfg
):
super
(
DPN
,
self
).
__init__
()
in_planes
,
out_planes
=
cfg
[
'in_planes'
],
cfg
[
'out_planes'
]
num_blocks
,
dense_depth
=
cfg
[
'num_blocks'
],
cfg
[
'dense_depth'
]
self
.
conv1
=
nn
.
Conv2d
(
3
,
64
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
64
)
self
.
last_planes
=
64
self
.
layer1
=
self
.
_make_layer
(
in_planes
[
0
],
out_planes
[
0
],
num_blocks
[
0
],
dense_depth
[
0
],
stride
=
1
)
self
.
layer2
=
self
.
_make_layer
(
in_planes
[
1
],
out_planes
[
1
],
num_blocks
[
1
],
dense_depth
[
1
],
stride
=
2
)
self
.
layer3
=
self
.
_make_layer
(
in_planes
[
2
],
out_planes
[
2
],
num_blocks
[
2
],
dense_depth
[
2
],
stride
=
2
)
self
.
layer4
=
self
.
_make_layer
(
in_planes
[
3
],
out_planes
[
3
],
num_blocks
[
3
],
dense_depth
[
3
],
stride
=
2
)
self
.
linear
=
nn
.
Linear
(
out_planes
[
3
]
+
(
num_blocks
[
3
]
+
1
)
*
dense_depth
[
3
],
10
)
def
_make_layer
(
self
,
in_planes
,
out_planes
,
num_blocks
,
dense_depth
,
stride
):
strides
=
[
stride
]
+
[
1
]
*
(
num_blocks
-
1
)
layers
=
[]
for
i
,
stride
in
enumerate
(
strides
):
layers
.
append
(
Bottleneck
(
self
.
last_planes
,
in_planes
,
out_planes
,
dense_depth
,
stride
,
i
==
0
))
self
.
last_planes
=
out_planes
+
(
i
+
2
)
*
dense_depth
return
nn
.
Sequential
(
*
layers
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
layer1
(
out
)
out
=
self
.
layer2
(
out
)
out
=
self
.
layer3
(
out
)
out
=
self
.
layer4
(
out
)
out
=
F
.
avg_pool2d
(
out
,
4
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
DPN26
():
cfg
=
{
'in_planes'
:
(
96
,
192
,
384
,
768
),
'out_planes'
:
(
256
,
512
,
1024
,
2048
),
'num_blocks'
:
(
2
,
2
,
2
,
2
),
'dense_depth'
:
(
16
,
32
,
24
,
128
)
}
return
DPN
(
cfg
)
def
DPN92
():
cfg
=
{
'in_planes'
:
(
96
,
192
,
384
,
768
),
'out_planes'
:
(
256
,
512
,
1024
,
2048
),
'num_blocks'
:
(
3
,
4
,
20
,
3
),
'dense_depth'
:
(
16
,
32
,
24
,
128
)
}
return
DPN
(
cfg
)
def
test
():
net
=
DPN92
()
x
=
torch
.
randn
(
1
,
3
,
32
,
32
)
y
=
net
(
x
)
print
(
y
)
# test()
examples/trials/pytorch_cifar10/models/googlenet.py
0 → 100644
View file @
8314d6ee
'''GoogLeNet with PyTorch.'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
Inception
(
nn
.
Module
):
def
__init__
(
self
,
in_planes
,
n1x1
,
n3x3red
,
n3x3
,
n5x5red
,
n5x5
,
pool_planes
):
super
(
Inception
,
self
).
__init__
()
# 1x1 conv branch
self
.
b1
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
n1x1
,
kernel_size
=
1
),
nn
.
BatchNorm2d
(
n1x1
),
nn
.
ReLU
(
True
),
)
# 1x1 conv -> 3x3 conv branch
self
.
b2
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
n3x3red
,
kernel_size
=
1
),
nn
.
BatchNorm2d
(
n3x3red
),
nn
.
ReLU
(
True
),
nn
.
Conv2d
(
n3x3red
,
n3x3
,
kernel_size
=
3
,
padding
=
1
),
nn
.
BatchNorm2d
(
n3x3
),
nn
.
ReLU
(
True
),
)
# 1x1 conv -> 5x5 conv branch
self
.
b3
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
n5x5red
,
kernel_size
=
1
),
nn
.
BatchNorm2d
(
n5x5red
),
nn
.
ReLU
(
True
),
nn
.
Conv2d
(
n5x5red
,
n5x5
,
kernel_size
=
3
,
padding
=
1
),
nn
.
BatchNorm2d
(
n5x5
),
nn
.
ReLU
(
True
),
nn
.
Conv2d
(
n5x5
,
n5x5
,
kernel_size
=
3
,
padding
=
1
),
nn
.
BatchNorm2d
(
n5x5
),
nn
.
ReLU
(
True
),
)
# 3x3 pool -> 1x1 conv branch
self
.
b4
=
nn
.
Sequential
(
nn
.
MaxPool2d
(
3
,
stride
=
1
,
padding
=
1
),
nn
.
Conv2d
(
in_planes
,
pool_planes
,
kernel_size
=
1
),
nn
.
BatchNorm2d
(
pool_planes
),
nn
.
ReLU
(
True
),
)
def
forward
(
self
,
x
):
y1
=
self
.
b1
(
x
)
y2
=
self
.
b2
(
x
)
y3
=
self
.
b3
(
x
)
y4
=
self
.
b4
(
x
)
return
torch
.
cat
([
y1
,
y2
,
y3
,
y4
],
1
)
class
GoogLeNet
(
nn
.
Module
):
def
__init__
(
self
):
super
(
GoogLeNet
,
self
).
__init__
()
self
.
pre_layers
=
nn
.
Sequential
(
nn
.
Conv2d
(
3
,
192
,
kernel_size
=
3
,
padding
=
1
),
nn
.
BatchNorm2d
(
192
),
nn
.
ReLU
(
True
),
)
self
.
a3
=
Inception
(
192
,
64
,
96
,
128
,
16
,
32
,
32
)
self
.
b3
=
Inception
(
256
,
128
,
128
,
192
,
32
,
96
,
64
)
self
.
maxpool
=
nn
.
MaxPool2d
(
3
,
stride
=
2
,
padding
=
1
)
self
.
a4
=
Inception
(
480
,
192
,
96
,
208
,
16
,
48
,
64
)
self
.
b4
=
Inception
(
512
,
160
,
112
,
224
,
24
,
64
,
64
)
self
.
c4
=
Inception
(
512
,
128
,
128
,
256
,
24
,
64
,
64
)
self
.
d4
=
Inception
(
512
,
112
,
144
,
288
,
32
,
64
,
64
)
self
.
e4
=
Inception
(
528
,
256
,
160
,
320
,
32
,
128
,
128
)
self
.
a5
=
Inception
(
832
,
256
,
160
,
320
,
32
,
128
,
128
)
self
.
b5
=
Inception
(
832
,
384
,
192
,
384
,
48
,
128
,
128
)
self
.
avgpool
=
nn
.
AvgPool2d
(
8
,
stride
=
1
)
self
.
linear
=
nn
.
Linear
(
1024
,
10
)
def
forward
(
self
,
x
):
out
=
self
.
pre_layers
(
x
)
out
=
self
.
a3
(
out
)
out
=
self
.
b3
(
out
)
out
=
self
.
maxpool
(
out
)
out
=
self
.
a4
(
out
)
out
=
self
.
b4
(
out
)
out
=
self
.
c4
(
out
)
out
=
self
.
d4
(
out
)
out
=
self
.
e4
(
out
)
out
=
self
.
maxpool
(
out
)
out
=
self
.
a5
(
out
)
out
=
self
.
b5
(
out
)
out
=
self
.
avgpool
(
out
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
test
():
net
=
GoogLeNet
()
x
=
torch
.
randn
(
1
,
3
,
32
,
32
)
y
=
net
(
x
)
print
(
y
.
size
())
# test()
examples/trials/pytorch_cifar10/models/lenet.py
0 → 100644
View file @
8314d6ee
'''LeNet in PyTorch.'''
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
LeNet
(
nn
.
Module
):
def
__init__
(
self
):
super
(
LeNet
,
self
).
__init__
()
self
.
conv1
=
nn
.
Conv2d
(
3
,
6
,
5
)
self
.
conv2
=
nn
.
Conv2d
(
6
,
16
,
5
)
self
.
fc1
=
nn
.
Linear
(
16
*
5
*
5
,
120
)
self
.
fc2
=
nn
.
Linear
(
120
,
84
)
self
.
fc3
=
nn
.
Linear
(
84
,
10
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
conv1
(
x
))
out
=
F
.
max_pool2d
(
out
,
2
)
out
=
F
.
relu
(
self
.
conv2
(
out
))
out
=
F
.
max_pool2d
(
out
,
2
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
F
.
relu
(
self
.
fc1
(
out
))
out
=
F
.
relu
(
self
.
fc2
(
out
))
out
=
self
.
fc3
(
out
)
return
out
examples/trials/pytorch_cifar10/models/mobilenet.py
0 → 100644
View file @
8314d6ee
'''MobileNet in PyTorch.
See the paper "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
for more details.
'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
Block
(
nn
.
Module
):
'''Depthwise conv + Pointwise conv'''
def
__init__
(
self
,
in_planes
,
out_planes
,
stride
=
1
):
super
(
Block
,
self
).
__init__
()
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
in_planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
groups
=
in_planes
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
in_planes
)
self
.
conv2
=
nn
.
Conv2d
(
in_planes
,
out_planes
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
out_planes
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
F
.
relu
(
self
.
bn2
(
self
.
conv2
(
out
)))
return
out
class
MobileNet
(
nn
.
Module
):
# (128,2) means conv planes=128, conv stride=2, by default conv stride=1
cfg
=
[
64
,
(
128
,
2
),
128
,
(
256
,
2
),
256
,
(
512
,
2
),
512
,
512
,
512
,
512
,
512
,
(
1024
,
2
),
1024
]
def
__init__
(
self
,
num_classes
=
10
):
super
(
MobileNet
,
self
).
__init__
()
self
.
conv1
=
nn
.
Conv2d
(
3
,
32
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
32
)
self
.
layers
=
self
.
_make_layers
(
in_planes
=
32
)
self
.
linear
=
nn
.
Linear
(
1024
,
num_classes
)
def
_make_layers
(
self
,
in_planes
):
layers
=
[]
for
x
in
self
.
cfg
:
out_planes
=
x
if
isinstance
(
x
,
int
)
else
x
[
0
]
stride
=
1
if
isinstance
(
x
,
int
)
else
x
[
1
]
layers
.
append
(
Block
(
in_planes
,
out_planes
,
stride
))
in_planes
=
out_planes
return
nn
.
Sequential
(
*
layers
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
layers
(
out
)
out
=
F
.
avg_pool2d
(
out
,
2
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
test
():
net
=
MobileNet
()
x
=
torch
.
randn
(
1
,
3
,
32
,
32
)
y
=
net
(
x
)
print
(
y
.
size
())
# test()
examples/trials/pytorch_cifar10/models/mobilenetv2.py
0 → 100644
View file @
8314d6ee
'''MobileNetV2 in PyTorch.
See the paper "Inverted Residuals and Linear Bottlenecks:
Mobile Networks for Classification, Detection and Segmentation" for more details.
'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
Block
(
nn
.
Module
):
'''expand + depthwise + pointwise'''
def
__init__
(
self
,
in_planes
,
out_planes
,
expansion
,
stride
):
super
(
Block
,
self
).
__init__
()
self
.
stride
=
stride
planes
=
expansion
*
in_planes
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
planes
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv2
=
nn
.
Conv2d
(
planes
,
planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
groups
=
planes
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv3
=
nn
.
Conv2d
(
planes
,
out_planes
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
bias
=
False
)
self
.
bn3
=
nn
.
BatchNorm2d
(
out_planes
)
self
.
shortcut
=
nn
.
Sequential
()
if
stride
==
1
and
in_planes
!=
out_planes
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
out_planes
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
bias
=
False
),
nn
.
BatchNorm2d
(
out_planes
),
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
F
.
relu
(
self
.
bn2
(
self
.
conv2
(
out
)))
out
=
self
.
bn3
(
self
.
conv3
(
out
))
out
=
out
+
self
.
shortcut
(
x
)
if
self
.
stride
==
1
else
out
return
out
class
MobileNetV2
(
nn
.
Module
):
# (expansion, out_planes, num_blocks, stride)
cfg
=
[(
1
,
16
,
1
,
1
),
(
6
,
24
,
2
,
1
),
# NOTE: change stride 2 -> 1 for CIFAR10
(
6
,
32
,
3
,
2
),
(
6
,
64
,
4
,
2
),
(
6
,
96
,
3
,
1
),
(
6
,
160
,
3
,
2
),
(
6
,
320
,
1
,
1
)]
def
__init__
(
self
,
num_classes
=
10
):
super
(
MobileNetV2
,
self
).
__init__
()
# NOTE: change conv1 stride 2 -> 1 for CIFAR10
self
.
conv1
=
nn
.
Conv2d
(
3
,
32
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
32
)
self
.
layers
=
self
.
_make_layers
(
in_planes
=
32
)
self
.
conv2
=
nn
.
Conv2d
(
320
,
1280
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
1280
)
self
.
linear
=
nn
.
Linear
(
1280
,
num_classes
)
def
_make_layers
(
self
,
in_planes
):
layers
=
[]
for
expansion
,
out_planes
,
num_blocks
,
stride
in
self
.
cfg
:
strides
=
[
stride
]
+
[
1
]
*
(
num_blocks
-
1
)
for
stride
in
strides
:
layers
.
append
(
Block
(
in_planes
,
out_planes
,
expansion
,
stride
))
in_planes
=
out_planes
return
nn
.
Sequential
(
*
layers
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
layers
(
out
)
out
=
F
.
relu
(
self
.
bn2
(
self
.
conv2
(
out
)))
# NOTE: change pooling kernel_size 7 -> 4 for CIFAR10
out
=
F
.
avg_pool2d
(
out
,
4
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
test
():
net
=
MobileNetV2
()
x
=
torch
.
randn
(
2
,
3
,
32
,
32
)
y
=
net
(
x
)
print
(
y
.
size
())
# test()
examples/trials/pytorch_cifar10/models/pnasnet.py
0 → 100644
View file @
8314d6ee
'''PNASNet in PyTorch.
Paper: Progressive Neural Architecture Search
'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
SepConv
(
nn
.
Module
):
'''Separable Convolution.'''
def
__init__
(
self
,
in_planes
,
out_planes
,
kernel_size
,
stride
):
super
(
SepConv
,
self
).
__init__
()
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
out_planes
,
kernel_size
,
stride
,
padding
=
(
kernel_size
-
1
)
//
2
,
bias
=
False
,
groups
=
in_planes
)
self
.
bn1
=
nn
.
BatchNorm2d
(
out_planes
)
def
forward
(
self
,
x
):
return
self
.
bn1
(
self
.
conv1
(
x
))
class
CellA
(
nn
.
Module
):
def
__init__
(
self
,
in_planes
,
out_planes
,
stride
=
1
):
super
(
CellA
,
self
).
__init__
()
self
.
stride
=
stride
self
.
sep_conv1
=
SepConv
(
in_planes
,
out_planes
,
kernel_size
=
7
,
stride
=
stride
)
if
stride
==
2
:
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
out_planes
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
out_planes
)
def
forward
(
self
,
x
):
y1
=
self
.
sep_conv1
(
x
)
y2
=
F
.
max_pool2d
(
x
,
kernel_size
=
3
,
stride
=
self
.
stride
,
padding
=
1
)
if
self
.
stride
==
2
:
y2
=
self
.
bn1
(
self
.
conv1
(
y2
))
return
F
.
relu
(
y1
+
y2
)
class
CellB
(
nn
.
Module
):
def
__init__
(
self
,
in_planes
,
out_planes
,
stride
=
1
):
super
(
CellB
,
self
).
__init__
()
self
.
stride
=
stride
# Left branch
self
.
sep_conv1
=
SepConv
(
in_planes
,
out_planes
,
kernel_size
=
7
,
stride
=
stride
)
self
.
sep_conv2
=
SepConv
(
in_planes
,
out_planes
,
kernel_size
=
3
,
stride
=
stride
)
# Right branch
self
.
sep_conv3
=
SepConv
(
in_planes
,
out_planes
,
kernel_size
=
5
,
stride
=
stride
)
if
stride
==
2
:
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
out_planes
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
out_planes
)
# Reduce channels
self
.
conv2
=
nn
.
Conv2d
(
2
*
out_planes
,
out_planes
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
out_planes
)
def
forward
(
self
,
x
):
# Left branch
y1
=
self
.
sep_conv1
(
x
)
y2
=
self
.
sep_conv2
(
x
)
# Right branch
y3
=
F
.
max_pool2d
(
x
,
kernel_size
=
3
,
stride
=
self
.
stride
,
padding
=
1
)
if
self
.
stride
==
2
:
y3
=
self
.
bn1
(
self
.
conv1
(
y3
))
y4
=
self
.
sep_conv3
(
x
)
# Concat & reduce channels
b1
=
F
.
relu
(
y1
+
y2
)
b2
=
F
.
relu
(
y3
+
y4
)
y
=
torch
.
cat
([
b1
,
b2
],
1
)
return
F
.
relu
(
self
.
bn2
(
self
.
conv2
(
y
)))
class
PNASNet
(
nn
.
Module
):
def
__init__
(
self
,
cell_type
,
num_cells
,
num_planes
):
super
(
PNASNet
,
self
).
__init__
()
self
.
in_planes
=
num_planes
self
.
cell_type
=
cell_type
self
.
conv1
=
nn
.
Conv2d
(
3
,
num_planes
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
num_planes
)
self
.
layer1
=
self
.
_make_layer
(
num_planes
,
num_cells
=
6
)
self
.
layer2
=
self
.
_downsample
(
num_planes
*
2
)
self
.
layer3
=
self
.
_make_layer
(
num_planes
*
2
,
num_cells
=
6
)
self
.
layer4
=
self
.
_downsample
(
num_planes
*
4
)
self
.
layer5
=
self
.
_make_layer
(
num_planes
*
4
,
num_cells
=
6
)
self
.
linear
=
nn
.
Linear
(
num_planes
*
4
,
10
)
def
_make_layer
(
self
,
planes
,
num_cells
):
layers
=
[]
for
_
in
range
(
num_cells
):
layers
.
append
(
self
.
cell_type
(
self
.
in_planes
,
planes
,
stride
=
1
))
self
.
in_planes
=
planes
return
nn
.
Sequential
(
*
layers
)
def
_downsample
(
self
,
planes
):
layer
=
self
.
cell_type
(
self
.
in_planes
,
planes
,
stride
=
2
)
self
.
in_planes
=
planes
return
layer
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
layer1
(
out
)
out
=
self
.
layer2
(
out
)
out
=
self
.
layer3
(
out
)
out
=
self
.
layer4
(
out
)
out
=
self
.
layer5
(
out
)
out
=
F
.
avg_pool2d
(
out
,
8
)
out
=
self
.
linear
(
out
.
view
(
out
.
size
(
0
),
-
1
))
return
out
def
PNASNetA
():
return
PNASNet
(
CellA
,
num_cells
=
6
,
num_planes
=
44
)
def
PNASNetB
():
return
PNASNet
(
CellB
,
num_cells
=
6
,
num_planes
=
32
)
def
test
():
net
=
PNASNetB
()
x
=
torch
.
randn
(
1
,
3
,
32
,
32
)
y
=
net
(
x
)
print
(
y
)
# test()
examples/trials/pytorch_cifar10/models/preact_resnet.py
0 → 100644
View file @
8314d6ee
'''Pre-activation ResNet in PyTorch.
Reference:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Identity Mappings in Deep Residual Networks. arXiv:1603.05027
'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
PreActBlock
(
nn
.
Module
):
'''Pre-activation version of the BasicBlock.'''
expansion
=
1
def
__init__
(
self
,
in_planes
,
planes
,
stride
=
1
):
super
(
PreActBlock
,
self
).
__init__
()
self
.
bn1
=
nn
.
BatchNorm2d
(
in_planes
)
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv2
=
nn
.
Conv2d
(
planes
,
planes
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
if
stride
!=
1
or
in_planes
!=
self
.
expansion
*
planes
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
self
.
expansion
*
planes
,
kernel_size
=
1
,
stride
=
stride
,
bias
=
False
)
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
x
))
shortcut
=
self
.
shortcut
(
out
)
if
hasattr
(
self
,
'shortcut'
)
else
x
out
=
self
.
conv1
(
out
)
out
=
self
.
conv2
(
F
.
relu
(
self
.
bn2
(
out
)))
out
+=
shortcut
return
out
class
PreActBottleneck
(
nn
.
Module
):
'''Pre-activation version of the original Bottleneck module.'''
expansion
=
4
def
__init__
(
self
,
in_planes
,
planes
,
stride
=
1
):
super
(
PreActBottleneck
,
self
).
__init__
()
self
.
bn1
=
nn
.
BatchNorm2d
(
in_planes
)
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
planes
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv2
=
nn
.
Conv2d
(
planes
,
planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
bias
=
False
)
self
.
bn3
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv3
=
nn
.
Conv2d
(
planes
,
self
.
expansion
*
planes
,
kernel_size
=
1
,
bias
=
False
)
if
stride
!=
1
or
in_planes
!=
self
.
expansion
*
planes
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
self
.
expansion
*
planes
,
kernel_size
=
1
,
stride
=
stride
,
bias
=
False
)
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
x
))
shortcut
=
self
.
shortcut
(
out
)
if
hasattr
(
self
,
'shortcut'
)
else
x
out
=
self
.
conv1
(
out
)
out
=
self
.
conv2
(
F
.
relu
(
self
.
bn2
(
out
)))
out
=
self
.
conv3
(
F
.
relu
(
self
.
bn3
(
out
)))
out
+=
shortcut
return
out
class
PreActResNet
(
nn
.
Module
):
def
__init__
(
self
,
block
,
num_blocks
,
num_classes
=
10
):
super
(
PreActResNet
,
self
).
__init__
()
self
.
in_planes
=
64
self
.
conv1
=
nn
.
Conv2d
(
3
,
64
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
self
.
layer1
=
self
.
_make_layer
(
block
,
64
,
num_blocks
[
0
],
stride
=
1
)
self
.
layer2
=
self
.
_make_layer
(
block
,
128
,
num_blocks
[
1
],
stride
=
2
)
self
.
layer3
=
self
.
_make_layer
(
block
,
256
,
num_blocks
[
2
],
stride
=
2
)
self
.
layer4
=
self
.
_make_layer
(
block
,
512
,
num_blocks
[
3
],
stride
=
2
)
self
.
linear
=
nn
.
Linear
(
512
*
block
.
expansion
,
num_classes
)
def
_make_layer
(
self
,
block
,
planes
,
num_blocks
,
stride
):
strides
=
[
stride
]
+
[
1
]
*
(
num_blocks
-
1
)
layers
=
[]
for
stride
in
strides
:
layers
.
append
(
block
(
self
.
in_planes
,
planes
,
stride
))
self
.
in_planes
=
planes
*
block
.
expansion
return
nn
.
Sequential
(
*
layers
)
def
forward
(
self
,
x
):
out
=
self
.
conv1
(
x
)
out
=
self
.
layer1
(
out
)
out
=
self
.
layer2
(
out
)
out
=
self
.
layer3
(
out
)
out
=
self
.
layer4
(
out
)
out
=
F
.
avg_pool2d
(
out
,
4
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
PreActResNet18
():
return
PreActResNet
(
PreActBlock
,
[
2
,
2
,
2
,
2
])
def
PreActResNet34
():
return
PreActResNet
(
PreActBlock
,
[
3
,
4
,
6
,
3
])
def
PreActResNet50
():
return
PreActResNet
(
PreActBottleneck
,
[
3
,
4
,
6
,
3
])
def
PreActResNet101
():
return
PreActResNet
(
PreActBottleneck
,
[
3
,
4
,
23
,
3
])
def
PreActResNet152
():
return
PreActResNet
(
PreActBottleneck
,
[
3
,
8
,
36
,
3
])
def
test
():
net
=
PreActResNet18
()
y
=
net
((
torch
.
randn
(
1
,
3
,
32
,
32
)))
print
(
y
.
size
())
# test()
examples/trials/pytorch_cifar10/models/resnet.py
0 → 100644
View file @
8314d6ee
'''ResNet in PyTorch.
For Pre-activation ResNet, see 'preact_resnet.py'.
Reference:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Deep Residual Learning for Image Recognition. arXiv:1512.03385
'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
BasicBlock
(
nn
.
Module
):
expansion
=
1
def
__init__
(
self
,
in_planes
,
planes
,
stride
=
1
):
super
(
BasicBlock
,
self
).
__init__
()
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv2
=
nn
.
Conv2d
(
planes
,
planes
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
planes
)
self
.
shortcut
=
nn
.
Sequential
()
if
stride
!=
1
or
in_planes
!=
self
.
expansion
*
planes
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
self
.
expansion
*
planes
,
kernel_size
=
1
,
stride
=
stride
,
bias
=
False
),
nn
.
BatchNorm2d
(
self
.
expansion
*
planes
)
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
bn2
(
self
.
conv2
(
out
))
out
+=
self
.
shortcut
(
x
)
out
=
F
.
relu
(
out
)
return
out
class
Bottleneck
(
nn
.
Module
):
expansion
=
4
def
__init__
(
self
,
in_planes
,
planes
,
stride
=
1
):
super
(
Bottleneck
,
self
).
__init__
()
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
planes
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv2
=
nn
.
Conv2d
(
planes
,
planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv3
=
nn
.
Conv2d
(
planes
,
self
.
expansion
*
planes
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn3
=
nn
.
BatchNorm2d
(
self
.
expansion
*
planes
)
self
.
shortcut
=
nn
.
Sequential
()
if
stride
!=
1
or
in_planes
!=
self
.
expansion
*
planes
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
self
.
expansion
*
planes
,
kernel_size
=
1
,
stride
=
stride
,
bias
=
False
),
nn
.
BatchNorm2d
(
self
.
expansion
*
planes
)
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
F
.
relu
(
self
.
bn2
(
self
.
conv2
(
out
)))
out
=
self
.
bn3
(
self
.
conv3
(
out
))
out
+=
self
.
shortcut
(
x
)
out
=
F
.
relu
(
out
)
return
out
class
ResNet
(
nn
.
Module
):
def
__init__
(
self
,
block
,
num_blocks
,
num_classes
=
10
):
super
(
ResNet
,
self
).
__init__
()
self
.
in_planes
=
64
self
.
conv1
=
nn
.
Conv2d
(
3
,
64
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
64
)
self
.
layer1
=
self
.
_make_layer
(
block
,
64
,
num_blocks
[
0
],
stride
=
1
)
self
.
layer2
=
self
.
_make_layer
(
block
,
128
,
num_blocks
[
1
],
stride
=
2
)
self
.
layer3
=
self
.
_make_layer
(
block
,
256
,
num_blocks
[
2
],
stride
=
2
)
self
.
layer4
=
self
.
_make_layer
(
block
,
512
,
num_blocks
[
3
],
stride
=
2
)
self
.
linear
=
nn
.
Linear
(
512
*
block
.
expansion
,
num_classes
)
def
_make_layer
(
self
,
block
,
planes
,
num_blocks
,
stride
):
strides
=
[
stride
]
+
[
1
]
*
(
num_blocks
-
1
)
layers
=
[]
for
stride
in
strides
:
layers
.
append
(
block
(
self
.
in_planes
,
planes
,
stride
))
self
.
in_planes
=
planes
*
block
.
expansion
return
nn
.
Sequential
(
*
layers
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
layer1
(
out
)
out
=
self
.
layer2
(
out
)
out
=
self
.
layer3
(
out
)
out
=
self
.
layer4
(
out
)
out
=
F
.
avg_pool2d
(
out
,
4
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
ResNet18
():
return
ResNet
(
BasicBlock
,
[
2
,
2
,
2
,
2
])
def
ResNet34
():
return
ResNet
(
BasicBlock
,
[
3
,
4
,
6
,
3
])
def
ResNet50
():
return
ResNet
(
Bottleneck
,
[
3
,
4
,
6
,
3
])
def
ResNet101
():
return
ResNet
(
Bottleneck
,
[
3
,
4
,
23
,
3
])
def
ResNet152
():
return
ResNet
(
Bottleneck
,
[
3
,
8
,
36
,
3
])
def
test
():
net
=
ResNet18
()
y
=
net
(
torch
.
randn
(
1
,
3
,
32
,
32
))
print
(
y
.
size
())
# test()
examples/trials/pytorch_cifar10/models/resnext.py
0 → 100644
View file @
8314d6ee
'''ResNeXt in PyTorch.
See the paper "Aggregated Residual Transformations for Deep Neural Networks" for more details.
'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
Block
(
nn
.
Module
):
'''Grouped convolution block.'''
expansion
=
2
def
__init__
(
self
,
in_planes
,
cardinality
=
32
,
bottleneck_width
=
4
,
stride
=
1
):
super
(
Block
,
self
).
__init__
()
group_width
=
cardinality
*
bottleneck_width
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
group_width
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
group_width
)
self
.
conv2
=
nn
.
Conv2d
(
group_width
,
group_width
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
groups
=
cardinality
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
group_width
)
self
.
conv3
=
nn
.
Conv2d
(
group_width
,
self
.
expansion
*
group_width
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn3
=
nn
.
BatchNorm2d
(
self
.
expansion
*
group_width
)
self
.
shortcut
=
nn
.
Sequential
()
if
stride
!=
1
or
in_planes
!=
self
.
expansion
*
group_width
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
self
.
expansion
*
group_width
,
kernel_size
=
1
,
stride
=
stride
,
bias
=
False
),
nn
.
BatchNorm2d
(
self
.
expansion
*
group_width
)
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
F
.
relu
(
self
.
bn2
(
self
.
conv2
(
out
)))
out
=
self
.
bn3
(
self
.
conv3
(
out
))
out
+=
self
.
shortcut
(
x
)
out
=
F
.
relu
(
out
)
return
out
class
ResNeXt
(
nn
.
Module
):
def
__init__
(
self
,
num_blocks
,
cardinality
,
bottleneck_width
,
num_classes
=
10
):
super
(
ResNeXt
,
self
).
__init__
()
self
.
cardinality
=
cardinality
self
.
bottleneck_width
=
bottleneck_width
self
.
in_planes
=
64
self
.
conv1
=
nn
.
Conv2d
(
3
,
64
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
64
)
self
.
layer1
=
self
.
_make_layer
(
num_blocks
[
0
],
1
)
self
.
layer2
=
self
.
_make_layer
(
num_blocks
[
1
],
2
)
self
.
layer3
=
self
.
_make_layer
(
num_blocks
[
2
],
2
)
# self.layer4 = self._make_layer(num_blocks[3], 2)
self
.
linear
=
nn
.
Linear
(
cardinality
*
bottleneck_width
*
8
,
num_classes
)
def
_make_layer
(
self
,
num_blocks
,
stride
):
strides
=
[
stride
]
+
[
1
]
*
(
num_blocks
-
1
)
layers
=
[]
for
stride
in
strides
:
layers
.
append
(
Block
(
self
.
in_planes
,
self
.
cardinality
,
self
.
bottleneck_width
,
stride
))
self
.
in_planes
=
Block
.
expansion
*
self
.
cardinality
*
self
.
bottleneck_width
# Increase bottleneck_width by 2 after each stage.
self
.
bottleneck_width
*=
2
return
nn
.
Sequential
(
*
layers
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
layer1
(
out
)
out
=
self
.
layer2
(
out
)
out
=
self
.
layer3
(
out
)
# out = self.layer4(out)
out
=
F
.
avg_pool2d
(
out
,
8
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
ResNeXt29_2x64d
():
return
ResNeXt
(
num_blocks
=
[
3
,
3
,
3
],
cardinality
=
2
,
bottleneck_width
=
64
)
def
ResNeXt29_4x64d
():
return
ResNeXt
(
num_blocks
=
[
3
,
3
,
3
],
cardinality
=
4
,
bottleneck_width
=
64
)
def
ResNeXt29_8x64d
():
return
ResNeXt
(
num_blocks
=
[
3
,
3
,
3
],
cardinality
=
8
,
bottleneck_width
=
64
)
def
ResNeXt29_32x4d
():
return
ResNeXt
(
num_blocks
=
[
3
,
3
,
3
],
cardinality
=
32
,
bottleneck_width
=
4
)
def
test_resnext
():
net
=
ResNeXt29_2x64d
()
x
=
torch
.
randn
(
1
,
3
,
32
,
32
)
y
=
net
(
x
)
print
(
y
.
size
())
# test_resnext()
examples/trials/pytorch_cifar10/models/senet.py
0 → 100644
View file @
8314d6ee
'''SENet in PyTorch.
SENet is the winner of ImageNet-2017. The paper is not released yet.
'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
BasicBlock
(
nn
.
Module
):
def
__init__
(
self
,
in_planes
,
planes
,
stride
=
1
):
super
(
BasicBlock
,
self
).
__init__
()
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv2
=
nn
.
Conv2d
(
planes
,
planes
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
planes
)
self
.
shortcut
=
nn
.
Sequential
()
if
stride
!=
1
or
in_planes
!=
planes
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
planes
,
kernel_size
=
1
,
stride
=
stride
,
bias
=
False
),
nn
.
BatchNorm2d
(
planes
)
)
# SE layers
self
.
fc1
=
nn
.
Conv2d
(
planes
,
planes
//
16
,
kernel_size
=
1
)
# Use nn.Conv2d instead of nn.Linear
self
.
fc2
=
nn
.
Conv2d
(
planes
//
16
,
planes
,
kernel_size
=
1
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
bn2
(
self
.
conv2
(
out
))
# Squeeze
w
=
F
.
avg_pool2d
(
out
,
out
.
size
(
2
))
w
=
F
.
relu
(
self
.
fc1
(
w
))
w
=
F
.
sigmoid
(
self
.
fc2
(
w
))
# Excitation
out
=
out
*
w
# New broadcasting feature from v0.2!
out
+=
self
.
shortcut
(
x
)
out
=
F
.
relu
(
out
)
return
out
class
PreActBlock
(
nn
.
Module
):
def
__init__
(
self
,
in_planes
,
planes
,
stride
=
1
):
super
(
PreActBlock
,
self
).
__init__
()
self
.
bn1
=
nn
.
BatchNorm2d
(
in_planes
)
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
planes
)
self
.
conv2
=
nn
.
Conv2d
(
planes
,
planes
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
if
stride
!=
1
or
in_planes
!=
planes
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
Conv2d
(
in_planes
,
planes
,
kernel_size
=
1
,
stride
=
stride
,
bias
=
False
)
)
# SE layers
self
.
fc1
=
nn
.
Conv2d
(
planes
,
planes
//
16
,
kernel_size
=
1
)
self
.
fc2
=
nn
.
Conv2d
(
planes
//
16
,
planes
,
kernel_size
=
1
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
x
))
shortcut
=
self
.
shortcut
(
out
)
if
hasattr
(
self
,
'shortcut'
)
else
x
out
=
self
.
conv1
(
out
)
out
=
self
.
conv2
(
F
.
relu
(
self
.
bn2
(
out
)))
# Squeeze
w
=
F
.
avg_pool2d
(
out
,
out
.
size
(
2
))
w
=
F
.
relu
(
self
.
fc1
(
w
))
w
=
F
.
sigmoid
(
self
.
fc2
(
w
))
# Excitation
out
=
out
*
w
out
+=
shortcut
return
out
class
SENet
(
nn
.
Module
):
def
__init__
(
self
,
block
,
num_blocks
,
num_classes
=
10
):
super
(
SENet
,
self
).
__init__
()
self
.
in_planes
=
64
self
.
conv1
=
nn
.
Conv2d
(
3
,
64
,
kernel_size
=
3
,
stride
=
1
,
padding
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
64
)
self
.
layer1
=
self
.
_make_layer
(
block
,
64
,
num_blocks
[
0
],
stride
=
1
)
self
.
layer2
=
self
.
_make_layer
(
block
,
128
,
num_blocks
[
1
],
stride
=
2
)
self
.
layer3
=
self
.
_make_layer
(
block
,
256
,
num_blocks
[
2
],
stride
=
2
)
self
.
layer4
=
self
.
_make_layer
(
block
,
512
,
num_blocks
[
3
],
stride
=
2
)
self
.
linear
=
nn
.
Linear
(
512
,
num_classes
)
def
_make_layer
(
self
,
block
,
planes
,
num_blocks
,
stride
):
strides
=
[
stride
]
+
[
1
]
*
(
num_blocks
-
1
)
layers
=
[]
for
stride
in
strides
:
layers
.
append
(
block
(
self
.
in_planes
,
planes
,
stride
))
self
.
in_planes
=
planes
return
nn
.
Sequential
(
*
layers
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
layer1
(
out
)
out
=
self
.
layer2
(
out
)
out
=
self
.
layer3
(
out
)
out
=
self
.
layer4
(
out
)
out
=
F
.
avg_pool2d
(
out
,
4
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
SENet18
():
return
SENet
(
PreActBlock
,
[
2
,
2
,
2
,
2
])
def
test
():
net
=
SENet18
()
y
=
net
(
torch
.
randn
(
1
,
3
,
32
,
32
))
print
(
y
.
size
())
# test()
examples/trials/pytorch_cifar10/models/shufflenet.py
0 → 100644
View file @
8314d6ee
'''ShuffleNet in PyTorch.
See the paper "ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices" for more details.
'''
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
ShuffleBlock
(
nn
.
Module
):
def
__init__
(
self
,
groups
):
super
(
ShuffleBlock
,
self
).
__init__
()
self
.
groups
=
groups
def
forward
(
self
,
x
):
'''Channel shuffle: [N,C,H,W] -> [N,g,C/g,H,W] -> [N,C/g,g,H,w] -> [N,C,H,W]'''
N
,
C
,
H
,
W
=
x
.
size
()
g
=
self
.
groups
return
x
.
view
(
N
,
g
,
C
/
g
,
H
,
W
).
permute
(
0
,
2
,
1
,
3
,
4
).
contiguous
().
view
(
N
,
C
,
H
,
W
)
class
Bottleneck
(
nn
.
Module
):
def
__init__
(
self
,
in_planes
,
out_planes
,
stride
,
groups
):
super
(
Bottleneck
,
self
).
__init__
()
self
.
stride
=
stride
mid_planes
=
out_planes
/
4
g
=
1
if
in_planes
==
24
else
groups
self
.
conv1
=
nn
.
Conv2d
(
in_planes
,
mid_planes
,
kernel_size
=
1
,
groups
=
g
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
mid_planes
)
self
.
shuffle1
=
ShuffleBlock
(
groups
=
g
)
self
.
conv2
=
nn
.
Conv2d
(
mid_planes
,
mid_planes
,
kernel_size
=
3
,
stride
=
stride
,
padding
=
1
,
groups
=
mid_planes
,
bias
=
False
)
self
.
bn2
=
nn
.
BatchNorm2d
(
mid_planes
)
self
.
conv3
=
nn
.
Conv2d
(
mid_planes
,
out_planes
,
kernel_size
=
1
,
groups
=
groups
,
bias
=
False
)
self
.
bn3
=
nn
.
BatchNorm2d
(
out_planes
)
self
.
shortcut
=
nn
.
Sequential
()
if
stride
==
2
:
self
.
shortcut
=
nn
.
Sequential
(
nn
.
AvgPool2d
(
3
,
stride
=
2
,
padding
=
1
))
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
shuffle1
(
out
)
out
=
F
.
relu
(
self
.
bn2
(
self
.
conv2
(
out
)))
out
=
self
.
bn3
(
self
.
conv3
(
out
))
res
=
self
.
shortcut
(
x
)
out
=
F
.
relu
(
torch
.
cat
([
out
,
res
],
1
))
if
self
.
stride
==
2
else
F
.
relu
(
out
+
res
)
return
out
class
ShuffleNet
(
nn
.
Module
):
def
__init__
(
self
,
cfg
):
super
(
ShuffleNet
,
self
).
__init__
()
out_planes
=
cfg
[
'out_planes'
]
num_blocks
=
cfg
[
'num_blocks'
]
groups
=
cfg
[
'groups'
]
self
.
conv1
=
nn
.
Conv2d
(
3
,
24
,
kernel_size
=
1
,
bias
=
False
)
self
.
bn1
=
nn
.
BatchNorm2d
(
24
)
self
.
in_planes
=
24
self
.
layer1
=
self
.
_make_layer
(
out_planes
[
0
],
num_blocks
[
0
],
groups
)
self
.
layer2
=
self
.
_make_layer
(
out_planes
[
1
],
num_blocks
[
1
],
groups
)
self
.
layer3
=
self
.
_make_layer
(
out_planes
[
2
],
num_blocks
[
2
],
groups
)
self
.
linear
=
nn
.
Linear
(
out_planes
[
2
],
10
)
def
_make_layer
(
self
,
out_planes
,
num_blocks
,
groups
):
layers
=
[]
for
i
in
range
(
num_blocks
):
stride
=
2
if
i
==
0
else
1
cat_planes
=
self
.
in_planes
if
i
==
0
else
0
layers
.
append
(
Bottleneck
(
self
.
in_planes
,
out_planes
-
cat_planes
,
stride
=
stride
,
groups
=
groups
))
self
.
in_planes
=
out_planes
return
nn
.
Sequential
(
*
layers
)
def
forward
(
self
,
x
):
out
=
F
.
relu
(
self
.
bn1
(
self
.
conv1
(
x
)))
out
=
self
.
layer1
(
out
)
out
=
self
.
layer2
(
out
)
out
=
self
.
layer3
(
out
)
out
=
F
.
avg_pool2d
(
out
,
4
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
linear
(
out
)
return
out
def
ShuffleNetG2
():
cfg
=
{
'out_planes'
:
[
200
,
400
,
800
],
'num_blocks'
:
[
4
,
8
,
4
],
'groups'
:
2
}
return
ShuffleNet
(
cfg
)
def
ShuffleNetG3
():
cfg
=
{
'out_planes'
:
[
240
,
480
,
960
],
'num_blocks'
:
[
4
,
8
,
4
],
'groups'
:
3
}
return
ShuffleNet
(
cfg
)
def
test
():
net
=
ShuffleNetG2
()
x
=
torch
.
randn
(
1
,
3
,
32
,
32
)
y
=
net
(
x
)
print
(
y
)
# test()
examples/trials/pytorch_cifar10/models/vgg.py
0 → 100644
View file @
8314d6ee
'''VGG11/13/16/19 in Pytorch.'''
import
torch
import
torch.nn
as
nn
cfg
=
{
'VGG11'
:
[
64
,
'M'
,
128
,
'M'
,
256
,
256
,
'M'
,
512
,
512
,
'M'
,
512
,
512
,
'M'
],
'VGG13'
:
[
64
,
64
,
'M'
,
128
,
128
,
'M'
,
256
,
256
,
'M'
,
512
,
512
,
'M'
,
512
,
512
,
'M'
],
'VGG16'
:
[
64
,
64
,
'M'
,
128
,
128
,
'M'
,
256
,
256
,
256
,
'M'
,
512
,
512
,
512
,
'M'
,
512
,
512
,
512
,
'M'
],
'VGG19'
:
[
64
,
64
,
'M'
,
128
,
128
,
'M'
,
256
,
256
,
256
,
256
,
'M'
,
512
,
512
,
512
,
512
,
'M'
,
512
,
512
,
512
,
512
,
'M'
],
}
class
VGG
(
nn
.
Module
):
def
__init__
(
self
,
vgg_name
):
super
(
VGG
,
self
).
__init__
()
self
.
features
=
self
.
_make_layers
(
cfg
[
vgg_name
])
self
.
classifier
=
nn
.
Linear
(
512
,
10
)
def
forward
(
self
,
x
):
out
=
self
.
features
(
x
)
out
=
out
.
view
(
out
.
size
(
0
),
-
1
)
out
=
self
.
classifier
(
out
)
return
out
def
_make_layers
(
self
,
cfg
):
layers
=
[]
in_channels
=
3
for
x
in
cfg
:
if
x
==
'M'
:
layers
+=
[
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)]
else
:
layers
+=
[
nn
.
Conv2d
(
in_channels
,
x
,
kernel_size
=
3
,
padding
=
1
),
nn
.
BatchNorm2d
(
x
),
nn
.
ReLU
(
inplace
=
True
)]
in_channels
=
x
layers
+=
[
nn
.
AvgPool2d
(
kernel_size
=
1
,
stride
=
1
)]
return
nn
.
Sequential
(
*
layers
)
def
test
():
net
=
VGG
(
'VGG11'
)
x
=
torch
.
randn
(
2
,
3
,
32
,
32
)
y
=
net
(
x
)
print
(
y
.
size
())
# test()
examples/trials/pytorch_cifar10/requirement.txt
0 → 100644
View file @
8314d6ee
This example requires pytorch.
pytorch install package should be chosen based on python version and cuda version.
Here is an example of the environment python==3.5 and cuda == 8.0, then using the following commands to install pytorch:
pip3 install http://download.pytorch.org/whl/cu80/torch-0.4.1-cp35-cp35m-linux_x86_64.whl
pip3 install torchvision
\ No newline at end of file
examples/trials/pytorch_cifar10/search_space.json
0 → 100644
View file @
8314d6ee
{
"lr"
:{
"_type"
:
"choice"
,
"_value"
:[
0.1
,
0.01
,
0.001
,
0.0001
]},
"optimizer"
:{
"_type"
:
"choice"
,
"_value"
:[
"SGD"
,
"Adadelta"
,
"Adagrad"
,
"Adam"
,
"Adamax"
]},
"model"
:{
"_type"
:
"choice"
,
"_value"
:[
"vgg"
,
"resnet18"
,
"googlenet"
,
"densenet121"
,
"mobilenet"
,
"dpn92"
,
"senet18"
]}
}
examples/trials/pytorch_cifar10/utils.py
0 → 100644
View file @
8314d6ee
'''Some helper functions for PyTorch, including:
- get_mean_and_std: calculate the mean and std value of dataset.
- msr_init: net parameter initialization.
- progress_bar: progress bar mimic xlua.progress.
'''
import
os
import
sys
import
time
import
math
import
torch.nn
as
nn
import
torch.nn.init
as
init
def
get_mean_and_std
(
dataset
):
'''Compute the mean and std value of dataset.'''
dataloader
=
torch
.
utils
.
data
.
DataLoader
(
dataset
,
batch_size
=
1
,
shuffle
=
True
,
num_workers
=
2
)
mean
=
torch
.
zeros
(
3
)
std
=
torch
.
zeros
(
3
)
print
(
'==> Computing mean and std..'
)
for
inputs
,
targets
in
dataloader
:
for
i
in
range
(
3
):
mean
[
i
]
+=
inputs
[:,
i
,:,:].
mean
()
std
[
i
]
+=
inputs
[:,
i
,:,:].
std
()
mean
.
div_
(
len
(
dataset
))
std
.
div_
(
len
(
dataset
))
return
mean
,
std
def
init_params
(
net
):
'''Init layer parameters.'''
for
m
in
net
.
modules
():
if
isinstance
(
m
,
nn
.
Conv2d
):
init
.
kaiming_normal
(
m
.
weight
,
mode
=
'fan_out'
)
if
m
.
bias
:
init
.
constant
(
m
.
bias
,
0
)
elif
isinstance
(
m
,
nn
.
BatchNorm2d
):
init
.
constant
(
m
.
weight
,
1
)
init
.
constant
(
m
.
bias
,
0
)
elif
isinstance
(
m
,
nn
.
Linear
):
init
.
normal
(
m
.
weight
,
std
=
1e-3
)
if
m
.
bias
:
init
.
constant
(
m
.
bias
,
0
)
term_width
=
0
try
:
_
,
term_width
=
os
.
popen
(
'stty size'
,
'r'
).
read
().
split
()
except
Exception
as
exception
:
term_width
=
200
term_width
=
int
(
term_width
)
TOTAL_BAR_LENGTH
=
65.
last_time
=
time
.
time
()
begin_time
=
last_time
def
progress_bar
(
current
,
total
,
msg
=
None
):
global
last_time
,
begin_time
if
current
==
0
:
begin_time
=
time
.
time
()
# Reset for new bar.
cur_len
=
int
(
TOTAL_BAR_LENGTH
*
current
/
total
)
rest_len
=
int
(
TOTAL_BAR_LENGTH
-
cur_len
)
-
1
sys
.
stdout
.
write
(
' ['
)
for
i
in
range
(
cur_len
):
sys
.
stdout
.
write
(
'='
)
sys
.
stdout
.
write
(
'>'
)
for
i
in
range
(
rest_len
):
sys
.
stdout
.
write
(
'.'
)
sys
.
stdout
.
write
(
']'
)
cur_time
=
time
.
time
()
step_time
=
cur_time
-
last_time
last_time
=
cur_time
tot_time
=
cur_time
-
begin_time
L
=
[]
L
.
append
(
' Step: %s'
%
format_time
(
step_time
))
L
.
append
(
' | Tot: %s'
%
format_time
(
tot_time
))
if
msg
:
L
.
append
(
' | '
+
msg
)
msg
=
''
.
join
(
L
)
sys
.
stdout
.
write
(
msg
)
for
i
in
range
(
term_width
-
int
(
TOTAL_BAR_LENGTH
)
-
len
(
msg
)
-
3
):
sys
.
stdout
.
write
(
' '
)
# Go back to the center of the bar.
for
i
in
range
(
term_width
-
int
(
TOTAL_BAR_LENGTH
/
2
)
+
2
):
sys
.
stdout
.
write
(
'
\b
'
)
sys
.
stdout
.
write
(
' %d/%d '
%
(
current
+
1
,
total
))
if
current
<
total
-
1
:
sys
.
stdout
.
write
(
'
\r
'
)
else
:
sys
.
stdout
.
write
(
'
\n
'
)
sys
.
stdout
.
flush
()
def
format_time
(
seconds
):
days
=
int
(
seconds
/
3600
/
24
)
seconds
=
seconds
-
days
*
3600
*
24
hours
=
int
(
seconds
/
3600
)
seconds
=
seconds
-
hours
*
3600
minutes
=
int
(
seconds
/
60
)
seconds
=
seconds
-
minutes
*
60
secondsf
=
int
(
seconds
)
seconds
=
seconds
-
secondsf
millis
=
int
(
seconds
*
1000
)
f
=
''
i
=
1
if
days
>
0
:
f
+=
str
(
days
)
+
'D'
i
+=
1
if
hours
>
0
and
i
<=
2
:
f
+=
str
(
hours
)
+
'h'
i
+=
1
if
minutes
>
0
and
i
<=
2
:
f
+=
str
(
minutes
)
+
'm'
i
+=
1
if
secondsf
>
0
and
i
<=
2
:
f
+=
str
(
secondsf
)
+
's'
i
+=
1
if
millis
>
0
and
i
<=
2
:
f
+=
str
(
millis
)
+
'ms'
i
+=
1
if
f
==
''
:
f
=
'0ms'
return
f
examples/tuners/ga_customer_tuner/README.md
0 → 100644
View file @
8314d6ee
# How to use ga_customer_tuner?
This tuner is a customized tuner which only suitable for trial whose code path is "~/nni/examples/trials/ga_squad",
type
`cd ~/nni/examples/trials/ga_squad`
and check readme.md to get more information for ga_squad trial.
# config
If you want to use ga_customer_tuner in your experiment, you could set config file as following format:
```
tuner:
codeDir: ~/nni/examples/tuners/ga_customer_tuner
classFileName: customer_tuner.py
className: CustomerTuner
classArgs:
optimize_mode: maximize
```
\ No newline at end of file
Prev
1
2
3
4
5
6
Next
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