Skip to content
The Chat Archive
gpt-4o-mini-2024-07-18· OpenAI WildChat-4.8M (AI2)

import torch from yolov5 import YOLO # 确保YOLO模块正确导入 加载预训练模型(YOLOv5s) model =…

  1. User

    import torch from yolov5 import YOLO # 确保YOLO模块正确导入

    加载预训练模型(YOLOv5s)

    model = YOLO('yolov5s.pt') # 使用轻量模型

    运行推理

    img_path = "D:\yolov8\images\bus.jpg" results = model.predict(img_path, device="cuda") # 或使用 "cpu"

    保存预测结果

    results.save()

  2. Assistant

    Your code snippet demonstrates how to use the YOLOv5 model for object detection with PyTorch. However, it appears that you're using an import path for YOLO that would not work unless you have set it up correctly in your Python environment. Below is a modified version of your code with explanations and checks:

    Code Explanation

    1. Library Imports: Ensure that the required YOLOv5 module is installed and available in your Python environment.

    2. Loading the Model: The model is loaded using a specific file, which should be available in your working directory or correctly pointed to.

    3. Image Path: Make sure the image path is correct and accessible.

    4. Performing Inference: Running the model for inference on the specified image.

    5. Saving Results: The results of the prediction will be saved to the default directory.

    Updated Code Example

    Make sure that when you run this code, you have the correct environment with the necessary packages installed (torch, opencv-python, etc.). Here is an updated version of your code:

    import torch
    from yolov5 import YOLO  # Make sure YOLO module is correctly imported
    
    # Load the pre-trained model (YOLOv5s)
    model = YOLO('yolov5s.pt')  # Ensure yolov5s.pt is in the current directory or specify full path
    
    # Define the path to the input image
    img_path = "D:\\yolov5\\images\\bus.jpg"  # Adjust path according to your file structure
    
    # Run inference
    results = model.predict(img_path, device="cuda" if torch.cuda.is_available() else "cpu")
    
    # Save the prediction results
    results.save()  # Saves to the 'runs/detect/exp' by default
    

    Important Notes

    1. Device Selection: The code checks if CUDA is available to use the GPU; otherwise, it falls back to the CPU. This is good practice for ensuring that your code can run on different setups.

    2. Image Path: Make sure that the image path is correctly formatted and that your image is actually located in that directory. You may need to use double backslashes (\\) or a raw string (r"D:\yolov5\images\bus.jpg") to prevent issues with escape characters in paths.

    3. YOLOv5 Module: Ensure you have the YOLOv5 repository cloned or installed correctly. You can do this by following the instructions from the official YOLOv5 GitHub repository.

    4. Results Saving Location: If you want to customize the location where results are saved, check the YOLO documentation to see if there are parameters available for that.

    5. Dependencies: Make sure you have all the necessary dependencies installed. You might need to set up a Python virtual environment and install required packages using pip if you haven't done so already.

    Example of Installing Dependencies

    pip install torch torchvision torchaudio  # Make sure to install the version compatible with your CUDA version
    pip install -r requirements.txt  # Install additional dependencies from YOLOv5 requirements
    

    Follow these guidelines, and you should be able to get your object detection script running correctly! If you encounter errors or issues, please provide specific details so I can assist you further.

  3. User

    PS F:\yolo8v> & f:/yolo8v/.venv/Scripts/python.exe f:/yolo8v/yp.py Traceback (most recent call last): File "f:\yolo8v\yp.py", line 2, in from yolov5 import YOLO # 确保YOLO模块正确导入 ^^^^^^^^^^^^^^^^^^^^^^^ ImportError: cannot import name 'YOLO' from 'yolov5' (F:\yolo8v.venv\Lib\site-packages\yolov5_init_.py)

  4. Assistant

    The error you're encountering indicates that the YOLO class or function cannot be imported from the yolov5 module. This could be due to several reasons, including how the YOLOv5 package is structured in the version you're using. Here's how to troubleshoot this issue:

    1. Check YOLOv5 Installation

    First, ensure that you have the YOLOv5 module installed correctly. The yolov5 repository from Ultralytics GitHub does not provide a direct from yolov5 import YOLO statement.

    You should instead run the YOLOv5 script from the command line or modify your import to follow the structure of the YOLOv5 package. A common structure involves the detect.py script for predictions.

    2. Use YOLOv5 from the Command Line

    Typically, you can use YOLOv5 directly from the command line. Here's an example command you might run:

    python detect.py --source "f:/yolo8v/images/bus.jpg" --weights "yolov5s.pt" --device "cuda"  # or device "cpu"
    

    3. Using YOLOv5 as a Library

    If you want to use it as a library within a script, you should refer to the specific functions for loading the model and performing inference. Here's how you can do it with YOLOv5:

    import torch
    from pathlib import Path
    import cv2  # For image handling
    
    # Load the model
    model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)  # Load the model from hub
    
    # Define the path to the input image
    img_path = "f:/yolo8v/images/bus.jpg"  # Ensure this path is correct
    
    # Perform inference
    results = model(img_path)  # You can pass the image path directly
    
    # Save the results, this will save to runs/detect/exp by default
    results.save()  # Save results to 'runs/detect/exp'
    

    4. Confirm YOLOv5 Directory Structure

    If you cloned the YOLOv5 repository directly from GitHub, ensure your script has access to the appropriate paths. Here's how to set things up if you're using clone:

    1. Clone the YOLOv5 repository:
    git clone https://github.com/ultralytics/yolov5.git
    cd yolov5
    
    1. Install the required dependencies from the requirements.txt file:
    pip install -r requirements.txt
    
    1. Make sure any custom scripts you write (like yp.py) are either placed inside the cloned YOLOv5 directory or properly reference the YOLOv5 modules.

    5. Run the Script Again

    After making the necessary corrections, try running your script again. If you continue to face issues, please let me know the specific version of YOLO you are using and any other details about your setup.

  5. User

    中文回答

  6. Assistant

    看起来你遇到的错误是因为试图从yolov5模块中导入YOLO类,但是该类并不存在于你安装的YOLOv5版本中。下面是一些进一步的排查和解决建议:

    1. 检查YOLOv5的安装

    YOLOv5的安装应该确保你按照以下步骤正确完成:

    • 从Ultralytics的GitHub仓库获得YOLOv5:

      git clone https://github.com/ultralytics/yolov5.git
      cd yolov5
      
    • 然后安装所需的依赖:

      pip install -r requirements.txt
      

    2. 使用YOLOv5命令行

    你可以直接在命令行中运行YOLOv5脚本进行推理,不需要单独导入YOLO类。以下是一个示例命令,用于检测图片中的对象:

    python detect.py --source "f:/yolo8v/images/bus.jpg" --weights "yolov5s.pt" --device "cuda"  # 或者使用 --device "cpu"
    

    3. 在Python脚本中使用YOLOv5

    如果你希望在Python脚本中使用YOLOv5进行预测,可以按如下方式进行:

    import torch
    from pathlib import Path
    import cv2  # 可用于处理图像
    
    # 从网络加载预训练模型
    model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)  # 加载轻量模型
    
    # 定义输入图片路径
    img_path = "f:/yolo8v/images/bus.jpg"  # 确保路径正确
    
    # 进行推理
    results = model(img_path)  # 直接传入图片路径
    
    # 保存预测结果,默认保存到 runs/detect/exp
    results.save()  # 将结果保存到 'runs/detect/exp'
    

    4. 确认YOLOv5的目录结构

    如果你直接克隆了YOLOv5仓库,请确保你的脚本能够访问到相关模块。你可以将自定义脚本(如yp.py)放在克隆的YOLOv5目录内,或者正确引用YOLOv5模块。

    5. 重新运行脚本

    完成上述更改后,请再次尝试运行你的Python脚本。如果依然出现问题,请提供更多关于你的环境和YOLOv5版本的信息,这样我可以更好地帮助你解决问题。

  7. User

    PS F:\yolo8v\yolov5> & f:/yolo8v/.venv/Scripts/python.exe f:/yolo8v/yp.py Using cache found in C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master YOLOv5 2025-1-4 Python-3.11.9 torch-2.5.1+cu118 CUDA:0 (NVIDIA GeForce GTX 760, 2048MiB)

    Traceback (most recent call last): File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 70, in _create model = DetectMultiBackend(path, device=device, fuse=autoshape) # detection model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\common.py", line 489, in init model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\experimental.py", line 99, in attempt_load ckpt = (ckpt.get("ema") or ckpt["model"]).to(device).float() # FP32 model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 1143, in float return self._apply(lambda t: t.float() if t.is_floating_point() else t) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\yolo.py", line 208, in _apply self = super()._apply(fn) ^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 927, in _apply param_applied = fn(param) ^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 1143, in return self._apply(lambda t: t.float() if t.is_floating_point() else t) ^^^^^^^^^ RuntimeError: CUDA error: no kernel image is available for execution on the device CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1 Compile with TORCH_USE_CUDA_DSA to enable device-side assertions.

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 85, in _create model = attempt_load(path, device=device, fuse=False) # arbitrary model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\experimental.py", line 99, in attempt_load ckpt = (ckpt.get("ema") or ckpt["model"]).to(device).float() # FP32 model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 1143, in float return self._apply(lambda t: t.float() if t.is_floating_point() else t) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\yolo.py", line 208, in _apply self = super()._apply(fn) ^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 927, in _apply param_applied = fn(param) ^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 1143, in return self._apply(lambda t: t.float() if t.is_floating_point() else t) ^^^^^^^^^ RuntimeError: CUDA error: no kernel image is available for execution on the device CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1 Compile with TORCH_USE_CUDA_DSA to enable device-side assertions.

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last): File "f:\yolo8v\yp.py", line 6, in model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) # 加载轻量模型 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\hub.py", line 647, in load model = _load_local(repo_or_dir, model, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\hub.py", line 676, in _load_local model = entry(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 215, in yolov5s return _create("yolov5s", pretrained, channels, classes, autoshape, _verbose, device) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 103, in _create raise Exception(s) from e Exception: CUDA error: no kernel image is available for execution on the device CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1 Compile with TORCH_USE_CUDA_DSA to enable device-side assertions. . Cache may be out of date, try force_reload=True or see https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading for help.

  8. Assistant

    你遇到的错误信息表明 CUDA 出现了问题,具体错误为“no kernel image is available for execution on the device”。这是一个常见的错误,通常是由于以下几个原因造成的:

    解决方法:

    1. 检查显卡兼容性: 你的显卡是 NVIDIA GeForce GTX 760,这是一款较旧的显卡,可能在模型编译时不是完全支持最新的 CUDA 版本。确保你安装的 PyTorch 和 CUDA 版本与显卡兼容。

      你可以在 PyTorch 官网 选择与 GTX 760 兼容的 CUDA 版本进行安装。

    2. 使用 CPU 模式: 如果 GPU 无法工作或者你不需要使用 GPU,可以尝试使用 CPU。只需在模型加载时指定使用 CPU:

      model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, device='cpu')
      

      这样可以确定问题是由 CUDA 引起的,而不是其他问题。

    3. 更新驱动程序: 确保你的 NVIDIA 驱动程序是最新的。旧版驱动程序可能会导致兼容性问题。访问 NVIDIA 官方网站 下载和安装最新驱动程序。

    4. 强制重新加载缓存: 你可以尝试使用 force_reload=True 来强制重新加载模型,有时候缓存中的旧数据可能会导致问题:

      model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, force_reload=True)
      
    5. 设置 CUDA_LAUNCH_BLOCKING: 可以尝试设置环境变量 CUDA_LAUNCH_BLOCKING=1 以获得更详细的错误信息,在 Windows 的命令提示符中运行:

      set CUDA_LAUNCH_BLOCKING=1
      

      然后再运行你的 Python 脚本。这会使错误信息更加详细,有助于调试问题。

    参考文献:

    如果上述步骤都未能解决问题,请随时提供更多细节,我会协助你进一步排查问题。

  9. User

    PS F:\yolo8v> & f:/yolo8v/.venv/Scripts/python.exe f:/yolo8v/yp.py Using cache found in C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master YOLOv5 2025-1-4 Python-3.11.9 torch-2.5.1+cu118 CUDA:0 (NVIDIA GeForce GTX 760, 2048MiB)

    Traceback (most recent call last): File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 70, in _create model = DetectMultiBackend(path, device=device, fuse=autoshape) # detection model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\common.py", line 489, in init model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\experimental.py", line 99, in attempt_load ckpt = (ckpt.get("ema") or ckpt["model"]).to(device).float() # FP32 model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 1143, in float return self._apply(lambda t: t.float() if t.is_floating_point() else t) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\yolo.py", line 208, in _apply self = super()._apply(fn) ^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 927, in _apply param_applied = fn(param) ^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 1143, in return self._apply(lambda t: t.float() if t.is_floating_point() else t) ^^^^^^^^^ RuntimeError: CUDA error: no kernel image is available for execution on the device CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1 Compile with TORCH_USE_CUDA_DSA to enable device-side assertions.

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 85, in _create model = attempt_load(path, device=device, fuse=False) # arbitrary model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\experimental.py", line 99, in attempt_load ckpt = (ckpt.get("ema") or ckpt["model"]).to(device).float() # FP32 model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 1143, in float return self._apply(lambda t: t.float() if t.is_floating_point() else t) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\models\yolo.py", line 208, in _apply self = super()._apply(fn) ^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 900, in _apply module._apply(fn) File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 927, in _apply param_applied = fn(param) ^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\nn\modules\module.py", line 1143, in return self._apply(lambda t: t.float() if t.is_floating_point() else t) ^^^^^^^^^ RuntimeError: CUDA error: no kernel image is available for execution on the device CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1 Compile with TORCH_USE_CUDA_DSA to enable device-side assertions.

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last): File "f:\yolo8v\yp.py", line 6, in model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) # 加载轻量模型 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\hub.py", line 647, in load model = _load_local(repo_or_dir, model, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\yolo8v.venv\Lib\site-packages\torch\hub.py", line 676, in _load_local model = entry(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 215, in yolov5s return _create("yolov5s", pretrained, channels, classes, autoshape, _verbose, device) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\chuuimc/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 103, in _create raise Exception(s) from e Exception: CUDA error: no kernel image is available for execution on the device CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1 Compile with TORCH_USE_CUDA_DSA to enable device-side assertions. . Cache may be out of date, try force_reload=True or see https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading for help.

  10. Assistant

    根据你提供的错误信息,CUDA 报告了“no kernel image is available for execution on the device”错误,这通常意味着模型的 CUDA 内核与当前显卡不兼容。由于你使用的 NVIDIA GeForce GTX 760 是一款较旧的显卡,这可能导致以下几个问题:

    解决方案

    1. 检查 CUDA 和 PyTorch 版本一致性: 确保你安装的 PyTorch 版本与 CUDA 驱动程序兼容。请访问 PyTorch 官网上的安装页面 来选择与 GTX 760 兼容的版本。对于较旧的 CUDA 版本,你可能需要使用更旧的 PyTorch 版本。

    2. 强制使用 CPU: 如果你无法解决 CUDA 的问题,可以尝试强制使用 CPU,而不是 GPU。这可以帮助你确认问题是否出在 GPU 上。修改你的代码如下:

      import torch
      
      # 强制使用 CPU
      model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, device='cpu')
      

      如果 CPU 模式运行正常,那么问题很可能出在 CUDA 的兼容性上。

    3. 检查 NVIDIA 驱动程序: 确保你的 NVIDIA 驱动程序是最新的。访问 NVIDIA 官方网站 下载并安装最新驱动程序。

    4. 使用 force_reload: 你可以尝试使用 force_reload=True 重新加载模型,确保没有缓存的问题:

      model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, force_reload=True)
      
    5. 设置环境变量: 如果错误依然存在,可以尝试设置环境变量 CUDA_LAUNCH_BLOCKING=1 以帮助调试:

      在命令行中运行:

      set CUDA_LAUNCH_BLOCKING=1
      

      然后再运行你的 Python 脚本。这可以帮助你获得更加详细的错误信息。

    6. 考虑降低模型版本: 由于 GTX 760 是较旧的显卡,建议你尝试使用更小或更旧的 YOLO 模型(如 YOLOv5n)。例如:

      model = torch.hub.load('ultralytics/yolov5', 'yolov5n', pretrained=True, device='cuda')  # 或使用 'cpu'
      

    总结

    如果你仍然遇到问题,请确保你的硬件和软件配置能够满足 YOLOv5 的要求。如果可能,考虑更新显卡或者使用支持 CUDA 的新设备。如果以上步骤不能解决问题,提供更多的系统和软件版本信息,我会尽力帮助你进一步排查问题。