Esempio n. 1
0
def extract_features(arguments, dataset_list):
    PATH = str(arguments.path)
    DATASET = str(arguments.file)
    DESCRIPTOR = str(arguments.desc)
    IMG_WIDTH = int(arguments.width)
    IMG_HEIGHT = int(arguments.height)
    KNOWN_SET_SIZE = float(arguments.known_set_size)
    TRAIN_SET_SIZE = float(arguments.train_set_size)

    matrix_x = []
    matrix_y = []
    matrix_z = []

    vgg_model = None
    if DESCRIPTOR == 'df':
        from vggface import VGGFace
        vgg_model = VGGFace()

    counterA = 0
    for sample in dataset_list:
        sample_path = sample[0]
        sample_name = sample[1]

        subject_path = PATH + sample_path
        subject_image = cv.imread(subject_path, cv.IMREAD_COLOR)

        if DESCRIPTOR == 'hog':
            subject_image = cv.resize(subject_image, (IMG_HEIGHT, IMG_WIDTH))
            feature_vector = Descriptor.get_hog(subject_image)
        elif DESCRIPTOR == 'df':
            feature_vector = Descriptor.get_deep_feature(subject_image,
                                                         vgg_model,
                                                         layer_name='fc6')

        matrix_x.append(feature_vector)
        matrix_y.append(sample_name)
        matrix_z.append(sample_path)

        counterA += 1
        print(counterA, sample_path, sample_name)

    return matrix_z, matrix_y, matrix_x
Esempio n. 2
0
def extract_features(arguments, dataset_list):
    PATH = str(arguments.path)
    DATASET = str(arguments.file)
    DESCRIPTOR = str(arguments.desc)
    IMG_WIDTH = int(arguments.width)
    IMG_HEIGHT = int(arguments.height)

    matrix_x = []
    matrix_y = []
    matrix_z = []

    vgg_model = None
    if DESCRIPTOR == 'df':
        from vggface import VGGFace
        vgg_model = VGGFace()

    counterA = 0
    for sample in dataset_list:
        try:
            sample_path = sample[0]
            sample_name = sample[1]
            subject_path = PATH + sample_path
            subject_image = cv.imread(subject_path, cv.IMREAD_COLOR)

            if DESCRIPTOR == 'hog':
                subject_image = cv.resize(subject_image, (IMG_HEIGHT, IMG_WIDTH))
                feature_vector = Descriptor.get_hog(subject_image)
            elif DESCRIPTOR == 'df':
                feature_vector = Descriptor.get_deep_feature(subject_image, vgg_model, layer_name='fc6')

            matrix_x.append(feature_vector)
            matrix_y.append(sample_name)
            matrix_z.append(sample_path)
            
            print(counterA, sample_path, sample_name, len(feature_vector))

        except Exception, e:
            print(counterA, sample_path + ' not loaded', str(e))
        counterA += 1
