-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
57 lines (46 loc) · 1.62 KB
/
classifier.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
import scipy.io as sio
from keras import Sequential
from keras.layers import Dense, Dropout
from keras.regularizers import l2
import configuration as cfg
def classifier_model():
model = Sequential()
model.add(
Dense(512, input_dim=4096, kernel_initializer='glorot_normal', kernel_regularizer=l2(0.001), activation='relu'))
model.add(Dropout(0.6))
model.add(Dense(32, kernel_initializer='glorot_normal', kernel_regularizer=l2(0.001)))
model.add(Dropout(0.6))
model.add(Dense(1, kernel_initializer='glorot_normal', kernel_regularizer=l2(0.001), activation='sigmoid'))
return model
def build_classifier_model():
model = classifier_model()
model = load_weights(model, cfg.classifier_model_weigts)
return model
def conv_dict(dict2):
dict = {}
for i in range(len(dict2)):
if str(i) in dict2:
if dict2[str(i)].shape == (0, 0):
dict[str(i)] = dict2[str(i)]
else:
weights = dict2[str(i)][0]
weights2 = []
for weight in weights:
if weight.shape in [(1, x) for x in range(0, 5000)]:
weights2.append(weight[0])
else:
weights2.append(weight)
dict[str(i)] = weights2
return dict
def load_weights(model, weights_file):
dict2 = sio.loadmat(weights_file)
dict = conv_dict(dict2)
i = 0
for layer in model.layers:
weights = dict[str(i)]
layer.set_weights(weights)
i += 1
return model
if __name__ == '__main__':
model = build_classifier_model()
model.summary()