Python tensorflow.compat.v1.ConfigProto() Examples

The following are 30 code examples of tensorflow.compat.v1.ConfigProto(). 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 tensorflow.compat.v1 , or try the search function .
Example #1
Source File: trainer.py    From meta-dataset with Apache License 2.0 6 votes vote down vote up
def initialize_session(self):
    """Initializes a tf.Session."""
    if ENABLE_TF_OPTIMIZATIONS:
      self.sess = tf.Session()
    else:
      session_config = tf.ConfigProto()
      rewrite_options = session_config.graph_options.rewrite_options
      rewrite_options.disable_model_pruning = True
      rewrite_options.constant_folding = rewrite_options.OFF
      rewrite_options.arithmetic_optimization = rewrite_options.OFF
      rewrite_options.remapping = rewrite_options.OFF
      rewrite_options.shape_optimization = rewrite_options.OFF
      rewrite_options.dependency_optimization = rewrite_options.OFF
      rewrite_options.function_optimization = rewrite_options.OFF
      rewrite_options.layout_optimizer = rewrite_options.OFF
      rewrite_options.loop_optimization = rewrite_options.OFF
      rewrite_options.memory_optimization = rewrite_options.NO_MEM_OPT
      self.sess = tf.Session(config=session_config)

    # Restore or initialize the variables.
    self.sess.run(tf.global_variables_initializer())
    self.sess.run(tf.local_variables_initializer()) 
Example #2
Source File: ops.py    From super-resolution-videos with The Unlicense 6 votes vote down vote up
def set_gpu_fraction(sess=None, gpu_fraction=0.3):
    """Set the GPU memory fraction for the application.

    Parameters
    ----------
    sess : a session instance of TensorFlow
        TensorFlow session
    gpu_fraction : a float
        Fraction of GPU memory, (0 ~ 1]

    References
    ----------
    - `TensorFlow using GPU <https://www.tensorflow.org/versions/r0.9/how_tos/using_gpu/index.html>`_
    """
    print("  tensorlayer: GPU MEM Fraction %f" % gpu_fraction)
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)
    sess = tf.Session(config = tf.ConfigProto(gpu_options = gpu_options))
    return sess 
Example #3
Source File: ppo2_cnn.py    From football with Apache License 2.0 6 votes vote down vote up
def __init__(self, player_config, env_config):
    player_base.PlayerBase.__init__(self, player_config)

    self._action_set = (env_config['action_set']
                        if 'action_set' in env_config else 'default')
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    self._sess = tf.Session(config=config)
    self._player_prefix = 'player_{}'.format(player_config['index'])
    stacking = 4 if player_config.get('stacked', True) else 1
    policy = player_config.get('policy', 'cnn')
    self._stacker = ObservationStacker(stacking)
    with tf.variable_scope(self._player_prefix):
      with tf.variable_scope('ppo2_model'):
        policy_fn = build_policy(DummyEnv(self._action_set, stacking), policy)
        self._policy = policy_fn(nbatch=1, sess=self._sess)
    _load_variables(player_config['checkpoint'], self._sess,
                    prefix=self._player_prefix + '/') 
Example #4
Source File: wmf.py    From top-k-rec with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, k: int, lu: float = 0.01, lv: float = 0.01, a: float = 1, b: float = 0.01) -> None:
        self.__sn = 'wmf'
        self.k = k
        self.lu = lu
        self.lv = lv
        self.a  = a
        self.b  = b
        self.tf_config = tf.ConfigProto()
        self.tf_config.gpu_options.allow_growth=True
        self.uids = None
        self.n_users = None
        self.usm = None
        self.iids = None
        self.n_items = None
        self.ism = None
        self.n_ratings = None
        self.u_rated = None
        self.i_rated = None
        self.fue = None
        self.fie = None 
Example #5
Source File: L2_Net.py    From pyslam with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, net_name, do_tf_logging=True, flagCS = False):
        set_tf_logging(do_tf_logging)
                
        # from https://kobkrit.com/using-allow-growth-memory-option-in-tensorflow-and-keras-dc8c8081bc96        
        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)
        sess = tf.Session(config=config)
        set_session(sess)  # set this TensorFlow session as the default session for Keras
        
        model, model_cen, pix_mean, pix_mean_cen = build_L2_net(net_name)
        self.flagCS = flagCS
        self.model = model
        self.model_cen = model_cen
        self.pix_mean = pix_mean
        self.pix_mean_cen = pix_mean_cen 
