huggingface/pytorch-image-models · error · RuntimeError

features_only not implemented for Vision Transformer models.

Error message

features_only not implemented for Vision Transformer models.

What it means

ConViT factory functions reject features_only=True because the model is a plain Vision Transformer (single feature resolution, non-CNN layout) and is not wired into timm's feature-extraction wrapper. The check is in _create_convit before the model is built.

Source

Thrown at timm/models/convit.py:416

            x = blk(x)
        x = self.norm(x)
        return x

    def forward_head(self, x, pre_logits: bool = False):
        if self.global_pool:
            x = x[:, 1:].mean(dim=1) if self.global_pool == 'avg' else x[:, 0]
        x = self.head_drop(x)
        return x if pre_logits else self.head(x)

    def forward(self, x):
        x = self.forward_features(x)
        x = self.forward_head(x)
        return x


def _create_convit(variant, pretrained=False, **kwargs):
    if kwargs.get('features_only', None):
        raise RuntimeError('features_only not implemented for Vision Transformer models.')

    return build_model_with_cfg(ConVit, variant, pretrained, **kwargs)


def _cfg(url='', **kwargs):
    return {
        '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', 'license': 'apache-2.0',
        **kwargs
    }


default_cfgs = generate_default_cfgs({
    # ConViT
    'convit_tiny.fb_in1k': _cfg(hf_hub_id='timm/'),
    'convit_small.fb_in1k': _cfg(hf_hub_id='timm/'),

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Create the model without features_only and use forward_intermediates(x, indices=...) to pull intermediate block outputs
  2. Pick a ViT variant that supports feature extraction (many timm ViTs expose feature_cfgs, e.g. 'vit_small_patch16_224' with features_only=True)
  3. Wrap the model and manually take the output of self.blocks[i] via hooks

Example fix

# before
model = timm.create_model('convit_small', pretrained=True, features_only=True)
# after
model = timm.create_model('convit_small', pretrained=True)
feats = model.forward_intermediates(x, indices=[3, 7, 11], output_fmt='NCHW')
Defensive patterns

Strategy: validation

Validate before calling

import timm
m = timm.create_model('convit_small')
if not hasattr(m, 'feature_info'):
    raise SystemExit('convit does not support features_only; use forward_intermediates')

Type guard

def supports_features_only(name: str) -> bool:
    import timm
    m = timm.create_model(name)
    ok = hasattr(m, 'feature_info')
    del m
    return ok

Try / catch

try:
    model = timm.create_model('convit_tiny', features_only=True)
except RuntimeError:
    model = timm.create_model('convit_tiny')
    feats = lambda x: model.forward_intermediates(x, indices=[3, 7, 11])

Prevention

When it happens

Trigger: Calling timm.create_model('convit_tiny'|'convit_small'|'convit_base', features_only=True).

Common situations: Generic backbone-registry code that passes features_only=True to every model; migrating a detection/segmentation pipeline from a CNN to ConViT without adjusting the feature-extraction strategy.

Related errors


AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27). Data as JSON: /api/errors/751c90e95f0694d7. Report an issue: GitHub.