Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions timm/models/_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
try:
from huggingface_hub import (
create_repo, get_hf_file_metadata,
hf_hub_download, hf_hub_url,
hf_hub_download, hf_hub_url, model_info,
repo_type_and_id_from_hf_id, upload_folder)
from huggingface_hub.utils import EntryNotFoundError
from huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundError
hf_hub_download = partial(hf_hub_download, library_name="timm", library_version=__version__)
_has_hf_hub = True
except ImportError:
Expand Down Expand Up @@ -540,3 +540,56 @@ def _get_safe_alternatives(filename: str) -> Iterable[str]:
yield HF_OPEN_CLIP_SAFE_WEIGHTS_NAME
if filename not in (HF_WEIGHTS_NAME, HF_OPEN_CLIP_WEIGHTS_NAME) and filename.endswith(".bin"):
yield filename[:-4] + ".safetensors"


def _get_license_from_hf_hub(model_id: str | None, hf_hub_id: str | None) -> str | None:
"""Retrieve license information for a model from Hugging Face Hub.

Fetches the license field from the model card metadata on Hugging Face Hub
for the specified model. Returns None if the model is not found, if
huggingface_hub is not installed, or if the model is marked as "untrained".

Args:
model_id: The model identifier/name. In the case of None we assume an untrained model.
hf_hub_id: The Hugging Face Hub organization/user ID. If it is None,
we will return None as we cannot infer the license terms.

Returns:
The license string in lowercase if found, None otherwise.

Note:
Requires huggingface_hub package to be installed. Will log a warning
and return None if the package is not available.
"""
if not has_hf_hub(True):
msg = "For updated license information run `pip install huggingface_hub`."
_logger.warning(msg=msg)
return None

if not (model_id and hf_hub_id):
return None

repo_id: str = hf_hub_id + model_id

try:
info = model_info(repo_id=repo_id)

except RepositoryNotFoundError:
msg = f"Repository {repo_id} was not found. Manual inspection of license needed."
_logger.warning(msg=msg)
return None

except Exception as _:
msg = f"Error for {repo_id}. Manual inspection of license needed."
_logger.warning(msg=msg)
return None

license = info.card_data.get("license").lower() if info.card_data else None

if license == 'other':
name = info.card_data.get("license_name", None)

if name is not None:
return name

return license
1 change: 1 addition & 0 deletions timm/models/beit.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ def _cfg(url: str = '', **kwargs) -> Dict[str, Any]:
'crop_pct': .9, 'interpolation': 'bicubic', 'fixed_input_size': True,
'mean': (0.5, 0.5, 0.5), 'std': (0.5, 0.5, 0.5),
'first_conv': 'patch_embed.proj', 'classifier': 'head',
'license': 'apache-2.0',
**kwargs
}

Expand Down
2 changes: 1 addition & 1 deletion timm/models/byoanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def _cfg(url: str = '', **kwargs) -> Dict[str, Any]:
'crop_pct': 0.95, 'interpolation': 'bicubic',
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'stem.conv1.conv', 'classifier': 'head.fc',
'fixed_input_size': False, 'min_input_size': (3, 224, 224),
'fixed_input_size': False, 'min_input_size': (3, 224, 224), 'license': 'apache-2.0',
**kwargs
}

Expand Down
23 changes: 23 additions & 0 deletions timm/models/byobnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2279,6 +2279,7 @@ def _cfg(url: str = '', **kwargs) -> Dict[str, Any]:
'crop_pct': 0.875, 'interpolation': 'bilinear',
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'stem.conv', 'classifier': 'head.fc',
'license': 'apache-2.0',
**kwargs
}

Expand All @@ -2298,6 +2299,7 @@ def _cfgr(url: str = '', **kwargs) -> Dict[str, Any]:
'crop_pct': 0.9, 'interpolation': 'bicubic',
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'stem.conv1.conv', 'classifier': 'head.fc',
'license': 'apache-2.0',
**kwargs
}

