-
Notifications
You must be signed in to change notification settings - Fork 3
/
CNN.py
230 lines (188 loc) · 8.55 KB
/
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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from utils import data_set, shared_dataset, build_update_functions, early_stop_train
import numpy as np
from lasagne.layers import InputLayer, DenseLayer, NonlinearityLayer, count_params
from lasagne.layers import DropoutLayer, get_all_layers, batch_norm, ElemwiseSumLayer
from lasagne.layers import Pool2DLayer as PoolLayer
from lasagne.layers.dnn import Conv2DDNNLayer as ConvLayer
from lasagne.nonlinearities import identity, rectify
import theano.tensor as T
import cPickle as pickle
def single_conv_layer(input_layer, **kwargs):
complex_layer = ConvLayer(incoming=input_layer,**kwargs)
# complex_layer = PoolLayer(complex_layer, pool_size=2, stride=2, mode='average_exc_pad')
return complex_layer
def build_model_vanila_CNN(X, channel = 1,stride=1):
# TODO: set according to daniels guide
conv1filters = 64
conv2filters = 64
conv3filters = 128
conv4filters = 256
net = {}
non_linear_function = rectify
net['input'] = InputLayer((None, channel, 96, 96), input_var=X)
net['conv1'] = single_conv_layer(net['input'],
num_filters=conv1filters,
filter_size=3,
stride=stride,
pad=1,
nonlinearity=non_linear_function,
flip_filters=False)
net['conv2'] = single_conv_layer(net['conv1'],
num_filters=conv2filters,
filter_size=2,
stride=stride,
pad=1,
nonlinearity=non_linear_function,
flip_filters=False)
net['conv2'] = PoolLayer(net['conv2'], pool_size=2, stride=2, mode='average_exc_pad')
net['conv3'] = single_conv_layer(net['conv2'],
num_filters=conv3filters,
filter_size=2,
stride=stride,
pad=1,
nonlinearity=non_linear_function,
flip_filters=False)
#
net['conv4'] = single_conv_layer(net['conv3'],
num_filters=conv4filters,
filter_size=3,
stride=stride,
pad=1,
nonlinearity=non_linear_function,
flip_filters=False)
net['conv4'] = PoolLayer(net['conv4'], pool_size=2, stride=2, mode='average_exc_pad')
# net['fc5'] = DenseLayer(net['conv4'], num_units=512, nonlinearity=non_linear_function)
net['fc5'] = ConvLayer(incoming=net['conv4'],
num_filters=500,
filter_size=1,
stride=1,
pad=0,
nonlinearity=non_linear_function,
flip_filters=False)
# net['fc5'] = DropoutLayer(net['fc5'], p=0.5)
# net['fc5'] = ConvLayer(incoming=net['fc5'],
# num_filters=500,
# filter_size=1,
# stride=1,
# pad=0,
# nonlinearity=non_linear_function,
# flip_filters=False)
# net['fc5'] = DropoutLayer(net['fc5'], p=0.3)
net['fc6'] = DenseLayer(net['fc5'], num_units=30, nonlinearity=identity)
net['prob'] = NonlinearityLayer(net['fc6'], nonlinearity=identity)
return net
def build_CNN_nopool(in_shape,
num_filter,
fil_size,
strides,
num_out,
nlin_func=rectify,
in_var=None):
# build a CNN
net = InputLayer(input_var=in_var,
shape=in_shape)
for i in xrange(len(fil_size)):
net = batch_norm(ConvLayer(net,
num_filters=num_filter[i],
filter_size=fil_size[i],
stride=strides[i],
pad=1,
nonlinearity=nlin_func,
flip_filters=False))
net = DenseLayer(incoming=net,
num_units=num_out,
nonlinearity=identity)
return net
#def resnet_base(net,
# n_f,
# f_size,
# strides,
# num_out,
# nlin_func=rectify,
# in_var=None):
#
# temp = ConvLayer(net, num_filters=n_f, filter_size=3, stride=1, pad=1, nonlinearity=identity, flip_filters=False )
# temp = ConvLayer(temp, num_filters=n_f, filter_size=1, stride=1, pad=0, nonlinearity=identity, flip_filters=False )
#
#
# return net
if __name__ == "__main__":
# path to train and testing data
PATH_train = "../data/training.csv"
PATH_test = "../data/test.csv"
# load data
print 'loading data \n'
data = data_set(path_train=PATH_train, path_test=PATH_test)
print 'sobel stacking image'
data.stack_origi_sobel()
# augmentation
# data.augment()
# center data
# print 'center alexnet \n'
# data.center_alexnet()
# print 'center Xs VGG Style, X doesnt have missing values \n'
# data.center_VGG()
# generate test validation split
data.split_trainval()
train_set_x = data.X
valid_set_x = data.X_val
train_set_y = data.y
valid_set_y = data.y_val
n_ch = train_set_x.shape[1]
print 'shape of train X', train_set_x.shape, 'and y', train_set_y.shape,'\n'
print 'shape of validation X', valid_set_x.shape, 'and y', valid_set_y.shape, '\n'
# build the mask matrix for missing values, load it into theano shared variable
# build masks where 0 values correspond to nan values
temp = np.isnan(train_set_y)
train_MASK = np.ones(temp.shape)
train_MASK[temp] = 0
# still have to replace nan with something to avoid propagation in theano
train_set_y[temp] = -1000
temp = np.isnan(valid_set_y)
val_MASK = np.ones(temp.shape)
val_MASK[temp] = 0
# still have to replace nan with something to avoid propagation in theano
valid_set_y[temp] = -1000
# load into theano shared variable
print 'load data to gpu \n'
train_set_x, train_set_y = shared_dataset(train_set_x, train_set_y)
valid_set_x, valid_set_y = shared_dataset(valid_set_x, valid_set_y)
val_MASK, train_MASK = shared_dataset(val_MASK, train_MASK)
X = T.ftensor4('X')
y = T.matrix('y')
batch_size = 32
l2 = .0002
learn_rate = 1e-3
#####################################################
# # Continue a previous run
# with open("results_backup.p", "rb") as f:
# best_network_params, best_val_loss_, best_epoch_,train_loss_history_, val_loss_history_, network = pickle.load(f)
# # extract input var
# print 'extract input var \n'
# X = get_all_layers(network)[0].input_var
#####################################################
# # VGG run
# net = build_model_vanila_CNN(X=X, channel= n_ch, stride=1 )
# network = net['prob']
#####################################################
# FULLCCN run
network = build_CNN_nopool(in_shape = (None, n_ch,96,96),
num_filter = [64,64,128,128,128,128],
fil_size = [ 3, 1, 3, 3, 3, 12],
strides = [ 1, 1, 2, 2, 2, 1],
num_out = 30,
nlin_func=rectify,
in_var=X)
print "num_params", count_params(network)
#####################################################
train_fn, val_fn = build_update_functions(train_set_x=train_set_x, train_set_y=train_set_y,
valid_set_x=valid_set_x,valid_set_y= valid_set_y,
y= y,X= X,network=network,
val_MASK=val_MASK, train_MASK=train_MASK,
learning_rate=learn_rate,batch_size=batch_size,l2_reg=l2)
print 'compile done successfully \n'
# call early_stop_train function
early_stop_train(train_set_x, train_set_y,
valid_set_x, valid_set_y,
network, train_fn, val_fn,
batch_size=batch_size)