0、准备工作
0.1 gcc降级
因为Ubuntu20.04自带的gcc版本为9(或者不自带gcc),而cuda10不支持gcc-9,因此要手动安装gcc-7,命令如下:
sudo apt-get install gcc-7 g++-7
设置优先使用gcc7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 9
恢复优先使用gcc9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 1
查看当前优先使用的版本:
sudo update-alternatives --display g++
0.2 github克隆太慢问题
https://blog.csdn.net/weixin_44684139/article/details/104541484
上述连接即可完成准备。当然可以先往下看,遇到克隆慢的问题再回来。
===============================================================================
1、配置pytorch深度学习环境
1.1 下载anoconda并设置
1.1.1 下载anoconda
感谢本blog引路:https://blog.csdn.net/qq_42779103/article/details/108754887
Anaconda是可以便捷获取包且对包能够进行管理,同时对环境可以统一管理的发行版本。Anaconda包含了conda、Python在内的超过180个科学包及其依赖项。
推荐在清华源进行下载,我选择的是最新的Anaconda3-2020.07-Linux-x86_64.sh,注意版本。
在上述.sh下载路径下:
sh Anaconda3-2020.07-Linux-x86_64.sh
一直按回车直到下方界面,提示安装路径,这里选择默认路径,直接回车即可
然后提示是否添加环境变量,这里直接回车
直至安装完毕,接下来验证是否安装成功:
source ~/.bashrc
python
显示:
1.1.2 conda换源
gedit ~/.condarc
在文件中加入:
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- defaults
show_channel_urls: true
1.1.3 conda创建新环境
我们可以使用conda创建一个新环境,在新环境中安装各种依赖库和使用代码。因此使用如下命令新建一个名称为“nlp”的环境:
conda create --name nlp python=3.8
可以使用source activate nlp
来激活环境,可以使用source deactivate
来退出环境。
让我们source activate nlp
,此后安装pytorch均在此环境中。
在安装pytorch之前,我们先配置显卡。
1.2 ubuntu20.04配置CUDA和CUDNN
1.2.1 分析
深度学习是需要显卡加持的。因此首先处理有关显卡的事项。
对于ubuntu20.04来说,对于英伟达的显卡已经能够很好的兼容。也就是说,电脑装上ubuntu20,04后直接自带英伟达的显卡驱动。因此在“设置”-“关于”中能够显示英伟达的显卡型号:
同样的,在“软件与更新”-“附加驱动”中,也能获取显卡驱动:
这是个很舒服的事情。如果是18或者16版本的ubuntu,通常无法直接识别英伟达的显卡。本文基于ubuntu20.04,因此默认已经装好显卡驱动。若需要手动安装显卡驱动则参考:https://blog.csdn.net/weixin_44684139/article/details/109063845
此时在终端输入:
nvidia-smi
输出
可以看到,显卡支持CUDA10.2以下的版本。
1.2.2 配置CUDA
这里选择了CUDA10.0。在英伟达官网中寻找10.0的包:
选择18.04的即可(虽然我们的系统是20.04)
下载最左侧的runfile格式最为方便。
下载后,进入下载目录,在终端中输入:
sudo sh cuda_10.0........_linux.run(刚下载的文件名)
先连续回车看完协议,然后输入accept
接下来会有一系列提示,提示是否安装显卡驱动那里选no。(Graphics Driver)其他的均为yes。默认的东西直接回车。
安装完毕后,配置环境变量:
gedit ~/.bashrc
加入以下内容:
# add cuda
export PATH=/usr/local/cuda-10.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64:$LD_LIBRARY_PATH
在终端输入:
source ~/.bashrc
刷新一下环境变量。然后检查cuda是否安装成功:
nvcc -V
出现信息即安装成功。
然后再编译测试:
cd ~/NVIDIA_CUDA-10.0_Samples/0_Simple/vectorAdd
make
sudo ./vectorAdd
提示测试通过
1.2.3 配置CUDNN
进入cudnn网址,这里选择了7.4.1版本。注意这里需要注册登陆。
下载下来后,解压。在文件夹中有cuda/include和cuda/lib64两个文件夹。将其中的文件拷贝到/usr/local/cuda/lib64/和/usr/local/cuda/include/之下:
cd cuda
sudo cp lib64/* /usr/local/cuda/lib64/
sudo cp include/* /usr/local/cuda/include/
CUDNN配置完毕。
1.3 安装pytorch
可以在官网下载。但是这里直接命令行下载:
注意首先激活nlp环境:
source activate nlp
安装:
conda install pytorch torchvision cudatoolkit=10.0
由于更换了conda源,因此下载会很快。但是注意,正是因为换源了,所以命令末尾没有-c pytorch。这一点和官网有差
注意,上述命令默认安装pytorch1.4
接下来测试以下:
在终端:
python
然后
>>> import torch
>>>
>>> import torchvision
>>>
>>> torch.cuda.is_available()
True
显示true,证明pytorch成功识别cuda
或:
Python 3.8.5 (default, Sep 4 2020, 07:30:14)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> a = torch.cuda.is_available()
>>> print(a)
True
>>>
>>> ngpu= 1
>>> # Decide which device we want to run on
>>> device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
>>> print(device)
cuda:0
>>> print(torch.cuda.get_device_name(0))
GeForce RTX 2070
>>> print(torch.rand(3,3).cuda())
tensor([[0.5773, 0.6857, 0.2050],
[0.6136, 0.4209, 0.6294],
[0.0319, 0.5956, 0.8974]], device='cuda:0')
以上,准备工作已经完毕。感谢博客:https://blog.csdn.net/qq_42779103/article/details/108754887
================================================================================
2、SSD目标检测
2.1 克隆并修改源码
首先克隆源码:
git clone https://github.com/amdegroot/ssd.pytorch.git
下载数据集:
sh data/scripts/VOC2007.sh
sh data/scripts/VOC2012.sh
下载预训练模型:
mkdir weights
cd weights
wget https://s3.amazonaws.com/amdegroot-models/vgg16_reducedfc.pth
wget https://s3.amazonaws.com/amdegroot-models/ssd300_mAP_77.43_v2.pth
wget https://s3.amazonaws.com/amdegroot-models/ssd_300_VOC0712.pth
修改源码:
- 在multibox_loss.py:中调换第97,98行:
loss_c = loss_c.view(num, -1)
loss_c[pos] = 0 # filter out pos boxes for now
- 修改第114行为:
N = num_pos.data.sum().double()
loss_l = loss_l.double()
loss_c = loss_c.double()
- tain.py中的183和184行的data[0]改为item():
loc_loss += loss_l.item() # 修改语句,原先语句 为:loc_loss += loss_l.data[0]
conf_loss += loss_c.item() # 修改语句,原先语句 为:conf_loss += loss_c.data[0]
- 188行的data[0]改为item()
print('iter ' + repr(iteration) + ' || Loss: %.4f ||' % (loss.item()), end=' ') # 类似修改
- demo/live.py中第十行用…/找到上一级目录
parser.add_argument('--weights', default='../weights/ssd_300_VOC0712.pth',
2.2 运行代码
可以train一下,也可以eval一下。直接在终端的nlp环境中:
python train.py
python eval.py
也可以运行demo/live.py进行摄像头识别:
python live.py
感谢:
https://blog.csdn.net/qq_40903958/article/details/104553199
https://blog.csdn.net/qq_39571318/article/details/89788687?utm_source=distribute.pc_relevant.none-task
2.3 使用pytorch1.6运行SSD需要修改的地方:
上述使用1.4运行,不会产生报错。但如果使用1.6运行,会产生如下报错:
RuntimeError: Legacy autograd function with non-static forward method
is deprecated. Please use new-style autograd function with static
forward method. (Example:
https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function)
则需要修改的地方:
(1)layers/functions/detection.py修改为如下:
"""
Copyright (c) 2017 Max deGroot, Ellis Brown
https://github.com/amdegroot/ssd.pytorch
Updated by: Sayak Banerjee
"""
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from torch.autograd import Function
from torch.autograd import Variable
from ..box_utils import decode, nms
from data import voc as cfg
class Detect(Function):
@staticmethod
def forward(self, num_classes, bkg_label, top_k, conf_thresh, nms_thresh, loc_data, conf_data, prior_data):
self.num_classes = num_classes
self.background_label = bkg_label
self.top_k = top_k
# Parameters used in nms.
self.nms_thresh = nms_thresh
if nms_thresh <= 0:
raise ValueError('nms_threshold must be non negative.')
self.conf_thresh = conf_thresh
self.variance = cfg['variance']
num = loc_data.size(0) # batch size
num_priors = prior_data.size(0)
output = torch.zeros(num, self.num_classes, self.top_k, 5)
conf_preds = conf_data.view(num, num_priors,
self.num_classes).transpose(2, 1)
# Decode predictions into bboxes.
for i in range(num):
decoded_boxes = decode(loc_data[i], prior_data, self.variance)
# For each class, perform nms
conf_scores = conf_preds[i].clone()
#num_det = 0
for cl in range(1, self.num_classes):
c_mask = conf_scores[cl].gt(self.conf_thresh)
scores = conf_scores[cl][c_mask]
if scores.size(0) == 0:
continue
l_mask = c_mask.unsqueeze(1).expand_as(decoded_boxes)
boxes = decoded_boxes[l_mask].view(-1, 4)
# idx of highest scoring and non-overlapping boxes per class
ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)
output[i, cl, :count] = \
torch.cat((scores[ids[:count]].unsqueeze(1),
boxes[ids[:count]]), 1)
flt = output.contiguous().view(num, -1, 5)
_, idx = flt[:, :, 0].sort(1, descending=True)
_, rank = idx.sort(1)
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
return output
(2)ssd.py中
if phase == 'test':
self.softmax = nn.Softmax(dim=-1)
self.detect = Detect(num_classes, 0, 200, 0.01, 0.45)
修改为
if phase == 'test':
self.softmax = nn.Softmax()
self.detect = Detect()
(2)ssd.py中def forward函数中
if self.phase == "test":
output = self.detect(
loc.view(loc.size(0), -1, 4), # loc preds
self.softmax(conf.view(conf.size(0), -1,
self.num_classes)), # conf preds
self.priors.type(type(x.data)) # default boxes
)
修改为
if self.phase == "test":
output = self.detect.apply(21, 0, 200, 0.01, 0.45,
loc.view(loc.size(0), -1, 4), # loc preds
self.softmax(conf.view(-1,
21)), # conf preds
self.priors.type(type(x.data)) # default boxes
)
3、YOLOV5目标检测
3.1 测试代码
先克隆下载:
https://github.com/ultralytics/yolov5
在yolov5文件夹中:
conda activate nlp
python detect.py --source ./inference/images/ --weights yolov5s.pt --conf 0.4
3.2 问题
yolov5是基于pytorch1.6训练的模型。因此必须使用pytorch1.6进行加载,版本错误则会报错:
yolov5 RuntimeError: version_ <= kMaxSupportedFileFormatVersion INTERNAL ASS ........
所以可以新建环境后,安装pytorch1.6
4、补充
在环境中安装包:conda install …
也可以使用pip工具。首先在电脑中下载pip:
sudo apt-get install python3-pip
使用pip下载,比如下载cv2和yaml:
pip3 install opencv-python
pip3 install pyyaml
pip换源
https://blog.csdn.net/sss_369/article/details/99177432