在colab上训练yolo

由于本地计算机速度有限,所以使用Colab进行训练。训练用的是VOC类型的数据集

# 下载darknet框架,该框架用c语言编写,是yolo的基础
!git clone https://github.com/AlexeyAB/darknet
Cloning into 'darknet'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 13503 (delta 0), reused 1 (delta 0), pack-reused 13498
Receiving objects: 100% (13503/13503), 12.07 MiB | 6.55 MiB/s, done.
Resolving deltas: 100% (9222/9222), done.
# 修改makefile 将OpenCV和GPU设置为可用
%cd /content/darknet
!sed -i 's/OPENCV=0/OPENCV=1/' Makefile
!sed -i 's/GPU=0/GPU=1/' Makefile
!sed -i 's/CUDNN=0/CUDNN=1/' Makefile
/content/darknet
#查验一下CUDA版本
!/usr/local/cuda/bin/nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243
# 编译darknet
!make
mkdir -p ./obj/
mkdir -p backup
chmod +x *.sh
g++ -std=c++11 -std=c++11 -Iinclude/ -I3rdparty/stb/include -DOPENCV `pkg-config --cflags opencv4 2> /dev/null || pkg-config --cflags opencv` -DGPU -I/usr/local/cuda/include/ -DCUDNN -Wall -Wfatal-errors -Wno-unused-result -Wno-
(后面删了)
# 与谷歌云盘挂载
from google.colab import drive
drive.mount('/content/mydrive')
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly

Enter your authorization code:
··········
Mounted at /content/mydrive
# 1.新建一个yolo文件夹,用来存放所需要的各种文件
# 2.下载预训练权重
#   百度网盘链接:链接:https://pan.baidu.com/s/1WIJRWpHEA4r3Pyt7yNXLmQ 提取码:ysjx
# 3.并将下载好的文件上传到谷歌云盘的yolo文件夹下
# 查看当前目录下是否有权重文件
!ls /content/mydrive/My\ Drive/yolo
# 由于目标文件的路径太长,所以简写路径
!ln -s /content/mydrive/My\ Drive/ /mydrive
1111.jpg			   obj.zip
backup				   train.txt
Gao_ship_hh_02016082545020305.jpg  yolo_1.ipynb
generate_train.py		   yolo.ipynb
myData				   yolov4.conv.137
obj_1.zip			   yolov4-custom2.cfg
obj.data			   yolov4.weights
obj.names			   ZHCAMRAW_ERR_20190817173627_512453.jpg
#将谷歌云盘中的yolov4上传到darknet目录下
!cp /mydrive/yolo/yolov4.weights ./
#定义imshow 调用opencv显示图片
def imShow(path):
  import cv2
  import matplotlib.pyplot as plt
  %matplotlib inline

  image = cv2.imread(path)
  height, width = image.shape[:2]
  resized_image = cv2.resize(image,(3*width, 3*height), interpolation = cv2.INTER_CUBIC)

  fig = plt.gcf()
  fig.set_size_inches(18, 10)
  plt.axis("off")
  plt.imshow(cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB))
  plt.show()

# 选用darknet自带的图片进行测试
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights  data/dog.jpg
 CUDA-version: 10010 (10010), cuDNN: 7.6.5, GPU count: 1  
 OpenCV version: 3.2.0
 0 : compute_capability = 370, cudnn_half = 0, GPU: Tesla K80 
net.optimized_memory = 0 
mini_batch = 1, batch = 8, time_steps = 1, train = 0 
   layer   filters  size/strd(dil)      input                output
   0 conv     32       3 x 3/ 1    608 x 608 x   3 ->  608 x 608 x  32 0.639 BF
   1 conv     64       3 x 3/ 2    608 x 608 x  32 ->  304 x 304 x  64 3.407 BF
   2 conv     64       1 x 1/ 1    304 x 304 x  64 ->  304 x 304 x  64 0.757 BF
   3 route  1 		                           ->  304 x 304 x  64 
   4 conv     64       1 x 1/ 1    304 x 304 x  64 ->  304 x 304 x  64 0.757 BF
   5 conv     32       1 x 1/ 1    304 x 304 x  64 ->  304 x 304 x  32 0.379 BF
   6 conv     64       3 x 3/ 1    304 x 304 x  32 ->  304 x 304 x  64 3.407 BF
   7 Shortcut Layer: 4,  wt = 0, wn = 0, outputs: 304 x 304 x  64 0.006 BF
   8 conv     64       1 x 1/ 1    304 x 304 x  64 ->  304 x 304 x  64 0.757 BF
   9 route  8 2 	                           ->  304 x 304 x 128 
  10 conv     64       1 x 1/ 1    304 x 304 x 128 ->  304 x 304 x  64 1.514 BF
	。。。(省略)
[yolo] params: iou loss: ciou (4), iou_norm: 0.07, cls_norm: 1.00, scale_x_y: 1.05
nms_kind: greedynms (1), beta = 0.600000 
Total BFLOPS 128.459 
avg_outputs = 1068395 
 Allocate additional workspace_size = 6.65 MB 