Example #6
Source File: learning_test.py    From tf-slim with Apache License 2.0 6 votes vote down vote up
def testTrainWithSessionConfig(self):
    with ops.Graph().as_default():
      random_seed.set_random_seed(0)
      tf_inputs = tf.constant(self._inputs, dtype=tf.float32)
      tf_labels = tf.constant(self._labels, dtype=tf.float32)

      tf_predictions = LogisticClassifier(tf_inputs)
      loss_ops.log_loss(tf_labels, tf_predictions)
      total_loss = loss_ops.get_total_loss()

      optimizer = gradient_descent.GradientDescentOptimizer(learning_rate=1.0)

      train_op = learning.create_train_op(total_loss, optimizer)

      session_config = tf.ConfigProto(allow_soft_placement=True)
      loss = learning.train(
          train_op,
          None,
          number_of_steps=300,
          log_every_n_steps=10,
          session_config=session_config)
    self.assertIsNotNone(loss)
    self.assertLess(loss, .015) 
Example #7
Source File: tf.py    From RLs with Apache License 2.0 5 votes vote down vote up
def generate_session_config() -> tf.ConfigProto:
    """
    Generate a ConfigProto to use for ML-Agents that doesn't consume all of the GPU memory
    and allows for soft placement in the case of multi-GPU.
    """
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    # For multi-GPU training, set allow_soft_placement to True to allow
    # placing the operation into an alternative device automatically
    # to prevent from exceptions if the device doesn't suppport the operation
    # or the device does not exist
    config.allow_soft_placement = True
    return config 
Example #8
Source File: run_ppo2.py    From football with Apache License 2.0 5 votes vote down vote up
def train(_):
  """Trains a PPO2 policy."""
  vec_env = SubprocVecEnv([
      (lambda _i=i: create_single_football_env(_i))
      for i in range(FLAGS.num_envs)
  ], context=None)

  # Import tensorflow after we create environments. TF is not fork sake, and
  # we could be using TF as part of environment if one of the players is
  # controled by an already trained model.
  import tensorflow.compat.v1 as tf
  ncpu = multiprocessing.cpu_count()
  config = tf.ConfigProto(allow_soft_placement=True,
                          intra_op_parallelism_threads=ncpu,
                          inter_op_parallelism_threads=ncpu)
  config.gpu_options.allow_growth = True
  tf.Session(config=config).__enter__()

  ppo2.learn(network=FLAGS.policy,
             total_timesteps=FLAGS.num_timesteps,
             env=vec_env,
             seed=FLAGS.seed,
             nsteps=FLAGS.nsteps,
             nminibatches=FLAGS.nminibatches,
             noptepochs=FLAGS.noptepochs,
             max_grad_norm=FLAGS.max_grad_norm,
             gamma=FLAGS.gamma,
             ent_coef=FLAGS.ent_coef,
             lr=FLAGS.lr,
             log_interval=1,
             save_interval=FLAGS.save_interval,
             cliprange=FLAGS.cliprange,
             load_path=FLAGS.load_path) 
Example #9
Source File: keras_model.py    From DeepPavlov with Apache License 2.0 5 votes vote down vote up
def _config_session():
        """
        Configure session for particular device

        Returns:
            tensorflow.Session
        """
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        config.gpu_options.visible_device_list = '0'
        return tf.Session(config=config) 
