1. 在程序中调用gpu加速,有两种方法
1.1方法1 终端运行程序
程序要想调用gpu推理,依靠的是CUDA和cudnn做桥梁,所以程序运行的环境必须要能够索引到cuda和cudnn的路径。一般我们在安装CUDA和cudnn的时候,都会在.bashrc里面加上这么一组路径:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64
export PATH=$PATH:/usr/local/cuda/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda
这就帮助我们在终端中运行py程序时,就可以自动索引cuda路径,从而调用gpu。
1.2方法2 在pycharm中运行程序
pycharm大家安装方式各有不同,如果在终端中使用pycharm-2021.3/bin/pycharm.sh 打开,那么也会自动加载cuda路径,但是如果是用别的方式打开,可能就会无法加载。此时可以改为使用pycharm-2021.3/bin/pycharm.sh 打开,为了方便期间,可以创建一个pycharm.desktop文件,文件内容为(需要注意,Exec和Icon需要改成自己pycharm的对应路径):
[Desktop Entry]
Name=pycharm
Comment=Code Editing. Redefined.
GenericName=Text Editor
Exec=/opt/pycharm-2021.3/bin/pycharm.sh
Icon=/opt/pycharm-2021.3/bin/pycharm.png
Type=Application
StartupNotify=false
StartupWMClass=Code
Categories=TextEditor;Development;IDE;
MimeType=text/plain;inode/directory;application/x-code-workspace;
Actions=new-empty-window;
Keywords=pycharm;
[Desktop Action new-empty-window]
Name=New Empty Window
Exec=/opt/pycharm-2021.3/bin/pycharm.sh --new-window %F
Icon=/opt/pycharm-2021.3/bin/pycharm.png
保存后,修改权限后复制到/usr/share/applications/目录下。
sudo chmod 777 pycharm.desktop
sudo cp pycharm.desktop /usr/share/applications/
然后打开开始菜单就会出现pycharm的图标,打开后可以添加到dock栏。
1.3 gpu测试程序
安装了tensorflow2.2.0:
import os
from tensorflow.python.client import device_lib
import tensorflow as tf
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"
if __name__ == "__main__":
print(device_lib.list_local_devices())
flag = tf.test.is_gpu_available()
if flag:
print('GPU is available~~')
else:
print("no GPU!!!")
运行后会显示GPU的信息,说明读到了GPU信息。
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 5098004230979266591
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 3107575638754036874
physical_device_desc: "device: XLA_CPU device"
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 14659935295890188920
physical_device_desc: "device: XLA_GPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 5689053024
locality {
bus_id: 1
links {
}
}
incarnation: 16846892936777864981
physical_device_desc: "device: 0, name: NVIDIA GeForce GTX 1660 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5"
]
GPU is available~~
Process finished with exit code 0
2. 使用tensorflow调用gpu遇到过的问题
2.1 错误1
运行一个推理程序后,报错:
tensorflow.python.framework.errors_impl.UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[node TrajectoryNet18/navigation_net/conv2d/Conv2D (defined at /study/Lidar_Based_Navigation/lidar_trajectory/modules/navigation_net.py:31) ]] [Op:__inference_predict_4030]
Errors may have originated from an input operation.
Input Source operations connected to node TrajectoryNet18/navigation_net/conv2d/Conv2D:
maps (defined at /study/Lidar_Based_Navigation/lidar_trajectory/scripts/ros_test_polar.py:701)
Function call stack:
predict
报错原因: 显存吃满了,需要加一个语句:
import tensorflow as tf
gpus= tf.config.experimental.list_physical_devices('GPU')
print(gpus)
tf.config.experimental.set_memory_growth(gpus[0], True)
|