-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP initial conversions * WIP commit, meant to be able to share current state easily * WIP before rebase * WIP rebased on latest master changes * update verification script to include classifier head * WIP, 2 variants verified * added tests, completed 2 variants * fix efficientnet script examples * removed copyright heading * removing unsupported variants for now * removed explicit cast, corrected tests * readd explicit cast * update model uri's * updated preset uri's * update projection_activation naming, fix final URI * update projection_activation variable naming in test
- Loading branch information
Showing
16 changed files
with
598 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from keras_hub.src.models.efficientnet.efficientnet_backbone import ( | ||
EfficientNetBackbone, | ||
) | ||
from keras_hub.src.models.efficientnet.efficientnet_presets import ( | ||
backbone_presets, | ||
) | ||
from keras_hub.src.utils.preset_utils import register_presets | ||
|
||
register_presets(backbone_presets, EfficientNetBackbone) |
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
14 changes: 14 additions & 0 deletions
14
keras_hub/src/models/efficientnet/efficientnet_image_classifier.py
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,14 @@ | ||
from keras_hub.src.api_export import keras_hub_export | ||
from keras_hub.src.models.efficientnet.efficientnet_backbone import ( | ||
EfficientNetBackbone, | ||
) | ||
from keras_hub.src.models.efficientnet.efficientnet_image_classifier_preprocessor import ( | ||
EfficientNetImageClassifierPreprocessor, | ||
) | ||
from keras_hub.src.models.image_classifier import ImageClassifier | ||
|
||
|
||
@keras_hub_export("keras_hub.models.EfficientNetImageClassifier") | ||
class EfficientNetImageClassifier(ImageClassifier): | ||
backbone_cls = EfficientNetBackbone | ||
preprocessor_cls = EfficientNetImageClassifierPreprocessor |
16 changes: 16 additions & 0 deletions
16
keras_hub/src/models/efficientnet/efficientnet_image_classifier_preprocessor.py
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,16 @@ | ||
from keras_hub.src.api_export import keras_hub_export | ||
from keras_hub.src.models.efficientnet.efficientnet_backbone import ( | ||
EfficientNetBackbone, | ||
) | ||
from keras_hub.src.models.efficientnet.efficientnet_image_converter import ( | ||
EfficientNetImageConverter, | ||
) | ||
from keras_hub.src.models.image_classifier_preprocessor import ( | ||
ImageClassifierPreprocessor, | ||
) | ||
|
||
|
||
@keras_hub_export("keras_hub.models.EfficientNetImageClassifierPreprocessor") | ||
class EfficientNetImageClassifierPreprocessor(ImageClassifierPreprocessor): | ||
backbone_cls = EfficientNetBackbone | ||
image_converter_cls = EfficientNetImageConverter |
84 changes: 84 additions & 0 deletions
84
keras_hub/src/models/efficientnet/efficientnet_image_classifier_test.py
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,84 @@ | ||
import pytest | ||
from keras import ops | ||
|
||
from keras_hub.src.models.efficientnet.efficientnet_backbone import ( | ||
EfficientNetBackbone, | ||
) | ||
from keras_hub.src.models.efficientnet.efficientnet_image_classifier import ( | ||
EfficientNetImageClassifier, | ||
) | ||
from keras_hub.src.tests.test_case import TestCase | ||
|
||
|
||
class EfficientNetImageClassifierTest(TestCase): | ||
def setUp(self): | ||
self.images = ops.ones((2, 16, 16, 3)) | ||
self.labels = [0, 3] | ||
backbone = EfficientNetBackbone( | ||
width_coefficient=1.0, | ||
depth_coefficient=1.0, | ||
stackwise_kernel_sizes=[3, 3, 5, 3, 5, 5, 3], | ||
stackwise_num_repeats=[1, 2, 2, 3, 3, 4, 1], | ||
stackwise_input_filters=[32, 16, 24, 40, 80, 112, 192], | ||
stackwise_output_filters=[16, 24, 40, 80, 112, 192, 320], | ||
stackwise_expansion_ratios=[1, 6, 6, 6, 6, 6, 6], | ||
stackwise_strides=[1, 2, 2, 2, 1, 2, 1], | ||
stackwise_block_types=["v1"] * 7, | ||
stackwise_squeeze_and_excite_ratios=[0.25] * 7, | ||
min_depth=None, | ||
include_stem_padding=True, | ||
use_depth_divisor_as_min_depth=True, | ||
cap_round_filter_decrease=True, | ||
stem_conv_padding="valid", | ||
batch_norm_momentum=0.9, | ||
batch_norm_epsilon=1e-5, | ||
dropout=0, | ||
projection_activation=None, | ||
) | ||
self.init_kwargs = { | ||
"backbone": backbone, | ||
"num_classes": 1000, | ||
} | ||
self.train_data = (self.images, self.labels) | ||
|
||
def test_classifier_basics(self): | ||
pytest.skip( | ||
reason="TODO: enable after preprocessor flow is figured out" | ||
) | ||
self.run_task_test( | ||
cls=EfficientNetImageClassifier, | ||
init_kwargs=self.init_kwargs, | ||
train_data=self.train_data, | ||
expected_output_shape=(2, 2), | ||
) | ||
|
||
@pytest.mark.large | ||
def test_smallest_preset(self): | ||
# Test that our forward pass is stable! | ||
image_batch = self.load_test_image()[None, ...] / 255.0 | ||
self.run_preset_test( | ||
cls=EfficientNetImageClassifier, | ||
preset="efficientnet_b0_ra_imagenet", | ||
input_data=image_batch, | ||
expected_output_shape=(1, 1000), | ||
expected_labels=[85], | ||
) | ||
|
||
@pytest.mark.large | ||
def test_saved_model(self): | ||
self.run_model_saving_test( | ||
cls=EfficientNetImageClassifier, | ||
init_kwargs=self.init_kwargs, | ||
input_data=self.images, | ||
) | ||
|
||
@pytest.mark.extra_large | ||
def test_all_presets(self): | ||
for preset in EfficientNetImageClassifier.presets: | ||
self.run_preset_test( | ||
cls=EfficientNetImageClassifier, | ||
preset=preset, | ||
init_kwargs={"num_classes": 2}, | ||
input_data=self.images, | ||
expected_output_shape=(2, 2), | ||
) |
10 changes: 10 additions & 0 deletions
10
keras_hub/src/models/efficientnet/efficientnet_image_converter.py
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,10 @@ | ||
from keras_hub.src.api_export import keras_hub_export | ||
from keras_hub.src.layers.preprocessing.image_converter import ImageConverter | ||
from keras_hub.src.models.efficientnet.efficientnet_backbone import ( | ||
EfficientNetBackbone, | ||
) | ||
|
||
|
||
@keras_hub_export("keras_hub.layers.EfficientNetImageConverter") | ||
class EfficientNetImageConverter(ImageConverter): | ||
backbone_cls = EfficientNetBackbone |
Oops, something went wrong.