def train():
    # load row dataset
    with open(config.RAW_DATA_DIR, "r") as f:
        data = f.read()

    # get tokenized data
    tokenized_data = dataset.get_tokenized_data(data)

    # split to train and test data
    train_data, test_data = dataset.split_to_train_and_test_sets(
        tokenized_data)

    # get vocabulary and processed train and test data
    minimum_freq = 2
    train_data_processed, test_data_processed, vocabulary = dataset.preprocess_data(
        train_data, test_data, minimum_freq)
    print(type(vocabulary))
Exemple #2
0
def train(
    dataset,
    job_folder,
    logger,
    video_root_path='/home/miruware/anomaly/anomaly-detection-and-localization/VIDEO_ROOT_PATH'
):
    """Build and train the model
    """
    import yaml
    import numpy as np
    from keras.callbacks import ModelCheckpoint, EarlyStopping
    from custom_callback import LossHistory
    import matplotlib.pyplot as plt
    from keras.utils.io_utils import HDF5Matrix

    logger.debug("Loading configs from {}".format(
        os.path.join(job_folder, 'config.yml')))
    with open(os.path.join(job_folder, 'config.yml'), 'r') as ymlfile:
        cfg = yaml.load(ymlfile)

    nb_epoch = cfg['epochs']
    batch_size = cfg['batch_size']
    loss = cfg['cost']
    optimizer = cfg['optimizer']
    time_length = cfg['time_length']
    # shuffle = cfg['shuffle']

    # logger.info("Building model of type {} and activation {}".format(model_type, activation))
    model = get_model(time_length)
    logger.info("Compiling model with {} and {} optimizer".format(
        loss, optimizer))
    compile_model(model, loss, optimizer)

    logger.info("Saving model configuration to {}".format(
        os.path.join(job_folder, 'model.yml')))
    yaml_string = model.to_yaml()
    with open(os.path.join(job_folder, 'model.yml'), 'w') as outfile:
        yaml.dump(yaml_string, outfile)

    logger.info("Preparing training and testing data")
    preprocess_data(logger, dataset, time_length, video_root_path)
    data = HDF5Matrix(
        os.path.join(video_root_path,
                     '{0}/{0}_train_t{1}.h5'.format(dataset, time_length)),
        'data')

    snapshot = ModelCheckpoint(
        os.path.join(job_folder,
                     'model_snapshot_e{epoch:03d}_{val_loss:.6f}.h5'))
    earlystop = EarlyStopping(patience=5)
    history_log = LossHistory(job_folder=job_folder, logger=logger)

    logger.info("Initializing training...")

    history = model.fit(data,
                        data,
                        batch_size=batch_size,
                        epochs=nb_epoch,
                        validation_split=0.15,
                        shuffle='batch',
                        callbacks=[snapshot, earlystop, history_log])

    logger.info("Training completed!")
    np.save(os.path.join(job_folder, 'train_profile.npy'), history.history)

    n_epoch = len(history.history['loss'])
    logger.info("Plotting training profile for {} epochs".format(n_epoch))
    plt.plot(range(1, n_epoch + 1),
             history.history['val_loss'],
             'g-',
             label='Val Loss')
    plt.plot(range(1, n_epoch + 1),
             history.history['loss'],
             'g--',
             label='Training Loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend()
    plt.savefig(os.path.join(job_folder, 'train_val_loss.png'))
Exemple #3
0
             (1 - hidden_outputs)), np.transpose(train_data))

    def query(self, inputs_list):
        query_inputs = np.array(inputs_list, ndmin=2).T

        hidden_inputs = np.dot(self.weights_input_to_hidden, query_inputs)
        hidden_outputs = activate(hidden_inputs)

        final_inputs = np.dot(self.weights_hidden_to_output, hidden_outputs)
        final_outputs = activate(final_inputs)

        return final_outputs


print('Начало работы\n')
training_data, target = preprocess_data('latin_alphabet/', 260, 1024)
print('Входные данные: ', training_data, '\n')
print('Выходные данные: ', target, '\n')
network = Network(1024, 512, 26)

print("\nПроверка нейронной сети до тренировки на букве 'a'\n")
result = network.query(training_data[2])
for j in range(len(result)):
    print(ALPHABET.get(j) + ") ", result[j])
print("\n")

for e in range(100):
    print('Эпоха: ', e + 1)
    for i in range(len(training_data)):
        inputs = training_data[i]
        targets = np.zeros(26) + 0.01