-
Notifications
You must be signed in to change notification settings - Fork 18
/
__training__.py
85 lines (69 loc) · 1.87 KB
/
__training__.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
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation
from keras.layers import Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras.optimizers import SGD
import keras.callbacks
def design():
np.random.seed(42)
# network design
model = Sequential()
model.add(
Conv2D(
32,
(3, 3),
padding="same",
input_shape=(80, 80, 3),
activation="relu",
)
)
model.add(MaxPooling2D(pool_size=(2, 2))) # 40x40
model.add(Dropout(0.25))
model.add(
Conv2D(
32, (3, 3), padding="same", activation="relu"
)
)
model.add(MaxPooling2D(pool_size=(2, 2))) # 20x20
model.add(Dropout(0.25))
model.add(
Conv2D(
32, (3, 3), padding="same", activation="relu"
)
)
model.add(MaxPooling2D(pool_size=(2, 2))) # 10x10
model.add(Dropout(0.25))
model.add(
Conv2D(
32, (10, 10), padding="same", activation="relu"
)
)
model.add(MaxPooling2D(pool_size=(2, 2))) # 5x5
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(2, activation="softmax"))
return model
def train(X_train, y_train):
model = design()
# optimization setup
sgd = SGD(lr=0.01, momentum=0.9, nesterov=True)
model.compile(
loss="categorical_crossentropy",
optimizer=sgd,
metrics=["accuracy"],
)
# training
model.fit(
X_train,
y_train,
batch_size=32,
epochs=18,
validation_split=0.2,
shuffle=True,
verbose=2,
)
return model