Skip to content

Commit

Permalink
Revert "Weights densenet (keras-team#1855)"
Browse files Browse the repository at this point in the history
This reverts commit f67b4db.
  • Loading branch information
divyashreepathihalli committed Oct 1, 2024
1 parent a77595e commit 69bf1f0
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 71 deletions.
3 changes: 0 additions & 3 deletions keras_hub/api/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
ResizingImageConverter,
)
from keras_hub.src.layers.preprocessing.start_end_packer import StartEndPacker
from keras_hub.src.models.densenet.densenet_image_converter import (
DenseNetImageConverter,
)
from keras_hub.src.models.pali_gemma.pali_gemma_image_converter import (
PaliGemmaImageConverter,
)
Expand Down
3 changes: 0 additions & 3 deletions keras_hub/api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@
from keras_hub.src.models.densenet.densenet_image_classifier import (
DenseNetImageClassifier,
)
from keras_hub.src.models.densenet.densenet_image_classifier_preprocessor import (
DenseNetImageClassifierPreprocessor,
)
from keras_hub.src.models.distil_bert.distil_bert_backbone import (
DistilBertBackbone,
)
Expand Down
19 changes: 8 additions & 11 deletions keras_hub/src/models/densenet/densenet_backbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,19 @@ def __init__(
channel_axis,
stackwise_num_repeats[stack_index],
growth_rate,
name=f"stack{stack_index+1}",
name=f"conv{index}",
)
pyramid_outputs[f"P{index}"] = x
x = apply_transition_block(
x,
channel_axis,
compression_ratio,
name=f"transition{stack_index+1}",
x, channel_axis, compression_ratio, name=f"pool{index}"
)

