forked from facebookresearch/ppuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_cnn.py
65 lines (48 loc) · 2.08 KB
/
custom_cnn.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
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
"""
Example of predicting parameters for a user-defined architecture.
The script predicts parameters for the network and evaluates them on CIFAR-10.
Example:
python examples/custom_cnn.py
"""
import torch.nn as nn
from ppuda.vision.loader import image_loader
from ppuda.ghn.nn import GHN2
from ppuda.utils import capacity, infer
dataset = 'cifar10'
ghn = GHN2(dataset)
is_imagenet = dataset == 'imagenet'
images_val, num_classes = image_loader(dataset, num_workers=8 * is_imagenet)[1:]
if is_imagenet:
images_val.sampler.generator.manual_seed(1111) # set the generator seed to reproduce results
# Define the network configuration
class CNN(nn.Module):
def __init__(self, C=64):
super(CNN, self).__init__()
self.layers = nn.Sequential(nn.Conv2d(3, C, kernel_size=5),
nn.AvgPool2d(2),
nn.ReLU(),
nn.Conv2d(C, C * 2, kernel_size=3),
nn.AvgPool2d(2),
nn.ReLU(),
nn.Conv2d(C * 2, C * 4, kernel_size=3),
nn.ReLU(),
nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(C * 4, num_classes),
)
def forward(self, x):
return self.layers(x)
model = CNN().eval() # Create the net
model.expected_input_sz = 224 if is_imagenet else 32 # to construct the graph
model = ghn(model) # Predict all parameters for the model
print('\nEvaluation of CNN with {} parameters'.format(capacity(model)[1]))
top1, top5 = infer(model, images_val, verbose=True)
# top1=16.84 for this CNN on CIFAR-10
if top1 != 16.84:
print('WARNING: results appear to be different from expected!' )
print('\ndone')