def svm_oneclass(args):
    PATH = str(args.path)
    DATASET = str(args.file)
    DESCRIPTOR = str(args.desc)
    NUM_HASH = int(args.hash)
    IMG_WIDTH = int(args.width)
    IMG_HEIGHT = int(args.height)

    matrix_x = []
    matrix_y = []
    models = []
    splits = []
    nmatrix_x = []
    nmatrix_y = []

    x_train = []
    y_train = []
    nx_train = []
    ny_train = []
    plotting_labels = []
    plotting_scores = []

    vgg_model = None
    if DESCRIPTOR == 'df':
        from vggface import VGGFace
        vgg_model = VGGFace()

    print('>> EXPLORING DATASET')
    dataset_list = load_txt_file(PATH + DATASET)
    known_tuples, unknown_tuples = split_known_unknown_sets(dataset_list,
                                                            known_set_size=0.5)
    known_train, known_test = split_train_test_sets(known_tuples,
                                                    train_set_size=0.5)
    print(known_train)

    counterA = 0
    for gallery_sample in known_train:
        sample_path = gallery_sample[0]
        sample_name = gallery_sample[1]

        gallery_path = PATH + sample_path
        gallery_image = cv.imread(gallery_path, cv.IMREAD_COLOR)

        if DESCRIPTOR == 'hog':
            gallery_image = cv.resize(gallery_image, (IMG_HEIGHT, IMG_WIDTH))
            feature_vector = Descriptor.get_hog(gallery_image)
        elif DESCRIPTOR == 'df':
            feature_vector = Descriptor.get_deep_feature(gallery_image,
                                                         vgg_model,
                                                         layer_name='fc6')

        matrix_x.append(feature_vector)
        matrix_y.append(sample_name)

        counterA += 1
        print(counterA, sample_path, sample_name)

    print('>> GENERATING FILES TO SVM')
    counterSVM = 0
    for feature in matrix_x:
        y_train.insert(counterSVM, 1)
        x_train.insert(counterSVM, {})
        count_inner = 0
        for pos in feature:
            x_train[counterSVM].update({count_inner: pos})
            count_inner += 1
        counterSVM += 1

    print('>> GENERATING THE SVM MODEL')
    x_train_total = x_train + nx_train
    y_train_total = y_train + ny_train
    besthit = 0
    bestn = 0
    bestg = 0
    for n in range(1, 50):
        for g in range(-15, 3):
            nu = n / 100
            gamma = pow(2, g)
            parameters = '-s 2 -t 2'
            parameters = parameters + ' -g ' + str(gamma) + ' -n ' + str(nu)
            m = svm_train(y_train_total, x_train_total, parameters)
            hits = 0
            #print('>> LOADING KNOWN PROBE: {0} samples'.format(len(known_test)))
            counterB = 0
            for probe_sample in known_test:
                sample_path = probe_sample[0]
                sample_name = probe_sample[1]
                query_path = PATH + sample_path
                query_image = cv.imread(query_path, cv.IMREAD_COLOR)
                if DESCRIPTOR == 'hog':
                    query_image = cv.resize(query_image,
                                            (IMG_HEIGHT, IMG_WIDTH))
                    feature_vector = Descriptor.get_hog(query_image)
                elif DESCRIPTOR == 'df':
                    feature_vector = Descriptor.get_deep_feature(
                        query_image, vgg_model)
                count_inner = 0
                x_teste = []
                y_teste = []
                y_teste.insert(0, 1)
                x_teste.insert(0, {})
                for pos in feature_vector:
                    x_teste[0].update({count_inner: pos})
                    count_inner += 1
                p_label, p_acc, p_val = svm_predict(y_teste, x_teste, m)
                counterB += 1
                # Getting known set plotting relevant information
                plotting_labels.append([(sample_name, 1)])
                plotting_scores.append([(sample_name, p_label[0])])
                if p_label[0] == 1:
                    hits = hits + 1

            print('>> LOADING UNKNOWN PROBE: {0} samples'.format(
                len(unknown_tuples)))
            counterC = 0
            for probe_sample in unknown_tuples:
                sample_path = probe_sample[0]
                sample_name = probe_sample[1]
                query_path = PATH + sample_path
                query_image = cv.imread(query_path, cv.IMREAD_COLOR)
                if DESCRIPTOR == 'hog':
                    query_image = cv.resize(query_image,
                                            (IMG_HEIGHT, IMG_WIDTH))
                    feature_vector = Descriptor.get_hog(query_image)
                elif DESCRIPTOR == 'df':
                    feature_vector = Descriptor.get_deep_feature(
                        query_image, vgg_model)

                count_inner = 0
                x_teste = []
                y_teste = []
                y_teste.insert(0, -1)
                x_teste.insert(0, {})
                for pos in feature_vector:
                    x_teste[0].update({count_inner: pos})
                    count_inner += 1
                p_label, p_acc, p_val = svm_predict(y_teste, x_teste, m)
                counterC += 1
                # Getting unknown set plotting relevant information
                plotting_labels.append([(sample_name, -1)])
                plotting_scores.append([(sample_name, p_label[0])])
                if p_label[0] == -1:
                    hits = hits + 1
            if hits > besthit:
                besthit = hits
                bestn = nu
                bestg = gamma
    # cmc_score_norm = np.divide(cmc_score, counterA)
    # generate_cmc_curve(cmc_score_norm, DATASET + '_' + str(NUM_HASH) + '_' + DESCRIPTOR)

    print(besthits)
    print(bestn)
    print(bestg)

    pr = generate_precision_recall(plotting_labels, plotting_scores)
    roc = generate_roc_curve(plotting_labels, plotting_scores)
    return pr, roc
