Beispiel #1
0
def computeMaskMatrix(target_sigset,
                      query_sigset,
                      target_filename,
                      query_filename,
                      symmetric=True):
    '''
    Computes a mask matrix from two sigsets.
    
    @param target_sigset: the target sigset to use.
    @param query_sigset: the query sigset to use.
    @param symmetric: if true and the sigsets are equal it assumes that the matrix is symmetric and will treat the low left triangle as DONT_CARE's.
    @returns: a bee mask matrix.
    '''
    assert len(target_sigset) > 0
    assert len(query_sigset) > 0
    target_subid = np.array([each[0] for each in target_sigset])
    query_subid = np.array([each[0] for each in query_sigset])
    target_recid = np.array([each[1][0]['name'] for each in target_sigset])
    query_recid = np.array([each[1][0]['name'] for each in query_sigset])

    cols = target_subid.shape[0]
    rows = query_subid.shape[0]

    target_subid.shape = (1, cols)
    query_subid.shape = (rows, 1)
    target_recid.shape = (1, cols)
    query_recid.shape = (rows, 1)

    # Initialize matrix to non match
    mat = np.zeros((rows, cols), dtype=np.byte)
    mat[:, :] = pv.BEE_NONMATCH

    # Set matches to match
    matches = target_subid == query_subid
    mat[matches] = pv.BEE_MATCH

    # Set duplicates to don't care.
    duplicates = target_recid == query_recid
    mat[duplicates] = pv.BEE_DONTCARE

    # Check for symetric matrix
    if symmetric and rows == cols:
        ts = target_recid.flatten()
        qs = query_recid.flatten()
        if (ts == qs).sum() == rows:
            # Exclude the lower triangle
            r = np.arange(rows)
            c = np.arange(cols)
            r.shape = (rows, 1)
            c.shape = (1, cols)
            tmp = r > c
            mat[tmp] = pv.BEE_DONTCARE

    return pv.BEEDistanceMatrix(mat, query_filename, target_filename)
Beispiel #2
0
def calc_ROC(experiment_key,
             out_path,
             far_value,
             mask_mtx_cutstom=''
             ):  # ref_file, simi_mtx, mask_mtx_file, mask_mtx_cutstom=''):
    simi_mtx = np.load(out_path + 'facescrub_similarity_matrix.npy')
    mask_mtx_file = np.load(out_path + 'facescrub_similarity_mask_matrix.npy')
    ref_file = 'F:/zn1/znMCM/MsCeleb1M_code/0code_verification_python/matrix_output/pasc_lrpcamax_video_to_video_control_simi.mtx'

    print "Reading Similarity Matrix."
    scores = pv.BEEDistanceMatrix(ref_file)
    try1 = simi_mtx
    scores.matrix = try1

    print "Reading Mask Matrix."
    mask = pv.BEEDistanceMatrix(ref_file)
    try2 = mask_mtx_file
    mask.matrix = try2

    if (mask_mtx_cutstom != ''):
        mtx = np.load(mask_mtx_cutstom)
        mask.matrix = mtx

    print "Checking matrix."
    r, c = scores.matrix.shape
    if np.isfinite(scores.matrix).sum() != r * c:
        print "Warning %d of %d scores are not finite numbers" % (
            r * c - np.isfinite(scores.matrix).sum(), r * c)
        print "   Treating scores like poor matches"
        flat = scores.matrix.flatten()
        idx = np.isfinite(flat)
        mn = flat[idx].min()
        mx = flat[idx].max()
        if scores.is_distance:
            flat[~idx] = mx
        else:
            flat[~idx] = mn
        scores.matrix = flat.reshape(r, c)

    assert np.isfinite(scores.matrix).sum() == r * c

    print "Computing ROC."
    rocdata = scores.getROC(mask=mask)
    # get verification accuracy
    print "Get verification accuracy:"
    accuracy = rocdata.getFAR(far_value).tar
    print("**** FAR=%f, accuracy=%f ****" % (far_value, accuracy))

    # # # Get Data of ROC curve
    # print "Get Data of ROC curve."
    # header, curve = rocdata.getCurve(method=roc.ROC_MATCH_SAMPLED)
    # res_path = out_path + 'roc_' + experiment_key + '_match.csv'
    # f = csv.writer(open(res_path, 'wb'))
    # f.writerow(header)
    # f.writerows(curve)
    # # draw_roc_def.draw_ROC(experiment_key, out_path, ref_file, res_path, plot_others=0)
    # draw_roc_def.draw_ROC(experiment_key, out_path, res_path)

    # # Get impostor and genuine score distribution
    # print "Get score distribution"
    # # m = len(rocdata.match)
    # order = rocdata.match.argsort()
    # match_scores = 0 - rocdata.match[order]
    # fit = stats.norm.pdf(match_scores, np.mean(match_scores), np.std(match_scores))  # this is a fitting indeed
    # # pl.plot(match_scores, fit, linewidth=2, color='blue')
    # # pl.hist(match_scores, 100, normed=True)  # use this to draw histogram of your data
    # pl.plot(match_scores, fit, '-b', label='Genuine')
    #
    # # non match
    # # n = len(rocdata.nonmatch)
    # norder = rocdata.nonmatch.argsort()
    # nonmatch_scores = 0 - rocdata.nonmatch[norder]
    # nfit = stats.norm.pdf(nonmatch_scores, np.mean(nonmatch_scores), np.std(nonmatch_scores))
    # # pl.plot(nonmatch_scores, nfit, linewidth=2, color='red')
    # pl.plot(nonmatch_scores, nfit, '-r', label='Impostor')
    # # pl.hist(nonmatch_scores, 100, normed=True)
    #
    # # leg = []
    # # leg.append('Genuine Scores')
    # # leg.append('Impostor Scores')
    # # pl.legend(leg, loc='upper left')
    # pl.legend(loc='upper left')
    #
    # pl.xlim([-1, 1])
    # # pl.ylim([0.0, 1])
    # pl.xlabel('Match Scores', fontsize=15)
    # pl.ylabel('Statistic number(s)', fontsize=15)
    # pl.title('match score distribution', fontsize=20) #and impostor   genuine
    # pl.grid()
    # pl.show()

    return accuracy  # FAR equals 0.01, 0.001, 0.0001.
