-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmodels.py
More file actions
78 lines (65 loc) · 2.72 KB
/
models.py
File metadata and controls
78 lines (65 loc) · 2.72 KB
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
from keras.layers import MaxPooling2D, Dropout, Dense, Flatten, Activation, Conv2D
from keras.models import Sequential
def build_mlp(training_data, width=28, height=28, verbose=True):
''' Build and train convolutional neural network. Also offloads the net in .yaml and the
weights in .h5 to the bin/.
Arguments:
training_data: the packed tuple from load_data()
Optional Arguments:
width: specified width
height: specified height
epochs: the number of epochs to train over
verbose: enable verbose printing
'''
# Initialize data
(x_train, y_train), (x_test, y_test), mapping, nb_classes = training_data
input_shape = (height, width, 1)
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
#model.add(Dense(512, activation='relu'))
#model.add(Dropout(0.2))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
#sgd = keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
if verbose == True: print(model.summary())
return model
def build_cnn(training_data, width=28, height=28, verbose=True):
''' Build and train convolutional neural network. Also offloads the net in .yaml and the
weights in .h5 to the bin/.
Arguments:
training_data: the packed tuple from load_data()
Optional Arguments:
width: specified width
height: specified height
epochs: the number of epochs to train over
verbose: enable verbose printing
'''
# Initialize data
(x_train, y_train), (x_test, y_test), mapping, nb_classes = training_data
input_shape = (height, width, 1)
# Hyperparameters
nb_filters = 64 # number of convolutional filters to use
pool_size = (2, 2) # size of pooling area for max pooling
kernel_size = (3, 3) # convolution kernel size
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
if verbose == True: print(model.summary())
return model