WZMIAOMIAO/deep-learning-for-image-processing · error · FileNotFoundError
not found weights file: {}
Error message
not found weights file: {} What it means
Same guard as error 22 but in Test9's train.py: if --weights is non-empty and os.path.exists fails, main raises FileNotFoundError naming the path. It exists so training never silently starts without the requested EfficientNet pretrained weights.
Source
Thrown at pytorch_classification/Test9_efficientNet/train.py:83
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():
# 除最后一个卷积层和全连接层外,其他权重全部冻结
if ("features.top" not in name) and ("classifier" 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
- Generate/download the weights file (run trans_weights_to_pytorch.py) and pass the existing path via --weights
- Pass --weights "" to train from scratch instead
- Use an absolute path or cd into the Test9_efficientNet directory before running
Example fix
// before python train.py --weights ./efficientnetb0.pth # not in cwd // after cd pytorch_classification/Test9_efficientNet python train.py --weights ./efficientnetb0.pth # or absolute path
Defensive patterns
Strategy: validation
Validate before calling
if args.weights and not os.path.exists(args.weights):
import sys; sys.exit(f'missing weights: {args.weights}') Try / catch
try:
main()
except FileNotFoundError as e:
print('Run trans_weights_to_pytorch.py to generate weights, or pass --weights "":', e) Prevention
- Run the weight-conversion script before first training run
- Store weights in a fixed project-relative dir and reference via Path(__file__)
- Document the empty-string default for training from scratch
When it happens
Trigger: python train.py --weights <path> where the EfficientNet .pth (e.g. efficientnetb0.pth converted from TF) was never downloaded, was renamed, or the path is relative to the wrong cwd.
Common situations: Missing the step that runs trans_weights_to_pytorch.py to produce the .pth; running from repo root while weights sit in Test9_efficientNet/; typos in filename.
Related errors
- not found weights file: {}
- not found weights file: {}
- not found weights file: {}
- not found weights file: {}
- not found weights file: {}
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/a7e5986aa7753c78.
Report an issue: GitHub.