Python tensorflow.enable_eager_execution() Examples

The following are 30 code examples of tensorflow.enable_eager_execution(). 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 , or try the search function .
Example #1
Source File: test_abstract_models.py    From delira with GNU Affero General Public License v3.0 6 votes vote down vote up
def _setup_tfeager(*args):
        import tensorflow as tf
        tf.enable_eager_execution()
        tf.reset_default_graph()
        from delira.models.backends.tf_eager import AbstractTfEagerNetwork

        class Model(AbstractTfEagerNetwork):
            def __init__(self):
                super().__init__()

                self.dense = tf.keras.layers.Dense(1, activation="relu")

            def call(self, x: tf.Tensor):
                return {"pred": self.dense(x)}

        return Model() 
Example #2
Source File: image_test.py    From VideoSuperResolution with MIT License 6 votes vote down vote up
def test_resize_upsample_tf(self):
    if BACKEND != 'tensorflow':
      return
    import tensorflow as tf
    tf.enable_eager_execution()
    from VSR.Backend.TF.Util import upsample

    Im = Image.open(URL)
    for X in [Im, Im.convert('L')]:
      w = X.width
      h = X.height
      for ss in [2, 3, 4, 5, 6]:
        GT = X.resize([w * ss, h * ss], Image.BICUBIC)
        gt = np.asarray(GT, dtype='float32') / 255
        x = tf.constant(np.asarray(X), dtype='float32') / 255
        y = upsample(x, ss).numpy().clip(0, 1)
        self.assertGreaterEqual(self.psnr(y, gt), 30, f"{X.mode}, {ss}") 
Example #3
Source File: trainer.py    From BERT with Apache License 2.0 6 votes vote down vote up
def main(_):
  logging.set_verbosity(FLAGS.log_level)

  if FLAGS.tf_eager:
    tf.enable_eager_execution()

  if FLAGS.tf_xla:
    tf.config.optimizer.set_jit(True)

  _setup_gin()

  # Setup output directory
  output_dir = FLAGS.output_dir or _default_output_dir()
  trax.log("Using --output_dir %s" % output_dir)
  output_dir = os.path.expanduser(output_dir)

  # If on TPU, let JAX know.
  if FLAGS.use_tpu:
    jax.config.update("jax_platform_name", "tpu")

  trax.train(output_dir=output_dir) 
