ubuntu20.04系统下anaconda构建虚拟环境同时搭建tensorflow1.0和tensorflow2.0
目录
1.ubuntu20.04与win10双系统搭建
(1)安装参考及系统资源
安装参考:
Win10+Ubuntu双系统
系统资源:这是我下载好的ubuntu20.04系统,百度网盘可下载
链接:网盘链接
提取码:2eqj
(2)开机引导项修复
在安装好ubuntu后,我发现无法正常切换到原来的win10系统,可以参考以下博客内容解决
引导项修复:
Win10+Ubuntu双系统修复Ubuntu系统引导
2.NVIDIA驱动安装
(1)找到自己电脑显卡版本所对应驱动,去官网下载安装即可
可参考linux下安装nvidia driver(1)
linux下安装nvidia drive(2)
以下是我的电脑对应下载的驱动,在官网下载.run文件然后用以下命令执行安装
sudo chmod a+x NVIDIA-Linux-x86_64-460.32.03.run
sudo ./NVIDIA-Linux-x86_64-460.32.03.run -no-nouveau-check -no-opengl-files
遇到的错误可参考以下博客
错误提示
错误提示2
我在这里安装报错后,进入BIOS关闭Secure Boot
再次安装就没问题了
安装后可以先为Ubuntu添加镜像源,这个很重要,会在之后的下载中省很多时间!!!
Ubuntu添加镜像源
记得把它添加到文件最上行
3.anaconda3安装
这里以Anaconda3-2020.02-Linux-x86_64版本为例,其它版本可在
清华软件源
中找到需要安装的版本,
我下载好的annacona3百度网盘链接如下
链接:网盘链接
提取码:7et5
下载后在终端执行以下命令
bash Anaconda3-2020.02-Linux-x86_64.sh
关于annaconda的详细安装步骤可以参考其他博客
3.1修改conda镜像源(重要)
镜像源可以使我们在anaconda中提高下载安装包的速度,不然会很慢或者下载失败
终端中执行以下命令,添加清华软件源
镜像源参考(很全)
channels:
- defaults
show_channel_urls: true
default_channels:
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
custom_channels:
conda-forge: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
通过以下代码查看,现有的下载路径
conda config --show
可以看见路径添加成功
channels:
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- defaults
4.构建不同的tensorflow虚拟环境
以tensorflow1.0为例,我需要的是python2.7的环境,所以输入以下命令
conda create -n tf1_gpu python=2.7
tf1_gpu是我自定义的环境名称,编译的python版本选择的是python2.7
由于我的ubuntu20.04系统自带的python是3.7的版本
conda会自动寻找并下载python2.7的安装包,这样我们就成功创建了python2.7的虚拟环境了。
同样我还创建了tensorflow2.0的环境,命名为tf2_gpu, python=3.7.6(我的ubuntu系统自带的python版本,这样就不用额外下载安装包了)
4.1在不同的虚拟环境下安装tensorflow1.0和2.0
首先激活我们创建好的虚拟环境,以tf1_gpu为例:
conda activate tf1_gpu
接下来在虚拟环境中安装tensorflow1.0.1
gpu版本代码如下:
conda install tensorflow-gpu=1.0.1
cpu版本代码如下:
conda install tensorflow=1.0.1
安装中,Anaconda会自动安装匹配版本的CUDA和cudnn,不用去英伟达官网下载了,也避免了版本不兼容的问题。因为有镜像源下载速度应该不会太慢。
按照同样的方法,我们可以建立不同的python版本虚拟环境,用来安装不同版本的tensorflow
段错误:是因为网络问题中断导致,要么找到未下完的包删掉,要么运行 conda clean -a
5.测试
在测试时,tensorflow可以正常编译但是却没有调用GPU来计算。之后发现是自己未安装NVIDIA和CUDA的原因。
装完驱动发现,可以正常使用了
(base) su@6:~$ conda activate tf1_gpu
(tf1_gpu) su@6:~$ cd pointnet-master/
(tf1_gpu) su@6:~/pointnet-master$ python train.py
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.7.5 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.7.5 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.7.5 locally
Tensor("Placeholder_2:0", shape=(), dtype=bool, device=/device:GPU:0)
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:910] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties:
name: GeForce MX250
major: 6 minor: 1 memoryClockRate (GHz) 1.582
pciBusID 0000:01:00.0
Total memory: 1.96GiB
Free memory: 1.74GiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0: Y
I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce MX250, pci bus id: 0000:01:00.0)
**** EPOCH 000 ****
----0-----
mean loss: 3.891339
accuracy: 0.185059
通过指令nvidia-smi
可查看GPU使用情况100%
(base) su@6:~/pointnet-master$ nvidia-smi
Tue Dec 15 19:40:04 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 455.45.01 Driver Version: 455.45.01 CUDA Version: 11.1 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 GeForce MX250 On | 00000000:01:00.0 Off | N/A |
| N/A 77C P0 N/A / N/A | 1846MiB / 2002MiB | 100% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1177 G /usr/lib/xorg/Xorg 45MiB |
| 0 N/A N/A 1852 G /usr/lib/xorg/Xorg 76MiB |
| 0 N/A N/A 2094 G /usr/bin/gnome-shell 23MiB |
| 0 N/A N/A 4370 C python 1689MiB |
+-----------------------------------------------------------------------------+