我正在尝试实现三重损失功能以进行人脸识别。任何人都可以澄清以下内容:
接下来我创建一个网络:
def create_base_net_simple(input):
x = Flatten()(input)
x = Dense(128, activation='relu')(x)
x = Dropout(0.1)(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.1)(x)
x = Dense(128, activation='relu')(x)
return Model(input, x)
def triplet_merge(inputs):
a, p, n = inputs
return K.sum(a * (p - n), axis=1)
def triplet_merge_shape(input_shapes):
return (input_shapes[0][0], 1)
def triplet_loss(y_true, y_pred):
return -K.mean(K.log(K.sigmoid(y_pred)))
def build_model(n_in):
a = Input(shape=(n_in,))
p = Input(shape=(n_in,))
n = Input(shape=(n_in,))
base_model = create_base_net_simple(Input(shape=(n_in, 1)))
a_model = base_model(a) # anchor
p_model = base_model(p) # pos
n_model = base_model(n) # neg
e = merge([a_model, p_model, n_model], mode=triplet_merge, output_shape=triplet_merge_shape)
model = Model(input=[a, p, n], output=e)
model.compile(loss=triplet_loss, optimizer='rmsprop')
return model
并使用我的数据库对其进行训练:
anch = [...] # array of anchor Encoders (128-vector)
pos = [...] # array of positive Encoders - same person
neg = [...] # array of neg Encoders - random other person
# so anch[i] is encoder of same person as pos[i], but neg[i] is different person
labels = [...] # array of zeros
n_in = 128 # encoder len
model = build_model(n_in)
model.fit([anch, pos, neg], labels, batch_size=2048, nb_epoch=60)
net_name = cur_dir + os.sep + 'TrainedModel.h5'
model.save_weights(net_name)
然后-如果两个随机编码器是同一个人或不同,我该如何预测?像这样:
similarity_lvl = model_predict.predict([enc0, enc1]) # enc0, enc1 - encoders of random persons
我是否必须创建另一个模型并将受过训练的权重加载到其中? (如果是,应该是哪种模型?)