Skip to content

Commit

Permalink
Migrate Efficientnetv1 to Backbone (#1716)
Browse files Browse the repository at this point in the history
* created new files

* moved old files to new one

* efficientnetv1 backbone, presets, and imports updated

* fix imports

* fix imports

* added preset unit test

* updated backbone

* fix init

* fix legacy init

* fix aliases presets

* fix backbone round filter argument

* fix depthwise conv

* fix conv kernel initializer

* fix depthwiseconv

* fix scope name match pattern error

* fix scope name

* fix block name

* remove block args preset

* remove model_name

* remove default_size from preset

* updated test cases

* updated docs

* fix id_skip

* fix test

* fix format

* reviewd comments

* fix format

* fix typo

* fix naming

* test with layer-matching

* fix typo

* fix format

* backbone test updated

* review changes

* port

* fix port

* fix port 2

* port: fix argument

* port: final fix

* port:docs typo

* port: update

* review comment

* format
  • Loading branch information
cosmo3769 authored Aug 17, 2023
1 parent 1eabccb commit 1602b17
Show file tree
Hide file tree
Showing 11 changed files with 1,409 additions and 1,003 deletions.
27 changes: 27 additions & 0 deletions keras_cv/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@
from keras_cv.models.backbones.efficientnet_lite.efficientnet_lite_backbone import ( # noqa: E501
EfficientNetLiteBackbone,
)
from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_aliases import (
EfficientNetV1B0Backbone,
)
from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_aliases import (
EfficientNetV1B1Backbone,
)
from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_aliases import (
EfficientNetV1B2Backbone,
)
from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_aliases import (
EfficientNetV1B3Backbone,
)
from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_aliases import (
EfficientNetV1B4Backbone,
)
from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_aliases import (
EfficientNetV1B5Backbone,
)
from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_aliases import (
EfficientNetV1B6Backbone,
)
from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_aliases import (
EfficientNetV1B7Backbone,
)
from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_aliases import (
EfficientNetV1Backbone,
)
from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_aliases import (
EfficientNetV2B0Backbone,
)
Expand Down
5 changes: 5 additions & 0 deletions keras_cv/models/backbones/backbone_presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from keras_cv.models.backbones.efficientnet_lite import (
efficientnet_lite_backbone_presets,
)
from keras_cv.models.backbones.efficientnet_v1 import (
efficientnet_v1_backbone_presets,
)
from keras_cv.models.backbones.efficientnet_v2 import (
efficientnet_v2_backbone_presets,
)
Expand All @@ -32,6 +35,7 @@
**resnet_v2_backbone_presets.backbone_presets_no_weights,
**mobilenet_v3_backbone_presets.backbone_presets_no_weights,
**csp_darknet_backbone_presets.backbone_presets_no_weights,
**efficientnet_v1_backbone_presets.backbone_presets_no_weights,
**efficientnet_v2_backbone_presets.backbone_presets_no_weights,
**densenet_backbone_presets.backbone_presets_no_weights,
**efficientnet_lite_backbone_presets.backbone_presets_no_weights,
Expand All @@ -43,6 +47,7 @@
**resnet_v2_backbone_presets.backbone_presets_with_weights,
**mobilenet_v3_backbone_presets.backbone_presets_with_weights,
**csp_darknet_backbone_presets.backbone_presets_with_weights,
**efficientnet_v1_backbone_presets.backbone_presets_with_weights,
**efficientnet_v2_backbone_presets.backbone_presets_with_weights,
**densenet_backbone_presets.backbone_presets_with_weights,
**efficientnet_lite_backbone_presets.backbone_presets_with_weights,
Expand Down
13 changes: 13 additions & 0 deletions keras_cv/models/backbones/efficientnet_v1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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.
315 changes: 315 additions & 0 deletions keras_cv/models/backbones/efficientnet_v1/efficientnet_v1_aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
# 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.models.backbones.efficientnet_v1.efficientnet_v1_backbone import (
EfficientNetV1Backbone,
)
from keras_cv.utils.python_utils import classproperty

ALIAS_DOCSTRING = """Instantiates the {name} architecture.
Reference:
- [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://arxiv.org/abs/1905.11946)
(ICML 2019)
Args:
include_rescaling: bool, whether to rescale the inputs. If set
to `True`, inputs will be passed through a `Rescaling(1/255.0)`
layer.
input_shape: optional shape tuple, defaults to (None, None, 3).
input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
to use as image input for the model.
""" # noqa: E501


class EfficientNetV1B0Backbone(EfficientNetV1Backbone):
def __new__(
cls,
include_rescaling=True,
input_shape=(None, None, 3),
input_tensor=None,
**kwargs,
):
# Pack args in kwargs
kwargs.update(
{
"include_rescaling": include_rescaling,
"input_shape": input_shape,
"input_tensor": input_tensor,
}
)
return EfficientNetV1Backbone.from_preset("efficientnetv1_b0", **kwargs)

@classproperty
def presets(cls):
"""Dictionary of preset names and configurations."""
return {}

@classproperty
def presets_with_weights(cls):
"""Dictionary of preset names and configurations that include
weights."""
return {}


class EfficientNetV1B1Backbone(EfficientNetV1Backbone):
def __new__(
cls,
include_rescaling=True,
input_shape=(None, None, 3),
input_tensor=None,
**kwargs,
):
# Pack args in kwargs
kwargs.update(
{
"include_rescaling": include_rescaling,
"input_shape": input_shape,
"input_tensor": input_tensor,
}
)
return EfficientNetV1Backbone.from_preset("efficientnetv1_b1", **kwargs)

