Ejemplo n.º 1
0
    def _run_initsync(self):
        # tparams = [list(chain(*tp)) for tp in self._tower_params]
        tparams = self._tower_params

        # Check to prevent from unnecessarily re-initializing and
        # synchronizing, i.e. when the model loads the weights.
        for v in chain.from_iterable(tparams):
            if getattr(v, '_keras_initialized', False):
                return

        KB.manual_variable_initialization(True)
        sess = KB.get_session()
        KB.manual_variable_initialization(False)

        # glob_variables = tf.global_variables()
        # sess.run(tf.variables_initializer(glob_variables))

        # Initialize on GPU0 and sync to other GPUs
        init_op = tf.variables_initializer(tparams[0])
        # init_op = tf.variables_initializer(self._tower_params[0])
        # init_op = tf.variables_initializer(self.trainable_weights)
        sess.run(init_op)

        # Important if using model_creator. Not necessary of model instance is
        # reused in which case the model layers are shared between slices
        # and are automatically sync'd.
        sync_op = all_sync_params(tparams, self._gdev_list,
                                  usenccl=self._usenccl)
        sess.run(sync_op)

        for v in chain.from_iterable(tparams):
            v._keras_initialized = True
Ejemplo n.º 2
0
    def __init__(self, env_name, name="Skynet"):
        #Creating a training queue
        self.train_queue = [
            [], [], [], [], []
        ]  #In the form of [state, action, reward, next_state]
        self.lock_queue = threading.Lock()

        #Creating the session
        self.session = tf.Session()
        Backend.set_session(self.session)
        Backend.manual_variable_initialization(True)

        #Setting up the environment variables
        self.env = gym.make(env_name)
        self.state_size = self.env.observation_space.shape[0]
        self.action_size = self.env.action_space.n
        NONE_STATE = np.zeros(self.state_size)

        #Building the graph
        self.model = self._build_model(policy_layers=[16], value_layers=[8])
        self.graph = self._build_graph(self.model)
        self.default_graph = tf.get_default_graph()
        #self.default_graph.finalize()

        #Initializing all the variables
        self.init = tf.global_variables_initializer()
        self.session.run(self.init)
def extract_features():
    seed(RANDOM_STATE)
    tf.random.set_seed(RANDOM_STATE)
    manual_variable_initialization(True)
    my_model = keras.models.load_model(GPATH / MODELS_PATH / USE_MODEL,
                                       compile=False)
    my_model.compile()
    my_model = my_model.get_layer('resnet50')

    datagen = ImageDataGenerator(rescale=1. / 255.)
    image_gen = datagen.flow_from_directory(
        (GPATH / READY_IMAGE_DIRECTORY).parent,
        class_mode=None,
        target_size=(224, 224),
        batch_size=32,
        shuffle=False)
    image_gen.reset()
    pred_my_model = pd.DataFrame(
        my_model.predict_generator(image_gen, verbose=1))
    filenames = image_gen.filenames
    df_all = pred_my_model
    df_all['Filename'] = filenames
    df_all['Filename'] = df_all.Filename.str.split('\\', expand=True).iloc[:,
                                                                           1]
    df_all['id'] = df_all.Filename.str.split('.', expand=True).iloc[:, 0]
    df_all.id = pd.to_numeric(df_all.id)
    df_all.set_index('id', inplace=True)
    df_all.drop(columns=['Filename'], inplace=True)
    df_all.columns = [str(i) for i in range(0, len(df_all.columns))]
    df_all = df_all.loc[:, (df_all != 0).any(axis=0)]
    return df_all
Ejemplo n.º 4
0
    def __init__(self, **kwargs):
        self.env = kwargs.get("environment")
        self.learning_rate = kwargs.get('learning_rate', LEARNING_RATE)
        self.tr_freq = kwargs.get('training_frequency', TR_FREQ)
        self.min_batch = kwargs.get('min_batch', MIN_BATCH)
        self.gamman = kwargs.get('gamma_n', GAMMA_N)
        self.models_directory = kwargs.get('models_directory',
                                           MODELS_DIRECTORY)
        self.num_state = self.env.env.observation_space.shape[0]
        self.num_tcl = self.env.env.num_tcls
        self.num_actions = self.env.env.action_space.n
        self.none_state = np.zeros(self.num_state)
        tf.compat.v1.disable_eager_execution()
        # self.session = tf.compat.v1.Session()
        # K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model(num_state=self.num_state,
                                       num_tcls=self.num_tcl)
        self.graph = self._build_graph(self.model)
        # self.session.run(tf.compat.v1.global_variables_initializer())
        # self.default_graph = tf.compat.v1.get_default_graph()
        # We keep track of the best rewards achieved so far for each day
        self.max_reward = max_reward
        self.rewards = {}
        for i in range(self.env.env.day0, self.env.env.dayn):
            self.rewards[i] = self.max_reward
