forked from andersbll/deeppy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvnet_cifar.py
executable file
·108 lines (97 loc) · 3.35 KB
/
convnet_cifar.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# coding: utf-8
import os
import numpy as np
import deeppy as dp
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]
x_test = x[test_idx]
y_test = y[test_idx]
scaler = dp.UniformScaler(feature_wise=True)
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
batch_size = 128
train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size)
test_input = dp.SupervisedInput(x_test, y_test, 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, monitor=True),
),
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, monitor=True),
),
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, monitor=True),
),
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, monitor=True),
),
dp.Activation('relu'),
dp.FullyConnected(
n_output=dataset.n_classes,
weights=dp.Parameter(dp.NormalFiller(sigma=0.1),
weight_decay=0.004, monitor=True),
),
dp.MultinomialLogReg(),
],
)
# Train neural network
def val_error():
return net.error(test_input)
n_epochs = [8, 8]
learn_rate = 0.001
for i, max_epochs in enumerate(n_epochs):
lr = learn_rate/10**i
trainer = dp.StochasticGradientDescent(
max_epochs=max_epochs,
learn_rule=dp.Momentum(learn_rate=lr, momentum=0.9),
)
trainer.train(net, train_input, val_error)
# Visualize convolutional filters to disk
for l, layer in enumerate(net.layers):
if not isinstance(layer, dp.Convolutional):
continue
W = np.array(layer.params()[0].array)
filepath = os.path.join('cifar10', 'conv_layer_%i.png' % l)
dp.misc.img_save(dp.misc.conv_filter_tile(W), filepath)
# Evaluate on test data
error = net.error(test_input)
print('Test error rate: %.4f' % error)
if __name__ == '__main__':
run()