Loading weights from yolov4.weights...
 seen 64, trained: 32032 K-images (500 Kilo-batches_64) 
Done! Loaded 162 layers from weights-file 
data/dog.jpg: Predicted in 171.094000 milli-seconds.
bicycle: 92%
dog: 98%
truck: 92%
pottedplant: 33%
Unable to init server: Could not connect: Connection refused

(predictions:1474): Gtk-WARNING **: 02:53:27.076: cannot open display: 
imShow('predictions.jpg')

在colab上训练yolo_第1张图片

接下来需要如下六个文件

# 用自己的数据集训练模型,此处为VOC数据集。
# 需要六个文件:
#   1.obj.zip:其中存放着两个文件夹:一个是图片,另一个是图片中目标的标签。
#   2.obj.data:其中写了一些文件的位置等信息。
#   3.obj.names:其中写了标签的名称。
#   4.yolov4-custom2.cfg:其中写了训练时的必须信息。
#   5.train.txt:其中写了训练图片的路径信息,要对应训练图片所在位置。
#   6.yolov4.conv.137:预训练权重信息。链接:https://pan.baidu.com/s/1h9jBMT551ZHXX21hlQ9bfg 提取码:93iw
  • obj.zip: 要包含如下两个文件夹,
    在colab上训练yolo_第2张图片
    JPEGImages中包含所有的图片:
    在colab上训练yolo_第3张图片
    labels里包含所有的label.txt:
    在colab上训练yolo_第4张图片
    每一个label.txt中的具体内容如下,是每个图片bbox的坐标信息,可从xml文件中提取,这个处理过程可在网上搜素voc_label.py,有现成的代码:
    在colab上训练yolo_第5张图片
    ImageSets/Main中存放了:
    在colab上训练yolo_第6张图片
    其中这些txt文件分别保存了图片的名字:
    在colab上训练yolo_第7张图片
  • obj.data:路径与种类数以及训练好的权重所存放的位置等信息在colab上训练yolo_第8张图片
  • obj.names:存放种类的信息
    在colab上训练yolo_第9张图片
  • yolov4-custom2.cfg:
    • batch = 64 subdivisions = 16,如果有错,可将subdivisions改为32或64
    • max_batches= ?:为自己数据集种类个数*2000
    • steps=?,? :分别为max_batches的80%和90%
    • 将每一个【yolo】(有三个)下的classes修改为自己的种类数
    • 将每一个【yolo】(有三个)上面的【convolutional】中fiters改为(5+classes)x3
      在colab上训练yolo_第10张图片
  • train.txt:里面存放了训练图片的绝对路径,可用generate_train.py生成:
    在colab上训练yolo_第11张图片
    generate_train.py:
import os
image_files = []
# os.chdir(os.path.join("data", "obj","JPEGImages"))
for filename in os.listdir(r'E:\file\bupt\satellite\xudata\JPEGImages'):
    if filename.endswith(".jpg"):
        image_files.append("data/obj/JPEGImages/" + filename)
# os.chdir("..")
with open("train.txt", "w") as outfile:
    for image in image_files:
        outfile.write(image)
        outfile.write("\n")
    outfile.close()
# os.chdir("..")

接下来继续将准备好的这些文件放到对应位置并训练

# 将obj.zip从谷歌云盘拷贝至colab根目录
!cp /mydrive/yolo/obj.zip ../
# 解压
!unzip ../obj.zip -d data/
# 将.cfg文件从谷歌云盘拷贝至colab/darknet/cfg文件夹下:
!cp /mydrive/yolo/yolov4-custom2.cfg ./cfg
# 将.names文件和.data文件从谷歌云盘拷贝至colab/darknet/data文件夹下:
!cp /mydrive/yolo/obj.data ./data
!cp /mydrive/yolo/obj.names ./data
# 尝试了generate_train.py但是有问题,所以在本地生成了train.txt
# 将train.txt文件从谷歌云盘拷贝至colab/darknet/data文件夹下:
!cp /mydrive/yolo/train.txt ./data
# 将预训练权重放在colab/darknet目录下
!cp /mydrive/yolo/yolov4.conv.137 ./
# 训练模型
!./darknet detector train data/obj.data cfg/yolov4-custom2.cfg yolov4.conv.137 -dont_show
# 更改cfg:将训练参数改为测试参数
%cd cfg
!sed -i 's/batch=1/batch=64/' yolov4-custom2.cfg
!sed -i 's/subdivisions=1/subdivisions=64/' yolov4-custom2.cfg
%cd ..
# 运行就好,从本地或者云端
!./darknet detector test data/obj.data cfg/yolov4-custom2.cfg /mydrive/yolo/backup/yolov4-ship_final.weights  -thresh 0.02
imShow('predictions.jpg')

你可能感兴趣的:(opencv,计算机视觉,深度学习)