Skip to content

Commit

Permalink
Merge branch 'keras-team:main' into add-affine
Browse files Browse the repository at this point in the history
  • Loading branch information
james77777778 authored Jul 16, 2023
2 parents 03845ba + 1f6b69a commit ecc825e
Show file tree
Hide file tree
Showing 34 changed files with 729 additions and 127 deletions.
492 changes: 492 additions & 0 deletions examples/keras_io/tensorflow/vision/zero_dce.py

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions keras_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
from keras_core.models import Functional
from keras_core.models import Model
from keras_core.models import Sequential

__version__ = "0.1.0"
from keras_core.version import __version__
2 changes: 1 addition & 1 deletion keras_core/backend/common/global_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def clear_session():
GLOBAL_SETTINGS_TRACKER = threading.local()

if backend.backend() == "tensorflow":
import tensorflow as tf
from keras_core.utils.module_utils import tensorflow as tf

tf.compat.v1.reset_default_graph()
if tf.executing_eagerly():
Expand Down
43 changes: 43 additions & 0 deletions keras_core/initializers/constant_initalizers_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import numpy as np

from keras_core import backend
from keras_core import initializers
from keras_core import testing


class ConstantInitializersTest(testing.TestCase):
def test_zeros_initializer(self):
shape = (3, 3)

initializer = initializers.Zeros()
values = initializer(shape=shape)
self.assertEqual(values.shape, shape)
np_values = backend.convert_to_numpy(values).data
self.assertEqual(np_values, np.zeros(shape=shape))

self.run_class_serialization_test(initializer)

def test_ones_initializer(self):
shape = (3, 3)

initializer = initializers.Ones()
values = initializer(shape=shape)
self.assertEqual(values.shape, shape)
np_values = backend.convert_to_numpy(values).data
self.assertEqual(np_values, np.ones(shape=shape))

self.run_class_serialization_test(initializer)

def test_constant_initializer(self):
shape = (3, 3)
constant_value = 6.0

initializer = initializers.Constant(value=constant_value)
values = initializer(shape=shape)
self.assertEqual(values.shape, shape)
np_values = backend.convert_to_numpy(values).data
self.assertEqual(
np_values, np.full(shape=shape, fill_value=constant_value)
)

self.run_class_serialization_test(initializer)
1 change: 1 addition & 0 deletions keras_core/initializers/constant_initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, value=0.0):
self.value = float(value)

def __call__(self, shape, dtype=None):
dtype = standardize_dtype(dtype)
return self.value * ops.ones(shape=shape, dtype=dtype)

def get_config(self):
Expand Down
5 changes: 2 additions & 3 deletions keras_core/layers/preprocessing/category_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.CategoryEncoding")
Expand Down Expand Up @@ -83,9 +84,7 @@ class CategoryEncoding(Layer):
"""

def __init__(self, num_tokens=None, output_mode="multi_hot", **kwargs):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer CategoryEncoding requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand Down
7 changes: 2 additions & 5 deletions keras_core/layers/preprocessing/discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.Discretization")
Expand Down Expand Up @@ -102,9 +103,7 @@ def __init__(
dtype=None,
**kwargs,
):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer Discretization requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand Down Expand Up @@ -193,8 +192,6 @@ def compute_output_spec(self, inputs):
return backend.KerasTensor(shape=inputs.shape, dtype="int32")

def __call__(self, inputs):
import tensorflow as tf

if not isinstance(inputs, (tf.Tensor, np.ndarray, backend.KerasTensor)):
inputs = tf.convert_to_tensor(backend.convert_to_numpy(inputs))
if not self.built:
Expand Down
5 changes: 2 additions & 3 deletions keras_core/layers/preprocessing/hashed_crossing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.HashedCrossing")
Expand Down Expand Up @@ -74,9 +75,7 @@ def __init__(
dtype=None,
**kwargs,
):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer HashedCrossing requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand Down
7 changes: 2 additions & 5 deletions keras_core/layers/preprocessing/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.Hashing")
Expand Down Expand Up @@ -143,9 +144,7 @@ def __init__(
name=None,
**kwargs,
):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer Hashing requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand Down Expand Up @@ -183,8 +182,6 @@ def __init__(
self.supports_jit = False

def call(self, inputs):
import tensorflow as tf

if not isinstance(inputs, (tf.Tensor, np.ndarray, list, tuple)):
inputs = tf.convert_to_tensor(np.array(inputs))
outputs = self.layer.call(inputs)
Expand Down
7 changes: 2 additions & 5 deletions keras_core/layers/preprocessing/integer_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.IntegerLookup")
Expand Down Expand Up @@ -309,9 +310,7 @@ def __init__(
name=None,
**kwargs,
):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer IntegerLookup requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand Down Expand Up @@ -449,8 +448,6 @@ def set_vocabulary(self, vocabulary, idf_weights=None):
self.layer.set_vocabulary(vocabulary, idf_weights=idf_weights)

def call(self, inputs):
import tensorflow as tf

if not isinstance(inputs, (tf.Tensor, np.ndarray, list, tuple)):
inputs = tf.convert_to_tensor(np.array(inputs))
outputs = self.layer.call(inputs)
Expand Down
3 changes: 1 addition & 2 deletions keras_core/layers/preprocessing/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from keras_core import ops
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.Normalization")
Expand Down Expand Up @@ -216,8 +217,6 @@ def adapt(self, data):
data is batched, and if that assumption doesn't hold, the mean
and variance may be incorrectly computed.
"""
import tensorflow as tf

