Exemple #1
0
    def __init__(self, config, data_loader=None):
        autoencoder_config = toml.load(open(config["network"]))

        self._batch_size = config["batch_size"]
        self._epoch = config["epoch"]
        self._val_step = config["val_step"]
        self._use_gpu = config["use_gpu"]
        self._save_model_path = config["save_model_path"]
        self._save_model_name = config["save_model_name"]

        self._data_loader = data_loader
        self._label_name = self._data_loader.get_label_name_list()

        self._auto_encoder = AutoEncoder(
            param=config,
            config=autoencoder_config,
            image_info=self._data_loader.get_image_info())
        self._auto_encoder.set_model()

        if self._use_gpu:
            config_system = tf.compat.v1.ConfigProto(
                gpu_options=tf.compat.v1.GPUOptions(
                    per_process_gpu_memory_fraction=0.8, allow_growth=True))
        else:
            config_system = tf.compat.v1.ConfigProto(device_count={'GPU': 0})

        self._sess = tf.compat.v1.Session(config=config_system)
        init = tf.compat.v1.global_variables_initializer()
        self._sess.run(init)
        self._saver = tf.compat.v1.train.Saver()

        self._accuracy = 0.0
        self._loss = Loss()
        self._tensorboard_path = "./logs/" + datetime.today().strftime(
            '%Y-%m-%d-%H-%M-%S')
Exemple #2
0
    def __init__(self, config, data_loader=None):
        autoencoder_config = toml.load(open(config["network"]))

        self._use_gpu = config["use_gpu"]
        self._saved_model_path = config["save_model_path"]
        self._saved_model_name = config["save_model_name"]
    
        self._data_loader = data_loader
        self._label_name = self._data_loader.get_label_name_list()
        self._image_width, self._image_height, self._images_channel = self._data_loader.get_image_info()

        self._auto_encoder = AutoEncoder(param=config, config=autoencoder_config,
                        image_info=self._data_loader.get_image_info())
        self._auto_encoder.set_model()

        if self._use_gpu:
            config_system = tf.compat.v1.ConfigProto(
                gpu_options=tf.compat.v1.GPUOptions(
                    per_process_gpu_memory_fraction=0.8,
                    allow_growth=True
                )
            )
        else:
            config_system = tf.compat.v1.ConfigProto(
                device_count = {'GPU': 0}
            )

        self._sess = tf.compat.v1.Session(config=config_system)
        self._saver = tf.compat.v1.train.Saver()
        self._saver.restore(self._sess, self._saved_model_path + "/" + self._saved_model_name)
Exemple #3
0
class Inferencer(object):
    def __init__(self, config, data_loader=None):
        autoencoder_config = toml.load(open(config["network"]))

        self._use_gpu = config["use_gpu"]
        self._saved_model_path = config["save_model_path"]
        self._saved_model_name = config["save_model_name"]
    
        self._data_loader = data_loader
        self._label_name = self._data_loader.get_label_name_list()
        self._image_width, self._image_height, self._images_channel = self._data_loader.get_image_info()

        self._auto_encoder = AutoEncoder(param=config, config=autoencoder_config,
                        image_info=self._data_loader.get_image_info())
        self._auto_encoder.set_model()

        if self._use_gpu:
            config_system = tf.compat.v1.ConfigProto(
                gpu_options=tf.compat.v1.GPUOptions(
                    per_process_gpu_memory_fraction=0.8,
                    allow_growth=True
                )
            )
        else:
            config_system = tf.compat.v1.ConfigProto(
                device_count = {'GPU': 0}
            )

        self._sess = tf.compat.v1.Session(config=config_system)
        self._saver = tf.compat.v1.train.Saver()
        self._saver.restore(self._sess, self._saved_model_path + "/" + self._saved_model_name)


    def inference(self):
        input_images, input_labels = self._data_loader.get_test_data()
        output_images = self._auto_encoder.test(self._sess, input_images)

        for num, (input_image, output_image) in enumerate(zip(input_images, output_images)):
            image = input_image*255
            cv2.imwrite("{}_input.png".format(num), image)
            image = output_image.reshape(28,28,1)*255
            cv2.imwrite("{}_output.png".format(num), image)