Expand Down Expand Up @@ -2451,26 +2453,31 @@ def _cfgr(url: str = '', **kwargs) -> Dict[str, Any]:
hf_hub_id='timm/',
crop_pct=0.875,
first_conv=('stem.conv_kxk.0.conv', 'stem.conv_scale.conv'),
license='mobileone-license',
),
'mobileone_s1.apple_in1k': _cfg(
hf_hub_id='timm/',
crop_pct=0.9,
first_conv=('stem.conv_kxk.0.conv', 'stem.conv_scale.conv'),
license='mobileone-license',
),
'mobileone_s2.apple_in1k': _cfg(
hf_hub_id='timm/',
crop_pct=0.9,
first_conv=('stem.conv_kxk.0.conv', 'stem.conv_scale.conv'),
license='mobileone-license',
),
'mobileone_s3.apple_in1k': _cfg(
hf_hub_id='timm/',
crop_pct=0.9,
first_conv=('stem.conv_kxk.0.conv', 'stem.conv_scale.conv'),
license='mobileone-license',
),
'mobileone_s4.apple_in1k': _cfg(
hf_hub_id='timm/',
crop_pct=0.9,
first_conv=('stem.conv_kxk.0.conv', 'stem.conv_scale.conv'),
license='mobileone-license',
),

# original attention pool head variants
Expand All @@ -2479,90 +2486,106 @@ def _cfgr(url: str = '', **kwargs) -> Dict[str, Any]:
num_classes=1024, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
fixed_input_size=True, input_size=(3, 224, 224), pool_size=(7, 7),
classifier='head.proj',
license='mit',
),
'resnet101_clip.openai': _cfgr(
hf_hub_id='timm/',
num_classes=512, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
fixed_input_size=True, input_size=(3, 224, 224), pool_size=(7, 7),
classifier='head.proj',
license='mit',
),
'resnet50x4_clip.openai': _cfgr(
hf_hub_id='timm/',
num_classes=640, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
fixed_input_size=True, input_size=(3, 288, 288), pool_size=(9, 9),
classifier='head.proj',
license='mit',
),
'resnet50x16_clip.openai': _cfgr(
hf_hub_id='timm/',
num_classes=768, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
fixed_input_size=True, input_size=(3, 384, 384), pool_size=(12, 12),
classifier='head.proj',
license='mit',
),
'resnet50x64_clip.openai': _cfgr(
hf_hub_id='timm/',
num_classes=1024, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
fixed_input_size=True, input_size=(3, 448, 448), pool_size=(14, 14),
classifier='head.proj',
license='mit',
),
'resnet50_clip.cc12m': _cfgr(
hf_hub_id='timm/',
num_classes=1024, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
fixed_input_size=True, input_size=(3, 224, 224), pool_size=(7, 7),
classifier='head.proj',
license='mit',
),
'resnet50_clip.yfcc15m': _cfgr(
hf_hub_id='timm/',
num_classes=1024, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
fixed_input_size=True, input_size=(3, 224, 224), pool_size=(7, 7),
classifier='head.proj',
license='mit',
),
'resnet101_clip.yfcc15m': _cfgr(
hf_hub_id='timm/',
num_classes=512, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
fixed_input_size=True, input_size=(3, 224, 224), pool_size=(7, 7),
classifier='head.proj',
license='mit',
),

