为解决图示问题:论文提出了一种有效的超大型人脸数据集的训练方法(F2C)。
##---------ins dataloader--------------------------------------
instance_db = MultiLMDBDataset(conf.source_lmdb, conf.source_file)
instance_sampler = DistributedSampler(instance_db)
instance_loader = DataLoader(instance_db, conf.batch_size, False, instance_sampler,
num_workers=8, pin_memory=False, drop_last=True)
##----------id dataloader-------------------------------------
id_db = PairLMDBDatasetV2(conf.source_lmdb, conf.source_file)
id_sampler = DistributedSampler(id_db)
id_loader = DataLoader(id_db, conf.batch_size, False, id_sampler, num_workers=8,
pin_memory=False, drop_last=True)
##-----------------------------------------------------------------
#训练技巧 利用python next iter 方法,在多任务或者多分枝中常用此方法迭代多个dataloader
try:
images1, images2, id_indexes = next(id_iter)
except:
id_iter = iter(id_loader)
images1, images2, id_indexes = next(id_iter)
class FFC(Module):
def __init__(self, net_type, feat_dim, queue_size=7409, scale=32.0, loss_type='AM',
margin=0.4, momentum=0.99,
neg_margin=0.25, pretrained_model_path=None, num_class=None):
super(FFC, self).__init__()
assert loss_type in ('AM', 'Arc', 'SV')
self.probe_net = create_net(net_type, feat_dim=feat_dim, fp16=True)
self.gallery_net = create_net(net_type, feat_dim=feat_dim, fp16=True)
3 PNet和GNe使用相同网络权重如何共享:Moveing avg
权重mov avg计算:
##-------------------Pnet 和Gnet params 如何整合输出到一个网络中
for param_p, param_g in zip(self.probe_net.parameters(), self.gallery_net.parameters()):
param_g.data = param_g.data * self.m + param_p.data * (1. - self.m)
##-----------参数copy 出-------------------------------
for param_p, param_g in zip(self.probe_net.parameters(), self.gallery_net.parameters()):
param_g.data.copy_(param_p.data) # initialize
param_g.requires_grad = False # not update by gradient
if pos_label_idx.numel() > 0:
pos_cos_theta = cos_theta[pos_label_idx].float()
pos_label = label[pos_label_idx]
batch_size = pos_cos_theta.shape[0]
gt = pos_cos_theta[torch.arange(0, batch_size), pos_label].view(-1, 1)
sin_theta = torch.sqrt(1.0 - torch.pow(gt, 2))
cos_theta_m = gt * math.cos(self.margin) - sin_theta * math.sin(self.margin)
pos_cos_theta.scatter_(1, pos_label.data.view(-1, 1), cos_theta_m)
cls_loss = F.cross_entropy(pos_cos_theta * self.scale, pos_label)
else:
cls_loss = 0
if outlier_label.numel() > 0:
outlier_cos_theta = cos_theta[outlier_label]
outlier_idx = torch.argsort(outlier_cos_theta, dim=1, descending=True)[:, :self.hard_neg]
hard_negative = torch.clip(torch.gather(outlier_cos_theta, 1, outlier_idx), 0)
neg_loss = torch.mean(hard_negative)
else:
neg_loss = 0
loss = cls_loss + neg_loss
return loss
5.其他:
训练数据加载加速,将数据先生成lmdb;相关脚本代码中,该方法类caffe训练也算人脸实际训练中偶用方法吧;
注:下班后扫读,如果个人理解有误,再及时更正。