Python keras.backend.tensorflow_backend.set_session() Examples

The following are 6 code examples of keras.backend.tensorflow_backend.set_session(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module keras.backend.tensorflow_backend , or try the search function .
Example #1
Source File: model.py    From EasyPR-python with Apache License 2.0 6 votes vote down vote up
def __init__(self, mode, config, model_dir):
        """
        mode: Either "training" or "inference"
        config: A Sub-class of the Config class
        model_dir: Directory to save training logs and trained weights
        """
        assert mode in ['training', 'inference']
        if mode == 'training':
            import keras.backend.tensorflow_backend as KTF
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            session = tf.Session(config=config)
            KTF.set_session(session)
        self.mode = mode
        self.config = config
        self.model_dir = model_dir
        self.set_log_dir()
        self.keras_model = self.build(mode=mode, config=config) 
Example #2
Source File: plate_detect.py    From EasyPR-python with Apache License 2.0 6 votes vote down vote up
def detect(self, src, model_dir):
        first = self.eval_sess is None
        if first:
            # if first load
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            self.eval_sess = tf.Session(graph=self.graph, config=config)
        with self.graph.as_default():
            if first:
                plate_config = PlateInferenceConfig()
                # plate_config.display()
                self.model = modellib.MaskRCNN(mode="inference", config=plate_config, model_dir=model_dir)
            KTF.set_session(self.eval_sess)
            model_path = self.model.find_last()[1]
            self.model.load_weights(model_path, by_name=True)
            result = self.model.detect([src])
        return self._post_process(result, src) 
Example #3
Source File: main.py    From evo-pawness with GNU General Public License v3.0 5 votes vote down vote up
def main_alpha_zero_train():
    """
    Main option to train the alpha zero model from start
    :return:
    """
    import tensorflow as tf
    from keras.backend.tensorflow_backend import set_session
    from reinforcement_learning_train.alpha_zero.train_module import fit_train
    from reinforcement_learning_train.util.action_encoder import ActionEncoder
    from reinforcement_learning_train.alpha_zero.deep_net_architecture import PawnNet, PawnNetZero
    from reinforcement_learning_train.util.alphazero_util import action_spaces_new
    from collections import deque

    # config = tf.ConfigProto()
    # config.gpu_options.allow_growth = True  # dynamically grow the memory used on the GPU
    # config.log_device_placement = True  # to log device placement (on which device the operation ran)
    # # (nothing gets printed in Jupyter, only if you run it standalone)
    # sess = tf.Session(config=config)
    # set_session(sess)  # set this TensorFlow session as the default session for Keras
    all_action_spaces = action_spaces_new()

    deepnet_model = PawnNetZero(len(all_action_spaces))
    global_list_training = deque(maxlen=9000)
    ae = ActionEncoder()
    ae.fit(list_all_action=all_action_spaces)
    print(deepnet_model.model.summary())
    fit_train(global_list_training,ae, deepnet_model)
    deepnet_model.model.save("best_model.hdf5") 
Example #4
Source File: main.py    From evo-pawness with GNU General Public License v3.0 5 votes vote down vote up
def main_alpha_zero_train_continue():
    """
    Main option to play to continue training the model of alpha zero
    :return:
    """
    from collections import deque

    from keras.models import load_model
    from reinforcement_learning_train.alpha_zero.train_module import fit_train
    from reinforcement_learning_train.util.action_encoder import ActionEncoder
    from reinforcement_learning_train.alpha_zero.deep_net_architecture import PawnNet, PawnNetZero
    from reinforcement_learning_train.util.alphazero_util import action_spaces_new
    import pickle

    # config = tf.ConfigProto()
    # config.gpu_options.allow_growth = True  # dynamically grow the memory used on the GPU
    # config.log_device_placement = True  # to log device placement (on which device the operation ran)
    # # (nothing gets printed in Jupyter, only if you run it standalone)
    # sess = tf.Session(config=config)
    # set_session(sess)  # set this TensorFlow session as the default session for Keras
    all_action_spaces = action_spaces_new()

    MODEL_PATH = "checkpoint.hdf5"
    BEST_MODEL = "best_model.hdf5"
    GLOBAL_LIST_TRAINING_PATH = "global_list_training.p"
    # Import Model
    deepnet_model = PawnNetZero(len(all_action_spaces))
    deepnet_model.model = load_model(MODEL_PATH)
    best_model = load_model(BEST_MODEL)
    global_list_training = pickle.load(open(GLOBAL_LIST_TRAINING_PATH, "rb"))
    print("GLOBAL LIST SHAPE : {}".format(len(global_list_training)))
    ae = ActionEncoder()
    ae.fit(list_all_action=all_action_spaces)
    fit_train(global_list_training, ae, deepnet_model, best_model=best_model) 
Example #5
Source File: trainer.py    From Carla-RL with MIT License 5 votes vote down vote up
def check_weights_size(model_path, weights_size):

    # Memory fraction
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=settings.TRAINER_MEMORY_FRACTION)
    backend.set_session(tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)))

    # create a model and save serialized weights' size
    trainer = ARTDQNTrainer(model_path)
    weights_size.value = len(trainer.serialize_weights())


# Runs trainer process 
Example #6
Source File: pred.py    From SRCNNKit with MIT License 5 votes vote down vote up
def setup_session():
    import tensorflow as tf
    from keras.backend import tensorflow_backend
    config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
    session = tf.Session(config=config)
    tensorflow_backend.set_session(session)