参考:vscode连接远程服务器(傻瓜式教学)

配置服务器pytorch/TensorFlow环境+远程连接vscode

在远程服务器安装anoconda并创建tensorflow-gpu环境并运行jupyter

【vscode连接远程服务器】

step 1:测试服务器连接

win+R 进入 cmd,在命令行输入以下命令,并根据提示输入密码

ssh username@severIP # 用户名@服务器IP地址

step 2:安装 ssh 插件

打开 vscode,在左侧菜单栏的 Extensions 中搜索 remote - SSH 插件,点击 Install 安装

安装完成后,在左侧的菜单栏里会新增加一个 Remote Explorer

step 3:添加服务器连接

点击“+”添加新的服务器连接,在提示框输入 ssh username@severIP, 回车,将其保存至配置文件 C:\Users\xxx\.ssh\config 中(若已有多个服务器,再次添加新的后,记得在 REMOTE 右边刷新一下)

step 4:连接服务器

右键想要连接的远程服务器,点击“Connect in Current Window...”,并根据提示输入密码

左下角显示连接成功

step 4:查看服务器文件

在左侧菜单栏的 Explorer 中点击 username [SSH: ip] 查看远程服务器目录下的文件

【Tensorflow2环境配置】

step 5:Anaconda 环境

连接成功后,在终端 TERMINAL 的右上角点击‘+’打开一个新的终端

我拿到的服务器已经安装过了 minianaconda(教程可参考置顶的链接)

配置 conda 镜像源(参考:conda安装Tensorflow和pytorch经验)

conda config --set show_channel_urls yes

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/main

conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/free

conda config --show channels

删除镜像等命令(参考:conda如何添加,删除镜像channel,管理虚拟环境,以及其他常见命令。)

conda config --remove channels https://pypi.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

step 6:安装 Tensorflow2

(1)创建虚拟环境

conda create -n tensorflow python=3.8

(2)激活虚拟环境

conda activate tensorflow

(3)搜索可用镜像

conda search tensorflow-gpu

(4)直接用 conda 安装 Tensorflow2.6.0(conda 会自动安装 cudnn、cudatoolkit)

conda install tensorflow-gpu==2.6.0

(5)测试 GPU 是否可用

python

import tensorflow as tf

tf.test.is_gpu_available()

【Tensorflow 环境配置问题汇总】

【问题 1】我刚开始是用 pip 安装的 Tensorflow2.10.0 版本,经历了各种报错。。。

pip install tensorflow-gpu==2.10.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/

conda install cudatoolkit==11.3.1

conda install cudnn=8.2.1

import tensorflow as tf 报错信息

2023-03-06 01:42:48.783815: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F AVX512_VNNI FMA

To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

2023-03-06 01:42:48.936614: I tensorflow/core/util/util.cc:169] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.

2023-03-06 01:42:48.942636: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory

2023-03-06 01:42:48.942659: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.

2023-03-06 01:42:48.969748: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered

2023-03-06 01:42:49.598911: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory

2023-03-06 01:42:49.598980: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory

2023-03-06 01:42:49.598988: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.

【解决办法】可参考这几篇文章:

小白笔记[1]| 运行tf问题整理(持续完善)

完美解决TensorFlow-gpu报错问题Could not load dynamic library ‘libnvinfer.so.6’ and ‘libcudart.so.11.0’

不要用 pip 安装(手动安装 cudatoolkit 和 cudnn),而是要用 conda 安装(自动安装 cudnn、cudatoolkit)

【问题 2】我换成 conda 来安装后,直接在 tensorflow 导入部分就报错。。。

ImportError: libflatbuffers.so.2: cannot open shared object file: No such file or directory

百度还没找到解决办法,大无语,遂放弃

我不得不放弃了 Tensorflow2.10.0,安装了 Tensorflow2.6.0,结果一次就成功了。。。

所有我前面配了两天的环境,就是全白瞎了呗。。。

【在远程服务器上运行项目】

step 1:将本地项目上传至远程服务器

(我这里使用的是 FileZilla,也可以使用 WinSCP、Xshell 等)

打开 FileZilla,在上方输入 主机、用户名、密码、端口(22) 后,点击右侧的“快速连接”

右侧的“远程站点”会列出远程服务器的文件夹,在左侧的“本地站点”中打开要传输的文件夹

将左侧本地文件夹下要上传的文件,拖拽至右侧远程服务器的文件夹下(右键可管理文件/文件夹)

最下方会显示文件传输队列,等待传输完成即可

step 2:查看服务器文件

在左侧菜单栏的 Explorer 中点击 username [SSH: ip] 查看当前远程服务器目录下的文件

step 3:配置 python 解释器

使用 Ctrl+Shift+P 打开命令面板,

输入 >Python: select Interpret,选择配置好的 Tensorflow2 环境作为远程 python 解释器

step 4:运行/调试远程项目

(1)在左侧菜单栏中点击 Run and Debug,继续点击蓝色框中的 Run and Debug 即可对当前文件进行运行/调试

(项目代码中,在行号前单击即可设置断点)

(2)当前文件右上角点击 Run/Debug Python File,选择运行/调试当前文件

本地和远端同步可参考:VScode实现本地与远端同步开发的两种方式

【注:远程项目更改文件路径】

从本地上传到远程服务器后,文件夹的层次结构发生了改变,因此可能需要改变文件路径

python os 文件路径参考:

Python--OS 方法-获取文件对应路径

python获取当前文件的目录与路径

【清理远程服务器缓存】

参考:记录:在Linux系统下清理GPU缓存

(1)使用 nvidia-smi 查看 gpu 使用情况

或者使用以下命令查看 GPU 上的进程占用

fuser -v /dev/nvidia*

(2)输入以下命令清理进程(PID 见上图,或见上行命令输出)

kill -9 PID

【在远程服务器后台运行 python 程序】

参考:python程序后台运行命令

Linux如何在后台一直运行python程序

当前终端挂掉后,远程服务器上的进程可能也会挂掉。。。

(1)输入以下命令在后台运行 python 程序(test.log 指定输出日志文件)

nohup python main.py > test.log &

注意:main.py 处一定要写全从根目录下的路径(main.py 直接在 /home/usr 根目录下)

(2)输入以下命令可以查看所有进程的状态

ps aux |grep python

每行每个参数的含义可参考:linux命令ps aux|grep 查看进程详解

Linux_ps aux指令与grep指令配合管理进程 (其中第二列为 PID)

(3)结束进程

kill -9 PID // 强制杀死进程

【conda 打包环境】

参考:Python | Conda pack 进行环境打包

conda创建的环境,打包下载,然后在其他设备离线安装环境,python部署时需要依赖,通过conda一步解决依赖迁移问题

(1)安装 conda-pack

conda install -c conda-forge conda-pack

(2)将虚拟环境 env_name 打包为 env_name.tar.gz,打包好的环境就在本文件夹下( /home/usr )

conda pack -n env_name

【pycharm 生成项目依赖】

参考:以pycharm为例,生成Python项目所需要的依赖库/包文档:requirements.txt_pycharm中如何导出虚拟环境中的相关库名称与版本号,形成相关requirements.txt文件-CSDN博客

(1)安装 pipreqs

pip install pipreqs

(2)输入以下命令,在项目根目录下生成 requirments.txt

pipreqs ./ --encoding=utf8

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: