-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
142 lines (103 loc) · 4.98 KB
/
models.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from keras.layers import Input,Conv2D,Activation,Dense,Lambda,Flatten,\
Embedding,PReLU,BatchNormalization
from keras.models import Model
import keras.backend as K
from keras.utils import to_categorical
from keras.optimizers import SGD
import numpy as np
from keras.callbacks import EarlyStopping,ModelCheckpoint
from keras.utils import plot_model
from customLayer import CenterLossLayer
from dataLoader import load_mnist
def raw_cls_model():
inputs = Input((28,28,1))
x = Conv2D(32,(3,3))(inputs)
x = BatchNormalization()(x)
x = PReLU()(x)
x = Conv2D(32,(3,3))(x)
x = BatchNormalization()(x)
x = PReLU()(x)
x = Conv2D(64,(5,5))(x)
x = BatchNormalization()(x)
x = PReLU()(x)
x = Conv2D(64,(5,5))(x)
x = BatchNormalization()(x)
x = PReLU()(x)
x = Conv2D(128,(7,7))(x)
x = BatchNormalization()(x)
x = PReLU()(x)
x = Conv2D(128,(7,7))(x)
x = BatchNormalization()(x)
x = PReLU()(x)
x = Flatten()(x)
x = Dense(2)(x)
out1 = PReLU(name="out1")(x) # 2 dimension for coord represention
out2 = Dense(3,activation="softmax")(out1) # 10 dimension for classification kernel_regularizer=l2(0.0005)
model = Model(inputs, out2)
# plot_model(model, to_file='images/raw_cls_model.png', show_shapes=True, show_layer_names=True)
model.compile(optimizer=SGD(lr=3e-3, momentum=0.9, decay=0.01, nesterov=True),
loss="categorical_crossentropy", metrics=["acc"])
return model
def center_loss_model_embedding():
# branch 1
basic_model = raw_cls_model()
out1 = basic_model.get_layer(name="out1").output # dense2 output
# branch 2
lambda_c = 1
input_ = Input(shape=(1,)) # raw GT label in [0,1,2,...,9]
centers = Embedding(3,2)(input_) # (None, 1, 2)
intra_loss = Lambda(lambda x:K.sum(K.square(x[0]-x[1][:,0]),1,keepdims=True))([out1,centers])
# multi-input, multi-output model
model = Model([basic_model.input,input_],[basic_model.output,intra_loss])
# plot_model(model, to_file='images/center_loss_model_embedding.png', show_shapes=True, show_layer_names=True)
model.compile(optimizer=SGD(lr=5e-3, momentum=0.9, decay=0.01, nesterov=True),
loss=["categorical_crossentropy",lambda y_true,y_pred:y_pred],
loss_weights=[1,lambda_c/2.], metrics=["acc"])
return model
def center_loss_model_custom():
# branch 1
basic_model = raw_cls_model()
out1 = basic_model.get_layer(name="out1").output
# branch 2
input_ = Input(shape=(3,)) # one-hot GT label
intra_loss = CenterLossLayer(alpha=0.5, name='centerlosslayer')([out1, input_])
model = Model([basic_model.input,input_],[basic_model.output,intra_loss])
# plot_model(model, to_file='images/center_loss_model_custom.png', show_shapes=True, show_layer_names=True)
lambda_c = 1
model.compile(optimizer=SGD(lr=3e-4, momentum=0.9, decay=0.01, nesterov=True),
loss=["categorical_crossentropy",lambda y_true,y_pred:y_pred],
loss_weights=[1,lambda_c/2.], metrics=["acc"])
return model
if __name__ == '__main__':
# data
x_train, y_train = load_mnist()
y_train_onehot = to_categorical(y_train)
x_train = np.expand_dims(x_train,axis=-1) # 28,28,1
dummy_matrix = np.zeros((x_train.shape[0],1))
# train raw cls model
# model = raw_cls_model()
# filepath = "./rawmodel_weights_{epoch:02d}_val_acc_{val_acc:.3f}.h5"
# checkpoint = ModelCheckpoint(filepath, verbose=1, monitor="val_loss", mode='min', save_best_only=True)
# model.fit(x_train, y_train_onehot,
# batch_size=512, epochs=10, verbose=1,
# validation_split=0.2,
# callbacks=[checkpoint, EarlyStopping(monitor="val_loss",patience=20)])
# train embedding model
# model = center_loss_model_embedding()
# filepath = "./embedding_weights_{epoch:02d}_val_acc_{val_dense_2_acc:.3f}.h5"
# checkpoint = ModelCheckpoint(filepath, verbose=1, monitor="val_dense_2_loss", mode='min', save_best_only=True)
# model.fit(x=[x_train, y_train],
# y=[y_train_onehot, dummy_matrix],
# batch_size=512, epochs=100, verbose=1,
# validation_split=0.2,
# callbacks=[checkpoint, EarlyStopping(monitor="val_dense_2_loss",patience=20)])
# train custom model
model = center_loss_model_custom()
model.load_weights("custom_weights_08_val_acc_0.937.h5", by_name=True)
filepath = "./custom_weights_{epoch:02d}_val_acc_{val_dense_2_acc:.3f}.h5"
checkpoint = ModelCheckpoint(filepath, verbose=1, monitor="val_dense_2_loss", mode='min', save_best_only=True)
model.fit(x=[x_train, y_train_onehot],
y=[y_train_onehot, dummy_matrix],
batch_size=512, epochs=100, verbose=1,
validation_split=0.2,
callbacks=[checkpoint, EarlyStopping(monitor="val_dense_2_loss",patience=20)])