Example #4
Source File: image_test.py    From VideoSuperResolution with MIT License 6 votes vote down vote up
def test_resize_downsample_tf(self):
    if BACKEND != 'tensorflow':
      return
    import tensorflow as tf
    tf.enable_eager_execution()
    from VSR.Backend.TF.Util import downsample

    Im = Image.open(URL)
    for X in [Im, Im.convert('L')]:
      w = X.width
      h = X.height
      for ss in [2, 4, 6, 8]:
        w_ = w - w % ss
        h_ = h - h % ss
        X = X.crop([0, 0, w_, h_])
        GT = X.resize([w_ // ss, h_ // ss], Image.BICUBIC)
        gt = np.asarray(GT, dtype='float32') / 255
        x = tf.constant(np.asarray(X), dtype='float32') / 255
        y = downsample(x, ss).numpy().clip(0, 1)
        self.assertGreaterEqual(self.psnr(y, gt), 30, f"{X.mode}, {ss}") 
Example #5
Source File: array_from_dataset.py    From causal-text-embeddings with MIT License 6 votes vote down vote up
def main():
    tf.enable_eager_execution()


    subreddit_based_sim_dfs(subreddits=subs, treat_strength=beta0, con_strength=beta1, noise_level=gamma, setting=mode, seed=0,
                            base_output_dir=base_output_dir)



    # print(itr.get_next()["token_ids"].name)
    # for i in range(1000):
    #     sample = itr.get_next()

    #
    # print(np.unique(df['year']))
    # print(df.groupby(['year'])['buzzy_title'].agg(np.mean))
    # print(df.groupby(['year'])['theorem_referenced'].agg(np.mean))
    # print(df.groupby(['year'])['accepted'].agg(np.mean)) 
Example #6
Source File: core_IT.py    From kglib with Apache License 2.0 6 votes vote down vote up
def test_kgcn_runs(self):
        tf.enable_eager_execution()

        graph = GraphsTuple(nodes=tf.convert_to_tensor(np.array([[1, 2, 0], [1, 0, 0], [1, 1, 0]], dtype=np.float32)),
                            edges=tf.convert_to_tensor(np.array([[1, 0, 0], [1, 0, 0]], dtype=np.float32)),
                            globals=tf.convert_to_tensor(np.array([[0, 0, 0, 0, 0]], dtype=np.float32)),
                            receivers=tf.convert_to_tensor(np.array([1, 2], dtype=np.int32)),
                            senders=tf.convert_to_tensor(np.array([0, 1], dtype=np.int32)),
                            n_node=tf.convert_to_tensor(np.array([3], dtype=np.int32)),
                            n_edge=tf.convert_to_tensor(np.array([2], dtype=np.int32)))

        thing_embedder = ThingEmbedder(node_types=['a', 'b', 'c'], type_embedding_dim=5, attr_embedding_dim=6,
                                       categorical_attributes={'a': ['a1', 'a2', 'a3'], 'b': ['b1', 'b2', 'b3']},
                                       continuous_attributes={'c': (0, 1)})

        role_embedder = RoleEmbedder(num_edge_types=2, type_embedding_dim=5)

        kgcn = KGCN(thing_embedder, role_embedder, edge_output_size=3, node_output_size=3)

        kgcn(graph, 2) 
Example #7
Source File: post_quantization.py    From tpu_models with Apache License 2.0 6 votes vote down vote up
def main(_):
  # Enables eager context for TF 1.x. TF 2.x will use eager by default.
  # This is used to conveniently get a representative dataset generator using
  # TensorFlow training input helper.
  tf.enable_eager_execution()

  converter = tf.lite.TFLiteConverter.from_saved_model(
      FLAGS.saved_model_dir,
      input_arrays=[FLAGS.input_name],
      output_arrays=[FLAGS.output_name])
  # Chooses a tf.lite.Optimize mode:
  # https://www.tensorflow.org/api_docs/python/tf/lite/Optimize
  converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_LATENCY]
  converter.representative_dataset = tf.lite.RepresentativeDataset(
      representative_dataset_gen)
  if FLAGS.require_int8:
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]

  tflite_buffer = converter.convert()
  tf.gfile.GFile(FLAGS.output_tflite, "wb").write(tflite_buffer)
  print("tflite model written to %s" % FLAGS.output_tflite) 
Example #8
Source File: typewise_test.py    From kglib with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        tf.enable_eager_execution() 
Example #9
Source File: test_tf.py    From delira with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_load_save_eager(self):
        import tensorflow as tf
        tf.enable_eager_execution()
        from delira.io.tf import load_checkpoint_eager, save_checkpoint_eager
        from delira.models import AbstractTfEagerNetwork

        import numpy as np

        class DummyNetwork(AbstractTfEagerNetwork):
            def __init__(self, in_channels, n_outputs):
                super().__init__(in_channels=in_channels, n_outputs=n_outputs)
                with tf.init_scope():
                    self.net = self._build_model(in_channels, n_outputs)

            @staticmethod
            def _build_model(in_channels, n_outputs):
                return tf.keras.models.Sequential(
                    layers=[
                        tf.keras.layers.Dense(
                            64,
                            input_shape=in_channels,
                            bias_initializer='glorot_uniform'),
                        tf.keras.layers.ReLU(),
                        tf.keras.layers.Dense(
                            n_outputs,
                            bias_initializer='glorot_uniform')])

            def call(self, inputs):
                return self.net(inputs)

        net = DummyNetwork((32,), 1)
        input_tensor = tf.constant(np.random.rand(1, 32).astype(np.float32))
        result_pre_save = net(input_tensor)
        save_checkpoint_eager("./model_eager", model=net)

        loaded_state = load_checkpoint_eager("./model_eager", model=net)
        loaded_net = loaded_state["model"]

        result_post_save = loaded_net(input_tensor)

        self.assertTrue(np.array_equal(result_post_save, result_pre_save)) 