x = apply_dense_block(
x,
channel_axis,
stackwise_num_repeats[-1],
growth_rate,
name=f"stack{len(stackwise_num_repeats)}",
name=f"conv{len(stackwise_num_repeats) + 1}",
)
pyramid_outputs[f"P{len(stackwise_num_repeats) + 1}"] = x
x = keras.layers.BatchNormalization(
Expand Down Expand Up @@ -138,7 +135,7 @@ def apply_dense_block(x, channel_axis, num_repeats, growth_rate, name=None):

for i in range(num_repeats):
x = apply_conv_block(
x, channel_axis, growth_rate, name=f"{name}_block{i+1}"
x, channel_axis, growth_rate, name=f"{name}_block_{i}"
)
return x

Expand Down Expand Up @@ -186,9 +183,9 @@ def apply_conv_block(x, channel_axis, growth_rate, name=None):

shortcut = x
x = keras.layers.BatchNormalization(
axis=channel_axis, epsilon=BN_EPSILON, name=f"{name}_1_bn"
axis=channel_axis, epsilon=BN_EPSILON, name=f"{name}_0_bn"
)(x)
x = keras.layers.Activation("relu", name=f"{name}_1_relu")(x)
x = keras.layers.Activation("relu", name=f"{name}_0_relu")(x)
x = keras.layers.Conv2D(
4 * growth_rate,
1,
Expand All @@ -197,9 +194,9 @@ def apply_conv_block(x, channel_axis, growth_rate, name=None):
name=f"{name}_1_conv",
)(x)
x = keras.layers.BatchNormalization(
axis=channel_axis, epsilon=BN_EPSILON, name=f"{name}_2_bn"
axis=channel_axis, epsilon=BN_EPSILON, name=f"{name}_1_bn"
)(x)
x = keras.layers.Activation("relu", name=f"{name}_2_relu")(x)
x = keras.layers.Activation("relu", name=f"{name}_1_relu")(x)
x = keras.layers.Conv2D(
growth_rate,
3,
Expand Down
31 changes: 4 additions & 27 deletions keras_hub/src/models/densenet/densenet_image_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

from keras_hub.src.api_export import keras_hub_export
from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone
from keras_hub.src.models.densenet.densenet_image_classifier_preprocessor import (
DenseNetImageClassifierPreprocessor,
)
from keras_hub.src.models.image_classifier import ImageClassifier


Expand All @@ -22,13 +19,7 @@ class DenseNetImageClassifier(ImageClassifier):
num_classes: int. The number of classes to predict.
activation: `None`, str or callable. The activation function to use on
the `Dense` layer. Set `activation=None` to return the output
logits. Defaults to `None`.
pooling: A pooling layer to use before the final classification layer,
must be one of "avg" or "max". Use "avg" for
`GlobalAveragePooling2D` and "max" for "GlobalMaxPooling2D.
preprocessor: A `keras_hub.models.DenseNetImageClassifierPreprocessor`
or `None`. If `None`, this model will not apply preprocessing, and
inputs should be preprocessed before calling the model.
logits. Defaults to `"softmax"`.
Examples:
Expand Down Expand Up @@ -82,29 +73,18 @@ class DenseNetImageClassifier(ImageClassifier):
"""

backbone_cls = DenseNetBackbone
preprocessor_cls = DenseNetImageClassifierPreprocessor

def __init__(
self,
backbone,
num_classes,
activation=None,
pooling="avg",
preprocessor=None,
activation="softmax",
preprocessor=None, # adding this dummy arg for saved model test
# TODO: once preprocessor flow is figured out, this needs to be updated
**kwargs,
):
# === Layers ===
self.backbone = backbone
self.preprocessor = preprocessor
if pooling == "avg":
self.pooler = keras.layers.GlobalAveragePooling2D()
elif pooling == "max":
self.pooler = keras.layers.GlobalMaxPooling2D()
else:
raise ValueError(
"Unknown `pooling` type. Polling should be either `'avg'` or "
f"`'max'`. Received: pooling={pooling}."
)
self.output_dense = keras.layers.Dense(
num_classes,
activation=activation,
Expand All @@ -114,7 +94,6 @@ def __init__(
# === Functional Model ===
inputs = self.backbone.input
x = self.backbone(inputs)
x = self.pooler(x)
outputs = self.output_dense(x)
super().__init__(
inputs=inputs,
Expand All @@ -125,7 +104,6 @@ def __init__(
# === Config ===
self.num_classes = num_classes
self.activation = activation
self.pooling = pooling

def get_config(self):
# Backbone serialized in `super`
Expand All @@ -134,7 +112,6 @@ def get_config(self):
{
"num_classes": self.num_classes,
"activation": self.activation,
"pooling": self.pooling,
}
)
return config
24 changes: 0 additions & 24 deletions keras_hub/src/models/densenet/densenet_image_classifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def setUp(self):
"backbone": self.backbone,
"num_classes": 2,
"activation": "softmax",
"pooling": "avg",
}
self.train_data = (
self.images,
Expand All @@ -41,33 +40,10 @@ def test_classifier_basics(self):
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=DenseNetImageClassifier,
preset="densenet_121_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=DenseNetImageClassifier,
init_kwargs=self.init_kwargs,
input_data=self.images,
)

@pytest.mark.extra_large
def test_all_presets(self):
for preset in DenseNetImageClassifier.presets:
self.run_preset_test(
cls=DenseNetImageClassifier,
preset=preset,
init_kwargs={"num_classes": 2},
input_data=self.images,
expected_output_shape=(2, 2),
)
3 changes: 0 additions & 3 deletions keras_hub/src/utils/timm/preset_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from keras_hub.src.models.image_classifier import ImageClassifier
from keras_hub.src.utils.preset_utils import PresetLoader
from keras_hub.src.utils.preset_utils import jax_memory_cleanup
from keras_hub.src.utils.timm import convert_densenet
from keras_hub.src.utils.timm import convert_resnet
from keras_hub.src.utils.transformers.safetensor_utils import SafetensorLoader

Expand All @@ -14,8 +13,6 @@ def __init__(self, preset, config):
architecture = self.config["architecture"]
if "resnet" in architecture:
self.converter = convert_resnet
if "densenet" in architecture:
self.converter = convert_densenet
else:
raise ValueError(
"KerasHub has no converter for timm models "
Expand Down

0 comments on commit 69bf1f0

Please sign in to comment.