{"record":{"id":"36777c42914f2d21","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"not-found-weights-file-36777c","errorCode":null,"errorMessage":"not found weights file: {}","messagePattern":"not found weights file: (.+?)","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/Test11_efficientnetV2/train.py","lineNumber":78,"sourceCode":"                                               collate_fn=train_dataset.collate_fn)\n\n    val_loader = torch.utils.data.DataLoader(val_dataset,\n                                             batch_size=batch_size,\n                                             shuffle=False,\n                                             pin_memory=True,\n                                             num_workers=nw,\n                                             collate_fn=val_dataset.collate_fn)\n\n    # 如果存在预训练权重则载入\n    model = create_model(num_classes=args.num_classes).to(device)\n    if args.weights != \"\":\n        if os.path.exists(args.weights):\n            weights_dict = torch.load(args.weights, map_location=device)\n            load_weights_dict = {k: v for k, v in weights_dict.items()\n                                 if model.state_dict()[k].numel() == v.numel()}\n            print(model.load_state_dict(load_weights_dict, strict=False))\n        else:\n            raise FileNotFoundError(\"not found weights file: {}\".format(args.weights))\n\n    # 是否冻结权重\n    if args.freeze_layers:\n        for name, para in model.named_parameters():\n            # 除head外，其他权重全部冻结\n            if \"head\" not in name:\n                para.requires_grad_(False)\n            else:\n                print(\"training {}\".format(name))\n\n    pg = [p for p in model.parameters() if p.requires_grad]\n    optimizer = optim.SGD(pg, lr=args.lr, momentum=0.9, weight_decay=1E-4)\n    # Scheduler https://arxiv.org/pdf/1812.01187.pdf\n    lf = lambda x: ((1 + math.cos(x * math.pi / args.epochs)) / 2) * (1 - args.lrf) + args.lrf  # cosine\n    scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf)\n\n    for epoch in range(args.epochs):\n        # train","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/Test11_efficientnetV2/train.py#L60-L96","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npython train.py --weights ./efficientnet_v2_rw_s weights.pth\n// after\npython train.py --weights /abs/path/efficientnet_v2_rw_s-dd5fe8b6.pth","handlingStrategy":"validation","validationCode":"import os\nweights = args.weights\nif weights and not os.path.isfile(weights):\n    raise FileNotFoundError(f\"weights not found: {os.path.abspath(weights)}\")","typeGuard":null,"tryCatchPattern":"try:\n    weights_dict = torch.load(args.weights, map_location=device)\nexcept FileNotFoundError as e:\n    print(f\"WARNING: {e}; training from scratch\")\n    weights_dict = None","preventionTips":["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."],"tags":["pytorch","file-not-found","weights","cli-args"],"backgroundTag":"weights-file-not-found","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}