Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling for missing TensorFlow dependency in KerasCV #2435

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion keras_cv/src/backend/tf_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@
from tensorflow import split # noqa: F403, F401
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These imports will also fail in case tensorflow is not installed. Making this same change across all the files makes it more hard to maintain. Instead, this should be wrapped around in some module like keras_cv/utils/conditional_imports.py and import tensorflow from this wrapper.


import numpy as np
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will keras-cv always depend on tensorflow for data processing layers?



def smart_resize(x, size, interpolation="bilinear"):
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/bounding_box/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
"""Converter functions for working with bounding box formats."""


import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.api_export import keras_cv_export
from keras_cv.src.backend import keras
Expand Down
9 changes: 8 additions & 1 deletion keras_cv/src/bounding_box/converters_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@

import numpy as np
import pytest
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
from absl.testing import parameterized

from keras_cv.src import bounding_box
Expand Down
9 changes: 8 additions & 1 deletion keras_cv/src/bounding_box/mask_invalid_detections_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@

import numpy as np
import pytest
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src import bounding_box
from keras_cv.src.backend import ops
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/bounding_box/to_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

import keras_cv.src.bounding_box.validate_format as validate_format
from keras_cv.src.api_export import keras_cv_export
Expand Down
9 changes: 8 additions & 1 deletion keras_cv/src/bounding_box/to_dense_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src import bounding_box
from keras_cv.src.tests.test_case import TestCase
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/bounding_box/to_ragged.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

import keras_cv.src.bounding_box.validate_format as validate_format
from keras_cv.src import backend
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/bounding_box/validate_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.api_export import keras_cv_export

Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/callbacks/waymo_evaluation_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
from tensorflow.keras.callbacks import Callback

from keras_cv.src.api_export import keras_cv_export
Expand Down
9 changes: 8 additions & 1 deletion keras_cv/src/callbacks/waymo_evaluation_callback_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@

import numpy as np
import pytest
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
from tensorflow import keras

from keras_cv.src.callbacks import WaymoEvaluationCallback
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/core/factor_sampler/constant_factor_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.api_export import keras_cv_export
from keras_cv.src.core.factor_sampler.factor_sampler import FactorSampler
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/core/factor_sampler/normal_factor_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.api_export import keras_cv_export
from keras_cv.src.core.factor_sampler.factor_sampler import FactorSampler
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/core/factor_sampler/uniform_factor_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.api_export import keras_cv_export
from keras_cv.src.core.factor_sampler.factor_sampler import FactorSampler
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/datasets/imagenet/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
from tensorflow.keras import layers

from keras_cv.src.api_export import keras_cv_export
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/datasets/pascal_voc/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
import tensorflow_datasets as tfds

from keras_cv.src import bounding_box
Expand Down
9 changes: 8 additions & 1 deletion keras_cv/src/datasets/pascal_voc/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ class and instance segmentation masks.
import xml

import numpy as np
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
import tensorflow_datasets as tfds
from tensorflow import keras

Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/datasets/pascal_voc/segmentation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
import pathlib
import sys

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
from absl import flags

from keras_cv.src.datasets.pascal_voc import segmentation
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/datasets/waymo/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
"""Data loader for the Waymo Open Dataset."""
import os

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.datasets.waymo import transformer
from keras_cv.src.utils import assert_waymo_open_dataset_installed
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/datasets/waymo/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
import dataclasses
from typing import Optional

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)


@dataclasses.dataclass
Expand Down
9 changes: 8 additions & 1 deletion keras_cv/src/datasets/waymo/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
from typing import Tuple

import numpy as np
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.api_export import keras_cv_export
from keras_cv.src.utils import assert_waymo_open_dataset_installed
Expand Down
9 changes: 8 additions & 1 deletion keras_cv/src/datasets/waymo/transformer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@

import numpy as np
import pytest
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.tests.test_case import TestCase

Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/keypoint/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
# limitations under the License.
"""Converter functions for working with keypoints formats."""

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.api_export import keras_cv_export

Expand Down
9 changes: 8 additions & 1 deletion keras_cv/src/keypoint/converters_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
import itertools

import numpy as np
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
from absl.testing import parameterized

from keras_cv.src import keypoint
Expand Down
8 changes: 7 additions & 1 deletion keras_cv/src/keypoint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utility functions for keypoint transformation."""
import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_cv.src.api_export import keras_cv_export

Expand Down
9 changes: 8 additions & 1 deletion keras_cv/src/keypoint/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
# limitations under the License.

import numpy as np
import tensorflow as tf

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
from absl.testing import parameterized

from keras_cv.src.keypoint.utils import filter_out_of_image
Expand Down
Loading
Loading