Ejemplo n.º 1
0
def main():
    is_running = True
    while is_running:
        trainer()
        again = get_binary_validation("Do you want to train another model ?",
                                      False)
        if not again:
            is_running = False
Ejemplo n.º 2
0
def tester():
    existing_files = get_file_list("model", "mdl")

    if not existing_files:
        print("No models files found. Come back when you have models.")
        exit()

    target_filename = get_existing_filename(existing_files)
    if len(target_filename) == 0:
        print("No files selected, quitting...")
        exit()

    model = None
    print(target_filename)
    with open("model/" + target_filename, 'rb') as file:
        model = pickle.load(file)

    is_running = True

    while is_running:
        raw_sample = get_single_sample()
        raw_sample.impostor = True
        parser = SampleParser(raw_sample)
        timings = parser.timings
        timings = timings[:timings[-1]]
        try:
            results = model.pipeline.predict([timings])
            if results[0] == -1:
                print('''\u001b[38;5;196m
 █████▒▄▄▄       ██▓ ██▓    ▓█████ ▓█████▄
▓██   ▒▒████▄    ▓██▒▓██▒    ▓█   ▀ ▒██▀ ██▌
▒████ ░▒██  ▀█▄  ▒██▒▒██░    ▒███   ░██   █▌
░▓█▒  ░░██▄▄▄▄██ ░██░▒██░    ▒▓█  ▄ ░▓█▄   ▌
░▒█░    ▓█   ▓██▒░██░░██████▒░▒████▒░▒████▓
 ▒ ░    ▒▒   ▓▒█░░▓  ░ ▒░▓  ░░░ ▒░ ░ ▒▒▓  ▒
 ░       ▒   ▒▒ ░ ▒ ░░ ░ ▒  ░ ░ ░  ░ ░ ▒  ▒
 ░ ░     ░   ▒    ▒ ░  ░ ░      ░    ░ ░  ░
             ░  ░ ░      ░  ░   ░  ░   ░
                                     ░
                \u001b[0m''')
            else:
                print('''\u001b[38;5;76m
███████╗██╗   ██╗ ██████╗ ██████╗███████╗███████╗███████╗
██╔════╝██║   ██║██╔════╝██╔════╝██╔════╝██╔════╝██╔════╝
███████╗██║   ██║██║     ██║     █████╗  ███████╗███████╗
╚════██║██║   ██║██║     ██║     ██╔══╝  ╚════██║╚════██║
███████║╚██████╔╝╚██████╗╚██████╗███████╗███████║███████║
╚══════╝ ╚═════╝  ╚═════╝ ╚═════╝╚══════╝╚══════╝╚══════╝
                \u001b[0m''')
            again = get_binary_validation("Do you want to try again ?", False)
            if not again:
                is_running = False
        except ValueError:
            print("It seems that you made a mistake, try again")
            continue
Ejemplo n.º 3
0
def trainer():
    existing_files = get_file_list()

    if not existing_files:
        print("No samples files found. Come back when you have samples.")
        exit()

    target_filenames = get_existing_filename(existing_files, True)
    if len(target_filenames) == 0:
        print("No files selected, quitting...")
        exit()

    sequence = []
    for target_filename in target_filenames:
        samples = get_sequence_from_file(target_filename)
        sequence.extend(samples)

    timings_sequences = []
    compared_size = None
    print("")

    for raw_sample in sequence:
        parser = SampleParser(raw_sample)
        timings_sequences.append(parser.timings)
        if compared_size is None:
            compared_size = parser.timings[-1]
        else:
            if parser.timings[-1] != compared_size:
                print(
                    "Error, one sample has a different size ({}), removing it".
                    format(parser.timings[-1]))
                del timings_sequences[-1]

    model = Model()

    print("{} samples".format(len(timings_sequences)))

    # Build the data
    trueData = [smp[:smp[-1]] for smp in timings_sequences if smp[-2] == 1]
    fakeData = [smp[:smp[-1]] for smp in timings_sequences if smp[-2] == 0]

    # Split for training/optimization and final evaluation
    train, test = train_test_split(trueData, train_size=0.8, test_size=None)

    print("{} samples from user".format(len(trueData)))
    print("    {:3d} samples for training".format(len(train)))
    print("    {:3d} samples for testing".format(len(test)))
    print("{} samples from impostor\n".format(len(fakeData)))

    spinner = PixelSpinner("Fitting data to the model... ", )
    spinner.start()

    # Create a thread for the spinner
    t = Thread(target=spinner_loop, args=(spinner, ))
    t.do_run = True
    t.start()

    # Train and optimize
    params = Model.findParameters(model, train)

    t.do_run = False
    t.join()
    print("")

    # Print a report on the training/optimization phase
    # evaluate = Model.evaluate(params["model"], train, test)

    # Print a final evaluation of the model agains impostors data
    report = Model.report(params["model"], train, test, fakeData)

    print_report(report)

    save_model = get_binary_validation("Do you want to keep this model ?",
                                       True)

    if save_model:
        filename = get_custom_filename(target_filenames)
        os.makedirs("model", exist_ok=True)
        with open("model/" + filename, 'wb') as file:
            pickle.dump(params["model"], file, pickle.HIGHEST_PROTOCOL)
            print("Model saved in model/" + filename)