Example #10
Source File: trainer_lib.py    From tensor2tensor with Apache License 2.0 5 votes vote down vote up
def create_session_config(log_device_placement=False,
                          enable_graph_rewriter=False,
                          gpu_mem_fraction=0.95,
                          use_tpu=False,
                          xla_jit_level=tf.OptimizerOptions.OFF,
                          inter_op_parallelism_threads=0,
                          intra_op_parallelism_threads=0):
  """The TensorFlow Session config to use."""
  if use_tpu:
    graph_options = tf.GraphOptions()
  else:
    if enable_graph_rewriter:
      rewrite_options = rewriter_config_pb2.RewriterConfig()
      rewrite_options.layout_optimizer = rewriter_config_pb2.RewriterConfig.ON
      graph_options = tf.GraphOptions(rewrite_options=rewrite_options)
    else:
      graph_options = tf.GraphOptions(
          optimizer_options=tf.OptimizerOptions(
              opt_level=tf.OptimizerOptions.L1,
              do_function_inlining=False,
              global_jit_level=xla_jit_level))

  gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_mem_fraction)

  config = tf.ConfigProto(
      allow_soft_placement=True,
      graph_options=graph_options,
      gpu_options=gpu_options,
      log_device_placement=log_device_placement,
      inter_op_parallelism_threads=inter_op_parallelism_threads,
      intra_op_parallelism_threads=intra_op_parallelism_threads,
      isolate_session_state=True)
  return config 
Example #11
Source File: sar.py    From top-k-rec with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, k: int, lambda_u: float = 2.5e-3, lambda_i: float = 2.5e-3, lambda_j: float = 2.5e-4,
                 lambda_b: float = 0, lr: float = 1.0e-4, mode: str = 'l2') -> None:
        self.k = k
        self.lu = lambda_u
        self.li = lambda_i
        self.lj = lambda_j
        self.lb = lambda_b
        self.lr = lr
        self.tf_config = tf.ConfigProto()
        self.tf_config.gpu_options.allow_growth = True
        self.uids = None
        self.iids = None
        self.data = None
        self.epoch_sample_limit = None
        self.n_users = None
        self.n_items = None
        self.tr_data = None
        self.tr_users = None
        self.__ue = None
        self.__ie = None
        self.__ib = None
        self.__sess = None
        self.__saver = None
        self.mode = mode
        self.pred = None
        self.obj = None
        self.solver = None
        self.fue = None
        self.fie = None
        self.fib = None 
Example #12
Source File: bpr.py    From top-k-rec with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, k: int, lambda_u: float = 2.5e-3, lambda_i: float = 2.5e-3, lambda_j: float = 2.5e-4, lambda_b: float = 0, lr: float = 1.0e-4, mode: str = 'l2') -> None:
        self.k = k
        self.lu = lambda_u
        self.li = lambda_i
        self.lj = lambda_j
        self.lb = lambda_b
        self.lr = lr
        self.tf_config = tf.ConfigProto()
        self.tf_config.gpu_options.allow_growth = True
        self.uids = None
        self.iids = None
        self.data = None
        self.epoch_sample_limit = None
        self.n_users = None
        self.n_items = None
        self.tr_data = None
        self.tr_users = None
        self.__ue = None
        self.__ie = None
        self.__ib = None
        self.__sess = None
        self.__saver = None
        self.mode = mode
        self.pred = None
        self.obj = None
        self.solver = None
        self.fue = None
        self.fie = None
        self.fib = None 
Example #13
Source File: runner_lib.py    From recsim with Apache License 2.0 5 votes vote down vote up
def _set_up(self, eval_mode):
    """Sets up the runner by creating and initializing the agent."""
    # Reset the tf default graph to avoid name collisions from previous runs
    # before doing anything else.
    tf.reset_default_graph()
    self._summary_writer = tf.summary.FileWriter(self._output_dir)
    if self._episode_log_file:
      self._episode_writer = tf.io.TFRecordWriter(
          os.path.join(self._output_dir, self._episode_log_file))
    # Set up a session and initialize variables.
    self._sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    self._agent = self._create_agent_fn(
        self._sess,
        self._env,
        summary_writer=self._summary_writer,
        eval_mode=eval_mode)
    # type check: env/agent must both be multi- or single-user
    if self._agent.multi_user and not isinstance(
        self._env.environment, environment.MultiUserEnvironment):
      raise ValueError('Multi-user agent requires multi-user environment.')
    if not self._agent.multi_user and isinstance(
        self._env.environment, environment.MultiUserEnvironment):
      raise ValueError('Single-user agent requires single-user environment.')
    self._summary_writer.add_graph(graph=tf.get_default_graph())
    self._sess.run(tf.global_variables_initializer())
    self._sess.run(tf.local_variables_initializer()) 
