-
Notifications
You must be signed in to change notification settings - Fork 0
/
KerasNet.py
executable file
·125 lines (87 loc) · 4.16 KB
/
KerasNet.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
import theano
import theano.tensor as T
import lasagne
import cPickle
import os
from lasagne.layers import Conv2DLayer,MaxPool2DLayer,InputLayer,dropout,ConcatLayer,DenseLayer,BatchNormLayer,flatten
batch_size = 32
nb_classes = 10
nb_epoch = 200
saveweights = True
def create_nn():
'''
Returns the theano function - train,test
Returns the 'KerasNet'
Using default values of adam - learning_rate=0.001, beta1=0.9, beta2=0.999, epsilon=1e-08
Input to the NN is (batch_size,3,32,32) and corresponding classes it belong to (batch_size,)
'''
l_in = InputLayer((batch_size,3,32,32))
l_in_bn = BatchNormLayer(l_in)
conv1 = Conv2DLayer(l_in_bn,pad='same',num_filters=64,filter_size=(3,3),nonlinearity=lasagne.nonlinearities.rectify) #Bx64x32x32
conv1_1 = Conv2DLayer(conv1,pad='same',num_filters=64,filter_size=(3,3),nonlinearity=lasagne.nonlinearities.rectify) #Bx64x32x32
conv1_mp = MaxPool2DLayer(conv1_1,pool_size=(2,2)) #Bx64x16x16
conv1_do = dropout(conv1_mp,p=0.25)
conv2 = Conv2DLayer(conv1_do,pad='same',num_filters=128,filter_size=(3,3),nonlinearity=lasagne.nonlinearities.rectify) #Bx128x16x16
conv2_1 = Conv2DLayer(conv2,pad='same',num_filters=128,filter_size=(3,3),nonlinearity=lasagne.nonlinearities.rectify) #Bx128x16x16
conv2_mp = MaxPool2DLayer(conv2_1,pool_size=(2,2)) #Bx128x8x8
conv2_do = dropout(conv2_mp,p=0.25)
flat = flatten(conv2_do,2) #Bx8192
fc = DenseLayer(flat,num_units=512,nonlinearity=lasagne.nonlinearities.rectify) #Bx512
fc_do = dropout(fc, p=0.5)
network = DenseLayer(fc_do, num_units=nb_classes, nonlinearity=lasagne.nonlinearities.softmax ) #Bxnb_classes
net_output = lasagne.layers.get_output(network)
true_output = T.matrix()
all_params = lasagne.layers.get_all_params(network,trainable=True)
loss = T.mean(lasagne.objectives.categorical_crossentropy(net_output,true_output))
updates = lasagne.updates.adam(loss,all_params)
train = theano.function(inputs= [l_in.input_var,true_output] , outputs=[net_output,loss], updates = updates)
test = theano.function(inputs= [l_in.input_var], outputs= [net_output])
return train,test,network
def train_model(train,network):
'''
Write your own implementation of training the model
'''
X_train,Y_train = get_training_set()
for k in xrange(nb_epoch):
net_output,loss = train(X_train,Y_train)
weights = lasagne.layers.get_all_param_values(network)
### If training accuracy needs to be calculated simultaneously it can be done so using net_output and Y_train values according to your needs.
if saveweights:
location = "weights/"
if not os.path.exists(location):
os.makedits(location)
#Using cPickle to store the weights..
cPickle.dump(weights,open(location+str(k)+"_epoch_weights.pkl",w))
'''
Or, In case you want to use only "batch_size" of inputs in memory
'''
for k in xrange(nb_epoch):
for i in xrange(0,total_number,batch_size):
X_train_batch,Y_train_batch = get_batch_training_set()
net_output,loss = train(X_train_batch,Y_train_batch)
### If training accuracy needs to be calculated simultaneously it can be done so using net_output and Y_train values according to your needs.
weights = lasagne.layers.get_all_param_values(network)
if saveweights:
location = "weights/"
if not os.path.exists(location):
os.makedits(location)
#Using cPickle to store the weights..
cPickle.dump(weights,open(location+str(k)+"_epoch_weights.pkl",w))
def test_model(test,network):
weights = cPickle.load() #insert the desired weights file
lasagne.layers.set_all_param_values(network,weights)
X_test,Y_test = get_testing_set()
predictions = test(X_test)
### Test accuracy can also be calculated depending on the type of data/metrics needed using 'predictions' and 'Y_test'
'''
In case testing in batch size is needed, similar thing can be implemented as above.
'''
predictions = []
for i in xrange(0,total_number,batch_size):
X_test_batch,Y_test_batch = get_batch_testing_set()
predictions += test(X_test_batch)
### Test accuracy can also be calculated depending on the type of data/metrics needed using 'predictions' and 'Y_test'
if __name__ = '__main__':
train,test,network = create_nn()
train_model(train,network)
test_model(test,network)