if isinstance(data, np.ndarray) or backend.is_tensor(data):
input_shape = data.shape
elif isinstance(data, tf.data.Dataset):
Expand Down
7 changes: 2 additions & 5 deletions keras_core/layers/preprocessing/random_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.RandomCrop")
Expand Down Expand Up @@ -52,9 +53,7 @@ class RandomCrop(Layer):
"""

def __init__(self, height, width, seed=None, name=None, **kwargs):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer RandomCrop requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand All @@ -74,8 +73,6 @@ def __init__(self, height, width, seed=None, name=None, **kwargs):
self._allow_non_tensor_positional_args = True

def call(self, inputs, training=True):
import tensorflow as tf

if not isinstance(inputs, (tf.Tensor, np.ndarray, list, tuple)):
inputs = tf.convert_to_tensor(backend.convert_to_numpy(inputs))
outputs = self.layer.call(inputs, training=training)
Expand Down
7 changes: 2 additions & 5 deletions keras_core/layers/preprocessing/random_flip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf

HORIZONTAL = "horizontal"
VERTICAL = "vertical"
Expand Down Expand Up @@ -45,9 +46,7 @@ class RandomFlip(Layer):
def __init__(
self, mode=HORIZONTAL_AND_VERTICAL, seed=None, name=None, **kwargs
):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer RandomFlip requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand All @@ -66,8 +65,6 @@ def __init__(
self._allow_non_tensor_positional_args = True

def call(self, inputs, training=True):
import tensorflow as tf

if not isinstance(inputs, (tf.Tensor, np.ndarray, list, tuple)):
inputs = tf.convert_to_tensor(backend.convert_to_numpy(inputs))
outputs = self.layer.call(inputs, training=training)
Expand Down
7 changes: 2 additions & 5 deletions keras_core/layers/preprocessing/random_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.RandomRotation")
Expand Down Expand Up @@ -85,9 +86,7 @@ def __init__(
name=None,
**kwargs,
):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer RandomRotation requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand All @@ -109,8 +108,6 @@ def __init__(
self._allow_non_tensor_positional_args = True

def call(self, inputs, training=True):
import tensorflow as tf

if not isinstance(inputs, (tf.Tensor, np.ndarray, list, tuple)):
inputs = tf.convert_to_tensor(backend.convert_to_numpy(inputs))
outputs = self.layer.call(inputs, training=training)
Expand Down
7 changes: 2 additions & 5 deletions keras_core/layers/preprocessing/random_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.RandomTranslation")
Expand Down Expand Up @@ -75,9 +76,7 @@ def __init__(
name=None,
**kwargs,
):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer RandomTranslation requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand All @@ -99,8 +98,6 @@ def __init__(
self._allow_non_tensor_positional_args = True

def call(self, inputs, training=True):
import tensorflow as tf

if not isinstance(inputs, (tf.Tensor, np.ndarray, list, tuple)):
inputs = tf.convert_to_tensor(backend.convert_to_numpy(inputs))
outputs = self.layer.call(inputs, training=training)
Expand Down
7 changes: 2 additions & 5 deletions keras_core/layers/preprocessing/random_zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.RandomZoom")
Expand Down Expand Up @@ -97,9 +98,7 @@ def __init__(
name=None,
**kwargs,
):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer RandomZoom requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand All @@ -122,8 +121,6 @@ def __init__(
self.supports_jit = False

def call(self, inputs, training=True):
import tensorflow as tf

if not isinstance(inputs, (tf.Tensor, np.ndarray, list, tuple)):
inputs = tf.convert_to_tensor(backend.convert_to_numpy(inputs))
outputs = self.layer.call(inputs, training=training)
Expand Down
7 changes: 2 additions & 5 deletions keras_core/layers/preprocessing/string_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras_core.api_export import keras_core_export
from keras_core.layers.layer import Layer
from keras_core.utils import backend_utils
from keras_core.utils.module_utils import tensorflow as tf


@keras_core_export("keras_core.layers.StringLookup")
Expand Down Expand Up @@ -307,9 +308,7 @@ def __init__(
dtype=None,
**kwargs,
):
try:
import tensorflow as tf
except ImportError:
if not tf.available:
raise ImportError(
"Layer StringLookup requires TensorFlow. "
"Install it via `pip install tensorflow`."
Expand Down Expand Up @@ -443,8 +442,6 @@ def set_vocabulary(self, vocabulary, idf_weights=None):
self.layer.set_vocabulary(vocabulary, idf_weights=idf_weights)

def call(self, inputs):
import tensorflow as tf

if not isinstance(inputs, (tf.Tensor, np.ndarray, list, tuple)):
inputs = tf.convert_to_tensor(np.array(inputs))
outputs = self.layer.call(inputs)
Expand Down
Loading

0 comments on commit ecc825e

Please sign in to comment.