-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcnn_age.py
48 lines (39 loc) · 2.12 KB
/
cnn_age.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
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
from keras import initializers
from keras.utils import plot_model
class AgeCNN():
def __init__(self, input_size):
# Initialising the CNN
self.classifier = Sequential()
initializer = initializers.RandomNormal(mean=0.0, stddev=0.01, seed=None)
# First conv layer
# common practice for the first CNN layer is 32 filters (the dimensionality of the output space)
self.classifier.add(Conv2D(96, (7, 7), input_shape = (input_size, input_size, 3), activation = 'relu', kernel_initializer = initializer))
self.classifier.add(MaxPooling2D(pool_size = (3, 3), strides = 2))
# Second conv layer
self.classifier.add(Conv2D(256, (5, 5), activation = 'relu',kernel_initializer = initializer))
self.classifier.add(MaxPooling2D(pool_size = (3, 3), strides = 2))
# Third conv layer
self.classifier.add(Conv2D(384, (3, 3), activation = 'relu',kernel_initializer = initializer))
self.classifier.add(MaxPooling2D(pool_size = (3, 3), strides = 2))
# Flatten layer
self.classifier.add(Flatten())
# Fully connected layer
self.classifier.add(Dense(units = 512, activation = 'relu',kernel_initializer = initializer))
self.classifier.add(Dropout(0.5))
self.classifier.add(Dense(units = 512, activation = 'relu',kernel_initializer = initializer))
self.classifier.add(Dropout(0.5))
self.classifier.add(Dense(units = 8, activation = 'softmax',kernel_initializer = initializer))
# Compiling the CNN. Stochastic gradient process is implied here.
# optimizer: gradient decent
# loss: if more than two output, need to use categorical_crossentropy
self.classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
def get_classifier(self):
return self.classifier
def plot_model(self):
plot_model(self.classifier,to_file='model.png')