Example #10
Source File: tf_wpe.py    From nara_wpe with MIT License 5 votes vote down vote up
def perform_filter_operation(Y, filter_matrix_conj, taps, delay):
    """

    >>> D, T, taps, delay = 1, 10, 2, 1
    >>> tf.enable_eager_execution()
    >>> Y = tf.ones([D, T])
    >>> filter_matrix_conj = tf.ones([taps, D, D])
    >>> X = perform_filter_operation_v2(Y, filter_matrix_conj, taps, delay)
    >>> X.shape
    TensorShape([Dimension(1), Dimension(10)])
    >>> X.numpy()
    array([[ 1.,  0., -1., -1., -1., -1., -1., -1., -1., -1.]], dtype=float32)
    """
    dyn_shape = tf.shape(Y)
    T = dyn_shape[1]

    def add_tap(accumulated, tau_minus_delay):
        new = tf.einsum(
            'de,dt',
            filter_matrix_conj[tau_minus_delay, :, :],
            Y[:, :(T - delay - tau_minus_delay)]
        )
        paddings = tf.convert_to_tensor([[0, 0], [delay + tau_minus_delay, 0]])
        new = tf.pad(new, paddings, "CONSTANT")
        return accumulated + new

    reverb_tail = tf.foldl(
        add_tap, tf.range(0, taps),
        initializer=tf.zeros_like(Y)
    )
    return Y - reverb_tail 
Example #11
Source File: tf2_checkpoint_converter.py    From Live-feed-object-device-identification-using-Tensorflow-and-OpenCV with Apache License 2.0 5 votes vote down vote up
def main(_):
  tf.enable_eager_execution()
  convert_checkpoint() 
Example #12
Source File: gen_obj2sen_caption.py    From unsupervised_captioning with MIT License 5 votes vote down vote up
def main(_):
  pool = multiprocessing.Pool(FLAGS.num_proc, initializer=initializer)
  os.environ['CUDA_VISIBLE_DEVICES'] = ''
  import tensorflow as tf
  tf.enable_eager_execution()
  with tf.python_io.TFRecordWriter('data/obj2sen_captions.tfrec') as writer:
    for i in pool.imap(run, image_generator(tf)):
      writer.write(i) 
Example #13
Source File: image.py    From camera-trap-classifier with MIT License 5 votes vote down vote up
def read_image_from_disk_and_convert_to_jpeg(
        path_to_image,
        image_save_quality=75):
    """ TF-Functions to read and convert jpeg
        Requires tf.enable_eager_execution()
    """
    with tf.gfile.GFile(path_to_image, 'rb') as f:
        file_bytes = f.read()
    image = tf.image.decode_image(file_bytes)
    jpeg = tf.image.encode_jpeg(image, quality=image_save_quality).numpy()
    return jpeg 
Example #14
Source File: image.py    From camera-trap-classifier with MIT License 5 votes vote down vote up
def read_image_from_disk_resize_and_convert_to_jpeg(
        path_to_image,
        smallest_side,
        image_save_quality=75):
    """ TF-Functions to read and convert jpeg
        Requires tf.enable_eager_execution()
    """
    with tf.gfile.GFile(path_to_image, 'rb') as f:
        file_bytes = f.read()
    image = tf.image.decode_image(file_bytes)
    image = _aspect_preserving_resize(image, smallest_side)
    image = tf.cast(image, dtype=tf.uint8)
    jpeg = tf.image.encode_jpeg(image, quality=image_save_quality).numpy()
    return jpeg 