Example #14
Source File: trainer.py    From rex-gym with Apache License 2.0 5 votes vote down vote up
def _train(self, config, env_processes):
        """Training and evaluation entry point yielding scores.

          Resolves some configuration attributes, creates environments, graph, and
          training loop. By default, assigns all operations to the CPU.

          Args:
            config: Object providing configurations via attributes.
            env_processes: Whether to step environments in separate processes.

          Yields:
            Evaluation scores.
        """
        tf.reset_default_graph()
        with config.unlocked:
            config.network = functools.partial(utility.define_network, config.network, config)
            config.policy_optimizer = getattr(tf.train, config.policy_optimizer)
            config.value_optimizer = getattr(tf.train, config.value_optimizer)
        if config.update_every % config.num_agents:
            logging.warning('Number of agents should divide episodes per update.')
        with tf.device('/cpu:0'):
            agents = self.agents if self.agents is not None else config.num_agents
            num_agents = 1 if self.playground else agents
            batch_env = utility.define_batch_env(lambda: self._create_environment(config), num_agents, env_processes)
            graph = utility.define_simulation_graph(batch_env, config.algorithm, config)
            loop = self._define_loop(graph, config.logdir, config.update_every * config.max_length,
                                     config.eval_episodes * config.max_length)
            total_steps = int(config.steps / config.update_every * (config.update_every + config.eval_episodes))
        # Exclude episode related variables since the Python state of environments is
        # not checkpointed and thus new episodes start after resuming.
        saver = utility.define_saver(exclude=(r'.*_temporary/.*',))
        sess_config = tf.ConfigProto(allow_soft_placement=True)
        sess_config.gpu_options.allow_growth = True
        with tf.Session(config=sess_config) as sess:
            utility.initialize_variables(sess, saver, config.logdir)
            for score in loop.run(sess, saver, total_steps):
                yield score
        batch_env.close() 
Example #15
Source File: recom_ncf_base.py    From cornac with Apache License 2.0 5 votes vote down vote up
def _sess_init(self):
        import tensorflow.compat.v1 as tf

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(graph=self.graph, config=config)
        self.sess.run(self.initializer) 
Example #16
Source File: model_executor.py    From mesh with Apache License 2.0 5 votes vote down vote up
def train_and_eval():
  """Trains and evaluates MeshTensorflow model without TPUEstimator.

  TODO(lehou): Pack everything nicely as a set of APIs.
  """

  mesh_context = None
  tf.logging.info('FLAGS.master: {}'.format(FLAGS.master))
  resolver = tf.distribute.cluster_resolver.TPUClusterResolver(FLAGS.master)
  config = tf.ConfigProto()
  config.allow_soft_placement = True
  cluster_spec = resolver.cluster_spec()
  if cluster_spec:
    config.cluster_def.CopyFrom(cluster_spec.as_cluster_def())
  with tf.Session(target=resolver.master(), config=config) as sess:
    tf.tpu.experimental.initialize_tpu_system(resolver)
    mesh_context = MeshContext(
        sess, FLAGS.use_tpu, FLAGS.mesh_shape, unet.get_layout())

  for _ in range(FLAGS.num_training_loops):
    _train_phase(mesh_context, config, resolver.get_master())
    _eval_phase(mesh_context, config, resolver.get_master())

  if FLAGS.use_tpu:
    with tf.Session(target=resolver.get_master(), config=config) as sess:
      sess.run(tpu.shutdown_system())

  tf.logging.info('finished.') 
Example #17
Source File: op_evaluator.py    From tensorboard with Apache License 2.0 5 votes vote down vote up
def _lazily_initialize(self):
        """Initialize the graph and session, if this has not yet been done."""
        # TODO(nickfelt): remove on-demand imports once dep situation is fixed.
        import tensorflow.compat.v1 as tf

        with self._initialization_lock:
            if self._session:
                return
            graph = tf.Graph()
            with graph.as_default():
                self.initialize_graph()
            # Don't reserve GPU because libpng can't run on GPU.
            config = tf.ConfigProto(device_count={"GPU": 0})
            self._session = tf.Session(graph=graph, config=config) 
