예제 #1
0
def encode_PQ_single(input_file, output_dir, fprint_type):
    isLatent = True if (fprint_type).lower() == 'latent' else False
    if isLatent:
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        n = 0
        print("PQ: " + input_file)
        latent_template = template.Bin2Template_Byte_TF_C(input_file, isLatent=True)

        outfile = output_dir + os.path.basename(input_file).split('.')[0] + '.dat'

        Template2Bin_Byte_latent(outfile, version=1, T=latent_template)
    else:
        print("Single template PQ is not available for rolled prints. Please specify an input directory instead.")
예제 #2
0
def training(rolled_template_path, subdim=8, Ks=256):
    rolled_template_files = glob.glob(rolled_template_path + '*.dat')

    rolled_template_files.sort(key=lambda filename: int(''.join(
        filter(str.isdigit, filename.encode("utf-8")))))

    des = None
    for i in range(259, 1000):
        rolled_template = template.Bin2Template_Byte_TF_C(
            rolled_template_files[i], isLatent=False)
        if len(rolled_template.texture_template) < 1:
            continue
        one_des = rolled_template.texture_template[0].des

        if len(one_des) < 100:
            continue
        ind = np.arange(len(one_des))
        shuffle(ind)

        one_des = one_des[::2, :]
        if des is None:
            des = one_des
        else:
            des = np.concatenate((des, one_des), axis=0)

    N, D = des.shape
    assert Ks < N, "the number of training vector should be more than Ks"
    assert D % subdim == 0, "   input dimension must be dividable by nunm_subdim"
    nrof_subdim = int(D / subdim)

    codewords = np.zeros((nrof_subdim, Ks, subdim), dtype=np.float)
    iteration = 20
    for m in range(nrof_subdim):
        des_sub = des[:, m * subdim:(m + 1) * subdim].astype(np.float)
        codewords[m], _ = kmeans2(des_sub, Ks, iter=iteration, minit='points')

    code_dtype = np.uint8 if Ks <= 2**8 else (
        np.uint16 if Ks <= 2**16 else np.uint32)
    codebook = TrainedPQEncoder(codewords, code_dtype)
    return codebook
예제 #3
0
def encode_PQ(input_dir, output_dir, fprint_type):
    dir_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    with open(dir_path + '/afis.config') as config_file:
        config = json.load(config_file)
    embedding_size = 96
    stride = 16
    subdim = 6
    istraining = False
    isLatent = True if (fprint_type).lower() == 'latent' else False
    if isLatent:
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        latent_template_files = glob.glob(input_dir + '*.dat')
        latent_template_files.sort()

        n = 0
        for i, file in enumerate(latent_template_files):
            print("PQ: " + file)
            latent_template = template.Bin2Template_Byte_TF_C(latent_template_files[i], isLatent=True)

            outfile = output_dir + os.path.basename(file).split('.')[0] + '.dat'

            Template2Bin_Byte_latent(outfile, version=1, T=latent_template)
    else:
        code_file = config["CodebookPath"]
        if istraining:
            print(istraining)
        else:

            if not os.path.exists(output_dir):
                os.makedirs(output_dir)

            # load codebook
            with open(code_file, 'rb') as file:
                tmp = struct.unpack('H' * 3, file.read(6))
                nrof_subs, nrof_clusters, sub_dim = tmp
                codewords = np.zeros((nrof_subs, nrof_clusters, sub_dim), dtype=np.float32)
                for i in range(nrof_subs):
                    for j in range(nrof_clusters):
                        tmp = struct.unpack('f' * sub_dim, file.read(4 * sub_dim))
                        codewords[i, j, :] = np.array(list(tmp))

            code_dtype = np.uint8 if nrof_clusters <= 2 ** 8 else (np.uint16 if nrof_clusters <= 2 ** 16 else np.uint32)
            PQEncoder = TrainedPQEncoder(codewords, code_dtype)

            # for rolled
            rolled_template_files = glob.glob(input_dir + '*.dat')
            rolled_template_files.sort(key=lambda filename: int(''.join(filter(str.isdigit, filename.encode("utf-8")))))
            #
            n = 0
            for i, file in enumerate(rolled_template_files):
                print("PQ: " + file)
                rolled_template = template.Bin2Template_Byte_TF_C(rolled_template_files[i], isLatent=False)
                outfile = output_dir + os.path.basename(file).split('.')[0] + '.dat'
                if rolled_template is None or len(rolled_template.texture_template) < 1:
                    with open(outfile, 'wb') as f:
                        tmp = (0,)
                        f.write(struct.pack('H', *tmp))
                    continue
                one_des = rolled_template.texture_template[0].des
                minutiae = rolled_template.texture_template[0].minutiae
                n = n + len(minutiae)
                codes = PQEncoder.encode_multi(one_des)

                rolled_template.texture_template[0].des = codes
                Template2Bin_Byte_PQ_rolled(outfile, version=1, T=rolled_template)
