facenet use triplet-loss to train net,minimizes the distance between anchor and a positive,maximizes the distance between anchor and a negative of a different identity.
while the network is training,the embedding of anchor and positive,negative sample is
change too,there is two solutions to generate triplets,the first is generate triplets offline every n steps,using the most recent network checkpoint and computing the argmin and argmax on a subset of the data,second is generate triplet online,this can be done bye seletcting the hard positive/negative exemplars from within a mini-batch.facenet choice second.every minibatch,according to the current embedding,generate triplets,calculate triplet-loss,update embedding,
minibatch begin,facenet select a group from data set.
facenet use triplet-loss to train net,minimizes the distance between anchor and a positive,maximizes the distance between anchor and a negative of a different identity. while the network is training,the embedding of anchor and positive,negative sample is change too,there is two solutions to generate triplets,the first is generate triplets offline every n steps,using the most recent network checkpoint and computing the argmin and argmax on a subset of the data,second is generate triplet online,this can be done bye seletcting the hard positive/negative exemplars from within a mini-batch.facenet choice second.every minibatch,according to the current embedding,generate triplets,calculate triplet-loss,update embedding,
minibatch begin,facenet select a group from data set.
从数据集中进行抽样图片,参数为训练数据集,每一个batch抽样多少人,每个人抽样多少张
def sample_people(dataset, people_per_batch, images_per_person):
总共应该抽样多少张
nrof_images = people_per_batch * images_per_person
数据集中一共有多少人的图像
nrof_classes = len(dataset)
每个人的索引
class_indices = np.arange(nrof_classes)
随机打乱一下
np.random.shuffle(class_indices) i = 0
保存抽样出来的图像的路径
image_paths = []
抽样的样本是属于哪一个人的,作为label
num_per_class = [] sampled_class_indices = []
Sample images from these classes until we have enough
不断抽样直到达到指定数量
while len(image_paths)<nrof_images:
从第i个人开始抽样
return image_paths, num_per_class
calculate embedding, save to emb_array,according to the array,calculate triplet
多少人,alpha参数
def select_triplets(embeddings, nrof_images_per_class, image_paths, people_per_batch, alpha): """ Select the triplets for training """ trip_idx = 0
某个人的图片的embedding在emb_arr中的开始的索引
emb_start_idx = 0 num_trips = 0 triplets = []
VGG Face: Choosing good triplets is crucial and should strike a balance between
selecting informative (i.e. challenging) examples and swamping training with examples that
are too hard. This is achieve by extending each pair (a, p) to a triplet (a, p, n) by sampling
the image n at random, but only between the ones that violate the triplet loss margin. The
latter is a form of hard-negative mining, but it is not as aggressive (and much cheaper) than
choosing the maximally violating example, as often done in structured output learning.
遍历每一个人
for i in xrange(people_per_batch):
这个人有多少张图片
np.random.shuffle(triplets) return triplets, num_trips, len(triplets)