Example #18
Source File: inference.py    From PINTO_model_zoo with MIT License 5 votes vote down vote up
def _build_session(self):
    sess_config = tf.ConfigProto()
    if self.use_xla:
      sess_config.graph_options.optimizer_options.global_jit_level = (
          tf.OptimizerOptions.ON_2)
    return tf.Session(config=sess_config) 
Example #19
Source File: fastgen.py    From magenta with Apache License 2.0 5 votes vote down vote up
def encode(wav_data, checkpoint_path, sample_length=64000):
  """Generate an array of encodings from an array of audio.

  Args:
    wav_data: Numpy array [batch_size, sample_length]
    checkpoint_path: Location of the pretrained model.
    sample_length: The total length of the final wave file, padded with 0s.
  Returns:
    encoding: a [mb, 125, 16] encoding (for 64000 sample audio file).
  """
  if wav_data.ndim == 1:
    wav_data = np.expand_dims(wav_data, 0)
  batch_size = wav_data.shape[0]

  # Load up the model for encoding and find the encoding of "wav_data"
  session_config = tf.ConfigProto(allow_soft_placement=True)
  session_config.gpu_options.allow_growth = True
  with tf.Graph().as_default(), tf.Session(config=session_config) as sess:
    hop_length = Config().ae_hop_length
    wav_data, sample_length = utils.trim_for_encoding(wav_data, sample_length,
                                                      hop_length)
    net = load_nsynth(batch_size=batch_size, sample_length=sample_length)
    saver = tf.train.Saver()
    saver.restore(sess, checkpoint_path)
    encodings = sess.run(net["encoding"], feed_dict={net["X"]: wav_data})
  return encodings 
Example #20
Source File: util.py    From nni with MIT License 5 votes vote down vote up
def make_session(config=None, num_cpu=None, make_default=False, graph=None):
    """Returns a session that will use <num_cpu> CPU's only"""
    if num_cpu is None:
        num_cpu = int(os.getenv('RCALL_NUM_CPU', multiprocessing.cpu_count()))
    if config is None:
        config = tf.ConfigProto(
            allow_soft_placement=True,
            inter_op_parallelism_threads=num_cpu,
            intra_op_parallelism_threads=num_cpu)
        config.gpu_options.allow_growth = True

    if make_default:
        return tf.InteractiveSession(config=config, graph=graph)
    else:
        return tf.Session(config=config, graph=graph) 
Example #21
Source File: gpt_2.py    From gpt2-estimator with MIT License 5 votes vote down vote up
def start_tf_sess():
    """
    Returns a tf.Session w/ config
    """
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    return tf.Session(config=config) 
Example #22
Source File: model_interface.py    From tensor2robot with Apache License 2.0 5 votes vote down vote up
def get_session_config(self):
    """Get the Session tf.ConfigProto for Estimator model.""" 
Example #23
Source File: train.py    From m-phate with GNU General Public License v3.0 5 votes vote down vote up
def build_config(limit_gpu_fraction=0.2, limit_cpu_fraction=10):
    if limit_gpu_fraction > 0:
        os.environ["CUDA_VISIBLE_DEVICES"] = "0"
        gpu_options = GPUOptions(
            allow_growth=True,
            per_process_gpu_memory_fraction=limit_gpu_fraction)
        config = ConfigProto(gpu_options=gpu_options)
    else:
        os.environ["CUDA_VISIBLE_DEVICES"] = ""
        config = ConfigProto(device_count={'GPU': 0})
    if limit_cpu_fraction is not None:
        if limit_cpu_fraction == 0:
            cpu_count = 1
        if limit_cpu_fraction < 0:
            # -2 gives all CPUs except 1
            cpu_count = max(
                1, int(os.cpu_count() + limit_cpu_fraction + 1))
        elif limit_cpu_fraction < 1:
            # 0.5 gives 50% of available CPUs
            cpu_count = max(
                1, int(os.cpu_count() * limit_cpu_fraction))
        else:
            # 2 gives 2 CPUs
            cpu_count = int(limit_cpu_fraction)
        config.inter_op_parallelism_threads = cpu_count
        config.intra_op_parallelism_threads = cpu_count
        os.environ['OMP_NUM_THREADS'] = str(1)
        os.environ['MKL_NUM_THREADS'] = str(cpu_count)
    return config 