Example #15
Source File: typewise_IT.py    From kglib with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        tf.enable_eager_execution() 
Example #16
Source File: color_space_conversion.py    From open_model_zoo with Apache License 2.0 5 votes vote down vote up
def __init__(self, config, name, input_shapes=None):
        super().__init__(config, name, input_shapes)
        if tf is None:
            raise ImportError('*tf_convert_image_dtype* operation requires TensorFlow. Please install it before usage')
        tf.enable_eager_execution()
        self.converter = tf.image.convert_image_dtype
        self.dtype = tf.float32 
Example #17
Source File: embedding_test.py    From kglib with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        tf.enable_eager_execution() 
Example #18
Source File: embedding_test.py    From kglib with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        tf.enable_eager_execution() 
Example #19
Source File: test_adapters.py    From tfpyth with MIT License 5 votes vote down vote up
def test_pytorch_in_tensorflow_eager_mode():
    tf.enable_eager_execution()
    tfe = tf.contrib.eager

    def pytorch_expr(a, b):
        return 3 * a + 4 * b * b

    x = tfpyth.eager_tensorflow_from_torch(pytorch_expr)

    assert tf.math.equal(x(tf.convert_to_tensor(1.0), tf.convert_to_tensor(3.0)), 39.0)

    dx = tfe.gradients_function(x)
    assert all(tf.math.equal(dx(tf.convert_to_tensor(1.0), tf.convert_to_tensor(3.0)), [3.0, 24.0]))
    tf.disable_eager_execution() 
Example #20
Source File: build_test.py    From mac-graph with The Unlicense 5 votes vote down vote up
def setUpClass(cls):
        tf.enable_eager_execution()

        argv = [
            '--gqa-paths',  'input_data/raw/test.yaml',
            '--input-dir', 'input_data/processed/test',
            '--limit', '100',
            '--predict-holdback', '0.1',
            '--eval-holdback', '0.1',
        ]

        args = get_args(argv=argv)
        cls.args = args

        build(args) 
Example #21
Source File: data_reader.py    From open_model_zoo with Apache License 2.0 5 votes vote down vote up
def __init__(self, data_source, config=None, **kwargs):
        super().__init__(data_source, config)
        if tf is None:
            raise ImportError('tf backend for image reading requires TensorFlow. Please install it before usage.')

        tf.enable_eager_execution()

        def read_func(path):
            img_raw = tf.read_file(str(path))
            img_tensor = tf.image.decode_image(img_raw, channels=3)
            return img_tensor.numpy()

        self.read_realisation = read_func 
Example #22
Source File: resize.py    From open_model_zoo with Apache License 2.0 5 votes vote down vote up
def __init__(self, interpolation):
        if tf is None:
            raise ImportError('tf backend for resize operation requires TensorFlow. Please install it before usage.')
        tf.enable_eager_execution()
        self._supported_interpolations = {
            'BILINEAR': tf.image.ResizeMethod.BILINEAR,
            'AREA': tf.image.ResizeMethod.AREA,
            'BICUBIC': tf.image.ResizeMethod.BICUBIC,
        }
        self.default_interpolation = 'BILINEAR'
        self._resize = tf.image.resize_images

        super().__init__(interpolation) 
Example #23
Source File: main.py    From spotify-tensorflow with Apache License 2.0 5 votes vote down vote up
def main():
    tf.enable_eager_execution()
    tf.app.run(main=train) 
Example #24
Source File: memory_data.py    From STGAN with MIT License 5 votes vote down vote up
def map_func(x):
        x['a'] = x['a'] * 10
        return x

    # tf.enable_eager_execution() 