# avg-pool w/ optional standard classifier head variants
'resnet50_clip_gap.openai': _cfgr(
hf_hub_id='timm/',
num_classes=0, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
input_size=(3, 224, 224), pool_size=(7, 7),
license='mit',
),
'resnet101_clip_gap.openai': _cfgr(
hf_hub_id='timm/',
num_classes=0, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
input_size=(3, 224, 224), pool_size=(7, 7),
license='mit',
),
'resnet50x4_clip_gap.openai': _cfgr(
hf_hub_id='timm/',
num_classes=0, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
input_size=(3, 288, 288), pool_size=(9, 9),
license='mit',
),
'resnet50x16_clip_gap.openai': _cfgr(
hf_hub_id='timm/',
num_classes=0, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
input_size=(3, 384, 384), pool_size=(12, 12),
license='mit',
),
'resnet50x64_clip_gap.openai': _cfgr(
hf_hub_id='timm/',
num_classes=0, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
input_size=(3, 448, 448), pool_size=(14, 14),
license='mit',
),
'resnet50_clip_gap.cc12m': _cfgr(
hf_hub_id='timm/',
num_classes=0, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
input_size=(3, 224, 224), pool_size=(7, 7),
license='mit',
),
'resnet50_clip_gap.yfcc15m': _cfgr(
hf_hub_id='timm/',
num_classes=0, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
input_size=(3, 224, 224), pool_size=(7, 7),
license='mit',
),
'resnet101_clip_gap.yfcc15m': _cfgr(
hf_hub_id='timm/',
num_classes=0, mean=OPENAI_CLIP_MEAN, std=OPENAI_CLIP_STD,
input_size=(3, 224, 224), pool_size=(7, 7),
license='mit',
),

'resnet50_mlp.untrained': _cfgr(
Expand Down
1 change: 1 addition & 0 deletions timm/models/cait.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ def _cfg(url='', **kwargs):
'crop_pct': 1.0, 'interpolation': 'bicubic', 'fixed_input_size': True,
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'patch_embed.proj', 'classifier': 'head',
'license': 'apache-2.0',
**kwargs
}

Expand Down
3 changes: 2 additions & 1 deletion timm/models/coat.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ def _cfg_coat(url='', **kwargs):
'crop_pct': .9, 'interpolation': 'bicubic', 'fixed_input_size': True,
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'patch_embed1.proj', 'classifier': 'head',
'license': 'apache-2.0',
**kwargs
}

Expand Down Expand Up @@ -800,4 +801,4 @@ def coat_lite_medium_384(pretrained=False, **kwargs) -> CoaT:
model_cfg = dict(
img_size=384, patch_size=4, embed_dims=[128, 256, 320, 512], serial_depths=[3, 6, 10, 8])
model = _create_coat('coat_lite_medium_384', pretrained=pretrained, **dict(model_cfg, **kwargs))
return model
return model
2 changes: 1 addition & 1 deletion timm/models/convit.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def _cfg(url='', **kwargs):
'url': url,
'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': None,
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD, 'fixed_input_size': True,
'first_conv': 'patch_embed.proj', 'classifier': 'head',
'first_conv': 'patch_embed.proj', 'classifier': 'head', 'license': 'apache-2.0',
**kwargs
}

Expand Down
4 changes: 2 additions & 2 deletions timm/models/convmixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _cfg(url='', **kwargs):
'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': None,
'crop_pct': .96, 'interpolation': 'bicubic',
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD, 'classifier': 'head',
'first_conv': 'stem.0',
'first_conv': 'stem.0', 'license': 'mit',
**kwargs
}

Expand Down Expand Up @@ -143,4 +143,4 @@ def convmixer_768_32(pretrained=False, **kwargs) -> ConvMixer:
@register_model
def convmixer_1024_20_ks9_p14(pretrained=False, **kwargs) -> ConvMixer:
model_args = dict(dim=1024, depth=20, kernel_size=9, patch_size=14, **kwargs)
return _create_convmixer('convmixer_1024_20_ks9_p14', pretrained, **model_args)
return _create_convmixer('convmixer_1024_20_ks9_p14', pretrained, **model_args)
10 changes: 5 additions & 5 deletions timm/models/convnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ def _cfg(url='', **kwargs):
'crop_pct': 0.875, 'interpolation': 'bicubic',
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'stem.0', 'classifier': 'head.fc',
**kwargs
'license': 'apache-2.0', **kwargs
}


Expand Down Expand Up @@ -1067,25 +1067,25 @@ def _cfgv2(url='', **kwargs):
hf_hub_id='timm/',
crop_pct=1.0,
num_classes=0,
license='dinov3',
license='dinov3-license',
),
'convnext_small.dinov3_lvd1689m': _cfg(
hf_hub_id='timm/',
crop_pct=1.0,
num_classes=0,
license='dinov3',
license='dinov3-license',
),
'convnext_base.dinov3_lvd1689m': _cfg(
hf_hub_id='timm/',
crop_pct=1.0,
num_classes=0,
license='dinov3',
license='dinov3-license',
),
'convnext_large.dinov3_lvd1689m': _cfg(
hf_hub_id='timm/',
crop_pct=1.0,
num_classes=0,
license='dinov3',
license='dinov3-license',
),

