'restore.
Restore a Tensorflow model
Arguments:
model_file: path of tensorflow model to restore
trainable_variable_only: If True, only restore trainable variables.
variable_name_map: - a (pattern, repl) tuple providing a regular expression pattern
and replacement, which is applied to variable names, before
restoration from the model file
- OR, a function map_func, used to perform the mapping, called as:
name_in_file = map_func(existing_var_op_name)
The function may return None to indicate a variable is not to be
restored.
scope_for_restore: string specifying the scope to limit to, when restoring variables.
Also removes the scope name prefix from the var name to use when restoring.
create_new_session: Set to False if the current session is to be kept.
Set to True (the default) to create a new session, and re-init all variables.
verbose           : Set to True to see a printout of what variables are being restored,
when using scope_for_restore or variable_name_map'

def restore(self, model_file, trainable_variable_only=False, variable_name_map=None, scope_for_restore=None, create_new_session=True, verbose=False):

    if (not os.path.isabs(model_file)):
        model_file = os.path.abspath(os.path.join(os.getcwd(), model_file))
    if create_new_session:
        self.close_session()
        config = None
        tflearn_conf = tf.get_collection(tf.GraphKeys.GRAPH_CONFIG)
        if tflearn_conf:
            config = tflearn_conf[0]
        self.session = tf.Session(config=config)
        try:
            self.session.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
        except Exception:
            self.session.run(tf.initialize_all_variables())
    if (scope_for_restore is not None):
        sname = scope_for_restore
        def vn_map_func(existing_name):
            if (not existing_name.startswith(sname)):
                return None
            name_in_file = re.sub(('^%s/' % sname), '', existing_name)
            if verbose:
                print(('[%s]    Restoring    %s    <-    %s' % (sname, existing_name, name_in_file)))
            return name_in_file
        variable_name_map = vn_map_func
    if (variable_name_map is not None):
        if (type(variable_name_map) == tuple):
            (pattern, repl) = variable_name_map
            def vn_map_func(existing_name):
                name_in_file = re.sub(pattern, repl, existing_name)
                if verbose:
                    print(('Restoring    %s    <-    %s' % (existing_name, name_in_file)))
                return name_in_file
        else:
            vn_map_func = variable_name_map
        if trainable_variable_only:
            to_restore = self.to_restore_trainvars
        else:
            to_restore = self.to_restore
        renamed_to_restore = {vn_map_func(v.op.name): v for v in to_restore}
        if (None in renamed_to_restore):
            renamed_to_restore.pop(None)
        restorer = tf.train.Saver(var_list=renamed_to_restore)
        restorer.restore(self.session, model_file)
    elif (not trainable_variable_only):
        self.restorer.restore(self.session, model_file)
    else:
        self.restorer_trainvars.restore(self.session, model_file)
    for o in self.train_ops:
        o.session = self.session
    self.restored = True
    self.training_state.step = int(self.global_step.eval(self.session))
