def _triplet_mate_frontalpose_nonmate_top1_probe_mixedpose(n_subjects=32): np.random.seed(42) # for repeatable take vggface2 = VGGFace2('/proj/janus6/vggface2') frontalset = [im for im in vggface2.frontalset(n_frontal=1)] matelist = frontalset[0:n_subjects] if n_subjects == 16: matelist[3] = frontalset[n_subjects+1] matelist[5] = frontalset[n_subjects+5] matelist[6] = frontalset[n_subjects+4] matelist[11] = frontalset[n_subjects+7] matelist[12] = frontalset[n_subjects+2] matelist[13] = frontalset[n_subjects+9] matelist[15] = frontalset[n_subjects+6] d_subjectid_to_topk_frontal_nonmates = vipy.util.load('_vggface2_topk_frontal_nonmates.pkl') # cached nonmateidlist = [] for m in matelist: for n in d_subjectid_to_topk_frontal_nonmates[m.category()]: if n not in nonmateidlist: nonmateidlist.append(n) break d_frontalset = {x.category():x for x in frontalset} # for id lookup nonmatelist = [d_frontalset[k] for k in nonmateidlist] # ordered probelist = [vggface2.take(n_subjects, im_mate.category()) for im_mate in matelist] assert(len(nonmatelist) == n_subjects) assert(len(probelist) == n_subjects) assert(len(probelist[0]) == n_subjects) assert(len(matelist) == n_subjects) return (matelist, nonmatelist, probelist)
def _triplet_mate_frontalpose_nonmate_top1_probe_frontalpose(): n_subjects = 9 np.random.seed(42) # for repeatable take vggface2 = VGGFace2('/proj/janus6/vggface2') frontalset = [im for im in vggface2.frontalset(n_frontal=n_subjects+1)] subjectid = list(set([im.category() for im in frontalset])) # unique matelist = [im for im in frontalset if im.category() in subjectid[0:n_subjects]] d_mate = vipy.util.groupbyasdict(matelist, lambda im: im.category()) matelist = [v[0] for (k,v) in d_mate.items()] probelist = [v[1:] for (k,v) in d_mate.items()] d_subjectid_to_topk_frontal_nonmates = vipy.util.load('_vggface2_topk_frontal_nonmates.pkl') # cached nonmateidlist = [] for m in matelist: for n in d_subjectid_to_topk_frontal_nonmates[m.category()]: if n not in nonmateidlist: # select unique identity from top-k nonmateidlist.append(n) break nonmatelist = [x for x in frontalset if x.category() in nonmateidlist] # ordered d_nonmate = vipy.util.groupbyasdict(nonmatelist, lambda im: im.category()) nonmatelist = [d_nonmate[k][0] for k in nonmateidlist] # ordered assert(len(nonmatelist) == n_subjects) assert(len(probelist) == n_subjects) assert(len(probelist[0]) == n_subjects) assert(len(matelist) == n_subjects) return (matelist, nonmatelist, probelist)
def _all_nonmates(n=None, mateset=set()): np.random.seed(42) # for repeatable take vggface2 = VGGFace2('/proj/janus6/vggface2') subjects = vggface2.subjects() nonmates = subjects if n is None else subjects[0:n] nonmatelist = [next(vggface2.subjectset(s)) for s in nonmates if s not in mateset] return (nonmatelist)
def _n_subjects_k_mates_with_m_probes(n_subjects, k_mates, m_probes, mateset=None): np.random.seed(42) # for repeatable take vggface2 = VGGFace2('/proj/janus6/vggface2') subjects = np.random.choice(vggface2.subjects(), n_subjects) if mateset is None else mateset imsubjects = {s:list(vggface2.subjectset(s)) for s in subjects} matelist = [imsubjects[s][0:k_mates] for s in subjects] probelist = [imsubjects[s][k_mates:m_probes+k_mates] for s in subjects] return (matelist, probelist)
def _k_mates_with_m_probes(n_subjects, n_probes): np.random.seed(42) # for repeatable take vggface2 = VGGFace2('/proj/janus6/vggface2') subjects = np.random.choice(vggface2.subjects(), n_subjects) imsubjects = {s:list(vggface2.subjectset(s)) for s in subjects} matelist = [imsubjects[s][0] for s in subjects] probelist = [imsubjects[s][1:n_probes+1] for s in subjects] return (matelist, probelist)
def _vggface2_topk_nonmates(wb, topk): np.random.seed(42) # for repeatable take n_minibatch = 2 vggface2 = VGGFace2('/proj/janus6/vggface2') imlist = vipy.util.chunklistbysize([im for im in vggface2.take_per_subject(n_minibatch)], n_minibatch) imlist_preprocessed = [torch.cat([wb.net.preprocess(f_detection(im).pil()) for im in iml], dim=0) for iml in imlist] # minibatch tensor X = [torch.squeeze(torch.sum(wb.net.encode(imchunk), dim=0)).detach().numpy() for imchunk in imlist_preprocessed] # minibatch encode template X = vipy.linalg.row_normalized(np.array(X)) X_subjectid = [imchunk[0].category() for imchunk in imlist] d_subjectid_to_topk_frontal_nonmates = {} for (k, d) in enumerate(squareform(pdist(X, metric='euclidean'))): j_sorted = np.argsort(d)[1:] # increasing, do not include self distance=0 on diagonal d_subjectid_to_topk_frontal_nonmates[X_subjectid[k]] = [X_subjectid[j] for j in j_sorted[0:topk]] vipy.util.save(d_subjectid_to_topk_frontal_nonmates, '_vggface2_topk_nonmates.pkl') # cache return d_subjectid_to_topk_frontal_nonmates
def _triplet_mate_frontalpose_nonmate_topk_probe_frontalpose(): n_subjects = 9 vggface2 = VGGFace2('/proj/janus6/vggface2', seed=42) frontalset = [im for im in vggface2.frontalset(n_frontal=n_subjects+1)] subjectid = sorted(list(set([im.category() for im in frontalset]))) # unique matelist = [im for im in frontalset if im.category() in subjectid[0:n_subjects]] d_mate = vipy.util.groupbyasdict(matelist, lambda im: im.category()) matelist = [v[0] for (k,v) in d_mate.items()] probelist = [v[1:] for (k,v) in d_mate.items()] d_subjectid_to_topk_frontal_nonmates = vipy.util.load('_vggface2_topk_nonmates.pkl') # cached nonmateidlist = d_subjectid_to_topk_frontal_nonmates[matelist[8].category()][0:n_subjects] nonmatelist = [vggface2.take(1, k)[0] for k in nonmateidlist] matelist = matelist[8] probelist = [probelist[8]] return (matelist, nonmatelist, probelist)
def _vggface2_nonmates(): np.random.seed(42) # for repeatable take return VGGFace2('/proj/janus6/vggface2').take_per_subject(1)