Example #24
Source File: tensorflow.py    From reaver with MIT License 5 votes vote down vote up
def __init__(self, sess=None, base_path='results/', checkpoint_freq=100, training_enabled=True):
        if not sess:
            sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
        tf.keras.backend.set_session(sess)

        self.sess = sess
        self.saver = None
        self.base_path = base_path
        self.checkpoint_freq = checkpoint_freq
        self.training_enabled = training_enabled
        self.global_step = tf.train.get_or_create_global_step()
        self.summary_writer = tf.summary.FileWriter(self.summaries_path) 
Example #25
Source File: run.py    From reaver with MIT License 4 votes vote down vote up
def main(argv):
    tf.disable_eager_execution()
    tf.disable_v2_behavior()

    args = flags.FLAGS
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu

    if args.env in rvr.utils.config.SC2_MINIGAMES_ALIASES:
        args.env = rvr.utils.config.SC2_MINIGAMES_ALIASES[args.env]

    if args.test:
        args.n_envs = 1
        args.log_freq = 1
        args.restore = True

    expt = rvr.utils.Experiment(args.results_dir, args.env, args.agent, args.experiment, args.restore)

    gin_files = rvr.utils.find_configs(args.env, os.path.dirname(os.path.abspath(__file__)))
    if args.restore:
        gin_files += [expt.config_path]
    gin_files += args.gin_files

    if not args.gpu:
        args.gin_bindings.append("build_cnn_nature.data_format = 'channels_last'")
        args.gin_bindings.append("build_fully_conv.data_format = 'channels_last'")

    gin.parse_config_files_and_bindings(gin_files, args.gin_bindings)
    args.n_envs = min(args.n_envs, gin.query_parameter('ACAgent.batch_sz'))

    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    sess_mgr = rvr.utils.tensorflow.SessionManager(sess, expt.path, args.ckpt_freq, training_enabled=not args.test)

    env_cls = rvr.envs.GymEnv if '-v' in args.env else rvr.envs.SC2Env
    env = env_cls(args.env, args.render, max_ep_len=args.max_ep_len)

    agent = rvr.agents.registry[args.agent](env.obs_spec(), env.act_spec(), sess_mgr=sess_mgr, n_envs=args.n_envs)
    agent.logger = rvr.utils.StreamLogger(args.n_envs, args.log_freq, args.log_eps_avg, sess_mgr, expt.log_path)

    if sess_mgr.training_enabled:
        expt.save_gin_config()
        expt.save_model_summary(agent.model)

    agent.run(env, args.n_updates * agent.traj_len * agent.batch_sz // args.n_envs) 
Example #26
Source File: cudnn_layers_test.py    From language with Apache License 2.0 4 votes vote down vote up
def test_stacked_bilstm_compatibility(self):
    checkpoint_dir = tempfile.mkdtemp(prefix="checkpoint_dir")
    checkpoint_path = os.path.join(checkpoint_dir, "model.ckpt")
    hidden_size = 10
    num_layers = 3
    dropout_ratio = 0.0
    input_emb = np.random.uniform(size=[3, 5, 9]).astype(np.float32)
    input_len = [4, 5, 2]

    # Make sure we fail explicitly if the specified devices can't be used.
    config = tf.ConfigProto(
        allow_soft_placement=False, log_device_placement=True)

    with tf.Graph().as_default():
      with tf.device("/gpu:0"):
        output_emb = cudnn_layers.stacked_bilstm(
            input_emb=input_emb,
            input_len=input_len,
            hidden_size=hidden_size,
            num_layers=num_layers,
            dropout_ratio=dropout_ratio,
            mode=tf.estimator.ModeKeys.TRAIN,
            use_cudnn=True)
      saver = tf.train.Saver()
      with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())
        gpu_output_emb = sess.run(output_emb)
        saver.save(sess, checkpoint_path)

    with tf.Graph().as_default():
      with tf.device("/cpu:0"):
        output_emb = cudnn_layers.stacked_bilstm(
            input_emb=input_emb,
            input_len=input_len,
            hidden_size=hidden_size,
            num_layers=num_layers,
            dropout_ratio=dropout_ratio,
            mode=tf.estimator.ModeKeys.TRAIN,
            use_cudnn=False)
      saver = tf.train.Saver()
      with tf.Session(config=config) as sess:
        saver.restore(sess, checkpoint_path)
        cpu_output_emb = sess.run(output_emb)

    for c, g, l in zip(cpu_output_emb, gpu_output_emb, input_len):
      self.assertAllClose(c[:l], g[:l]) 