Exemple #4
0
def load_model(directory):
    model = AutoEncoder()
    model.load_state_dict(directory)
    return model
Exemple #5
0
    embedded_movie_contents = np.zeros((len(movie_content), len(dictionary)))
    for movie_id, value in tqdm(movie_content.items()):
        embeddings = embedding_helper(value, model="bow", dictionary=dictionary).get_embedding()
        embedded_movie_contents[data_manager.get_index_from_movie_id(movie_id)] = embeddings  # array start at 0
    np.save('data/embedded_movie_content.npy', embedded_movie_contents)

print("\nEncoding content")

if encoded_movie_path:
    print(encoded_movie_path)
    encoded_movie_contents = np.load(encoded_movie_path)
else:
    print(embedded_movie_contents.shape)
    intermediate_size = int(embedded_movie_contents.shape[1])
    encoded_size = int(intermediate_size)
    ae = AutoEncoder(embedded_movie_contents, validation_perc=0.2, lr=1e-3, intermediate_size=5000, encoded_size=100,
                     is_enable_bath_norm=True)
    ae.train_loop(epochs=60)
    encoded_movie_contents = ae.get_encoded_representations()
    ae.save_encoder()
    ae.save_decoder()
    print(encoded_movie_contents.shape)
    np.save('data/encoded_movie_contents.npy', encoded_movie_contents)

    losses = pd.DataFrame(data=list(zip(ae.train_losses, ae.val_losses)), columns=['train_loss', 'validation_loss'])
    losses['epoch'] = (losses.index + 1)

    fig, ax = plt.subplots()
    ax.plot(losses['epoch'], losses['train_loss'], label='train_loss')
    ax.plot(losses['epoch'], losses['validation_loss'], label='validation_loss')
    ax.set_ylabel('MSE loss')
    ax.set_xlabel('epoch')
from model.autoencoder import AutoEncoder
import numpy as np

all_data = np.load(
    "/content/drive/My Drive/python_code/project/data/similarity_matrix.npy")
ae = AutoEncoder(all_data,
                 validation_perc=0.1,
                 lr=1e-3,
                 intermediate_size=100,
                 encoded_size=50,
                 is_enable_bath_norm=True)
ae.train_loop(epochs=5)
    cfg_dataset = config.hyper_parameters["dataset"]
    cfg_dataset["batch_size"] = args.batch_size
    dataloader_train = DataLoader(dataset_train, **cfg_dataset)
    dataloader_val = None if dataset_val is None else DataLoader(
        dataset_val, **cfg_dataset)

    # instanciate autoencoder
    hyper_parameters = config.hyper_parameters
    cfg_model = config.hyper_parameters["model"]
    cfg_model_enc_dec = cfg_model["encoder-decoder"]
    cfg_model_enc_dec["n_dim_emb"] = cfg_model_enc_dec.get(
        "n_dim_emb", dataset.n_dim)
    encoder = SimpleEncoder(**cfg_model_enc_dec)
    decoder = SimpleDecoder(**cfg_model_enc_dec)
    model = AutoEncoder(encoder=encoder,
                        decoder=decoder,
                        **cfg_model["autoencoder"])

    # instanciate trainer
    cfg_trainer = config.hyper_parameters["trainer"]
    trainer = UnsupervisedTrainer(model=model,
                                  dataloader_train=dataloader_train,
                                  dataloader_val=dataloader_val,
                                  **cfg_trainer)

    # instanciate experiment system
    cfg_system = config.experiment_system
    cfg_system["max_nb_epochs"] = args.epochs
    cfg_system["gpus"] = args.gpus
    cfg_system["checkpoint_callback"].update(filepath=args.model_dir,
                                             prefix=args.experiment_name)
Exemple #8
0
from utils.load_data import load_images
from utils.train_model import training
from utils.visualise_outputs import visualise_reconstruction_loss, plot_training_loss

# Define parameters
directory_dict = {
    'DATA_DIR': '/Users/alessandropreviero/Downloads',
    'MODEL_DIR': '/Users/alessandropreviero/PycharmProjects/deep-leukemia-detection/src/saved/ae.pt',
    'RECON_DIR': "reconstructions.png",
    'TRAIN_LOSS_DIR': "training_loss.png"
}

