[Python] Trouble using GPU of the school computer [closed]

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    So im relatively new to all of this and I am required to program a gait analysis for multiple people using Yolov7. The program is supposed to use the GPU of the computer but it can only detect the CPU. My Cuda version is 12.2 and the model of the GPU is NVIDIA Geforce RTX 2080 Ti. May I know what is the possible issue? Whether is it the codes or the PC.
    "" Python

    # Import necessary libraries
    import torch

    # Function to load YOLOv7 model
    def load_yolov7_model(model_path='yolov7-w6-pose.pt'):
    """
    Function to load the YOLOv7 model.

    Args:
    model_path (str): Path to the model file (default: 'yolov7-w6-pose.pt').

    Returns:
    yolo_model: Loaded YOLOv7 model.
    device: The device where the model is loaded (GPU or CPU).
    """
    try:
    # Check if GPU is available and set the device
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print(f"Loading model on device: {device}")

    # Load the YOLOv7 model
    yolo_model = torch.load(model_path, map_location=device)['model']
    yolo_model.float().eval() # Set to evaluation mode

    # Move model to GPU if available
    if torch.cuda.is_available():
    yolo_model.half().to(device)

    print("Model loaded successfully.")
    return yolo_model, device

    except Exception as e:
    print(f"Error loading model: {e}")
    return None, None

    # Example usage
    if __name__ == "__main__":
    model_path = 'yolov7-w6-pose.pt' # Ensure the path to your model file is correct
    model, device = load_yolov7_model(model_path)

    if model is None:
    print("Failed to load model.")
    else:
    print("Model ready for inference.")

    Continue reading...

Compartilhe esta Página