Ejemplo n.º 5
0
    def __init__(self, s_space, a_space, none_state, saved_model=False, t_queue=None):
        self.logger = logging.getLogger('sc2rl.' + __name__)

        self.s_space = s_space
        self.a_space = a_space
        self.none_state = none_state
        self.queue = t_queue

        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        if saved_model:
            self.model = load_model(saved_model)
            self.model._make_predict_function()
        else:
            self.model = self._build_model()

        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())

        if saved_model:
            self.model.load_weights(saved_model)

        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()  # avoid modifications
Ejemplo n.º 6
0
    def _run_initsync(self):
        # tparams = [list(chain(*tp)) for tp in self._tower_params]
        tparams = self._tower_params

        # Check to prevent from unnecessarily re-initializing and
        # synchronizing, i.e. when the model loads the weights.
        for v in chain.from_iterable(tparams):
            if getattr(v, '_keras_initialized', False):
                return

        KB.manual_variable_initialization(True)
        sess = KB.get_session()
        KB.manual_variable_initialization(False)

        # glob_variables = tf.global_variables()
        # sess.run(tf.variables_initializer(glob_variables))

        # Initialize on GPU0 and sync to other GPUs
        init_op = tf.variables_initializer(tparams[0])
        # init_op = tf.variables_initializer(self._tower_params[0])
        # init_op = tf.variables_initializer(self.trainable_weights)
        sess.run(init_op)

        # Important if using model_creator. Not necessary of model instance is
        # reused in which case the model layers are shared between slices
        # and are automatically sync'd.
        sync_op = all_sync_params(tparams,
                                  self._gdev_list,
                                  usenccl=self._usenccl)
        sess.run(sync_op)

        for v in chain.from_iterable(tparams):
            v._keras_initialized = True
Ejemplo n.º 7
0
    def __init__(self, deg, load_checkpoint = True):
        global frames
        frames = 0
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.deg = deg
        self.edge = sum(deg)
        self.num_input = self.edge*3;

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()
        self.saver = tf.train.Saver()
        if load_checkpoint:
            checkpoint = tf.train.get_checkpoint_state(CHECKPOINT_DIR)
            if checkpoint and checkpoint.model_checkpoint_path:
                self.saver.restore(self.session, checkpoint.model_checkpoint_path)
                print("checkpoint loaded:", checkpoint.model_checkpoint_path)
                tokens = checkpoint.model_checkpoint_path.split("-")
                # set global step
                frames = int(tokens[1])
                print(">>> global step set: ", frames)
        else:
            print("Could not find old checkpoint")

        self.default_graph.finalize()    # avoid modifications
Ejemplo n.º 8
0
    def __init__(self):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        global vae
        self.vae = vae
        vae.load_weights(WEIGHTS_FILE)
        s_output_layer = Lambda(lambda x: x[:, 32:])(vae.layers[-2].outputs[2])
        self.vae = Model(vae.inputs, [s_output_layer])
        for layer in self.vae.layers:
            layer.trainable = False

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()  # avoid modifications

        self.frame_count = 0

        self.csvfile = open("myvae_a3c_history.csv", 'w')
        self.csvwriter = csv.writer(self.csvfile, delimiter=',', quotechar='"')
        self.csvwriter.writerow(
            ['Policy Loss', 'Value Loss', 'Reward', 'Frame Count'])
