WZMIAOMIAO/deep-learning-for-image-processing · error · FileNotFoundError
not found weights file: {}
Error message
not found weights file: {} What it means
train.py only attempts to load pretrained weights if os.path.exists(args.weights); when the file path is set but missing, it raises FileNotFoundError instead of silently training from scratch.
Source
Thrown at pytorch_classification/Test10_regnet/train.py:76
batch_size=batch_size,
shuffle=False,
pin_memory=True,
num_workers=nw,
collate_fn=val_dataset.collate_fn)
# 如果存在预训练权重则载入
model = create_regnet(model_name=args.model_name,
num_classes=args.num_classes).to(device)
# print(model)
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():
# 除最后的全连接层外,其他权重全部冻结
if "head" not in name:
para.requires_grad_(False)
else:
print("train {}".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=5E-5)
# 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
- Download the weights first (run pretrain_weights.py) so the file exists at args.weights.
- Verify/correct the --weights path (absolute path or correct relative path from the CWD).
- Pass --weights '' or omit it if you intend to train from scratch (per the script's empty-string convention).
Example fix
// before python train.py --weights ./regnetx_400mf.pth # file not downloaded // after python pretrain_weights.py && python train.py --weights ./regnetx_400mf.pth
Defensive patterns
Strategy: validation
Validate before calling
import os
weights = "./regnetx_400mf.pth"
if weights and not os.path.exists(weights):
raise SystemExit(f"weights missing: {weights}; run pretrain_weights.py first") Type guard
def weights_available(path) -> bool:
return not path or os.path.isfile(path) Try / catch
try:
main()
except FileNotFoundError as e:
print(f"{e}; downloading weights...")
os.system("python pretrain_weights.py")
main() Prevention
- Run pretrain_weights.py before train.py
- Verify --weights path exists (os.path.exists) at script start
- Use absolute paths or paths relative to the script, not the shell CWD
- Pass empty string for --weights to intentionally train from scratch
When it happens
Trigger: Running train.py with --weights ./regnetx_400mf.pth where the .pth was never downloaded, renamed, or is in a different working directory.
Common situations: Forgot to run pretrain_weights.py first; typo in the path; weights downloaded under a different filename than the --weights argument.
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/3c0425ba40bdb3b0.
Report an issue: GitHub.