"test_convnext.r160_in1k": _cfg(
Expand Down
1 change: 1 addition & 0 deletions timm/models/crossvit.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ def _cfg(url='', **kwargs):
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD, 'fixed_input_size': True,
'first_conv': ('patch_embed.0.proj', 'patch_embed.1.proj'),
'classifier': ('head.0', 'head.1'),
'license': 'apache-2.0',
**kwargs
}

Expand Down
4 changes: 2 additions & 2 deletions timm/models/cspnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ def _cfg(url='', **kwargs):
'num_classes': 1000, 'input_size': (3, 256, 256), 'pool_size': (8, 8),
'crop_pct': 0.887, 'interpolation': 'bilinear',
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'stem.conv1.conv', 'classifier': 'head.fc',
'first_conv': 'stem.conv1.conv', 'classifier': 'head.fc', 'license': 'apache-2.0',
**kwargs
}

Expand Down Expand Up @@ -1111,4 +1111,4 @@ def cs3edgenet_x(pretrained=False, **kwargs) -> CspNet:

@register_model
def cs3se_edgenet_x(pretrained=False, **kwargs) -> CspNet:
return _create_cspnet('cs3se_edgenet_x', pretrained=pretrained, **kwargs)
return _create_cspnet('cs3se_edgenet_x', pretrained=pretrained, **kwargs)
1 change: 1 addition & 0 deletions timm/models/davit.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ def _cfg(url='', **kwargs):
'crop_pct': 0.95, 'interpolation': 'bicubic',
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'stem.conv', 'classifier': 'head.fc',
'license': 'apache-2.0',
**kwargs
}

Expand Down
1 change: 1 addition & 0 deletions timm/models/deit.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def _cfg(url='', **kwargs):
'crop_pct': .9, 'interpolation': 'bicubic', 'fixed_input_size': True,
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'patch_embed.proj', 'classifier': 'head',
'license': 'apache-2.0',
**kwargs
}

Expand Down
3 changes: 2 additions & 1 deletion timm/models/densenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ def _cfg(url: str = '', **kwargs) -> Dict[str, Any]:
'url': url, 'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': (7, 7),
'crop_pct': 0.875, 'interpolation': 'bicubic',
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'features.conv0', 'classifier': 'classifier', **kwargs,
'first_conv': 'features.conv0', 'classifier': 'classifier', 'license': 'apache-2.0',
**kwargs,
}


Expand Down
6 changes: 3 additions & 3 deletions timm/models/dla.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def _cfg(url='', **kwargs):
'num_classes': 1000, 'input_size': (3, 224, 224), 'pool_size': (7, 7),
'crop_pct': 0.875, 'interpolation': 'bilinear',
'mean': IMAGENET_DEFAULT_MEAN, 'std': IMAGENET_DEFAULT_STD,
'first_conv': 'base_layer.0', 'classifier': 'fc',
'first_conv': 'base_layer.0', 'classifier': 'fc', 'license': 'bsd-3-clause',
**kwargs
}

Expand All @@ -415,8 +415,8 @@ def _cfg(url='', **kwargs):
'dla102x.in1k': _cfg(hf_hub_id='timm/'),
'dla102x2.in1k': _cfg(hf_hub_id='timm/'),
'dla169.in1k': _cfg(hf_hub_id='timm/'),
'dla60_res2net.in1k': _cfg(hf_hub_id='timm/'),
'dla60_res2next.in1k': _cfg(hf_hub_id='timm/'),
'dla60_res2net.in1k': _cfg(hf_hub_id='timm/', license='unknown'),
'dla60_res2next.in1k': _cfg(hf_hub_id='timm/', license='unknown'),
})


Expand Down
Loading