Example #25
Source File: main.py    From spotify-tensorflow with Apache License 2.0 5 votes vote down vote up
def main():
    # Enable eager execution for DataFrame endpoint
    import tensorflow as tf
    tf.enable_eager_execution()

    # Set up training data
    train_data_dir = get_data_dir("train")
    train_data = os.path.join(train_data_dir, "part-*")
    schema_path = os.path.join(train_data_dir, "_inferred_schema.pb")

    df_train_data = next(Datasets.dataframe.examples_via_schema(train_data,
                                                                schema_path,
                                                                batch_size=1024))

    # the feature keys are ordered alphabetically for determinism
    label_keys = sorted([l for l in set(df_train_data.columns) if l.startswith("class_name")])
    feature_keys = sorted(set(df_train_data.columns).difference(label_keys))

    label = df_train_data[label_keys].apply(transform_labels, axis=1)
    features = df_train_data[feature_keys]

    # Build model
    from sklearn.linear_model import LogisticRegression
    model = LogisticRegression(multi_class="multinomial", solver="newton-cg")
    model.fit(features, label)

    # Set up eval data
    eval_data_dir = get_data_dir("eval")
    eval_data = os.path.join(eval_data_dir, "part-*")
    df_eval_data = next(Datasets.dataframe.examples_via_schema(eval_data,
                                                               schema_path,
                                                               batch_size=1024))

    eval_label = df_eval_data[label_keys].apply(transform_labels, axis=1)
    eval_features = df_eval_data[feature_keys]

    # Evaluate model
    score = model.score(eval_features, eval_label)
    print("Score is %f" % score) 
Example #26
Source File: backend.py    From deep_architect with MIT License 5 votes vote down vote up
def set_backend(backend):
    global _backend, _func_dict
    if _backend is not None:
        raise RuntimeError('Backend is already specified')
    if type(backend) is not int or backend < 0 or backend > 4:
        raise ValueError('value of backend not valid')

    _backend = backend
    if backend is TENSORFLOW:
        from .tensorflow_ops import func_dict
    elif backend is TENSORFLOW_EAGER:
        import tensorflow as tf
        tf_version = tf.__version__.split('.')
        if int(tf_version[0]) < 1:
            raise RuntimeError('Tensorflow version too low')
        if int(tf_version[0]) == 1 and int(tf_version[1]) < 7:
            raise RuntimeError('Tensorflow version too low')
        tf.enable_eager_execution()
        from .tensorflow_eager_ops import func_dict
    elif backend is TENSORFLOW_KERAS:
        import tensorflow as tf
        from .tensorflow_keras_ops import func_dict
    elif backend is PYTORCH:
        from .pytorch_ops import func_dict
    elif backend is KERAS:
        from .keras_ops import func_dict
    _func_dict = func_dict
    if _func_dict is None:
        raise RuntimeError('Backend %s is not supported' % backend) 
Example #27
Source File: test_extend_segments.py    From bert-for-tf2 with MIT License 5 votes vote down vote up
def setUp(self) -> None:
        tf.compat.v1.reset_default_graph()
        tf.compat.v1.enable_eager_execution()
        print("Eager Execution:", tf.executing_eagerly()) 
Example #28
Source File: build_test.py    From shortest-path with The Unlicense 5 votes vote down vote up
def setUpClass(cls):
        tf.enable_eager_execution()

        argv = [
            '--gqa-paths',  'input_data/raw/test.yaml',
            '--input-dir', 'input_data/processed/test',
            '--limit', '100',
            '--predict-holdback', '0.1',
            '--eval-holdback', '0.1',
        ]

        args = get_args(argv=argv)
        cls.args = args

        build(args) 
Example #29
Source File: array_from_dataset.py    From causal-text-embeddings with MIT License 5 votes vote down vote up
def main():
    tf.enable_eager_execution()

    buzzy_title_based_sim_dfs(treat_strength=beta0, con_strength=beta1, noise_level=gamma, setting=mode, seed=0,
                            base_output_dir=base_output_dir) 
Example #30
Source File: test_tf.py    From delira with GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self) -> None:
        import tensorflow as tf
        tf.reset_default_graph()
        if "_eager" in self._testMethodName:
            tf.enable_eager_execution()
        else:
            tf.disable_eager_execution()