Instructions to use kd13/Modern-SqueezeNet with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use kd13/Modern-SqueezeNet with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="kd13/Modern-SqueezeNet", trust_remote_code=True) pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForImageClassification model = AutoModelForImageClassification.from_pretrained("kd13/Modern-SqueezeNet", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload 2 files
Browse files- configuration_squeezenet.py +16 -0
- modeling_squeezenet.py +139 -0
configuration_squeezenet.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
class SqueezeNetConfig(PretrainedConfig):
|
| 4 |
+
model_type = "squeezenet_custom"
|
| 5 |
+
|
| 6 |
+
def __init__(
|
| 7 |
+
self,
|
| 8 |
+
num_classes: int = 200,
|
| 9 |
+
fire_dropout: float = 0.0,
|
| 10 |
+
final_dropout: float = 0.50,
|
| 11 |
+
**kwargs
|
| 12 |
+
):
|
| 13 |
+
self.num_classes = num_classes
|
| 14 |
+
self.fire_dropout = fire_dropout
|
| 15 |
+
self.final_dropout = final_dropout
|
| 16 |
+
super().__init__(**kwargs)
|
modeling_squeezenet.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from transformers import PreTrainedModel
|
| 5 |
+
from transformers.modeling_outputs import ImageClassifierOutput
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
from .configuration_squeezenet import SqueezeNetConfig
|
| 9 |
+
except ImportError:
|
| 10 |
+
from configuration_squeezenet import SqueezeNetConfig
|
| 11 |
+
|
| 12 |
+
class FP32LayerNorm2d(nn.GroupNorm):
|
| 13 |
+
def __init__(self, num_channels):
|
| 14 |
+
super().__init__(1, num_channels)
|
| 15 |
+
|
| 16 |
+
def forward(self, x):
|
| 17 |
+
input_dtype = x.dtype
|
| 18 |
+
with torch.autocast(device_type=x.device.type, enabled=False):
|
| 19 |
+
normalized = super().forward(x.float())
|
| 20 |
+
return normalized.to(dtype=input_dtype)
|
| 21 |
+
|
| 22 |
+
class SwiGLU(nn.Module):
|
| 23 |
+
def __init__(self, channels):
|
| 24 |
+
super().__init__()
|
| 25 |
+
self.proj = nn.Conv2d(channels, channels * 2, kernel_size=1, bias=True)
|
| 26 |
+
self.residual_scale = nn.Parameter(torch.tensor(0.1))
|
| 27 |
+
|
| 28 |
+
def forward(self, x):
|
| 29 |
+
gate, value = self.proj(x).chunk(2, dim=1)
|
| 30 |
+
gated = F.silu(gate) * value
|
| 31 |
+
return x + self.residual_scale * gated
|
| 32 |
+
|
| 33 |
+
class FireModule(nn.Module):
|
| 34 |
+
def __init__(self, in_channels, squeeze_channels, expand1x1_channels, expand3x3_channels, dropout=0.0):
|
| 35 |
+
super().__init__()
|
| 36 |
+
self.squeeze = nn.Conv2d(in_channels, squeeze_channels, kernel_size=1, bias=False)
|
| 37 |
+
self.squeeze_norm = FP32LayerNorm2d(squeeze_channels)
|
| 38 |
+
self.squeeze_act = SwiGLU(squeeze_channels)
|
| 39 |
+
|
| 40 |
+
self.expand1x1 = nn.Conv2d(squeeze_channels, expand1x1_channels, kernel_size=1, bias=False)
|
| 41 |
+
self.expand1x1_norm = FP32LayerNorm2d(expand1x1_channels)
|
| 42 |
+
self.expand1x1_act = SwiGLU(expand1x1_channels)
|
| 43 |
+
|
| 44 |
+
self.expand3x3 = nn.Conv2d(squeeze_channels, expand3x3_channels, kernel_size=3, padding=1, bias=False)
|
| 45 |
+
self.expand3x3_norm = FP32LayerNorm2d(expand3x3_channels)
|
| 46 |
+
self.expand3x3_act = SwiGLU(expand3x3_channels)
|
| 47 |
+
self.dropout = nn.Dropout2d(p=dropout) if dropout > 0 else nn.Identity()
|
| 48 |
+
|
| 49 |
+
def forward(self, x):
|
| 50 |
+
x = self.squeeze_act(self.squeeze_norm(self.squeeze(x)))
|
| 51 |
+
e1 = self.expand1x1_act(self.expand1x1_norm(self.expand1x1(x)))
|
| 52 |
+
e3 = self.expand3x3_act(self.expand3x3_norm(self.expand3x3(x)))
|
| 53 |
+
return self.dropout(torch.cat([e1, e3], dim=1))
|
| 54 |
+
|
| 55 |
+
class SqueezeNetForImageClassification(PreTrainedModel):
|
| 56 |
+
config_class = SqueezeNetConfig
|
| 57 |
+
|
| 58 |
+
def __init__(self, config: SqueezeNetConfig):
|
| 59 |
+
super().__init__(config)
|
| 60 |
+
self.num_classes = config.num_classes
|
| 61 |
+
|
| 62 |
+
self.conv1 = nn.Conv2d(3, 96, kernel_size=7, stride=2, padding=3, bias=False)
|
| 63 |
+
self.norm1 = FP32LayerNorm2d(96)
|
| 64 |
+
self.act1 = SwiGLU(96)
|
| 65 |
+
self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
|
| 66 |
+
|
| 67 |
+
self.fire2 = FireModule(96, 16, 64, 64, config.fire_dropout)
|
| 68 |
+
self.fire3 = FireModule(128, 16, 64, 64, config.fire_dropout)
|
| 69 |
+
self.fire4 = FireModule(128, 32, 128, 128, config.fire_dropout)
|
| 70 |
+
self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
|
| 71 |
+
|
| 72 |
+
self.fire5 = FireModule(256, 32, 128, 128, config.fire_dropout)
|
| 73 |
+
self.fire6 = FireModule(256, 48, 192, 192, config.fire_dropout)
|
| 74 |
+
self.fire7 = FireModule(384, 48, 192, 192, config.fire_dropout)
|
| 75 |
+
self.fire8 = FireModule(384, 64, 256, 256, config.fire_dropout)
|
| 76 |
+
self.pool3 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
|
| 77 |
+
|
| 78 |
+
self.fire9 = FireModule(512, 64, 256, 256, config.fire_dropout)
|
| 79 |
+
self.dropout = nn.Dropout2d(p=config.final_dropout)
|
| 80 |
+
self.conv10 = nn.Conv2d(512, self.num_classes, kernel_size=1, bias=True)
|
| 81 |
+
|
| 82 |
+
self.gap = nn.AdaptiveAvgPool2d((1, 1))
|
| 83 |
+
|
| 84 |
+
# Important: Automatically initializes weights if needed, required by Transformers
|
| 85 |
+
self.post_init()
|
| 86 |
+
|
| 87 |
+
def _init_weights(self, module):
|
| 88 |
+
if isinstance(module, nn.Conv2d):
|
| 89 |
+
nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
|
| 90 |
+
if module.bias is not None:
|
| 91 |
+
nn.init.zeros_(module.bias)
|
| 92 |
+
elif isinstance(module, nn.GroupNorm):
|
| 93 |
+
nn.init.ones_(module.weight)
|
| 94 |
+
nn.init.zeros_(module.bias)
|
| 95 |
+
|
| 96 |
+
# Override for the classifier
|
| 97 |
+
if module == self.conv10:
|
| 98 |
+
nn.init.normal_(module.weight, mean=0.0, std=0.001)
|
| 99 |
+
nn.init.zeros_(module.bias)
|
| 100 |
+
|
| 101 |
+
def forward(
|
| 102 |
+
self,
|
| 103 |
+
pixel_values: torch.Tensor,
|
| 104 |
+
labels: torch.Tensor | None = None,
|
| 105 |
+
return_dict: bool | None = None
|
| 106 |
+
):
|
| 107 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 108 |
+
|
| 109 |
+
x = self.pool1(self.act1(self.norm1(self.conv1(pixel_values))))
|
| 110 |
+
|
| 111 |
+
x = self.fire2(x)
|
| 112 |
+
x = self.fire3(x)
|
| 113 |
+
x = self.fire4(x)
|
| 114 |
+
x = self.pool2(x)
|
| 115 |
+
|
| 116 |
+
x = self.fire5(x)
|
| 117 |
+
x = self.fire6(x)
|
| 118 |
+
x = self.fire7(x)
|
| 119 |
+
x = self.fire8(x)
|
| 120 |
+
x = self.pool3(x)
|
| 121 |
+
|
| 122 |
+
x = self.fire9(x)
|
| 123 |
+
x = self.conv10(self.dropout(x))
|
| 124 |
+
x = self.gap(x)
|
| 125 |
+
logits = torch.flatten(x, 1)
|
| 126 |
+
|
| 127 |
+
loss = None
|
| 128 |
+
if labels is not None:
|
| 129 |
+
loss_fct = nn.CrossEntropyLoss()
|
| 130 |
+
loss = loss_fct(logits.view(-1, self.num_classes), labels.view(-1))
|
| 131 |
+
|
| 132 |
+
if not return_dict:
|
| 133 |
+
output = (logits,)
|
| 134 |
+
return ((loss,) + output) if loss is not None else output
|
| 135 |
+
|
| 136 |
+
return ImageClassifierOutput(
|
| 137 |
+
loss=loss,
|
| 138 |
+
logits=logits,
|
| 139 |
+
)
|