max_epochs = 5
hyperparameters = {'lr': 1e-3, 'l1': 1e-3}
train_loader, validation_loader = load_images(directory_dict['DATA_DIR'], train_split=0.5, batch_size=256)
sparse_ae = AutoEncoder()


# Run example
def run_model(model, train, validation, epochs, hyper_params, directories):

    model_output, losses_vector = training(model, train, validation, epochs, hyper_params, directories, to_print=2)
    visualise_reconstruction_loss(model_output, directories['RECON_DIR'], max_epochs)
    plot_training_loss(losses_vector[0], directories['TRAIN_LOSS_DIR'])


if __name__ == "__main__":
    run_model(sparse_ae,
              train_loader,
              validation_loader,
              max_epochs,
Exemple #9
0
def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu
    # preprocessing class
    pre_process = MinMaxNormalization01()
    print('load train, validate, test data...')
    split = [17520, 4416, 4368]
    data, train_data, val_data, test_data = load_npy_data(
        filename=['data/citybike/p_map.npy', 'data/citybike/d_map.npy'],
        split=split)
    # data: [num, row, col, channel]
    print('preprocess train data...')
    pre_process.fit(train_data)

    if 'ResNet' in FLAGS.model:
        pre_index = max(FLAGS.closeness * 1, FLAGS.period * 7,
                        FLAGS.trend * 7 * 24)
        all_timestamps = gen_timestamps(['2013', '2014', '2015', '2016'])
        all_timestamps = all_timestamps[4344:-4416]
        data = pre_process.transform(data)
        # train_data = train_data
        train_data = data[:split[0]]
        val_data = data[split[0] - pre_index:split[0] + split[1]]
        test_data = data[split[0] + split[1] - pre_index:split[0] + split[1] +
                         split[2]]
        # get train, validate, test timestamps
        train_timestamps = all_timestamps[:split[0]]
        val_timestamps = all_timestamps[split[0] - pre_index:split[0] +
                                        split[1]]
        test_timestamps = all_timestamps[split[0] + split[1] -
                                         pre_index:split[0] + split[1] +
                                         split[2]]
        # get x, y
        train_x, train_y = batch_data_cpt_ext(train_data,
                                              train_timestamps,
                                              batch_size=FLAGS.batch_size,
                                              close=FLAGS.closeness,
                                              period=FLAGS.period,
                                              trend=FLAGS.trend)
        val_x, val_y = batch_data_cpt_ext(val_data,
                                          val_timestamps,
                                          batch_size=FLAGS.batch_size,
                                          close=FLAGS.closeness,
                                          period=FLAGS.period,
                                          trend=FLAGS.trend)
        test_x, test_y = batch_data_cpt_ext(test_data,
                                            test_timestamps,
                                            batch_size=FLAGS.batch_size,
                                            close=FLAGS.closeness,
                                            period=FLAGS.period,
                                            trend=FLAGS.trend)
        train = {'x': train_x, 'y': train_y}
        val = {'x': val_x, 'y': val_y}
        test = {'x': test_x, 'y': test_y}
        nb_flow = train_data.shape[-1]
        row = train_data.shape[1]
        col = train_data.shape[2]
        if FLAGS.model == 'AttResNet':
            print('k-means to cluster...')
            model_path = 'citybike-results/model_save/AttResNet/'
            log_path = 'citybike-results/log/AttResNet/'
            if FLAGS.pre_saved_cluster:
                cluster_centroid = np.load(model_path + 'cluster_centroid.npy')
            else:
                vector_data = np.reshape(train_data, (train_data.shape[0], -1))
                # init_vectors = vector_data[:FLAGS.cluster_num, :]
                # cluster_centroid = init_vectors
                kmeans = KMeans(n_clusters=FLAGS.cluster_num,
                                init='random',
                                n_init=FLAGS.kmeans_run_num,
                                tol=0.00000001).fit(vector_data)
                cluster_centroid = kmeans.cluster_centers_
                # reshape to [cluster_num, row, col, channel]
                cluster_centroid = np.reshape(
                    cluster_centroid,
                    (-1, train_data.shape[1], train_data.shape[2],
                     train_data.shape[3]))
                if not os.path.exists(model_path):
                    os.makedirs(model_path)
                if not os.path.exists(log_path):
                    os.makedirs(log_path)
                np.save(model_path + 'cluster_centroid.npy', cluster_centroid)
            print('build AttResNet model...')
            model = AttResNet(input_conf=[[FLAGS.closeness, nb_flow, row, col],
                                          [FLAGS.period, nb_flow, row, col],
                                          [FLAGS.trend, nb_flow, row, col],
                                          [8]],
                              att_inputs=cluster_centroid,
                              att_nodes=FLAGS.att_nodes,
                              att_layer=['conv', 'conv'],
                              att_layer_param=[[[3, 3], [1, 1, 1, 1], 8],
                                               [[3, 3], [1, 1, 1, 1], 2]],
                              batch_size=FLAGS.batch_size,
                              layer=['conv', 'res_net', 'conv'],
                              layer_param=[[[3, 3], [1, 1, 1, 1], 64],
                                           [
                                               3,
                                               [[[3, 3], [1, 1, 1, 1], 64],
                                                [[3, 3], [1, 1, 1, 1], 64]]
                                           ], [[3, 3], [1, 1, 1, 1], 2]])
        else:
            print('build ResNet model...')
            model_path = 'citybike-results/model_save/ResNet/'
            log_path = 'citybike-results/log/ResNet/'
            model = ResNet(input_conf=[[FLAGS.closeness, nb_flow, row, col],
                                       [FLAGS.period, nb_flow, row, col],
                                       [FLAGS.trend, nb_flow, row, col], [8]],
                           batch_size=FLAGS.batch_size,
                           layer=['conv', 'res_net', 'conv'],
                           layer_param=[[[3, 3], [1, 1, 1, 1], 64],
                                        [
                                            3,
                                            [[[3, 3], [1, 1, 1, 1], 64],
                                             [[3, 3], [1, 1, 1, 1], 64]]
                                        ], [[3, 3], [1, 1, 1, 1], 2]])
        print('model solver...')
        solver = ModelSolver(
            model,
            train,
            val,
            preprocessing=pre_process,
            n_epochs=FLAGS.n_epochs,
            batch_size=FLAGS.batch_size,
            update_rule=FLAGS.update_rule,
            learning_rate=FLAGS.lr,
            save_every=FLAGS.save_every,
            pretrained_model=FLAGS.pretrained_model,
            model_path=model_path,
            test_model='citybike-results/model_save/ResNet/model-' +
            str(FLAGS.n_epochs),
            log_path=log_path,
            cross_val=False,
            cpt_ext=True)
        if FLAGS.train:
            print('begin training...')
            test_n = {'data': test_data, 'timestamps': test_timestamps}
            _, test_prediction = solver.train(test,
                                              test_n,
                                              output_steps=FLAGS.output_steps)
            # get test_target and test_prediction
            i = pre_index
            test_target = []
            while i < len(test_data) - FLAGS.output_steps:
                test_target.append(test_data[i:i + FLAGS.output_steps])
                i += 1
            test_target = np.asarray(test_target)
            # np.save('results/ResNet/test_target.npy', test_target)
            # np.save('results/ResNet/test_prediction.npy', test_prediction)
        if FLAGS.test:
            print('begin testing for predicting next 1 step')
            solver.test(test)
            # test 1 to n
            print('begin testing for predicting next ' +
                  str(FLAGS.output_steps) + ' steps')
            test_n = {'data': test_data, 'timestamps': test_timestamps}
            solver.test_1_to_n(test_n)
            #solver.test_1_to_n(test_n, n=FLAGS.output_steps, close=FLAGS.closeness, period=FLAGS.period, trend=FLAGS.trend)
    else:
        train_data = pre_process.transform(train_data)
        train_x, train_y = batch_data(data=train_data,
                                      batch_size=FLAGS.batch_size,
                                      input_steps=FLAGS.input_steps,
                                      output_steps=FLAGS.output_steps)
        val_data = pre_process.transform(val_data)
        val_x, val_y = batch_data(data=val_data,
                                  batch_size=FLAGS.batch_size,
                                  input_steps=FLAGS.input_steps,
                                  output_steps=FLAGS.output_steps)
        test_data = pre_process.transform(test_data)
        test_x, test_y = batch_data(data=test_data,
                                    batch_size=FLAGS.batch_size,
                                    input_steps=FLAGS.input_steps,
                                    output_steps=FLAGS.output_steps)
        train = {'x': train_x, 'y': train_y}
        val = {'x': val_x, 'y': val_y}
        test = {'x': test_x, 'y': test_y}
        input_dim = [
            train_data.shape[1], train_data.shape[2], train_data.shape[3]
        ]
        if FLAGS.model == 'ConvLSTM':
            print('build ConvLSTM model...')
            model = ConvLSTM(input_dim=input_dim,
                             batch_size=FLAGS.batch_size,
                             layer={
                                 'encoder':
                                 ['conv', 'conv', 'conv_lstm', 'conv_lstm'],
                                 'decoder':
                                 ['conv_lstm', 'conv_lstm', 'conv', 'conv']
                             },
                             layer_param={
                                 'encoder': [[[3, 3], [1, 1, 1, 1], 8],
                                             [[3, 3], [1, 1, 1, 1], 16],
                                             [[16, 16], [3, 3], 64],
                                             [[16, 16], [3, 3], 64]],
                                 'decoder': [[[16, 16], [3, 3], 64],
                                             [[16, 16], [3, 3], 64],
                                             [[3, 3], [1, 1, 1, 1], 8],
                                             [[3, 3], [1, 1, 1, 1], 2]]
                             },
                             input_steps=10,
                             output_steps=10)
            print('model solver...')
            solver = ModelSolver(
                model,
                train,
                val,
                preprocessing=pre_process,
                n_epochs=FLAGS.n_epochs,
                batch_size=FLAGS.batch_size,
                update_rule=FLAGS.update_rule,
                learning_rate=FLAGS.lr,
                save_every=FLAGS.save_every,
                pretrained_model=FLAGS.pretrained_model,
                model_path='citybike-results/model_save/ConvLSTM/',
                test_model='citybike-results/model_save/ConvLSTM/model-' +
                str(FLAGS.n_epochs),
                log_path='citybike-results/log/ConvLSTM/')
        elif 'AttConvLSTM' in FLAGS.model:
            # train_data: [num, row, col, channel]
            if FLAGS.use_ae:
                # auto-encoder to cluster train_data
                print('auto-encoder to cluster...')
                model_path = 'citybike-results/model_save/AEAttConvLSTM/'
                log_path = 'citybike-results/log/AEAttConvLSTM/'
                if FLAGS.pre_saved_cluster:
                    cluster_centroid = np.load(model_path +
                                               'cluster_centroid.npy')
                else:
                    ae = AutoEncoder(input_dim=input_dim,
                                     z_dim=[4, 4, 16],
                                     layer={
                                         'encoder': ['conv', 'conv'],
                                         'decoder': ['conv', 'conv']
                                     },
                                     layer_param={
                                         'encoder': [[[3, 3], [1, 2, 2, 1], 8],
                                                     [[3, 3], [1, 2, 2, 1],
                                                      16]],
                                         'decoder': [[[3, 3], [1, 2, 2, 1], 8],
                                                     [[3, 3], [1, 2, 2, 1], 2]]
                                     },
                                     model_save_path=model_path,
                                     batch_size=FLAGS.batch_size)
                    if FLAGS.ae_train:
                        ae.train(train_data,
                                 batch_size=FLAGS.batch_size,
                                 learning_rate=FLAGS.lr,
                                 n_epochs=20,
                                 pretrained_model=FLAGS.ae_pretrained_model)
                    train_z_data = ae.get_z(
                        train_data, pretrained_model=FLAGS.ae_pretrained_model)
                    print train_z_data.shape
                    # k-means to cluster train_z_data
                    vector_data = np.reshape(train_z_data,
                                             (train_z_data.shape[0], -1))
                    # save vector data to visualize
                    np.save(model_path + 'vector_data.npy', vector_data)
                    kmeans = KMeans(n_clusters=FLAGS.cluster_num,
                                    init='random',
                                    n_init=FLAGS.kmeans_run_num,
                                    tol=0.00000001).fit(vector_data)
                    cluster_centroid = kmeans.cluster_centers_
                    print np.array(cluster_centroid).shape
                    # reshape to [cluster_num, row, col, channel]
                    cluster_centroid = np.reshape(
                        cluster_centroid,
                        (-1, train_z_data.shape[1], train_z_data.shape[2],
                         train_z_data.shape[3]))
                    # decoder to original space
                    cluster_centroid = ae.get_y(
                        cluster_centroid,
                        pretrained_model=FLAGS.ae_pretrained_model)
                    print cluster_centroid.shape
                    np.save(model_path + 'cluster_centroid.npy',
                            cluster_centroid)
            else:
                # k-means to cluster train_data
                print('k-means to cluster...')
                model_path = 'citybike-results/model_save/' + FLAGS.model + '/'
                log_path = 'citybike-results/log/' + FLAGS.model + '/'
                if not os.path.exists(model_path):
                    os.makedirs(model_path)
                if not os.path.exists(log_path):
                    os.makedirs(log_path)
                if FLAGS.pre_saved_cluster:
                    cluster_centroid = np.load(model_path +
                                               'cluster_centroid.npy')
                else:
                    vector_data = np.reshape(train_data,
                                             (train_data.shape[0], -1))
                    #init_vectors = vector_data[:FLAGS.cluster_num, :]
                    #cluster_centroid = init_vectors
                    kmeans = KMeans(n_clusters=FLAGS.cluster_num,
                                    init='random',
                                    n_init=FLAGS.kmeans_run_num,
                                    tol=0.00000001).fit(vector_data)
                    cluster_centroid = kmeans.cluster_centers_
                    # reshape to [cluster_num, row, col, channel]
                    cluster_centroid = np.reshape(
                        cluster_centroid,
                        (-1, train_data.shape[1], train_data.shape[2],
                         train_data.shape[3]))
                    np.save(model_path + 'cluster_centroid.npy',
                            cluster_centroid)
            # build model
            print 'build ' + FLAGS.model + ' model...'
            if FLAGS.model == 'AttConvLSTM':
                model = AttConvLSTM(
                    input_dim=input_dim,
                    att_inputs=cluster_centroid,
                    att_nodes=FLAGS.att_nodes,
                    batch_size=FLAGS.batch_size,
                    layer={
                        'encoder': ['conv', 'conv', 'conv_lstm', 'conv_lstm'],
                        'decoder': ['conv_lstm', 'conv_lstm', 'conv', 'conv'],
                        'attention': ['conv', 'conv']
                    },
                    layer_param={
                        'encoder': [[[3, 3], [1, 1, 1, 1], 8],
                                    [[3, 3], [1, 1, 1, 1], 16],
                                    [[16, 16], [3, 3], 64],
                                    [[16, 16], [3, 3], 64]],
                        'decoder': [[[16, 16], [3, 3], 64],
                                    [[16, 16], [3, 3], 64],
                                    [[3, 3], [1, 1, 1, 1], 8],
                                    [[3, 3], [1, 1, 1, 1], 2]],
                        'attention': [[[3, 3], [1, 1, 1, 1], 8],
                                      [[3, 3], [1, 1, 1, 1], 16]]
                    },
                    input_steps=10,
                    output_steps=10)
            elif FLAGS.model == 'MultiAttConvLSTM':
                model = MultiAttConvLSTM(
                    input_dim=input_dim,
                    att_inputs=cluster_centroid,
                    att_nodes=FLAGS.att_nodes,
                    batch_size=FLAGS.batch_size,
                    layer={
                        'encoder': ['conv', 'conv', 'conv_lstm', 'conv_lstm'],
                        'decoder': ['conv_lstm', 'conv_lstm', 'conv', 'conv'],
                        'attention': ['conv', 'conv']
                    },
                    layer_param={
                        'encoder': [[[3, 3], [1, 1, 1, 1], 8],
                                    [[3, 3], [1, 1, 1, 1], 16],
                                    [[16, 16], [3, 3], 64],
                                    [[16, 16], [3, 3], 64]],
                        'decoder': [[[16, 16], [3, 3], 64],
                                    [[16, 16], [3, 3], 64],
                                    [[3, 3], [1, 1, 1, 1], 8],
                                    [[3, 3], [1, 1, 1, 1], 2]],
                        'attention': [[[3, 3], [1, 1, 1, 1], 8],
                                      [[3, 3], [1, 1, 1, 1], 16]]
                    },
                    input_steps=10,
                    output_steps=10)
            print('model solver...')
            solver = ModelSolver(model,
                                 train,
                                 val,
                                 preprocessing=pre_process,
                                 n_epochs=FLAGS.n_epochs,
                                 batch_size=FLAGS.batch_size,
                                 update_rule=FLAGS.update_rule,
                                 learning_rate=FLAGS.lr,
                                 save_every=FLAGS.save_every,
                                 pretrained_model=FLAGS.pretrained_model,
                                 model_path=model_path,
                                 test_model=model_path + 'model-' +
                                 str(FLAGS.n_epochs),
                                 log_path=log_path)
        if FLAGS.train:
            print('begin training...')
            test_prediction, _ = solver.train(test)
            test_target = np.asarray(test_y)
        if FLAGS.test:
            print('test trained model...')
            solver.test_model = solver.model_path + FLAGS.pretrained_model
            test_prediction = solver.test(test)
            test_target = np.asarray(test_y)
    np.save('citybike-results/results/' + FLAGS.model + '/test_target.npy',
            test_target)
    np.save('citybike-results/results/' + FLAGS.model + '/test_prediction.npy',
            test_prediction)
Exemple #10
0
class Trainer(object):
    def __init__(self, config, data_loader=None):
        autoencoder_config = toml.load(open(config["network"]))

        self._batch_size = config["batch_size"]
        self._epoch = config["epoch"]
        self._val_step = config["val_step"]
        self._use_gpu = config["use_gpu"]
        self._save_model_path = config["save_model_path"]
        self._save_model_name = config["save_model_name"]

        self._data_loader = data_loader
        self._label_name = self._data_loader.get_label_name_list()

        self._auto_encoder = AutoEncoder(
            param=config,
            config=autoencoder_config,
            image_info=self._data_loader.get_image_info())
        self._auto_encoder.set_model()

        if self._use_gpu:
            config_system = tf.compat.v1.ConfigProto(
                gpu_options=tf.compat.v1.GPUOptions(
                    per_process_gpu_memory_fraction=0.8, allow_growth=True))
        else:
            config_system = tf.compat.v1.ConfigProto(device_count={'GPU': 0})

        self._sess = tf.compat.v1.Session(config=config_system)
        init = tf.compat.v1.global_variables_initializer()
        self._sess.run(init)
        self._saver = tf.compat.v1.train.Saver()

        self._accuracy = 0.0
        self._loss = Loss()
        self._tensorboard_path = "./logs/" + datetime.today().strftime(
            '%Y-%m-%d-%H-%M-%S')

    def _save_model(self):
        os.makedirs(self._save_model_path, exist_ok=True)
        self._saver.save(self._sess,
                         self._save_model_path + "/" + self._save_model_name)

    def _save_tensorboard(self, loss):
        with tf.name_scope('log'):
            tf.compat.v1.summary.scalar('loss', loss)
            merged = tf.compat.v1.summary.merge_all()
            writer = tf.compat.v1.summary.FileWriter(self._tensorboard_path,
                                                     self._sess.graph)

    def train(self):
        accuracy = 0
        with tqdm(range(self._epoch)) as pbar:
            for i, ch in enumerate(pbar):  #train

                input_images, input_labels = self._data_loader.get_train_data(
                    self._batch_size)
                loss, _ = self._auto_encoder.train(self._sess, input_images)
                pbar.set_postfix(OrderedDict(loss=loss, accuracy=accuracy))

                #self._save_tensorboard(loss)
                self._loss.append(loss)

                if i % self._val_step == 0:  #test
                    self._save_model()
                    self._loss.save_log()