GPU端到端目标检测YOLOV3全过程(下)
GPU端到端目標(biāo)檢測YOLOV3全過程(下)
Ubuntu18.04系統(tǒng)下最新版GPU環(huán)境配置
·
安裝顯卡驅(qū)動
·
安裝Cuda 10.0
·
安裝cuDNN
1.安裝顯卡驅(qū)動
(1)這里采用的是PPA源的安裝方式,首先添加Graphic
Drivers的PPA源,打開終端輸入以下指令代碼(添加PPA源并更新):
sudo add-apt-repository ppa:graphics-drivers/ppasudo apt-get update
(2)使用命令行自動查看合適的驅(qū)動版本,系統(tǒng)會自動查找并給出推薦的驅(qū)動版本,鍵入如下代碼:
ubuntu-drivers devices· 1
運行以上代碼結(jié)果如下圖所示:
上面的結(jié)果給出了可供選擇的驅(qū)動版本,其中推薦的是最高版本nvidia-driver-440,下面通過命令行進行安裝該版本驅(qū)動;
(3)在終端鍵入以下代碼安裝對應(yīng)驅(qū)動(這里輸入上面系統(tǒng)推薦的驅(qū)動版本):
sudo apt-get install nvidia-driver-440· 1
安裝如圖:
(4)驅(qū)動安裝完成后使之生效需要重啟計算機,可手動重啟也可輸入如下命令:
sudo reboot· 1
(5)待重啟完成可檢測驅(qū)動是否安裝成功,在終端輸入以下代碼:
sudo nvidia-smi· 1
如果安裝成功會出現(xiàn)如下圖的信息顯示,這里可以看到顯卡的型號及顯存利用情況等信息,如我這里是個人電腦的兩塊1080顯卡:
- 安裝Cuda 10.0
(1)點擊進入英偉達驅(qū)動安裝指引網(wǎng)站,在網(wǎng)站頁面上選擇要安裝的Cuda版本,這里建議安裝Cuda 10.0版本(Cuda 10.1和Cuda 9可能會出問題,安裝過程可能會麻煩些),如下圖所示點擊選擇Cuda 10.0會跳轉(zhuǎn)至具體選擇界面;
(2)在上面跳出的界面中依次選擇“Linux”、“x86_64”、“Ubuntu”、“18.04”、“deb(local)”選項,會展開Cuda 10.0的安裝指導(dǎo)頁面,在網(wǎng)站頁面上選擇的對應(yīng)的系統(tǒng)、平臺和版本等信息如下圖所示:
(3)選擇對應(yīng)版本之后頁面下方會提示下載對應(yīng)的Cuda安裝文件,如下圖點擊下載鏈接下載(該文件較大可能下載時間較長);
(4)等待上一步中的文件下載完成后按照網(wǎng)站上給出的安裝方法進行安裝,如下圖所示:
首先打開剛剛下載文件所在的文件夾,右擊‘在終端打開’(或者cd到該文件夾下),其中上圖中第二行命令中“”應(yīng)改成對應(yīng)的Cuda版本(可在/var/路徑下查找)這里是cuda-repo-10-0-local-10.0.130-410.48,因此在打開的終端應(yīng)輸入的命令如下:
sudo dpkg -i cuda-repo-ubuntu1804-10-0-local-10.0.130-410.48_1.0-1_amd64.debsudo apt-key add /var/cuda-repo-10-0-local-10.0.130-410.48/7fa2af80.pubsudo apt-get updatesudo apt-get install cuda
(4)以上步驟完成,需要重啟電腦,待重啟完成后測試一下Cuda是否安裝成功,依次輸入如下代碼:
第一步,進入例子文件cd /usr/local/cuda-10.0/samples/1_Utilities/deviceQuery# 第二步,執(zhí)行make命令sudo make# 第三步./deviceQuery
如果出現(xiàn)下圖類似的GPU信息顯示,則說明安裝成功
- 安裝cuDNN
(1)與Cuda的安裝相似,首先進入英偉達開發(fā)者網(wǎng)站點擊下載cuDNN,這時網(wǎng)頁會跳出注冊的界面點擊“Join”注冊一個賬號(想要下載cuDNN,這一步是必須的)。注冊登錄完成后,再次點擊下載就會跳轉(zhuǎn)至如下下載頁面:
注冊登錄完成后,再次點擊下載就會跳轉(zhuǎn)至如下下載頁面:
點擊上圖紅框位置展開更多下載選項,選擇與自己Cuda版本對應(yīng)的cuDNN版本點擊下載(如下圖所示),這時會彈出具體下載選項;
如下圖所示在彈出的具體下載選項中選擇紅色框標(biāo)注的三個文件,以此點擊進行下載
(2)以上文件下載完成后,進入下載文件所在文件夾并在該文件夾下打開終端(或終端輸入cd命令轉(zhuǎn)到該文件目錄下),在終端以此輸入以下命令(命令中的文件名應(yīng)與具體下載的文件名字一致):
sudo dpkg -i libcudnn7_7.5.1.10-1+cuda10.0_amd64.debsudo dpkg -i libcudnn7-dev_7.5.1.10-1+cuda10.0_amd64.debsudo dpkg -i libcudnn7-doc_7.5.1.10-1+cuda10.0_amd64.deb· 1· 2· 3
執(zhí)行如下圖
(3)同樣地安裝完成后需要測試一下是否安裝成功,在終端輸入以下代碼:
cp -r /usr/src/cudnn_samples_v7/ $HOMEcd $HOME/cudnn_samples_v7/mnistCUDNNmake clean && make./mnistCUDNN
最終如果有提示信息:“Test passed !”,則說明安裝成功,如圖所示:
至此cuDNN的安裝就宣告完成,到這里意味著深度學(xué)習(xí)GPU的環(huán)境支持已設(shè)置完畢,接下來可以進行深度學(xué)習(xí)框架如TensorFlow的安裝了。
ubuntu安裝GPU版的TensorFlow
版本對應(yīng)關(guān)系:
https://blog.csdn.net/qq_27825451/article/details/89082978
安裝顯卡版本的必須要前面的安裝步驟pip3 install tensorflow-gpu
失敗就去官網(wǎng)下載
https://tensorflow.google.cn/install/pip
然后sudo pip3 install
tensorflow…whl
測試代碼:
import tensorflow as tf with tf.Session() as sess: with tf.device(’/gpu:0’): a = tf.constant([1,2],name=‘a(chǎn)’) b = tf.constant([1,2],name=‘b’) res = a+b print(res)
ubuntu安裝gpu版的PyTorch
安裝命令:
https://pytorch.org/get-started/locally/
pip install torch torchvision
測試代碼:
import torchtorch.cuda.is_aviliable()
2 yolov3搭建GPU環(huán)境
2.1
軟件環(huán)境及插件
·
Ubuntu 16.04.5
·
wget
2.2
安裝步驟
【(1) 獲取圖像檢測訓(xùn)練模型】
git clone https://github.com/pjreddie/darknet
· 1
【(2) GPU參數(shù)配置】
進入darknet目錄cd darknet# 打開Makefile文件vim Makefile
【(3) 修改Makefile文件】
使用GPU,設(shè)為1GPU=1# 啟用CUDNN,設(shè)為1CUDNN=1
【(4) 編譯】
makemake clean(清除編譯)
【(5) 獲取訓(xùn)練模型權(quán)重】
wget https://pjreddie.com/media/files/yolov3.weights
【(6) 測試】
【(7) 結(jié)果】
【(8) GPU處理耗時】
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
具體來說(基于YOLOV3)
yolo_v3結(jié)構(gòu)圖
1:輸入一張任意大小圖片,保持長寬比不變的情況下,縮放至 w 或 h
達到416,再覆蓋在416416的新圖上,作為網(wǎng)絡(luò)的輸入。即網(wǎng)絡(luò)的輸入是一張416416,3通道的RGB圖。
2:運行網(wǎng)絡(luò)。YOLO的CNN網(wǎng)絡(luò)把圖片分成 SS 個網(wǎng)格(yolov3多尺度預(yù)測,輸出3層,每層
S * S個網(wǎng)格,分別為 1313 ,26 26 ,5252),然后每個單元格負責(zé)去檢測那些中心點落在該格子內(nèi)的目標(biāo),如圖所示。
每個單元格需要預(yù)測 3*(4+1+B)個值。如果將輸入圖片劃分為 SS 網(wǎng)格,那么每層最終預(yù)測值為 SS3(4+1+B) 大小的張量。 B:類別數(shù)(coco集為80類),即B=80.
3 為每層anchorbox數(shù)量,4 為邊界框大小和位置(x , y , w , h )1 為置信度。
3: 通過NMS,非極大值抑制,篩選出框boxes,輸出框class_boxes和置信度class_box_scores,再生成類別信息classes,生成最終的檢測數(shù)據(jù)框,并返回。
YOLOv3在feature map上對每個位置進行bbox預(yù)測。圖中,t為預(yù)測值,但是,注意這些值都是相對當(dāng)前grid的相對值,分別是(tx,ty,tw,th)。最終的預(yù)測bbox為:bx,by,bw,bh,這是在image的bbox。
loss
function
在目標(biāo)檢測任務(wù)里,有幾個關(guān)鍵信息是需要確定的: (x,y),(w,h),class,confidence(x,y)
根據(jù)關(guān)鍵信息的特點可以分為上述四類,損失函數(shù)應(yīng)該由各自特點確定。最后加到一起就可以組成最終的loss_function了,也就是一個loss_function搞定端到端的訓(xùn)練。
xy_loss
= K.sum(xy_loss) / mf
wh_loss = K.sum(wh_loss) / mf
confidence_loss = K.sum(confidence_loss) / mf
class_loss = K.sum(class_loss) / mf
loss += xy_loss + wh_loss + confidence_loss + class_loss
網(wǎng)絡(luò)結(jié)構(gòu)說明。
前言:yolo v3的結(jié)構(gòu)是融合了googlenet的inception以及resnet的shortcut。
結(jié)構(gòu)重點是resnet和googlenet,還有一點需要強調(diào)的是:最終輸出是一個三分類,分別對于大物體(5252),中物體(2626),小物體(1313)進行檢測,在train.py文件中,因為輸入的圖像大小是416416,然后,分別除以【32,16,8】,得到相應(yīng)尺寸。
用紅色做了注釋。residual使用殘差結(jié)構(gòu)。什么是殘差結(jié)構(gòu)?舉個例子在第一層殘差結(jié)構(gòu)(其輸出為208208128),其輸入為20820864,經(jīng)過3211和6433的卷積后,其生成的特征圖與輸入疊加起來。其結(jié)構(gòu)如下:
其疊加后的特征圖作為新的輸入輸入下一層。YOLO主體是由許多這種殘差模塊組成,減小了梯度爆炸的風(fēng)險,加強了網(wǎng)絡(luò)的學(xué)習(xí)能力。
多尺度:
yolov3采用多尺度預(yù)測。【(1313)(2626)(52*52)】
?小尺度:(13*13的feature map)
·
網(wǎng)絡(luò)接收一張(416416)的圖,經(jīng)過5個步長為2的卷積來進行降采樣(416 / 2?5 = 13).輸出(1313)。
?中尺度 : (26*26的feature map)
·
從小尺度中的倒數(shù)第二層的卷積層上采樣(x2,up sampling)再與最后一個13x13大小的特征圖相加,輸出(26*26)。
?大尺度: (52*52的feature map)
YOLO有3個尺度的輸出,分別在52×52,26×26,13×13。都是奇數(shù),使得網(wǎng)格會有個中心位置。同時YOLO輸出為3個尺度,每個尺度之間還有聯(lián)系。比如說,13×13這個尺度輸出用于檢測大型目標(biāo),對應(yīng)的26×26為中型的,52×52用于檢測小型目標(biāo)。
示例:
YOLO: Real-Time
Object Detection
You only look
once (YOLO) is a state-of-the-art, real-time object detection system. On a
Pascal Titan X it processes images at 30 FPS and has a mAP of 57.9% on COCO
test-dev.
Comparison to Other Detectors
YOLOv3 is
extremely fast and accurate. In mAP measured at .5 IOU YOLOv3 is on par with
Focal Loss but about 4x faster. Moreover, you can easily tradeoff between speed
and accuracy simply by changing the size of the model, no retraining required!
Performance on the COCO Dataset
the predicted
probabilities.
Our model has
several advantages over classifier-based systems. It looks at the whole image
at test time so its predictions are informed by global context in the image. It
also makes predictions with a single network evaluation unlike systems like R-CNN which require thousands
for a single image. This makes it extremely fast, more than 1000x faster than
R-CNN and 100x faster than Fast R-CNN. See our paper for more details on the
full system.
What’s New in Version 3?
YOLOv3 uses a
few tricks to improve training and increase performance, including: multi-scale
predictions, a better backbone classifier, and more. The full details are in
our paper!
Detection Using A Pre-Trained Model
This post will
guide you through detecting objects with the YOLO system using a pre-trained
model. If you don’t already have Darknet installed, you should do that first. Or instead of reading
all that just run:
git clone https://github.com/pjreddie/darknetcd darknetmake
Easy!
You already have
the config file for YOLO in the cfg/ subdirectory. You will have to
download the pre-trained weight file here (237 MB). Or just run this:
wget https://pjreddie.com/media/files/yolov3.weights
Then run the
detector!
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
You will see
some output like this:
layer filters size input output 0 conv 32 3 x 3 / 1 416 x 416 x 3 -> 416 x 416 x 32 0.299 BFLOPs 1 conv 64 3 x 3 / 2 416 x 416 x 32 -> 208 x 208 x 64 1.595 BFLOPs … 105 conv 255 1 x 1 / 1 52 x 52 x 256 -> 52 x 52 x 255 0.353 BFLOPs 106 detectiontruth_thresh: Using default '1.000000’Loading weights from yolov3.weights…Done!data/dog.jpg: Predicted in 0.029329 seconds.dog: 99%truck: 93%bicycle: 99%
Darknet prints
out the objects it detected, its confidence, and how long it took to find them.
We didn’t compile Darknet with OpenCV so it can’t display the
detections directly. Instead, it saves them in predictions.png. You can open
it to see the detected objects. Since we are using Darknet on the CPU it takes
around 6-12 seconds per image. If we use the GPU version it would be much
faster.
I’ve included
some example images to try in case you need inspiration. Try data/eagle.jpg,
data/dog.jpg,
data/person.jpg,
or data/horses.jpg!
The detect
command is shorthand for a more general version of the command. It is
equivalent to the command:
./darknet detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights data/dog.jpg
You don’t need
to know this if all you want to do is run detection on one image but it’s
useful to know if you want to do other things like run on a webcam (which you
will see later on).
Multiple Images
Instead of
supplying an image on the command line, you can leave it blank to try multiple
images in a row. Instead you will see a prompt when the config and weights are
done loading:
./darknet detect cfg/yolov3.cfg yolov3.weightslayer filters size input output 0 conv 32 3 x 3 / 1 416 x 416 x 3 -> 416 x 416 x 32 0.299 BFLOPs 1 conv 64 3 x 3 / 2 416 x 416 x 32 -> 208 x 208 x 64 1.595 BFLOPs … 104 conv 256 3 x 3 / 1 52 x 52 x 128 -> 52 x 52 x 256 1.595 BFLOPs 105 conv 255 1 x 1 / 1 52 x 52 x 256 -> 52 x 52 x 255 0.353 BFLOPs 106 detectionLoading weights from yolov3.weights…Done!Enter Image Path:
Enter an image
path like data/horses.jpg
to have it predict boxes for that image.
Once it is done
it will prompt you for more paths to try different images. Use Ctrl-C
to exit the program once you are done.
Changing The Detection Threshold
By default, YOLO
only displays objects detected with a confidence of .25 or higher. You can
change this by passing the -thresh flag to the yolo
command. For example, to display all detection you can set the threshold to 0:
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg -thresh 0
Which produces:
![][all]
So that’s
obviously not super useful but you can set it to different values to control
what gets thresholded by the model.
Tiny YOLOv3
We have a very
small model as well for constrained environments, yolov3-tiny. To use this model,
first download the weights:
wget https://pjreddie.com/media/files/yolov3-tiny.weights
Then run the
detector with the tiny config file and weights:
./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg
Real-Time Detection on a Webcam
Running YOLO on
test data isn’t very interesting if you can’t see the result. Instead of
running it on a bunch of images let’s run it on the input from a webcam!
To run this demo
you will need to compile Darknet with CUDA and OpenCV. Then run the command:
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights
YOLO will
display the current FPS and predicted classes as well as the image with
bounding boxes drawn on top of it.
You will need a
webcam connected to the computer that OpenCV can connect to or it won’t work.
If you have multiple webcams connected and want to select which one to use you
can pass the flag -c to pick (OpenCV uses webcam 0 by
default).
You can also run
it on a video file if OpenCV can read the video:
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights
That’s how we
made the YouTube video above.
Training YOLO on VOC
You can train
YOLO from scratch if you want to play with different training regimes,
hyper-parameters, or datasets. Here’s how to get it working on the Pascal VOC
dataset.
Get The Pascal VOC Data
To train YOLO
you will need all of the VOC data from 2007 to 2012. You can find links to the
data here. To get all the data,
make a directory to store it all and from that directory run:
wget https://pjreddie.com/media/files/VOCtrainval_11-May-2012.tarwget https://pjreddie.com/media/files/VOCtrainval_06-Nov-2007.tarwget https://pjreddie.com/media/files/VOCtest_06-Nov-2007.tartar xf VOCtrainval_11-May-2012.tartar xf VOCtrainval_06-Nov-2007.tartar xf VOCtest_06-Nov-2007.tar
There will now
be a VOCdevkit/
subdirectory with all the VOC training data in it.
Generate Labels for VOC
Now we need to
generate the label files that Darknet uses. Darknet wants a .txt
file for each image with a line for each ground truth object in the image that
looks like:
Where x, y, width,
and height
are relative to the image’s width and height. To generate these file we will
run the voc_label.py
script in Darknet’s scripts/ directory. Let’s just download it again because
we are lazy.
wget https://pjreddie.com/media/files/voc_label.pypython voc_label.py
After a few minutes,
this script will generate all of the requisite files. Mostly it generates a lot
of label files in VOCdevkit/VOC2007/labels/ and VOCdevkit/VOC2012/labels/.
In your directory you should see:
ls2007_test.txt VOCdevkit2007_train.txt voc_label.py2007_val.txt VOCtest_06-Nov-2007.tar2012_train.txt VOCtrainval_06-Nov-2007.tar2012_val.txt VOCtrainval_11-May-2012.tar
The text files
like 2007_train.txt
list the image files for that year and image set. Darknet needs one text file
with all of the images you want to train on. In this example, let’s train with
everything except the 2007 test set so that we can test our model. Run:
cat 2007_train.txt 2007_val.txt 2012_*.txt > train.txt
Now we have all
the 2007 trainval and the 2012 trainval set in one big list. That’s all we have
to do for data setup!
Modify Cfg for Pascal Data
Now go to your
Darknet directory. We have to change the cfg/voc.data config file to
point to your data:
1 classes= 20 2 train = /train.txt 3 valid = 2007_test.txt 4 names = data/voc.names 5 backup = backup
You should
replace
with the directory where you put the VOC data.
Download Pretrained Convolutional Weights
For training we
use convolutional weights that are pre-trained on Imagenet. We use weights from
the darknet53 model. You can just
download the weights for the convolutional layers here (76 MB).
wget https://pjreddie.com/media/files/darknet53.conv.74
Train The Model
Now we can
train! Run the command:
./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74
Training YOLO on COCO
You can train
YOLO from scratch if you want to play with different training regimes,
hyper-parameters, or datasets. Here’s how to get it working on the COCO dataset.
Get The COCO Data
To train YOLO
you will need all of the COCO data and labels. The script scripts/get_coco_dataset.sh
will do this for you. Figure out where you want to put the COCO data and
download it, for example:
cp scripts/get_coco_dataset.sh datacd databash get_coco_dataset.sh
Now you should
have all the data and the labels generated for Darknet.
Modify cfg for COCO
Now go to your
Darknet directory. We have to change the cfg/coco.data config file to
point to your data:
1 classes= 80 2 train = /trainvalno5k.txt 3 valid = /5k.txt 4 names = data/coco.names 5 backup = backup
You should
replace
with the directory where you put the COCO data.
You should also
modify your model cfg for training instead of testing. cfg/yolo.cfg
should look like this:
[net]# Testing# batch=1# subdivisions=1# Trainingbatch=64subdivisions=8…
Train The Model
Now we can
train! Run the command:
./darknet detector train cfg/coco.data cfg/yolov3.cfg darknet53.conv.74
If you want to
use multiple gpus run:
./darknet detector train cfg/coco.data cfg/yolov3.cfg darknet53.conv.74 -gpus 0,1,2,3
If you want to
stop and restart training from a checkpoint:
./darknet detector train cfg/coco.data cfg/yolov3.cfg backup/yolov3.backup -gpus 0,1,2,3
YOLOv3 on the Open Images dataset
wget https://pjreddie.com/media/files/yolov3-openimages.weights ./darknet detector test cfg/openimages.data cfg/yolov3-openimages.cfg yolov3-openimages.weights
What Happened to the Old YOLO Site?
If you are using
YOLO version 2 you can still find the site here: https://pjreddie.com/darknet/yolov2/
總結(jié)
以上是生活随笔為你收集整理的GPU端到端目标检测YOLOV3全过程(下)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GPU端到端目标检测YOLOV3全过程(
- 下一篇: NVIDIA TensorRT高性能深度