WZMIAOMIAO/deep-learning-for-image-processing · error · FileNotFoundError
not found weights file: {}
Error message
not found weights file: {} What it means
This FileNotFoundError is raised in main() of the efficientnetV2 training script when args.weights points to a path that does not exist. The script only attempts torch.load if os.path.exists(args.weights) is true; otherwise it aborts training early rather than silently starting from random weights.
Source
Thrown at pytorch_classification/Test11_efficientnetV2/train.py:78
collate_fn=train_dataset.collate_fn)
val_loader = torch.utils.data.DataLoader(val_dataset,
batch_size=batch_size,
shuffle=False,
pin_memory=True,
num_workers=nw,
collate_fn=val_dataset.collate_fn)
# 如果存在预训练权重则载入
model = create_model(num_classes=args.num_classes).to(device)
if args.weights != "":
if os.path.exists(args.weights):
weights_dict = torch.load(args.weights, map_location=device)
load_weights_dict = {k: v for k, v in weights_dict.items()
if model.state_dict()[k].numel() == v.numel()}
print(model.load_state_dict(load_weights_dict, strict=False))
else:
raise FileNotFoundError("not found weights file: {}".format(args.weights))
# 是否冻结权重
if args.freeze_layers:
for name, para in model.named_parameters():
# 除head外,其他权重全部冻结
if "head" not in name:
para.requires_grad_(False)
else:
print("training {}".format(name))
pg = [p for p in model.parameters() if p.requires_grad]
optimizer = optim.SGD(pg, lr=args.lr, momentum=0.9, weight_decay=1E-4)
# Scheduler https://arxiv.org/pdf/1812.01187.pdf
lf = lambda x: ((1 + math.cos(x * math.pi / args.epochs)) / 2) * (1 - args.lrf) + args.lrf # cosine
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf)
for epoch in range(args.epochs):
# trainView on GitHub (pinned to 1ec3fe6f37)
Solutions
- Check the path exists before running: ls the value you pass to --weights and correct it.
- Download the pre-trained weights the tutorial expects and pass its exact absolute path to --weights.
- Run with --weights '' (empty) if you intend to train from scratch, since the script only enters the load branch when args.weights is non-empty/truthy.
- Use an absolute path to eliminate working-directory ambiguity.
Example fix
// before python train.py --weights ./efficientnet_v2_rw_s weights.pth // after python train.py --weights /abs/path/efficientnet_v2_rw_s-dd5fe8b6.pth
Defensive patterns
Strategy: validation
Validate before calling
import os
weights = args.weights
if weights and not os.path.isfile(weights):
raise FileNotFoundError(f"weights not found: {os.path.abspath(weights)}") Try / catch
try:
weights_dict = torch.load(args.weights, map_location=device)
except FileNotFoundError as e:
print(f"WARNING: {e}; training from scratch")
weights_dict = None Prevention
- Always pass absolute paths to --weights.
- Download checkpoints in a setup script and verify with os.path.isfile.
- Run training from a consistent working directory.
- Print os.path.abspath(args.weights) at startup to confirm resolution.
When it happens
Trigger: Running `python train.py --weights path/to/weights.pth` where the file path is wrong, the .pth was never downloaded, or the path is relative to a different working directory than the one the script runs from.
Common situations: Following a tutorial and forgetting to download the official efficientnetv2 pre-trained weights; renaming or moving the weights file after download; running the script from a different cwd so the relative path no longer resolves.
Related errors
- not found weights file: {}
- not found weights file: {}
- not found weights file: {}
- not found weights file: {}
- dataset have {} classes, but input {}
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/36777c42914f2d21.
Report an issue: GitHub.