Ejemplo n.º 9
0
    def __init__(self, state_shape, num_outputs):
        global STATE_SHAPE
        global BATCH_SHAPE
        global NONE_STATE
        global NUM_OUTPUTS

        STATE_SHAPE = state_shape
        BATCH_SHAPE = [MIN_BATCH, *STATE_SHAPE]
        NUM_OUTPUTS = num_outputs
        NONE_STATE = np.zeros_like(STATE_SHAPE)

        self.global_t = 0
        self.saved_solutions = {}
        self.global_best_solution = 0

        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)
        self.tr_loss = None

        self.not_enough_optimizers = False

        self.merged_summaries = tf.summary.merge_all()
        self.train_writer = tf.summary.FileWriter(SUMMARY_DIR + '/train')

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()  # avoid modifications

        if REPORT_LEVEL == "steps" or REPORT_LEVEL == "episodes":
            print("\nglb_t".rjust(6),
                  "ag".rjust(4),
                  "ag_t".rjust(5),
                  "iter".rjust(5),
                  "stg".rjust(4),
                  "b_ratio".rjust(9),
                  "b_val".rjust(5),
                  "p_ratio".rjust(9),
                  "p_val".rjust(5),
                  "g_ratio".rjust(9),
                  "g_val".rjust(5),
                  "time".rjust(10),
                  sep="\t")

        elif REPORT_LEVEL == "learning":
            print("\nglb_t".rjust(6),
                  "batch".rjust(6),
                  "queue".rjust(6),
                  "policy".rjust(10),
                  "value".rjust(10),
                  "entropy".rjust(10),
                  "total".rjust(10),
                  "g_ratio".rjust(9),
                  "g_val".rjust(5),
                  sep="\t")
Ejemplo n.º 10
0
    def __init__(self):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()
Ejemplo n.º 11
0
    def updateTargets(self):
        self.timer.checkpoint("updateTargets start")
        # Prevent expensive keras initialization check
        K.manual_variable_initialization(True)

        self.critic_target_update([])
        self.timer.checkpoint("Target actor policy updated")

        self.actor_target_update([])
        self.timer.checkpoint("Target critic policy updated")
    def __init__(self):
        self.session = tf.compat.v1.Session()
        # K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.compat.v1.global_variables_initializer())
        self.default_graph = tf.compat.v1.get_default_graph()

        self.default_graph.finalize()  # avoid modifications
Ejemplo n.º 13
0
	def __init__(self):
		self.session = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
		K.set_session(self.session)			
		K.manual_variable_initialization(True)		

		self.model = self._build_model()		
		self.graph = self._build_graph(self.model)	

		self.session.run(tf.global_variables_initializer())	
		self.default_graph = tf.get_default_graph()		

		self.default_graph.finalize()	# avoid modifications
    def __init__(self):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()
        self.rewards = {}
        for i in range(DAY0, DAYN):
            self.rewards[i]=0.0
Ejemplo n.º 15
0
    def __init__(self):
        self.train_queue = [[], [], [], []]  # s, a, r, s' (terminal mask)
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()
Ejemplo n.º 16
0
    def __init__(self):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        if SAVE: self.model.load_weights("network.h5")
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()
Ejemplo n.º 17
0
    def __init__(self):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        if os.path.exists('model_output/a3c_agent/WeightsafterTraining2'):
            self.model.load_weights(
                'model_output/a3c_agent/WeightsafterTraining2')
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()
Ejemplo n.º 18
0
def train_from_hdf5_file(filename=USE_MODEL):
    logging.info(f"About to start training again from {USE_MODEL}")
    # These three lines are attempting to prevent a weird keras bug where a
    #    model that is read in gets its weights randomized
    seed(RANDOM_STATE)
    tf.random.set_seed(RANDOM_STATE)
    manual_variable_initialization(True)

    full_path = GPATH / MODELS_PATH / filename

    model = keras.models.load_model(full_path, compile=False)
    model.load_weights(full_path)
    return train_model(model)
    def __init__(self, stateCnt, actionCnt):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)
        self.default_graph = tf.get_default_graph()
        # self.model = self._build_model()
        # self.graph = self._build_graph(self.model)
        self.stateCnt = stateCnt
        self.actionCnt = actionCnt
        self.model = self._createModel()
        self.model_ = self._createModel()
        print("models created")

        self.session.run(tf.global_variables_initializer())
Ejemplo n.º 20
0
    def __init__(self, name, game, **kwargs):
        super().__init__(name, game, **kwargs)

        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model._make_predict_function()
        self.model._make_train_function()
        
        self.session.run(tf.global_variables_initializer())
        if "load_weights" in kwargs and kwargs['load_weights']: self.load_weights()

        self.default_graph = tf.get_default_graph()