Example #27
Source File: video_super_resolver.py    From super-resolution-videos with The Unlicense 4 votes vote down vote up
def evaluate(video_path):
    ## create folders to save result images

    tl.global_flag['mode'] = 'srgan'

    save_dir = os.path.join("images", "srgan_frames")
    tl.files.exists_or_mkdir(save_dir)
    checkpoint_dir = "checkpoint"
    output_video_name = video_path.split(".")[0]
    output_video_name += "_srgan."+video_path.split(".")[1]
    
    read_video_filepath=os.path.join(os.getcwd(), "videos", video_path)
    
    videogen = skvideo.io.vreader(read_video_filepath)
    metadata = skvideo.io.ffprobe(read_video_filepath)
    metadata=metadata['video']
    H=int(metadata['@height'])
    W=int(metadata['@width'])
    fps=metadata['@r_frame_rate']
    
    C=3
    t_image = tf.placeholder('float32', [None, H, W, C], name='input_image')
    net_g = SRGAN_g(t_image, is_train=False, reuse=False)
    # # ###=============RESTORE G======================================================
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False))
    tl.layers.initialize_global_variables(sess)
    tl.files.load_and_assign_npz(sess=sess, name=os.path.join(checkpoint_dir, 'g_srgan.npz'), network=net_g)
    write_video_filepath=os.path.join(os.getcwd(), 'videos', output_video_name)
    writer = skvideo.io.FFmpegWriter(write_video_filepath,inputdict={'-r': fps},outputdict={'-r': fps,'-vcodec': 'libx264','-pix_fmt': 'yuv420p'})    
    for i, frame in enumerate(videogen):        
        avg=frame.max()-frame.min()
        frame = (frame / avg) - 1  
        out = sess.run(net_g.outputs, {t_image: [frame]})
        #tl.vis.save_image(out[0], save_dir+'/'+str(i)+'.png')
        out=out[0]
        out=(255*(out-np.min(out))/(np.max(out)-np.min(out))).astype(np.uint8)
        writer.writeFrame(out)
        print (i)
        
        
        
    writer.close() 
Example #28
Source File: bert_as_summarizer.py    From DeepPavlov with Apache License 2.0 4 votes vote down vote up
def __init__(self, bert_config_file: str,
                 pretrained_bert: str,
                 vocab_file: str,
                 max_summary_length: int,
                 max_summary_length_in_tokens: Optional[bool] = False,
                 max_seq_length: Optional[int] = 128,
                 do_lower_case: Optional[bool] = False,
                 lang: Optional[str] = 'ru',
                 **kwargs) -> None:

        self.max_summary_length = max_summary_length
        self.max_summary_length_in_tokens = max_summary_length_in_tokens
        self.bert_config = BertConfig.from_json_file(str(expand_path(bert_config_file)))

        self.bert_preprocessor = BertPreprocessor(vocab_file=vocab_file, do_lower_case=do_lower_case,
                                                  max_seq_length=max_seq_length)

        self.tokenize_reg = re.compile(r"[\w']+|[^\w ]")

        if lang == 'ru':
            from ru_sent_tokenize import ru_sent_tokenize
            self.sent_tokenizer = ru_sent_tokenize
        else:
            from nltk import sent_tokenize
            self.sent_tokenizer = sent_tokenize

        self.sess_config = tf.ConfigProto(allow_soft_placement=True)
        self.sess_config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=self.sess_config)

        self._init_graph()

        self.sess.run(tf.global_variables_initializer())

        if pretrained_bert is not None:
            pretrained_bert = str(expand_path(pretrained_bert))

            if tf.train.checkpoint_exists(pretrained_bert):
                logger.info('[initializing model with Bert from {}]'.format(pretrained_bert))
                tvars = tf.trainable_variables()
                assignment_map, _ = get_assignment_map_from_checkpoint(tvars, pretrained_bert)
                tf.train.init_from_checkpoint(pretrained_bert, assignment_map) 
