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
File size: 10,379 Bytes
587d427 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | """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",
}
|