-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
155 lines (126 loc) · 6.07 KB
/
models.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from lasagne.layers import *
from lasagne.nonlinearities import *
from lasagne.init import *
def logreg(in_shape, n_classes):
net = InputLayer(shape=(None,) + in_shape, name='Input')
net = DenseLayer(net, num_units=n_classes, nonlinearity=softmax,
name='Output', b=Constant(0.))
return net
def small_vgg(in_shape, n_classes):
""" Compile net architecture """
nonlin = rectify
def init_conv():
return HeNormal('relu')
def conv_bn(in_layer, num_filters, filter_size, nonlinearity, pad):
in_layer = Conv2DLayer(in_layer, num_filters=num_filters,
filter_size=filter_size,
nonlinearity=nonlinearity, pad=pad, name='conv',
W=init_conv())
in_layer = batch_norm(in_layer)
return in_layer
net1 = InputLayer(shape=(None, in_shape[0], in_shape[1], in_shape[2]), name='Input')
# number of filters
nf0 = 32
pad = 'same'
net1 = conv_bn(net1, num_filters=nf0, filter_size=3, nonlinearity=nonlin,
pad=pad)
net1 = conv_bn(net1, num_filters=nf0, filter_size=3, nonlinearity=nonlin,
pad=pad)
net1 = MaxPool2DLayer(net1, pool_size=2, stride=2, name='pool1')
net1 = DropoutLayer(net1, p=0.25)
net1 = conv_bn(net1, num_filters=nf0*2, filter_size=3, nonlinearity=nonlin,
pad=pad)
net1 = conv_bn(net1, num_filters=nf0*2, filter_size=3, nonlinearity=nonlin,
pad=pad)
net1 = MaxPool2DLayer(net1, pool_size=2, stride=2, name='pool2')
net1 = DropoutLayer(net1, p=0.25)
net1 = conv_bn(net1, num_filters=nf0*4, filter_size=3, nonlinearity=nonlin,
pad=pad)
net1 = conv_bn(net1, num_filters=nf0*4, filter_size=3, nonlinearity=nonlin,
pad=pad)
net1 = MaxPool2DLayer(net1, pool_size=2, stride=2, name='pool2')
net1 = DropoutLayer(net1, p=0.25)
net1 = conv_bn(net1, num_filters=512, filter_size=3, nonlinearity=nonlin,
pad='valid')
net1 = DropoutLayer(net1, p=0.5)
net1 = conv_bn(net1, num_filters=512, filter_size=1, nonlinearity=nonlin,
pad='valid')
net1 = DropoutLayer(net1, p=0.5)
net1 = conv_bn(net1, num_filters=n_classes, filter_size=1,
nonlinearity=nonlin, pad=pad)
net1 = GlobalPoolLayer(net1)
net1 = FlattenLayer(net1)
net1 = NonlinearityLayer(net1, nonlinearity=softmax)
return net1
def vgg(in_shape, n_classes):
nonlin = rectify
def init_conv():
return HeNormal('relu')
l_in = InputLayer(shape=(None, in_shape[0], in_shape[1], in_shape[2]),
name='Input')
net = Conv2DLayer(l_in, num_filters=64, filter_size=3, pad=1,
W=init_conv(), nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = Conv2DLayer(net, num_filters=64, filter_size=3, pad=1, W=init_conv(),
nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = MaxPool2DLayer(net, pool_size=2, name='Pool')
net = DropoutLayer(net, p=0.25, name='Dropout')
net = Conv2DLayer(net, num_filters=128, filter_size=3, pad=1,
W=init_conv(), nonlinearity=nonlin, name='Conv')
net = Conv2DLayer(net, num_filters=128, filter_size=3, pad=1,
W=init_conv(), nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = MaxPool2DLayer(net, pool_size=2, name='Pool')
net = DropoutLayer(net, p=0.25, name='Dropout')
net = Conv2DLayer(net, num_filters=256, filter_size=3, pad=1,
W=init_conv(), nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = Conv2DLayer(net, num_filters=256, filter_size=3, pad=1,
W=init_conv(), nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = Conv2DLayer(net, num_filters=256, filter_size=3, pad=1,
W=init_conv(), nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = Conv2DLayer(net, num_filters=256, filter_size=3, pad=1,
W=init_conv(), nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = MaxPool2DLayer(net, pool_size=2, name='Pool')
net = DropoutLayer(net, p=0.25, name='Dropout')
net = Conv2DLayer(net, num_filters=1024, filter_size=3, pad=0,
W=init_conv(), nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = DropoutLayer(net, p=0.5, name='Dropout')
net = Conv2DLayer(net, num_filters=1024, filter_size=1, pad=0,
W=init_conv(), nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = DropoutLayer(net, p=0.5, name='Dropout')
net = Conv2DLayer(net, num_filters=n_classes, filter_size=1, W=init_conv(),
nonlinearity=nonlin, name='Conv')
net = batch_norm(net)
net = GlobalPoolLayer(net)
net = DenseLayer(net, num_units=n_classes, nonlinearity=softmax)
return net
def cifarnet(in_shape, n_classes):
nonlin = rectify
def init_weights():
return HeNormal('relu')
init_bias_const = 0.
net = InputLayer(shape=(None, in_shape[0], in_shape[1], in_shape[2]),
name='Input')
net = Conv2DLayer(net, num_filters=64, filter_size=6, pad='valid',
W=init_weights(), b=Constant(init_bias_const),
nonlinearity=nonlin, name='Conv')
net = LocalResponseNormalization2DLayer(net)
net = MaxPool2DLayer(net, pool_size=2)
net = Conv2DLayer(net, num_filters=64, filter_size=6, pad='valid',
W=init_weights(), b=Constant(init_bias_const),
nonlinearity=nonlin, name='Conv')
net = LocalResponseNormalization2DLayer(net)
net = MaxPool2DLayer(net, pool_size=2)
net = DenseLayer(net, num_units=384, W=init_weights(), b=Constant(0.))
net = DropoutLayer(net, p=0.5, name='Dropout')
net = DenseLayer(net, num_units=192, W=init_weights(), b=Constant(0.))
net = DenseLayer(net, num_units=n_classes, nonlinearity=softmax,
W=init_weights(), b=Constant(0.))
return net