-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address review comments and address saving bug
- Use `keras_cv.export_api.keras_cv_export` instead of `keras.saving.register_keras_serializable`. - Add a `SerializableSequential` class to address the saving bug with the `Sequential` model. - Push the helper functions in `keras_cv/layers/detectron2_layers.py` to the bottom of the file. - Add the detectron2 layers to the `keras_cv/layers/__init__.py` file. - Add a test for the `ViTDetPatchingAndEmbedding` layer.
- Loading branch information
1 parent
43b0f2b
commit ac7f30e
Showing
12 changed files
with
293 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2023 The KerasCV Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# 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. | ||
|
||
from keras_cv.api_export import keras_cv_export | ||
from keras_cv.backend import keras | ||
from keras_cv.utils.python_utils import classproperty | ||
|
||
|
||
# TODO(tirthasheshpatel): Use `Sequential` model once the bug is resolved. | ||
# Temporarily substitute the `Sequential` model with this because a | ||
# bug in Keras/Keras Core prevents the weights of a sequential model to | ||
# load in TensorFlow if they are saved in JAX/PyTorch and vice versa. | ||
# This only happens when the `build` method is called in the `__init__` | ||
# step. | ||
@keras_cv_export("keras_cv.layers.SerializableSequential") | ||
class SerializableSequential(keras.layers.Layer): | ||
def __init__(self, layers_list, **kwargs): | ||
super().__init__(**kwargs) | ||
self.layers_list = layers_list | ||
|
||
def build(self, input_shape): | ||
output_shape = input_shape | ||
for layer in self.layers_list: | ||
layer.build(output_shape) | ||
output_shape = layer.compute_output_shape(output_shape) | ||
self.built = True | ||
|
||
def call(self, x): | ||
for layer in self.layers_list: | ||
x = layer(x) | ||
return x | ||
|
||
def get_config(self): | ||
config = super().get_config() | ||
layers_list_serialized = [ | ||
keras.saving.serialize_keras_object(layer) | ||
for layer in self.layers_list | ||
] | ||
config.update({"layers_list": layers_list_serialized}) | ||
|
||
@classproperty | ||
def from_config(self, config): | ||
config.update( | ||
{ | ||
"layers_list": [ | ||
keras.layers.deserialize(layer) | ||
for layer in config["layers_list"] | ||
] | ||
} | ||
) | ||
return super().from_config(config) |
Oops, something went wrong.