{"record":{"id":"f912b4eacf211243","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"not-found-weights-file-f912b4","errorCode":null,"errorMessage":"not found weights file: {}","messagePattern":"not found weights file: (.+?)","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/Test7_shufflenet/train.py","lineNumber":73,"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 = shufflenet_v2_x1_0(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            # 除最后的全连接层外，其他权重全部冻结\n            if \"fc\" not in name:\n                para.requires_grad_(False)\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=4E-5)\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\n        mean_loss = train_one_epoch(model=model,\n                                    optimizer=optimizer,","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/Test7_shufflenet/train.py#L55-L91","documentation":"The shufflenet training script raises FileNotFoundError in main() when args.weights is set to a path that does not exist on disk. It fails fast rather than silently training from scratch, since users who pass --weights expect pre-trained initialization.","triggerScenarios":"Running `python train.py --weights some/path.pth` where the file is absent: wrong filename, weights never downloaded, or a relative path that doesn't resolve from the current working directory.","commonSituations":"Tutorial users missing the shufflenetv2 pre-trained checkpoint download; moving/renaming the .pth after download; launching the script from another directory (IDE run configs often change cwd).","solutions":["Verify and correct the --weights path (ls the exact value, prefer absolute paths).","Download the shufflenetv2 pre-trained weights and pass their absolute path.","Pass --weights '' if you intend to train from random initialization, since the load branch only runs for a non-empty path."],"exampleFix":"// before\npython train.py --weights ./shufflenetv2.pth\n// after\npython train.py --weights /abs/path/shufflenetv2_x1-5666bf0f80.pth","handlingStrategy":"validation","validationCode":"import os\nif args.weights and not os.path.isfile(args.weights):\n    sys.exit(f\"weights file missing: {os.path.abspath(args.weights)}\")","typeGuard":null,"tryCatchPattern":"try:\n    weights_dict = torch.load(args.weights, map_location=device)\nexcept FileNotFoundError as e:\n    print(f\"WARNING: {e}; starting from random init\")\n    weights_dict = None","preventionTips":["Use absolute paths for --weights.","Verify checkpoint existence in a pre-run setup script.","Keep a stable weights/ directory with documented filenames.","Echo os.path.abspath(args.weights) before loading."],"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"}