Ejemplo n.º 21
0
 def init_model(self):
     if self.initialized == True:
         return
     if self.visualization == False:
     #######################################
         self.session = tf.Session()
         K.set_session(self.session)
         K.manual_variable_initialization(True)
         self.graph = self.create_graph(self.model)
 
         self.session.run(tf.global_variables_initializer())
         self.default_graph = tf.get_default_graph()
         
     self.initialized = True
Ejemplo n.º 22
0
    def __init__(self):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(
            True
        )  #whether variables should be initialized as they are instantiated (default), or if the user should handle the initialization

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()  # avoid modifications,read only
Ejemplo n.º 23
0
Archivo: a3c.py Proyecto: tacalvin/a3c
    def __init__(self, ob_space, ac_space):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)
        self.ob_space = ob_space
        self.ac_space = ac_space

        self.model_p, self.model_v = self.build_model()#LSTMmodel(ob_space,ac_space,'doom')
        
        self.graph = self.build_comp_graph(self.model_p, self.model_v)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()
        self.default_graph.finalize() #to avoid modifications
Ejemplo n.º 24
0
    def __init__(self, name, game, **kwargs):
        super().__init__(name, game, **kwargs)

        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.graph = self._build_graph()

        self.session.run(tf.global_variables_initializer())
        if "load_weights" in kwargs and kwargs['load_weights']:
            self.load_weights()

        self.default_graph = tf.get_default_graph()
        self.default_graph.finalize()
Ejemplo n.º 25
0
    def __init__(self):

        coord = tf.train.Coordinator()

        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)
        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()

        #self.default_graph.finalize()  # avoid modifications
        self.weights = self.model.get_weights()  # ネットワークの重みを保存
Ejemplo n.º 26
0
    def __init__(self):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        merged_summaries = tf.summary.merge_all()
        self.train_writer = tf.summary.FileWriter(
            '../tensorboard_data/a3c_' + ENV + "_tutorial", self.session.graph)
        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()  # avoid modifications
Ejemplo n.º 27
0
    def __init__(self, state_size=INPUT_IMG, action_space=nb_actions):
        self.session = tf.Session()
        self.state_size = state_size
        self.action_space = action_space

        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()  # avoid modifications
Ejemplo n.º 28
0
    def __init__(self):
        self.sess = tf.compat.v1.Session()
        self.c_loss_weight = 0.5
        self.entropy_panelty_weight = 0.01
        tf.compat.v1.keras.backend.set_session(self.sess)
        K.manual_variable_initialization(True)
        self.learning_rate = 5e-3
        self.batch_size = 32
        self.epsilon = 0.5

        self.model = self.build_model()
        self._init_op = self._init_op(self.model)
        # s, a, r, s', s' terminal mask
        self.training_queue = [[], [], [], [], []]
        self.training_queue_lock = threading.Lock()
Ejemplo n.º 29
0
    def __init__(self, NUM_STATE, NUM_ACTIONS):

        self.NUM_STATE = NUM_STATE
        self.NUM_ACTIONS = NUM_ACTIONS

        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = self._build_model()
        self.graph = self._build_graph(self.model)

        self.session.run(tf.global_variables_initializer())
        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()  # avoid modifications
Ejemplo n.º 30
0
    def __init__(self):
        g = tf.Graph()
        SESSION = tf.Session(graph=g)
        self.session = SESSION
        with g.as_default():
            tf.set_random_seed(7)
            K.set_session(self.session)
            K.manual_variable_initialization(True)
            self.model = self.BuildModel()
            self.graph = self.BuildGraph()
            self.session.run(tf.global_variables_initializer())
            self.default_graph = tf.get_default_graph()
        #self.default_graph.finalize()

        self.buffer = [[], [], [], [], []]
        self.lock = threading.Lock()
Ejemplo n.º 31
0
def predict_folder(input_dir, output_dir, mode, data):
    sess = K.get_session()
    K.manual_variable_initialization(True)

    data_frame = get_data_frame(data, input_dir)
    mdl = get_mdl(data, data_frame)

    #config = tf.ConfigProto()
    #config.gpu_options.allow_growth = True
    #sess = tf.Session(config=config)
    #K.set_session(sess)

    submit_test(sess, mdl, data_frame, output_dir, mode)

    #K.clear_session()
    return