iwin-small-patch4-window7-224 / configuration_iwin.py
cominder's picture
Add Transformers remote code so from_pretrained(trust_remote_code=True) works
557cece verified
Raw History Blame Contribute Delete
2.45 kB
# coding=utf-8
# Copyright 2025 Simin Huo and Ning Li. Iwin Transformer.
# SPDX-License-Identifier: MIT
"""Hugging Face configuration for Iwin Transformer."""
from transformers import PretrainedConfig
class IwinConfig(PretrainedConfig):
r"""
Configuration for [`IwinModel`] / [`IwinForImageClassification`].
Matches the Hub checkpoints under `cominder/iwin-*`. The published
`config.json` used `path_norm` for patch-embedding LayerNorm; that alias is
accepted and written back as `patch_norm`.
"""
model_type = "iwin"
attribute_map = {
"num_attention_heads": "num_heads",
"num_hidden_layers": "num_layers",
}
def __init__(
self,
image_size=224,
patch_size=4,
num_channels=3,
embed_dim=96,
depths=None,
num_heads=None,
window_size=7,
mlp_ratio=4.0,
qkv_bias=True,
hidden_dropout_prob=0.0,
attention_probs_dropout_prob=0.0,
drop_path_rate=0.1,
hidden_act="gelu",
use_absolute_embeddings=False,
initializer_range=0.02,
layer_norm_eps=1e-5,
patch_norm=None,
path_norm=None,
encoder_stride=32,
**kwargs,
):
super().__init__(**kwargs)
if depths is None:
depths = [2, 2, 6, 2]
if num_heads is None:
num_heads = [3, 6, 12, 24]
if patch_norm is None:
patch_norm = True if path_norm is None else bool(path_norm)
self.image_size = image_size
self.patch_size = patch_size
self.num_channels = num_channels
self.embed_dim = embed_dim
self.depths = list(depths)
self.num_layers = len(self.depths)
self.num_heads = list(num_heads)
self.window_size = window_size
self.mlp_ratio = mlp_ratio
self.qkv_bias = qkv_bias
self.hidden_dropout_prob = hidden_dropout_prob
self.attention_probs_dropout_prob = attention_probs_dropout_prob
self.drop_path_rate = drop_path_rate
self.hidden_act = hidden_act
self.use_absolute_embeddings = use_absolute_embeddings
self.initializer_range = initializer_range
self.layer_norm_eps = layer_norm_eps
self.patch_norm = bool(patch_norm)
self.path_norm = self.patch_norm
self.encoder_stride = encoder_stride
self.hidden_size = int(embed_dim * 2 ** (len(self.depths) - 1))