@classproperty
def presets(cls):
"""Dictionary of preset names and configurations."""
return {}

@classproperty
def presets_with_weights(cls):
"""Dictionary of preset names and configurations that include
weights."""
return {}


class EfficientNetV1B2Backbone(EfficientNetV1Backbone):
def __new__(
cls,
include_rescaling=True,
input_shape=(None, None, 3),
input_tensor=None,
**kwargs,
):
# Pack args in kwargs
kwargs.update(
{
"include_rescaling": include_rescaling,
"input_shape": input_shape,
"input_tensor": input_tensor,
}
)
return EfficientNetV1Backbone.from_preset("efficientnetv1_b2", **kwargs)

@classproperty
def presets(cls):
"""Dictionary of preset names and configurations."""
return {}

@classproperty
def presets_with_weights(cls):
"""Dictionary of preset names and configurations that include
weights."""
return {}


class EfficientNetV1B3Backbone(EfficientNetV1Backbone):
def __new__(
cls,
include_rescaling=True,
input_shape=(None, None, 3),
input_tensor=None,
**kwargs,
):
# Pack args in kwargs
kwargs.update(
{
"include_rescaling": include_rescaling,
"input_shape": input_shape,
"input_tensor": input_tensor,
}
)
return EfficientNetV1Backbone.from_preset("efficientnetv1_b3", **kwargs)

@classproperty
def presets(cls):
"""Dictionary of preset names and configurations."""
return {}

@classproperty
def presets_with_weights(cls):
"""Dictionary of preset names and configurations that include
weights."""
return {}


class EfficientNetV1B4Backbone(EfficientNetV1Backbone):
def __new__(
cls,
include_rescaling=True,
input_shape=(None, None, 3),
input_tensor=None,
**kwargs,
):
# Pack args in kwargs
kwargs.update(
{
"include_rescaling": include_rescaling,
"input_shape": input_shape,
"input_tensor": input_tensor,
}
)
return EfficientNetV1Backbone.from_preset("efficientnetv1_b4", **kwargs)

@classproperty
def presets(cls):
"""Dictionary of preset names and configurations."""
return {}

@classproperty
def presets_with_weights(cls):
"""Dictionary of preset names and configurations that include
weights."""
return {}


class EfficientNetV1B5Backbone(EfficientNetV1Backbone):
def __new__(
cls,
include_rescaling=True,
input_shape=(None, None, 3),
input_tensor=None,
**kwargs,
):
# Pack args in kwargs
kwargs.update(
{
"include_rescaling": include_rescaling,
"input_shape": input_shape,
"input_tensor": input_tensor,
}
)
return EfficientNetV1Backbone.from_preset("efficientnetv1_b5", **kwargs)

@classproperty
def presets(cls):
"""Dictionary of preset names and configurations."""
return {}

@classproperty
def presets_with_weights(cls):
"""Dictionary of preset names and configurations that include
weights."""
return {}


class EfficientNetV1B6Backbone(EfficientNetV1Backbone):
def __new__(
cls,
include_rescaling=True,
input_shape=(None, None, 3),
input_tensor=None,
**kwargs,
):
# Pack args in kwargs
kwargs.update(
{
"include_rescaling": include_rescaling,
"input_shape": input_shape,
"input_tensor": input_tensor,
}
)
return EfficientNetV1Backbone.from_preset("efficientnetv1_b6", **kwargs)

@classproperty
def presets(cls):
"""Dictionary of preset names and configurations."""
return {}

@classproperty
def presets_with_weights(cls):
"""Dictionary of preset names and configurations that include
weights."""
return {}


class EfficientNetV1B7Backbone(EfficientNetV1Backbone):
def __new__(
cls,
include_rescaling=True,
input_shape=(None, None, 3),
input_tensor=None,
**kwargs,
):
# Pack args in kwargs
kwargs.update(
{
"include_rescaling": include_rescaling,
"input_shape": input_shape,
"input_tensor": input_tensor,
}
)
return EfficientNetV1Backbone.from_preset("efficientnetv1_b7", **kwargs)

@classproperty
def presets(cls):
"""Dictionary of preset names and configurations."""
return {}

@classproperty
def presets_with_weights(cls):
"""Dictionary of preset names and configurations that include
weights."""
return {}


setattr(
EfficientNetV1B0Backbone,
"__doc__",
ALIAS_DOCSTRING.format(name="EfficientNetV1B0"),
)
setattr(
EfficientNetV1B1Backbone,
"__doc__",
ALIAS_DOCSTRING.format(name="EfficientNetV1B1"),
)
setattr(
EfficientNetV1B2Backbone,
"__doc__",
ALIAS_DOCSTRING.format(name="EfficientNetV1B2"),
)
setattr(
EfficientNetV1B3Backbone,
"__doc__",
ALIAS_DOCSTRING.format(name="EfficientNetV1B3"),
)
setattr(
EfficientNetV1B4Backbone,
"__doc__",
ALIAS_DOCSTRING.format(name="EfficientNetV1B4"),
)
setattr(
EfficientNetV1B5Backbone,
"__doc__",
ALIAS_DOCSTRING.format(name="EfficientNetV1B5"),
)
setattr(
EfficientNetV1B6Backbone,
"__doc__",
ALIAS_DOCSTRING.format(name="EfficientNetV1B6"),
)
setattr(
EfficientNetV1B7Backbone,
"__doc__",
ALIAS_DOCSTRING.format(name="EfficientNetV1B7"),
)
Loading

0 comments on commit 1602b17

Please sign in to comment.