Esempio n. 4
0
def plshface(args):
    PATH = str(args.path)
    DATASET = str(args.file)
    DESCRIPTOR = str(args.desc)
    NUM_HASH = int(args.hash)
    IMG_WIDTH = int(args.width)
    IMG_HEIGHT = int(args.height)
    TRAIN_SET_SIZE = float(args.train_set_size)

    matrix_x = []
    matrix_y = []
    splits = []

    plotting_labels = []
    plotting_scores = []

    vgg_model = None
    if DESCRIPTOR == 'df':
        vgg_model = VGGFace()

    print('>> EXPLORING DATASET')
    dataset_list = load_txt_file(PATH + DATASET)
    known_train, known_test = split_train_test_sets(
        dataset_list, train_set_size=TRAIN_SET_SIZE)

    print('>> LOADING GALLERY: {0} samples'.format(len(known_train)))
    counterA = 0
    for gallery_sample in known_train:
        sample_path = gallery_sample[0]
        sample_name = gallery_sample[1]

        gallery_path = PATH + sample_path
        gallery_image = cv.imread(gallery_path, cv.IMREAD_COLOR)

        if DESCRIPTOR == 'hog':
            gallery_image = cv.resize(gallery_image, (IMG_HEIGHT, IMG_WIDTH))
            feature_vector = Descriptor.get_hog(gallery_image)
        elif DESCRIPTOR == 'df':
            feature_vector = Descriptor.get_deep_feature(gallery_image,
                                                         vgg_model,
                                                         layer_name='fc6')

        matrix_x.append(feature_vector)
        matrix_y.append(sample_name)

        counterA += 1
        print(counterA, sample_path, sample_name)

    print('>> SPLITTING POSITIVE/NEGATIVE SETS')
    individuals = list(set(matrix_y))
    cmc_score = np.zeros(len(individuals))
    for index in range(0, NUM_HASH):
        splits.append(generate_pos_neg_dict(individuals))

    print('>> LEARNING PLS MODELS:')
    input_list = itertools.izip(splits, itertools.repeat((matrix_x, matrix_y)))
    models = Parallel(n_jobs=1, verbose=11,
                      backend='threading')(map(delayed(learn_plsh_model),
                                               input_list))

    print('>> LOADING KNOWN PROBE: {0} samples'.format(len(known_test)))
    counterB = 0
    for probe_sample in known_test:
        sample_path = probe_sample[0]
        sample_name = probe_sample[1]

        query_path = PATH + sample_path
        query_image = cv.imread(query_path, cv.IMREAD_COLOR)
        if DESCRIPTOR == 'hog':
            query_image = cv.resize(query_image, (IMG_HEIGHT, IMG_WIDTH))
            feature_vector = Descriptor.get_hog(query_image)
        elif DESCRIPTOR == 'df':
            feature_vector = Descriptor.get_deep_feature(
                query_image, vgg_model)

        vote_dict = dict(map(lambda vote: (vote, 0), individuals))
        for model in models:
            pos_list = [
                key for key, value in model[1].iteritems() if value == 1
            ]
            response = model[0].predict_confidence(feature_vector)
            for pos in pos_list:
                vote_dict[pos] += response
        result = vote_dict.items()
        result.sort(key=lambda tup: tup[1], reverse=True)

        for outer in range(len(individuals)):
            for inner in range(outer + 1):
                if result[inner][0] == sample_name:
                    cmc_score[outer] += 1
                    break

        counterB += 1
        denominator = np.absolute(np.mean([result[1][1], result[2][1]]))
        if denominator > 0:
            output = result[0][1] / denominator
        else:
            output = result[0][1]
        print(counterB, sample_name, result[0][0], output)

        # Getting known set plotting relevant information
        plotting_labels.append([(sample_name, 1)])
        plotting_scores.append([(sample_name, output)])

    cmc_score_norm = np.divide(cmc_score, counterA)
    return cmc_score_norm