Example #29
Source File: utils.py    From jann with MIT License 4 votes vote down vote up
def __init__(self, annoy_index_path, unique_strings,
                 use_sentence_piece, module_path):
        self.annoy_index_path = annoy_index_path
        self.unique_strings = unique_strings

        # load the annoy index for mmap speed
        # Length of item vector that will be indexed
        self.annoy_index = AnnoyIndex(512, metric='angular')

        # super fast, will just mmap the file
        self.annoy_index.load(self.annoy_index_path)

        g = tf.Graph()
        with g.as_default():
            # define the module
            module = hub.Module(module_path, trainable=False)

            if use_sentence_piece:
                # build an input placeholder
                self.input_placeholder = tf.sparse_placeholder(
                  tf.int64, shape=[None, None])

                # build an input / output from the placeholders
                self.embeddings = module(inputs=dict(
                    values=self.input_placeholder.values,
                    indices=self.input_placeholder.indices,
                    dense_shape=self.input_placeholder.dense_shape
                  )
                )
            else:
                # build an input placeholder
                self.input_placeholder = tf.placeholder(
                  tf.string, shape=(None))
                self.embeddings = module(self.input_placeholder)

            init_op = tf.group([tf.global_variables_initializer(),
                                tf.tables_initializer()])

        # do not finalize the graph if using sentence piece module
        if not use_sentence_piece:
            g.finalize()

        # define the configuration
        config = tf.ConfigProto(allow_soft_placement=True)
        self.sess = tf.Session(graph=g, config=config)
        self.sess.run(init_op)

        if use_sentence_piece:
            # spm_path now contains a path to the SentencePiece
            # model stored inside the TF-Hub module
            with g.as_default():
                spm_path = self.sess.run(module(signature="spm_path"))
            self.sp = spm.SentencePieceProcessor()
            self.sp.Load(spm_path)

        tf.logging.info('Interactive session is initialized...') 
Example #30
Source File: tf_utils.py    From ludwig with Apache License 2.0 4 votes vote down vote up
def get_tf_config(gpus=None, gpu_fraction=1, horovod=None,
                  allow_parallel_threads=True):
    intra_op_parallelism_threads = 0  # tf determines automatically
    inter_op_parallelism_threads = 0  # tf determines automatically
    if not allow_parallel_threads:
        # this is needed for reproducibility
        intra_op_parallelism_threads = 1
        inter_op_parallelism_threads = 1

    if gpus is not None:
        if gpu_fraction > 0 and gpu_fraction < 1:
            # this is the source of freezing in tensorflow 1.3.1
            gpu_options = GPUOptions(
                per_process_gpu_memory_fraction=gpu_fraction,
                allow_growth=True)
        else:
            gpu_options = GPUOptions(allow_growth=True)
            # allow_growth=True is needed for a weird behavior with CUDA 10
            # https://github.com/tensorflow/tensorflow/issues/24828
        if isinstance(gpus, int):
            gpus = [gpus]
        gpu_options.visible_device_list = ','.join(str(g) for g in gpus)
        tf_config = ConfigProto(
            allow_soft_placement=True,
            log_device_placement=False,
            intra_op_parallelism_threads=intra_op_parallelism_threads,
            inter_op_parallelism_threads=inter_op_parallelism_threads,
            gpu_options=gpu_options
        )
    else:
        tf_config = ConfigProto(
            allow_soft_placement=True,
            log_device_placement=False,
            intra_op_parallelism_threads=intra_op_parallelism_threads,
            inter_op_parallelism_threads=inter_op_parallelism_threads,
            gpu_options=GPUOptions(allow_growth=True)
        )

    if horovod is not None:
        tf_config.gpu_options.visible_device_list = str(horovod.local_rank())

    return tf_config