Image Classification
Keras
computer-vision
efficientnet
self-attention
weight-sharing
hybrid-vision-backbone
tensorflow
Eval Results (legacy)
Instructions to use meatloafer/efficientnet-b0-stage-attention with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use meatloafer/efficientnet-b0-stage-attention with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://meatloafer/efficientnet-b0-stage-attention") - Notebooks
- Google Colab
- Kaggle
Upload verified model weights, LiteRT FlatBuffers, DP profiles, code, and model card
587d427 verified Download code/shared_weight_efficientnet.py from meatloafer/efficientnet-b0-stage-attention: direct link, hf CLI and curl.
- Browser
- Download file 10.4 kB
-
https://huggingface.co/meatloafer/efficientnet-b0-stage-attention/resolve/main/code/shared_weight_efficientnet.py
- Command line
-
hf download hf://meatloafer/efficientnet-b0-stage-attention/code/shared_weight_efficientnet.py
-
curl -L -o shared_weight_efficientnet.py https://huggingface.co/meatloafer/efficientnet-b0-stage-attention/resolve/main/code/shared_weight_efficientnet.py
10.4 kB
| """Experimental EfficientNet-B0 variants with tied repeat-block kernels. | |
| The reference ``models.py`` implementation remains untouched while the live | |
| recovery registrations that hash it are running. This isolated module reuses | |
| the reference stage geometry, preserves its stable layer names, and builds the | |
| two requested tied-weight variants. Convolutional and SE kernels are shared; | |
| each repeat keeps distinct BatchNorm state and residual/drop-path operations. | |
| """ | |
| from __future__ import annotations | |
| import math | |
| import tensorflow as tf | |
| from imagenet_research.models import ( | |
| STAGE_SPECS, | |
| VARIANTS, | |
| _conv_bn_relu6, | |
| _mbconv, | |
| round_filters, | |
| round_repeats, | |
| ) | |
| def _make_repeat_kernel_set( | |
| *, | |
| channels: int, | |
| kernel: int, | |
| expansion: int, | |
| se_ratio: float, | |
| name: str, | |
| ) -> dict[str, tf.keras.layers.Layer]: | |
| """Create one stride-1 MBConv kernel set with baseline-compatible names.""" | |
| expanded = channels * expansion | |
| squeeze = max(1, int(channels * se_ratio)) | |
| layers = {} | |
| if expansion != 1: | |
| layers["expand"] = tf.keras.layers.Conv2D( | |
| expanded, | |
| 1, | |
| padding="same", | |
| use_bias=False, | |
| kernel_initializer="he_normal", | |
| name=f"{name}_expand_conv", | |
| ) | |
| layers["depthwise"] = tf.keras.layers.DepthwiseConv2D( | |
| kernel, | |
| strides=1, | |
| padding="same", | |
| use_bias=False, | |
| depthwise_initializer="he_normal", | |
| name=f"{name}_depthwise", | |
| ) | |
| layers["se_reduce"] = tf.keras.layers.Conv2D( | |
| squeeze, | |
| 1, | |
| activation="relu", | |
| kernel_initializer="he_normal", | |
| name=f"{name}_se_reduce", | |
| ) | |
| layers["se_expand"] = tf.keras.layers.Conv2D( | |
| expanded, | |
| 1, | |
| activation="sigmoid", | |
| kernel_initializer="glorot_uniform", | |
| name=f"{name}_se_expand", | |
| ) | |
| layers["project"] = tf.keras.layers.Conv2D( | |
| channels, | |
| 1, | |
| padding="same", | |
| use_bias=False, | |
| kernel_initializer="he_normal", | |
| name=f"{name}_project_conv", | |
| ) | |
| return layers | |
| def _shared_repeat( | |
| inputs, | |
| *, | |
| filters: int, | |
| kernel: int, | |
| expansion: int, | |
| se_ratio: float, | |
| drop_rate: float, | |
| call_name: str, | |
| kernels: dict[str, tf.keras.layers.Layer], | |
| ): | |
| """Apply a shared MBConv kernel set with call-specific BatchNorm layers.""" | |
| residual = inputs | |
| x = inputs | |
| if expansion != 1: | |
| x = kernels["expand"](x) | |
| x = tf.keras.layers.BatchNormalization( | |
| momentum=0.99, epsilon=1e-3, name=f"{call_name}_expand_bn" | |
| )(x) | |
| x = tf.keras.layers.ReLU(max_value=6.0, name=f"{call_name}_expand_relu6")(x) | |
| x = kernels["depthwise"](x) | |
| x = tf.keras.layers.BatchNormalization( | |
| momentum=0.99, epsilon=1e-3, name=f"{call_name}_depthwise_bn" | |
| )(x) | |
| x = tf.keras.layers.ReLU(max_value=6.0, name=f"{call_name}_depthwise_relu6")(x) | |
| squeeze = tf.keras.layers.GlobalAveragePooling2D( | |
| keepdims=True, name=f"{call_name}_se_pool" | |
| )(x) | |
| squeeze = kernels["se_reduce"](squeeze) | |
| squeeze = kernels["se_expand"](squeeze) | |
| x = tf.keras.layers.Multiply(name=f"{call_name}_se_scale")([x, squeeze]) | |
| x = kernels["project"](x) | |
| x = tf.keras.layers.BatchNormalization( | |
| momentum=0.99, epsilon=1e-3, name=f"{call_name}_project_bn" | |
| )(x) | |
| if drop_rate > 0: | |
| x = tf.keras.layers.Dropout( | |
| drop_rate, | |
| noise_shape=(None, 1, 1, 1), | |
| name=f"{call_name}_drop_path", | |
| )(x) | |
| return tf.keras.layers.Add(name=f"{call_name}_add")([residual, x]) | |
| def build_shared_weight_efficientnet( | |
| variant: str = "b0", | |
| num_classes: int = 1_000, | |
| *, | |
| tie_mode: str = "single", | |
| tied_stages: tuple[int, ...] | None = None, | |
| stage6_blocks: int | None = None, | |
| ) -> tf.keras.Model: | |
| """Build an EfficientNet whose repeated stride-1 blocks share kernels. | |
| ``tie_mode='single'`` maps all repeated blocks in selected stages to one | |
| shared kernel set, yielding ``[B1, A, A, ...]``. ``tie_mode='pair'`` maps | |
| repeats alternately to two sets, yielding ``[B1, A, B, A, B, ...]``. The | |
| preset pair experiment uses only stage 6 and five total stage-6 blocks. | |
| """ | |
| if variant not in VARIANTS: | |
| raise ValueError(f"Unknown EfficientNet scale {variant!r}") | |
| if tie_mode not in ("single", "pair"): | |
| raise ValueError("tie_mode must be 'single' or 'pair'") | |
| if tied_stages is None: | |
| tied_stages = (4, 5, 6) if tie_mode == "single" else (6,) | |
| tied_stages = tuple(sorted(set(int(stage) for stage in tied_stages))) | |
| if any(stage < 1 or stage > 7 for stage in tied_stages): | |
| raise ValueError("tied_stages must be stage numbers from 1 through 7") | |
| if stage6_blocks is not None and stage6_blocks < 2: | |
| raise ValueError("stage6_blocks must be at least two") | |
| scale = VARIANTS[variant] | |
| image = tf.keras.Input( | |
| shape=(scale.image_size, scale.image_size, 3), name="image" | |
| ) | |
| x = _conv_bn_relu6( | |
| image, round_filters(32, scale.width), 3, 2, "stem" | |
| ) | |
| repeats_by_stage = [round_repeats(spec[3], scale.depth) for spec in STAGE_SPECS] | |
| if stage6_blocks is not None: | |
| repeats_by_stage[5] = int(stage6_blocks) | |
| total_blocks = sum(repeats_by_stage) | |
| block_index = 0 | |
| filters_in = round_filters(32, scale.width) | |
| stage_shared_kernels: dict[int, list[dict[str, tf.keras.layers.Layer]]] = {} | |
| for stage_index, (kernel, expansion, base_filters, _, first_stride) in enumerate(STAGE_SPECS): | |
| stage_number = stage_index + 1 | |
| filters_out = round_filters(base_filters, scale.width) | |
| repeats = repeats_by_stage[stage_index] | |
| for block in range(repeats): | |
| stride = first_stride if block == 0 else 1 | |
| drop_rate = 0.1 * block_index / max(total_blocks - 1, 1) | |
| call_name = f"stage{stage_number}_block{block + 1}" | |
| if stage_number in tied_stages and block > 0: | |
| if stride != 1 or filters_in != filters_out: | |
| raise ValueError( | |
| f"Stage {stage_number} repeat {block + 1} is not a shape-compatible residual block" | |
| ) | |
| slots = 1 if tie_mode == "single" else 2 | |
| if stage_number not in stage_shared_kernels: | |
| stage_shared_kernels[stage_number] = [ | |
| _make_repeat_kernel_set( | |
| channels=filters_out, | |
| kernel=kernel, | |
| expansion=expansion, | |
| se_ratio=0.25, | |
| name=f"stage{stage_number}_block{slot + 2}", | |
| ) | |
| for slot in range(slots) | |
| ] | |
| slot = 0 if tie_mode == "single" else (block - 1) % 2 | |
| x = _shared_repeat( | |
| x, | |
| filters=filters_out, | |
| kernel=kernel, | |
| expansion=expansion, | |
| se_ratio=0.25, | |
| drop_rate=drop_rate, | |
| call_name=call_name, | |
| kernels=stage_shared_kernels[stage_number][slot], | |
| ) | |
| else: | |
| x = _mbconv( | |
| x, | |
| filters_in=filters_in, | |
| filters_out=filters_out, | |
| kernel=kernel, | |
| stride=stride, | |
| expansion=expansion, | |
| se_ratio=0.25, | |
| drop_rate=drop_rate, | |
| name=call_name, | |
| ) | |
| filters_in = filters_out | |
| block_index += 1 | |
| head = _conv_bn_relu6( | |
| x, round_filters(1280, scale.width), 1, 1, "head" | |
| ) | |
| pooled = tf.keras.layers.GlobalAveragePooling2D(name="global_pool")(head) | |
| pooled = tf.keras.layers.Dropout(scale.dropout, name="classifier_dropout")(pooled) | |
| logits = tf.keras.layers.Dense(num_classes, dtype="float32", name="logits")(pooled) | |
| suffix = "shared_single" if tie_mode == "single" else "shared_pair" | |
| return tf.keras.Model(image, logits, name=f"efficientnet_{variant}_{suffix}_relu6") | |
| def build_parameter_compression_variant(num_classes: int = 1_000) -> tf.keras.Model: | |
| """Arm 1: tie repeated kernels in stages 4, 5, and 6.""" | |
| return build_shared_weight_efficientnet( | |
| "b0", num_classes, tie_mode="single", tied_stages=(4, 5, 6) | |
| ) | |
| def build_stage6_pair_variant(num_classes: int = 1_000) -> tf.keras.Model: | |
| """Arm 2: stage 6 becomes ``[B1, A, B, A, B]`` using two repeat sets.""" | |
| return build_shared_weight_efficientnet( | |
| "b0", num_classes, tie_mode="pair", tied_stages=(6,), stage6_blocks=5 | |
| ) | |
| def build_stage6_pair_four_variant(num_classes: int = 1_000) -> tf.keras.Model: | |
| """Kernel-budget control: stage 6 becomes ``[B1, A, B, A]``.""" | |
| return build_shared_weight_efficientnet( | |
| "b0", num_classes, tie_mode="pair", tied_stages=(6,), stage6_blocks=4 | |
| ) | |
| def kernel_sharing_report(model: tf.keras.Model) -> dict: | |
| """Summarize unique parameter ownership in a built shared-weight model.""" | |
| shared = [] | |
| for layer in model.layers: | |
| if not isinstance(layer, (tf.keras.layers.Conv2D, tf.keras.layers.DepthwiseConv2D)): | |
| continue | |
| nodes = len(getattr(layer, "_inbound_nodes", ())) | |
| if nodes > 1: | |
| parameter_count = sum(int(math.prod(weight.shape)) for weight in layer.trainable_weights) | |
| shared.append({"layer": layer.name, "calls": nodes, "kernel_and_bias_parameters": parameter_count}) | |
| trainable_parameter_count = sum( | |
| int(math.prod(weight.shape)) for weight in model.trainable_weights | |
| ) | |
| non_trainable_parameter_count = sum( | |
| int(math.prod(weight.shape)) for weight in model.non_trainable_weights | |
| ) | |
| return { | |
| "model": model.name, | |
| "unique_trainable_parameters": trainable_parameter_count, | |
| "non_trainable_parameters": non_trainable_parameter_count, | |
| "total_model_parameters": trainable_parameter_count + non_trainable_parameter_count, | |
| "reused_kernel_layers": shared, | |
| "reused_kernel_layer_count": len(shared), | |
| "batch_norm_rule": "BatchNorm objects are created per repeat call and are not shared", | |
| } | |