|
| 1 | +# Copyright 2023 The KerasCV Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import types |
| 16 | + |
| 17 | +from keras_cv.backend import keras |
| 18 | + |
| 19 | +try: |
| 20 | + import namex |
| 21 | +except ImportError: |
| 22 | + namex = None |
| 23 | + |
| 24 | + |
| 25 | +def maybe_register_serializable(symbol): |
| 26 | + if isinstance(symbol, types.FunctionType) or hasattr(symbol, "get_config"): |
| 27 | + keras.saving.register_keras_serializable(package="keras_cv")(symbol) |
| 28 | + |
| 29 | + |
| 30 | +if namex: |
| 31 | + |
| 32 | + class keras_cv_export(namex.export): |
| 33 | + def __init__(self, path): |
| 34 | + super().__init__(package="keras_cv", path=path) |
| 35 | + |
| 36 | + def __call__(self, symbol): |
| 37 | + maybe_register_serializable(symbol) |
| 38 | + return super().__call__(symbol) |
| 39 | + |
| 40 | +else: |
| 41 | + |
| 42 | + class keras_cv_export: |
| 43 | + def __init__(self, path): |
| 44 | + pass |
| 45 | + |
| 46 | + def __call__(self, symbol): |
| 47 | + maybe_register_serializable(symbol) |
| 48 | + return symbol |
0 commit comments