-
Notifications
You must be signed in to change notification settings - Fork 2
/
cls_model.py
79 lines (54 loc) · 2.12 KB
/
cls_model.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
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, GlobalAveragePooling2D, Dense, BatchNormalization, ReLU
from keras.optimizers import SGD
from keras.losses import categorical_crossentropy
from keras.callbacks import ModelCheckpoint
from utils import *
import numpy as np
def cls_model(input_shape=(28,28,1), without_top=False):
inpt = Input(input_shape)
x = Conv2D(16, 3, strides=1, padding='same')(inpt)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(16, 3, strides=1, padding='same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = MaxPooling2D(pool_size=2, strides=2, padding='same')(x)
x = Conv2D(32, 3, strides=1, padding='same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(32, 3, strides=1, padding='same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = MaxPooling2D(pool_size=2, strides=2, padding='same')(x)
x = Conv2D(64, 3, strides=1, padding='same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(64, 3, strides=1, padding='same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = MaxPooling2D(pool_size=2, strides=2, padding='same')(x)
if without_top:
model = Model(inpt, x)
else:
x = GlobalAveragePooling2D()(x)
# x = Dense(128, activation='relu')(x)
x = Dense(3, activation='softmax')(x)
model = Model(inpt, x)
model.compile(SGD(5e-3,5e-4), loss=categorical_crossentropy, metrics=['acc'])
return model
if __name__ == '__main__':
# train
x_train, y_train = load_training_data()
model = cls_model(input_shape=(28,28,1))
filepath = "ce_cls3_ep_{epoch:02d}_loss_{loss:.3f}.h5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', mode='min',verbose=1, save_weights_only=True)
model.fit(x=x_train, y=y_train, shuffle=True,
batch_size=64, epochs=20,
verbose=1,
callbacks=[checkpoint])
# test
x_test, y_test = load_test_data()
y_pred = model.predict(x_test)
y_pred = np.argmax(y_pred, axis=1)
print('acc:', np.sum(y_test==y_pred) / 300.)