{"record":{"id":"a7e5986aa7753c78","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"not-found-weights-file-a7e598","errorCode":null,"errorMessage":"not found weights file: {}","messagePattern":"not found weights file: (.+?)","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/Test9_efficientNet/train.py","lineNumber":83,"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            # 除最后一个卷积层和全连接层外，其他权重全部冻结\n            if (\"features.top\" not in name) and (\"classifier\" 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":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/Test9_efficientNet/train.py#L65-L101","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\npython train.py --weights ./efficientnetb0.pth   # not in cwd\n// after\ncd pytorch_classification/Test9_efficientNet\npython train.py --weights ./efficientnetb0.pth   # or absolute path","handlingStrategy":"validation","validationCode":"if args.weights and not os.path.exists(args.weights):\n    import sys; sys.exit(f'missing weights: {args.weights}')","typeGuard":null,"tryCatchPattern":"try:\n    main()\nexcept FileNotFoundError as e:\n    print('Run trans_weights_to_pytorch.py to generate weights, or pass --weights \"\":', e)","preventionTips":["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"],"tags":["pytorch","file-not-found","pretrained-weights"],"backgroundTag":"missing-weights-file","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}