[Solved] CUDA error:-UserWarning: CUDA initialization: CUDA unknown error

When running yolov5, run the script after installing pytorch

import torch

An error occurred:

UserWarning: CUDA initialization: CUDA unknown error - this may be due to an incorrectly set up environment, 
e.g. changing env variable CUDA_VISIBLE_DEVICES after program start. Setting the available devices to be zero.
 (Triggered internally at  /opt/conda/conda-bld/pytorch_1623448255797/work/c10/cuda/CUDAFunctions.cpp:115.)
  return torch._C._cuda_getDeviceCount() > 0

First, check whether the versions of the graphics card driver, CUDA, cudnn and pytorch match. If not, uninstall and reinstall the corresponding version.

CUDA10.2 Python3.8 pytorch1.8 no mistake.

If the versions are correct, you need to set the environment variable and enter sudo vim ~/.bashrc, add at the end:

# The first three lines are required when installing CUDA
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_HOME=/usr/local/cuda/bin


Save and exit. Try whether you can use CUDA.

If not, enter apt-get install NVIDIA-modprobe, and there should be no problem.

It will be OK when you installed  apt-get install NVIDIA-modprobe

Read More: