forked from andersbll/deeppy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile_convnet.py
executable file
·83 lines (73 loc) · 2.34 KB
/
profile_convnet.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import deeppy as dp
def preprocess_imgs(imgs):
imgs = imgs.astype(dp.float_)
imgs -= np.mean(imgs, axis=0, keepdims=True)
return imgs
def run():
# Prepare data
dataset = dp.datasets.CIFAR10()
x, y = dataset.data()
x = x.astype(dp.float_)
y = y.astype(dp.int_)
train_idx, test_idx = dataset.split()
x_train = x[train_idx]
y_train = y[train_idx]
batch_size = 128
train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size)
# Setup neural network
pool_kwargs = {
'win_shape': (3, 3),
'strides': (2, 2),
'border_mode': 'same',
'method': 'max',
}
net = dp.NeuralNetwork(
layers=[
dp.Convolutional(
n_filters=32,
filter_shape=(5, 5),
border_mode='same',
weights=dp.Parameter(dp.NormalFiller(sigma=0.0001),
weight_decay=0.004),
),
dp.Activation('relu'),
dp.Pool(**pool_kwargs),
dp.Convolutional(
n_filters=32,
filter_shape=(5, 5),
border_mode='same',
weights=dp.Parameter(dp.NormalFiller(sigma=0.01),
weight_decay=0.004),
),
dp.Activation('relu'),
dp.Pool(**pool_kwargs),
dp.Convolutional(
n_filters=64,
filter_shape=(5, 5),
border_mode='same',
weights=dp.Parameter(dp.NormalFiller(sigma=0.01),
weight_decay=0.004),
),
dp.Activation('relu'),
dp.Pool(**pool_kwargs),
dp.Flatten(),
dp.FullyConnected(
n_output=64,
weights=dp.Parameter(dp.NormalFiller(sigma=0.1),
weight_decay=0.004),
),
dp.Activation('relu'),
dp.FullyConnected(
n_output=dataset.n_classes,
weights=dp.Parameter(dp.NormalFiller(sigma=0.1),
weight_decay=0.004),
),
dp.MultinomialLogReg(),
],
)
dp.misc.profile(net, train_input)
if __name__ == '__main__':
run()