Beispiel #3
0
def FRGCExp4Test(database, algorithm, face_detector=None, eye_locator=None, n=None,verbose=10.0,ilog=None):
    ''' 
    Run the FRGC Experiment 4 Test 
    
    On completion this will produce a BEE distance matrix.
    '''
    message_time = time.time()
    timer = pv.Timer()

    # Produce face records for each image in the query set
    query_keys = database.query()
    if n != None:
        query_keys = query_keys[:n]
    query_recs = []
    timer.mark("QueryStart")
    i = 0
    for key in query_keys:
        i += 1
        face = database[key]
        
        face_rec = algorithm.getFaceRecord(face.image,None,face.left_eye,face.right_eye)
        query_recs.append(face_rec)    
        if verbose:
            if time.time() - message_time > verbose:
                message_time = time.time()
                print "Processed query image %d of %d"%(i,len(query_keys))
                
    timer.mark("QueryStop",notes="Processed %d images."%len(query_keys))
    
    
    # Produce face records for each image in the target set
    message_time = time.time()
    target_keys = database.target()
    if n != None:
        target_keys = target_keys[:n]
    target_recs = []
    timer.mark("TargetStart")
    i = 0
    for key in target_keys:
        i += 1
        face = database[key]
        
        face_rec = algorithm.getFaceRecord(face.image,None,face.left_eye,face.right_eye)
        target_recs.append(face_rec)    
        if verbose:
            if time.time() - message_time > verbose:
                message_time = time.time()
                print "Processed target image %d of %d"%(i,len(target_keys))
                
    timer.mark("TargetStop",notes="Processed %d images."%len(target_keys))
    
    print "Finished processing FaceRecs (%d query, %d target)"%(len(query_keys),len(target_keys))
    
    # Compute the  matrix
    print "Computing similarity matrix..."
    timer.mark("SimilarityStart")
    mat = algorithm.similarityMatrix(query_recs,target_recs)
    timer.mark("SimilarityStop",notes="Processed %d comparisons."%(mat.shape[0]*mat.shape[1],))

    print "Completing task..."
    print mat.shape
    
    bee_mat = pv.BEEDistanceMatrix(mat,"FRGC_Exp_2.0.4_Query.xml", "FRGC_Exp_2.0.4_Target.xml", sigset_dir=database.sigset_dir, is_distance=False)
    
    if ilog != None:
        ilog(timer)
        
    return bee_mat, timer