-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcnn12.py
115 lines (94 loc) · 3.53 KB
/
cnn12.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
"""
A deep network was constructed accepting input of image dimensions of 128 x 128 with one channel (grayscale images).
The network was constructed by following Karpathy's tutorial.
Based on https://github.com/hemavakade/CNN-for-Image-Classification
@author: Netanel Azoulay
@author: Roman Koifman
"""
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Flatten, Dense, Dropout
from keras.layers.advanced_activations import PReLU
from keras.layers.convolutional import ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.layers.pooling import AveragePooling2D
from keras.models import Sequential
from keras.optimizers import adam
from projectParams import classes, imgDim, learnRate
def getModel(weightsPath=None):
"""
Build cnn12 model and load pre-trained weights if provided.
:param weightsPath: pre-trained weights h5 file.
:return: compiled cnn12 model.
"""
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(imgDim, imgDim, 1)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(Conv2D(32, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(Conv2D(64, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(Conv2D(128, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(Conv2D(256, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(512, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(Conv2D(512, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(1028, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(Conv2D(1028, (3, 3)))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(BatchNormalization())
model.add(PReLU())
model.add(AveragePooling2D(pool_size=(2, 2)))
model.add(Flatten())
# Dense = Fully connected layer
model.add(Dense(2048, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(len(classes), activation='softmax'))
# opt = SGD(lr=learnRate, decay=1e-6, momentum=0.9, nesterov=True)
opt = adam(lr=learnRate)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
if weightsPath:
try:
model.load_weights(weightsPath)
print("cnn12 weights loaded.")
except OSError:
print("Failed loading cnn12 weights!")
else:
print("cnn12 weights are not provided.")
return model