open-mmlab/mmdetection · warning
No pre-trained weights for {self.__class__.__name__}, traini
Error message
No pre-trained weights for {self.__class__.__name__}, training start from scratch What it means
Swin's init_weights logs (via logger.warn) that no checkpoint was configured, so weights are randomly initialized and training starts from scratch. This happens when init_cfg is None. It is expected for training-from-scratch runs but a red flag when you intended to load a pretrained model.
Source
Thrown at mmdet/models/backbones/swin.py:674
self.drop_after_pos.eval()
for i in range(1, self.frozen_stages + 1):
if (i - 1) in self.out_indices:
norm_layer = getattr(self, f'norm{i-1}')
norm_layer.eval()
for param in norm_layer.parameters():
param.requires_grad = False
m = self.stages[i - 1]
m.eval()
for param in m.parameters():
param.requires_grad = False
def init_weights(self):
logger = MMLogger.get_current_instance()
if self.init_cfg is None:
logger.warn(f'No pre-trained weights for '
f'{self.__class__.__name__}, '
f'training start from scratch')
if self.use_abs_pos_embed:
trunc_normal_(self.absolute_pos_embed, std=0.02)
for m in self.modules():
if isinstance(m, nn.Linear):
trunc_normal_init(m, std=.02, bias=0.)
elif isinstance(m, nn.LayerNorm):
constant_init(m, 1.0)
else:
assert 'checkpoint' in self.init_cfg, f'Only support ' \
f'specify `Pretrained` in ' \
f'`init_cfg` in ' \
f'{self.__class__.__name__} '
ckpt = CheckpointLoader.load_checkpoint(
self.init_cfg.checkpoint, logger=logger, map_location='cpu')
if 'state_dict' in ckpt:
_state_dict = ckpt['state_dict']View on GitHub (pinned to cfd5d3a985)
Solutions
- Verify you intended to train from scratch; if not, add init_cfg=dict(type='Pretrained', checkpoint='<path-or-url>') to the backbone config
- Check that the checkpoint path/URL exists and is readable
- If from-scratch is intended, ignore the message
Example fix
// before backbone=dict(type='SwinTransformer', embed_dims=96, ...) // after backbone=dict(type='SwinTransformer', embed_dims=96, init_cfg=dict(type='Pretrained', checkpoint='https://download.openmmlab.com/...'), ...)
Defensive patterns
Strategy: validation
Validate before calling
backbone_cfg = cfg['model']['backbone']
if backbone_cfg.get('init_cfg') is None:
logging.warning('Swin will train from scratch; add init_cfg if a checkpoint was intended') Prevention
- Always verify backbone.init_cfg before training when fine-tuning is intended
- Log the effective init_cfg at startup
When it happens
Trigger: Building a SwinTransformer backbone without init_cfg/pretrained and calling .init_weights() (directly or via detector init).
Common situations: Checkpoint path typo or missing init_cfg in config; user assumed default weights load automatically; fine-tuning config copied but init_cfg removed.
Related errors
- DeprecationWarning: pretrained is deprecated, please use "in
- pretrained must be a str or None
- `init_cfg` must contain the key "type"
- pretrained must be a str or None
- pretrained must be a str or None
AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27).
Data as JSON: /api/errors/27b45c57643f00de.
Report an issue: GitHub.