예제 #4
0
def template_compression_single(input_file='',
                                output_dir=None,
                                model_path=None,
                                isLatent=True,
                                config=None):
    if not input_file:
        print("No input file specified for single template processing.")
        return
    if not config and not model_path:
        dir_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
        with open(dir_path + 'config') as config_file:
            config = json.load(config_file)
    if not output_dir:
        output_dir = os.path.dirname(input_file)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # parse the arguments
    random.seed(0)
    torch.manual_seed(0)
    cuda = True

    # Create Model
    # filename_model = './dim_reduction/testmodel'

    model = setup(model_path, cuda)
    batch_size = 128
    nthreads = 8
    assert (output_dir is not None)
    output_file = input_file
    T = template.Bin2Template_Byte_TF_C(input_file, isLatent=isLatent)
    num = len(T.minu_template)
    for j in range(num):
        des = T.minu_template[j].des.copy()
        if len(des) == 0:
            continue
        dataset_feat = datasets.Featarray(des)
        dataloader = torch.utils.data.DataLoader(dataset_feat,
                                                 batch_size=batch_size,
                                                 num_workers=int(nthreads),
                                                 shuffle=False,
                                                 pin_memory=True)
        features = extract_features(model, dataloader, cuda)
        for k in range(features.shape[0]):
            norm = np.linalg.norm(features[k])
            features[k] = features[k] / norm * 1.73
        T.minu_template[j].des = features.copy()
    num = len(T.texture_template)
    for j in range(num):
        des = T.texture_template[j].des.copy()
        if len(des) == 0:
            print("Skipping, length of descriptor is 0.")
            continue
        dataset_feat = datasets.Featarray(des)
        dataloader = torch.utils.data.DataLoader(dataset_feat,
                                                 batch_size=batch_size,
                                                 num_workers=int(nthreads),
                                                 shuffle=False,
                                                 pin_memory=True)
        features = extract_features(model, dataloader, cuda)
        for k in range(features.shape[0]):
            norm = np.linalg.norm(features[k])
            features[k] = features[k] / norm * 1.73
        T.texture_template[j].des = features.copy()
        template.Template2Bin_Byte_TF_C(output_file,
                                        T,
                                        isLatent=isLatent,
                                        save_mask=False)
예제 #5
0
def template_compression(input_dir='',
                         output_dir=None,
                         model_path=None,
                         isLatent=False,
                         config=None):
    if not config and not (input_dir and model_path):
        dir_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
        with open(dir_path + 'config') as config_file:
            config = json.load(config_file)
    if not output_dir:
        output_dir = input_dir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # parse the arguments
    random.seed(0)
    torch.manual_seed(0)
    cuda = True

    import glob
    file_list = glob.glob(input_dir + '*.dat')
    if file_list is None or len(file_list) == 0:
        print("File list empty", file_list)
        return

    file_list.sort(key=lambda filename: int(''.join(
        filter(str.isdigit, filename.encode("utf-8")))))

    # Create Model
    # filename_model = './dim_reduction/testmodel'

    model = setup(model_path, cuda)
    batch_size = 128
    nthreads = 8
    assert (output_dir is not None)
    for i, file in enumerate(file_list):
        if i > 100000:
            break
        print("DR: ", file)

        output_file = output_dir + file.split('/')[-1]
        T = template.Bin2Template_Byte_TF_C(file, isLatent=isLatent)
        num = len(T.minu_template)
        for j in range(num):
            des = T.minu_template[j].des.copy()
            if len(des) == 0:
                continue
            dataset_feat = datasets.Featarray(des)
            dataloader = torch.utils.data.DataLoader(dataset_feat,
                                                     batch_size=batch_size,
                                                     num_workers=int(nthreads),
                                                     shuffle=False,
                                                     pin_memory=True)
            features = extract_features(model, dataloader, cuda)
            for k in range(features.shape[0]):
                norm = np.linalg.norm(features[k])
                features[k] = features[k] / norm * 1.73
            T.minu_template[j].des = features.copy()
        num = len(T.texture_template)
        for j in range(num):
            des = T.texture_template[j].des.copy()
            if len(des) == 0:
                print("Skipping, length of descriptor is 0.")
                continue
            dataset_feat = datasets.Featarray(des)
            dataloader = torch.utils.data.DataLoader(dataset_feat,
                                                     batch_size=batch_size,
                                                     num_workers=int(nthreads),
                                                     shuffle=False,
                                                     pin_memory=True)
            features = extract_features(model, dataloader, cuda)
            for k in range(features.shape[0]):
                norm = np.linalg.norm(features[k])
                features[k] = features[k] / norm * 1.73
            T.texture_template[j].des = features.copy()
            template.Template2Bin_Byte_TF_C(output_file,
                                            T,
                                            isLatent=isLatent,
                                            save_mask=False)