Instructions to use bn22/convmixer_1024_32_ks9_p16 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use bn22/convmixer_1024_32_ks9_p16 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="bn22/convmixer_1024_32_ks9_p16", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("bn22/convmixer_1024_32_ks9_p16", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload model
Browse files- README.md +199 -0
- config.json +20 -0
- convmixer_wd.py +382 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags: []
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# Model Card for Model ID
|
| 7 |
+
|
| 8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
## Model Details
|
| 13 |
+
|
| 14 |
+
### Model Description
|
| 15 |
+
|
| 16 |
+
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
+
|
| 18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
| 19 |
+
|
| 20 |
+
- **Developed by:** [More Information Needed]
|
| 21 |
+
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
+
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
+
- **Model type:** [More Information Needed]
|
| 24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
+
- **License:** [More Information Needed]
|
| 26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
+
|
| 28 |
+
### Model Sources [optional]
|
| 29 |
+
|
| 30 |
+
<!-- Provide the basic links for the model. -->
|
| 31 |
+
|
| 32 |
+
- **Repository:** [More Information Needed]
|
| 33 |
+
- **Paper [optional]:** [More Information Needed]
|
| 34 |
+
- **Demo [optional]:** [More Information Needed]
|
| 35 |
+
|
| 36 |
+
## Uses
|
| 37 |
+
|
| 38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
+
|
| 40 |
+
### Direct Use
|
| 41 |
+
|
| 42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
+
|
| 44 |
+
[More Information Needed]
|
| 45 |
+
|
| 46 |
+
### Downstream Use [optional]
|
| 47 |
+
|
| 48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
+
|
| 50 |
+
[More Information Needed]
|
| 51 |
+
|
| 52 |
+
### Out-of-Scope Use
|
| 53 |
+
|
| 54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 55 |
+
|
| 56 |
+
[More Information Needed]
|
| 57 |
+
|
| 58 |
+
## Bias, Risks, and Limitations
|
| 59 |
+
|
| 60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
+
|
| 62 |
+
[More Information Needed]
|
| 63 |
+
|
| 64 |
+
### Recommendations
|
| 65 |
+
|
| 66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 67 |
+
|
| 68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
+
|
| 70 |
+
## How to Get Started with the Model
|
| 71 |
+
|
| 72 |
+
Use the code below to get started with the model.
|
| 73 |
+
|
| 74 |
+
[More Information Needed]
|
| 75 |
+
|
| 76 |
+
## Training Details
|
| 77 |
+
|
| 78 |
+
### Training Data
|
| 79 |
+
|
| 80 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
+
|
| 82 |
+
[More Information Needed]
|
| 83 |
+
|
| 84 |
+
### Training Procedure
|
| 85 |
+
|
| 86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
+
|
| 88 |
+
#### Preprocessing [optional]
|
| 89 |
+
|
| 90 |
+
[More Information Needed]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
#### Training Hyperparameters
|
| 94 |
+
|
| 95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
+
|
| 97 |
+
#### Speeds, Sizes, Times [optional]
|
| 98 |
+
|
| 99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
+
|
| 101 |
+
[More Information Needed]
|
| 102 |
+
|
| 103 |
+
## Evaluation
|
| 104 |
+
|
| 105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
+
|
| 107 |
+
### Testing Data, Factors & Metrics
|
| 108 |
+
|
| 109 |
+
#### Testing Data
|
| 110 |
+
|
| 111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
+
|
| 113 |
+
[More Information Needed]
|
| 114 |
+
|
| 115 |
+
#### Factors
|
| 116 |
+
|
| 117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
+
|
| 119 |
+
[More Information Needed]
|
| 120 |
+
|
| 121 |
+
#### Metrics
|
| 122 |
+
|
| 123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
+
|
| 125 |
+
[More Information Needed]
|
| 126 |
+
|
| 127 |
+
### Results
|
| 128 |
+
|
| 129 |
+
[More Information Needed]
|
| 130 |
+
|
| 131 |
+
#### Summary
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
## Model Examination [optional]
|
| 136 |
+
|
| 137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
+
|
| 139 |
+
[More Information Needed]
|
| 140 |
+
|
| 141 |
+
## Environmental Impact
|
| 142 |
+
|
| 143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
+
|
| 145 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
+
|
| 147 |
+
- **Hardware Type:** [More Information Needed]
|
| 148 |
+
- **Hours used:** [More Information Needed]
|
| 149 |
+
- **Cloud Provider:** [More Information Needed]
|
| 150 |
+
- **Compute Region:** [More Information Needed]
|
| 151 |
+
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
+
|
| 153 |
+
## Technical Specifications [optional]
|
| 154 |
+
|
| 155 |
+
### Model Architecture and Objective
|
| 156 |
+
|
| 157 |
+
[More Information Needed]
|
| 158 |
+
|
| 159 |
+
### Compute Infrastructure
|
| 160 |
+
|
| 161 |
+
[More Information Needed]
|
| 162 |
+
|
| 163 |
+
#### Hardware
|
| 164 |
+
|
| 165 |
+
[More Information Needed]
|
| 166 |
+
|
| 167 |
+
#### Software
|
| 168 |
+
|
| 169 |
+
[More Information Needed]
|
| 170 |
+
|
| 171 |
+
## Citation [optional]
|
| 172 |
+
|
| 173 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
+
|
| 175 |
+
**BibTeX:**
|
| 176 |
+
|
| 177 |
+
[More Information Needed]
|
| 178 |
+
|
| 179 |
+
**APA:**
|
| 180 |
+
|
| 181 |
+
[More Information Needed]
|
| 182 |
+
|
| 183 |
+
## Glossary [optional]
|
| 184 |
+
|
| 185 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
+
|
| 187 |
+
[More Information Needed]
|
| 188 |
+
|
| 189 |
+
## More Information [optional]
|
| 190 |
+
|
| 191 |
+
[More Information Needed]
|
| 192 |
+
|
| 193 |
+
## Model Card Authors [optional]
|
| 194 |
+
|
| 195 |
+
[More Information Needed]
|
| 196 |
+
|
| 197 |
+
## Model Card Contact
|
| 198 |
+
|
| 199 |
+
[More Information Needed]
|
config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"ConvMixerModel"
|
| 4 |
+
],
|
| 5 |
+
"attn_pool_heads": 16,
|
| 6 |
+
"attn_pool_mlp_ratio": 4.0,
|
| 7 |
+
"auto_map": {
|
| 8 |
+
"AutoConfig": "convmixer_wd.ConvMixerConfig",
|
| 9 |
+
"AutoModel": "convmixer_wd.ConvMixerModel"
|
| 10 |
+
},
|
| 11 |
+
"depth": 32,
|
| 12 |
+
"dim": 1024,
|
| 13 |
+
"dropout": 0.0,
|
| 14 |
+
"dtype": "float32",
|
| 15 |
+
"kernel_size": 9,
|
| 16 |
+
"model_type": "convmixer",
|
| 17 |
+
"num_classes": 1000,
|
| 18 |
+
"patch_size": 16,
|
| 19 |
+
"transformers_version": "5.0.0"
|
| 20 |
+
}
|
convmixer_wd.py
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
from typing import Optional, Tuple, List
|
| 6 |
+
from transformers import PretrainedConfig, PreTrainedModel
|
| 7 |
+
from transformers.modeling_outputs import ModelOutput
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# ----------------------------------------------------------------------
|
| 11 |
+
# Custom Output Dataclass
|
| 12 |
+
# ----------------------------------------------------------------------
|
| 13 |
+
@dataclass
|
| 14 |
+
class ConvMixerOutput(ModelOutput):
|
| 15 |
+
"""
|
| 16 |
+
Output type for ConvMixerForImageClassification.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
loss (`torch.FloatTensor`, *optional*):
|
| 20 |
+
Classification loss.
|
| 21 |
+
logits (`torch.FloatTensor`):
|
| 22 |
+
Classification logits (before softmax).
|
| 23 |
+
last_hidden_state (`torch.FloatTensor`):
|
| 24 |
+
Sequence of spatial features (flattened), shape (batch_size, num_patches, dim).
|
| 25 |
+
pooler_output (`torch.FloatTensor`):
|
| 26 |
+
Attention‑pooled representation, shape (batch_size, dim).
|
| 27 |
+
hidden_states (`tuple(torch.FloatTensor)`, *optional*):
|
| 28 |
+
Hidden states from each block (stem + convmixer blocks).
|
| 29 |
+
attentions (`tuple(torch.FloatTensor)`, *optional*):
|
| 30 |
+
Attention weights from the pooling layer (if `output_attentions=True`).
|
| 31 |
+
"""
|
| 32 |
+
loss: Optional[torch.FloatTensor] = None
|
| 33 |
+
logits: Optional[torch.FloatTensor] = None
|
| 34 |
+
last_hidden_state: Optional[torch.FloatTensor] = None
|
| 35 |
+
pooler_output: Optional[torch.FloatTensor] = None
|
| 36 |
+
hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None
|
| 37 |
+
attentions: Optional[Tuple[torch.FloatTensor, ...]] = None
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# ----------------------------------------------------------------------
|
| 41 |
+
# Custom Configuration
|
| 42 |
+
# ----------------------------------------------------------------------
|
| 43 |
+
class ConvMixerConfig(PretrainedConfig):
|
| 44 |
+
"""
|
| 45 |
+
Configuration class for ConvMixer models.
|
| 46 |
+
|
| 47 |
+
Args:
|
| 48 |
+
dim (`int`, *optional*, defaults to 256):
|
| 49 |
+
Embedding dimension throughout the network.
|
| 50 |
+
depth (`int`, *optional*, defaults to 8):
|
| 51 |
+
Number of ConvMixer blocks.
|
| 52 |
+
kernel_size (`int`, *optional*, defaults to 5):
|
| 53 |
+
Kernel size of depthwise convolutions.
|
| 54 |
+
patch_size (`int`, *optional*, defaults to 2):
|
| 55 |
+
Stem convolution stride / patch size.
|
| 56 |
+
num_classes (`int`, *optional*, defaults to 1000):
|
| 57 |
+
Number of classes for classification head.
|
| 58 |
+
attn_pool_heads (`int`, *optional*, defaults to 8):
|
| 59 |
+
Number of attention heads in the pooling layer.
|
| 60 |
+
attn_pool_mlp_ratio (`float`, *optional*, defaults to 4.0):
|
| 61 |
+
MLP hidden ratio in the pooling layer.
|
| 62 |
+
dropout (`float`, *optional*, defaults to 0.0):
|
| 63 |
+
Dropout rate applied in the pooling MLP.
|
| 64 |
+
"""
|
| 65 |
+
model_type = "convmixer"
|
| 66 |
+
|
| 67 |
+
def __init__(
|
| 68 |
+
self,
|
| 69 |
+
dim: int = 256,
|
| 70 |
+
depth: int = 8,
|
| 71 |
+
kernel_size: int = 5,
|
| 72 |
+
patch_size: int = 2,
|
| 73 |
+
num_classes: int = 1000,
|
| 74 |
+
attn_pool_heads: int = 8,
|
| 75 |
+
attn_pool_mlp_ratio: float = 4.0,
|
| 76 |
+
dropout: float = 0.0,
|
| 77 |
+
**kwargs,
|
| 78 |
+
):
|
| 79 |
+
super().__init__(**kwargs)
|
| 80 |
+
self.dim = dim
|
| 81 |
+
self.depth = depth
|
| 82 |
+
self.kernel_size = kernel_size
|
| 83 |
+
self.patch_size = patch_size
|
| 84 |
+
self.num_classes = num_classes
|
| 85 |
+
self.attn_pool_heads = attn_pool_heads
|
| 86 |
+
self.attn_pool_mlp_ratio = attn_pool_mlp_ratio
|
| 87 |
+
self.dropout = dropout
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# ----------------------------------------------------------------------
|
| 91 |
+
# Core ConvMixer Components (unchanged except removed classification head)
|
| 92 |
+
# ----------------------------------------------------------------------
|
| 93 |
+
class Residual(nn.Module):
|
| 94 |
+
"""Residual wrapper used in ConvMixer."""
|
| 95 |
+
def __init__(self, fn):
|
| 96 |
+
super().__init__()
|
| 97 |
+
self.fn = fn
|
| 98 |
+
|
| 99 |
+
def forward(self, x):
|
| 100 |
+
return self.fn(x) + x
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
class AttentionPooling(nn.Module):
|
| 104 |
+
"""
|
| 105 |
+
Multi‑head attention pooling that aggregates a spatial feature map into a single vector.
|
| 106 |
+
Optionally returns attention weights.
|
| 107 |
+
"""
|
| 108 |
+
def __init__(
|
| 109 |
+
self,
|
| 110 |
+
dim: int,
|
| 111 |
+
num_heads: int = 8,
|
| 112 |
+
mlp_ratio: float = 4.0,
|
| 113 |
+
dropout: float = 0.0,
|
| 114 |
+
):
|
| 115 |
+
super().__init__()
|
| 116 |
+
self.dim = dim
|
| 117 |
+
self.num_heads = num_heads
|
| 118 |
+
|
| 119 |
+
self.probe = nn.Parameter(torch.randn(1, 1, dim))
|
| 120 |
+
self.attention = nn.MultiheadAttention(
|
| 121 |
+
embed_dim=dim,
|
| 122 |
+
num_heads=num_heads,
|
| 123 |
+
batch_first=True,
|
| 124 |
+
dropout=dropout,
|
| 125 |
+
)
|
| 126 |
+
self.layernorm = nn.LayerNorm(dim)
|
| 127 |
+
mlp_hidden = int(dim * mlp_ratio)
|
| 128 |
+
self.mlp = nn.Sequential(
|
| 129 |
+
nn.Linear(dim, mlp_hidden),
|
| 130 |
+
nn.GELU(),
|
| 131 |
+
nn.Dropout(dropout),
|
| 132 |
+
nn.Linear(mlp_hidden, dim),
|
| 133 |
+
nn.Dropout(dropout),
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
def forward(self, x: torch.Tensor, output_attentions: bool = False):
|
| 137 |
+
# x shape: (B, dim, H, W)
|
| 138 |
+
B, C, H, W = x.shape
|
| 139 |
+
x = x.flatten(2).transpose(1, 2) # (B, L, C)
|
| 140 |
+
probe = self.probe.expand(B, -1, -1) # (B, 1, C)
|
| 141 |
+
|
| 142 |
+
attn_out, attn_weights = self.attention(probe, x, x) # (B, 1, C), (B, 1, L)
|
| 143 |
+
|
| 144 |
+
residual = attn_out
|
| 145 |
+
attn_out = self.layernorm(attn_out)
|
| 146 |
+
attn_out = residual + self.mlp(attn_out)
|
| 147 |
+
|
| 148 |
+
pooled = attn_out[:, 0] # (B, C)
|
| 149 |
+
|
| 150 |
+
if output_attentions:
|
| 151 |
+
return pooled, attn_weights
|
| 152 |
+
return pooled, None
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
class ConvMixerWithAttnPool(nn.Module):
|
| 156 |
+
"""
|
| 157 |
+
ConvMixer backbone with multi‑head attention pooling.
|
| 158 |
+
Returns pooled representation, spatial features, and optional hidden states / attention weights.
|
| 159 |
+
"""
|
| 160 |
+
def __init__(
|
| 161 |
+
self,
|
| 162 |
+
dim: int,
|
| 163 |
+
depth: int,
|
| 164 |
+
kernel_size: int = 5,
|
| 165 |
+
patch_size: int = 2,
|
| 166 |
+
attn_pool_heads: int = 8,
|
| 167 |
+
attn_pool_mlp_ratio: float = 4.0,
|
| 168 |
+
dropout: float = 0.0,
|
| 169 |
+
):
|
| 170 |
+
super().__init__()
|
| 171 |
+
self.dim = dim
|
| 172 |
+
self.depth = depth
|
| 173 |
+
|
| 174 |
+
# Stem
|
| 175 |
+
self.stem = nn.Sequential(
|
| 176 |
+
nn.Conv2d(3, dim, kernel_size=patch_size, stride=patch_size),
|
| 177 |
+
nn.GELU(),
|
| 178 |
+
nn.BatchNorm2d(dim),
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
# ConvMixer blocks
|
| 182 |
+
self.blocks = nn.ModuleList([
|
| 183 |
+
nn.Sequential(
|
| 184 |
+
Residual(
|
| 185 |
+
nn.Sequential(
|
| 186 |
+
nn.Conv2d(dim, dim, kernel_size, groups=dim, padding="same"),
|
| 187 |
+
nn.GELU(),
|
| 188 |
+
nn.BatchNorm2d(dim),
|
| 189 |
+
)
|
| 190 |
+
),
|
| 191 |
+
nn.Conv2d(dim, dim, kernel_size=1),
|
| 192 |
+
nn.GELU(),
|
| 193 |
+
nn.BatchNorm2d(dim),
|
| 194 |
+
)
|
| 195 |
+
for _ in range(depth)
|
| 196 |
+
])
|
| 197 |
+
|
| 198 |
+
# Attention pooling
|
| 199 |
+
self.pool = AttentionPooling(
|
| 200 |
+
dim=dim,
|
| 201 |
+
num_heads=attn_pool_heads,
|
| 202 |
+
mlp_ratio=attn_pool_mlp_ratio,
|
| 203 |
+
dropout=dropout,
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
def forward(
|
| 207 |
+
self,
|
| 208 |
+
x: torch.Tensor,
|
| 209 |
+
output_hidden_states: bool = False,
|
| 210 |
+
output_attentions: bool = False,
|
| 211 |
+
):
|
| 212 |
+
"""
|
| 213 |
+
Returns:
|
| 214 |
+
- pooled: (B, dim) attention‑pooled vector
|
| 215 |
+
- spatial_features: (B, L, dim) flattened spatial map before pooling
|
| 216 |
+
- hidden_states: tuple of intermediate feature maps (if requested)
|
| 217 |
+
- attentions: attention weights from pooling layer (if requested)
|
| 218 |
+
"""
|
| 219 |
+
hidden_states = () if output_hidden_states else None
|
| 220 |
+
attentions = None
|
| 221 |
+
|
| 222 |
+
# Stem
|
| 223 |
+
x = self.stem(x)
|
| 224 |
+
if output_hidden_states:
|
| 225 |
+
hidden_states += (x,)
|
| 226 |
+
|
| 227 |
+
# ConvMixer blocks
|
| 228 |
+
for blk in self.blocks:
|
| 229 |
+
x = blk(x)
|
| 230 |
+
if output_hidden_states:
|
| 231 |
+
hidden_states += (x,)
|
| 232 |
+
|
| 233 |
+
# Store spatial features before pooling
|
| 234 |
+
spatial_features = x.flatten(2).transpose(1, 2) # (B, L, C)
|
| 235 |
+
|
| 236 |
+
# Attention pooling
|
| 237 |
+
pooled, attn_weights = self.pool(x, output_attentions=output_attentions)
|
| 238 |
+
if output_attentions:
|
| 239 |
+
attentions = attn_weights
|
| 240 |
+
|
| 241 |
+
return pooled, spatial_features, hidden_states, attentions
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
# ----------------------------------------------------------------------
|
| 245 |
+
# Base ConvMixer Model (backbone only)
|
| 246 |
+
# ----------------------------------------------------------------------
|
| 247 |
+
class ConvMixerModel(PreTrainedModel):
|
| 248 |
+
"""
|
| 249 |
+
Bare ConvMixer model outputting raw features and optional hidden states/attentions.
|
| 250 |
+
|
| 251 |
+
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the
|
| 252 |
+
generic methods the library implements for all its model (such as downloading or saving).
|
| 253 |
+
|
| 254 |
+
It wraps the `ConvMixerWithAttnPool` backbone to be compatible with the Transformers API.
|
| 255 |
+
"""
|
| 256 |
+
config_class = ConvMixerConfig
|
| 257 |
+
base_model_prefix = "convmixer"
|
| 258 |
+
|
| 259 |
+
def __init__(self, config: ConvMixerConfig):
|
| 260 |
+
super().__init__(config)
|
| 261 |
+
self.backbone = ConvMixerWithAttnPool(
|
| 262 |
+
dim=config.dim,
|
| 263 |
+
depth=config.depth,
|
| 264 |
+
kernel_size=config.kernel_size,
|
| 265 |
+
patch_size=config.patch_size,
|
| 266 |
+
attn_pool_heads=config.attn_pool_heads,
|
| 267 |
+
attn_pool_mlp_ratio=config.attn_pool_mlp_ratio,
|
| 268 |
+
dropout=config.dropout,
|
| 269 |
+
)
|
| 270 |
+
# Initialize weights and apply final processing
|
| 271 |
+
self.post_init()
|
| 272 |
+
|
| 273 |
+
def forward(
|
| 274 |
+
self,
|
| 275 |
+
pixel_values: torch.FloatTensor,
|
| 276 |
+
output_hidden_states: Optional[bool] = None,
|
| 277 |
+
output_attentions: Optional[bool] = None,
|
| 278 |
+
return_dict: Optional[bool] = None,
|
| 279 |
+
):
|
| 280 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 281 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 282 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 283 |
+
|
| 284 |
+
pooled, spatial_features, hidden_states, attentions = self.backbone(
|
| 285 |
+
pixel_values,
|
| 286 |
+
output_hidden_states=output_hidden_states,
|
| 287 |
+
output_attentions=output_attentions,
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
if not return_dict:
|
| 291 |
+
return (spatial_features, pooled, hidden_states, attentions)
|
| 292 |
+
|
| 293 |
+
return ConvMixerOutput(
|
| 294 |
+
last_hidden_state=spatial_features,
|
| 295 |
+
pooler_output=pooled,
|
| 296 |
+
hidden_states=hidden_states,
|
| 297 |
+
attentions=attentions,
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
# ----------------------------------------------------------------------
|
| 302 |
+
# ConvMixer for Image Classification (Trainer‑compatible)
|
| 303 |
+
# ----------------------------------------------------------------------
|
| 304 |
+
class ConvMixerForImageClassification(PreTrainedModel):
|
| 305 |
+
"""
|
| 306 |
+
ConvMixer model with an image classification head on top (a linear layer on top of the
|
| 307 |
+
attention‑pooled output), e.g. for ImageNet.
|
| 308 |
+
|
| 309 |
+
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the
|
| 310 |
+
generic methods the library implements for all its model (such as downloading or saving).
|
| 311 |
+
|
| 312 |
+
Args:
|
| 313 |
+
config ([`ConvMixerConfig`]): Model configuration class with all the parameters of the model.
|
| 314 |
+
"""
|
| 315 |
+
config_class = ConvMixerConfig
|
| 316 |
+
base_model_prefix = "convmixer"
|
| 317 |
+
|
| 318 |
+
def __init__(self, config: ConvMixerConfig):
|
| 319 |
+
super().__init__(config)
|
| 320 |
+
self.backbone = ConvMixerWithAttnPool(
|
| 321 |
+
dim=config.dim,
|
| 322 |
+
depth=config.depth,
|
| 323 |
+
kernel_size=config.kernel_size,
|
| 324 |
+
patch_size=config.patch_size,
|
| 325 |
+
attn_pool_heads=config.attn_pool_heads,
|
| 326 |
+
attn_pool_mlp_ratio=config.attn_pool_mlp_ratio,
|
| 327 |
+
dropout=config.dropout,
|
| 328 |
+
)
|
| 329 |
+
self.classifier = nn.Linear(config.dim, config.num_classes) if config.num_classes > 0 else nn.Identity()
|
| 330 |
+
self.loss_fn = nn.CrossEntropyLoss() if config.num_classes > 0 else None
|
| 331 |
+
|
| 332 |
+
# Initialize weights and apply final processing
|
| 333 |
+
self.post_init()
|
| 334 |
+
|
| 335 |
+
def forward(
|
| 336 |
+
self,
|
| 337 |
+
pixel_values: torch.FloatTensor,
|
| 338 |
+
labels: Optional[torch.LongTensor] = None,
|
| 339 |
+
output_hidden_states: Optional[bool] = None,
|
| 340 |
+
output_attentions: Optional[bool] = None,
|
| 341 |
+
return_dict: Optional[bool] = None,
|
| 342 |
+
) -> ConvMixerOutput:
|
| 343 |
+
r"""
|
| 344 |
+
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
| 345 |
+
Labels for computing the image classification loss. Indices must be in `[0, ..., config.num_classes - 1]`.
|
| 346 |
+
"""
|
| 347 |
+
output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 348 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
| 349 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 350 |
+
|
| 351 |
+
pooled, spatial_features, hidden_states, attentions = self.backbone(
|
| 352 |
+
pixel_values,
|
| 353 |
+
output_hidden_states=output_hidden_states,
|
| 354 |
+
output_attentions=output_attentions,
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
logits = self.classifier(pooled)
|
| 358 |
+
|
| 359 |
+
loss = None
|
| 360 |
+
if labels is not None and self.loss_fn is not None:
|
| 361 |
+
loss = self.loss_fn(logits, labels)
|
| 362 |
+
|
| 363 |
+
if not return_dict:
|
| 364 |
+
output = (logits, spatial_features, pooled, hidden_states, attentions)
|
| 365 |
+
return ((loss,) + output) if loss is not None else output
|
| 366 |
+
|
| 367 |
+
return ConvMixerOutput(
|
| 368 |
+
loss=loss,
|
| 369 |
+
logits=logits,
|
| 370 |
+
last_hidden_state=spatial_features,
|
| 371 |
+
pooler_output=pooled,
|
| 372 |
+
hidden_states=hidden_states,
|
| 373 |
+
attentions=attentions,
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
# ----------------------------------------------------------------------
|
| 378 |
+
# Optional: Register models with auto classes for easy loading
|
| 379 |
+
# ----------------------------------------------------------------------
|
| 380 |
+
ConvMixerConfig.register_for_auto_class()
|
| 381 |
+
ConvMixerModel.register_for_auto_class("AutoModel")
|
| 382 |
+
ConvMixerForImageClassification.register_for_auto_class("AutoModelForImageClassification")
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8bb43f33b35482d04414cd1d1fefefbcb45ce7ba26af533538045013dbedd198
|
| 3 |
+
size 199739632
|