当前位置:   article > 正文

利用GPU云服务和yolov5-5.0训练自己的目标检测模型_云服务器部署yolov5

云服务器部署yolov5

系统配置:win11

一、制作自己训练数据集

1.1新建文件夹VOCdevkit

进入VOCdevkit,创建images、labels文件夹,

进入images文件夹,创建test、train、val文件夹,用于储存图片数据集

进入labels文件夹,创建train、val文件夹,用于储存数据标签

1.2制作图片数据集

1.2.1下载目标图片

从数据集网站下载所需检测目标的图片,或百度图片下载,存放于VOCdevkit\images\train中


数据集网站:Kaggle: Your Machine Learning and Data Science Community

1.2.2创建python虚拟环境

1.2.2.1安装miniconda3

下载地址:Miniconda — Anaconda documentation

找到对应系统、Python版本下载。下载完,打开安装包,按照默认提示,下一步下一步。(不建议安装到C盘)

1.2.2.2配置环境变量

右键此电脑 -> 属性 -> 下滑找到“高级系统设置”

点击环境变量

找到 “系统变量” 里的path,点编辑

新建环境变量

  1. 我的安装路径是E:\miniconda\miniconda
  2.  我的新建环境变量是:
  3.  E:\miniconda\miniconda
  4.  E:\miniconda\miniconda\Scripts
  5.  E:\miniconda\miniconda\Library\bin

测试:win + r,输入cmd,打开命令提示符,输入conda,如下图就是安装成功了

1.2.2.3安装Pillow和labelimg

win + r,输入cmd,打开命令提示符,输入

  1.  activate base
  2.  ​
  3.  pip install Pillow
  4.  pip install labelimg -i https://pypi.tuna.tsinghua.edu.cn/simple
常用命令

创建python虚拟环境

conda create -n yolov5 python=3.8     # -n是name的缩写,python不指定版本就默认最新版

查看已经安装的虚拟环境列表

 conda env list

进入虚拟环境

 activate yolov5

退出当前虚拟环境

 deactivate

1.2.3利用python文件对图片进行排序

安装Sublime Text 3,网址:Download - Sublime Text

创建translate.py文件,并用Sublime Text 3打开,使用快捷键Ctrl+B运行Python代码(代码如下)

使用方法:将图片跟脚本放在同一个目录,然后运行脚本即可,图片会自动保存在save文件夹中

  1. from PIL import Image
  2. import os
  3. d = os.listdir('.')
  4. pic_id = 1
  5. save_path = os.path.join(os.path.abspath(''), 'save')
  6. if not os.path.exists(save_path):
  7. os.mkdir(save_path)
  8. for p in [i for i in d if ('.jpg' in i) or ('.JPG' in i) or ('.png' in i)]:
  9. im = Image.open(p)
  10. try:
  11. im.save(os.path.join(save_path, str(pic_id) + '.jpg'))
  12. except Exception as e:
  13. pass
  14. pic_id += 1

1.2.4标注训练集

win + r,输入cmd,打开命令提示符,输入

 labelimg

打开labelimg

简单配置

标注

  1.  1.按下w,出现十字叉
  2.  2.鼠标左键按住开始拖动选控
  3.  3.松开鼠标左键
  4.  4.输入标签名称,例如yellow bell pepper
  5.  5.按下d,进入下一张图片

示例:

1.2.5划分训练集和验证集

下面以训练集:验证集=3:1为例,划分数据集,python代码如下,若验证集不需要太多,自己手动移动即可(图片和标注文件,不要忘了class.txt)(经测试,下面代码对于已经排好序号的数据集并非随机划分),若要使用需修改文件路径

  1. import os
  2. import shutil
  3. image_path = 'E:/test/Image/save' # 图片文件
  4. txt_path = 'E:/test/Image/label' # 标签文件
  5. new_file_path = 'E:/test/Image/later' # 划分数据后的文件
  6. train_rate = 0.75 # 训练集比例
  7. val_rate = 0.25 # 验证集比例
  8. # 将有对应标签的图片找出来,放到新文件夹下
  9. labels = []
  10. for label in os.listdir(txt_path):
  11. labels.append(os.path.splitext(label)[0])
  12. for image_name in os.listdir(image_path):
  13. image_name = os.path.splitext(image_name)[0]
  14. if image_name in labels:
  15. image_name = image_name + ".jpg"
  16. shutil.copy(image_path + '/' + image_name, new_file_path)
  17. # 计算训练集与验证集数量
  18. images = []
  19. for image in os.listdir(new_file_path):
  20. images.append(image)
  21. total = len(images)
  22. train_images = images[0:int(train_rate * total)]
  23. val_images = images[int(train_rate * total):int((train_rate + val_rate) * total)]
  24. # 图片-train
  25. for image in train_images:
  26. print(image)
  27. old_path = new_file_path + '/' + image
  28. new_path1 = new_file_path + '/' + 'images' + '/' + 'train'
  29. # new_path1 = new_file_path + '/' + 'train' + '/' + 'images'
  30. if not os.path.exists(new_path1):
  31. os.makedirs(new_path1)
  32. # new_path = new_path1 + '/' + image
  33. shutil.copy(old_path, new_path1)
  34. # 图片-val
  35. for image in val_images:
  36. old_path = new_file_path + '/' + image
  37. new_path1 = new_file_path + '/' + 'images' + '/' + 'val'
  38. # new_path1 = new_file_path + '/' + 'val' + '/' + 'images'
  39. if not os.path.exists(new_path1):
  40. os.makedirs(new_path1)
  41. # new_path = new_path1 + '/' + image
  42. shutil.copy(old_path, new_path1)
  43. # 标签-train
  44. images1 = []
  45. for image in os.listdir(new_file_path + '/' + 'images' + '/' + 'train'):
  46. images1.append(os.path.splitext(image)[0])
  47. for label_name in os.listdir(txt_path):
  48. label_name = os.path.splitext(label_name)[0]
  49. if label_name in images1:
  50. label_name = label_name + ".txt"
  51. label_train_path = new_file_path + '/' + 'labels' + '/' + 'train'
  52. if not os.path.exists(label_train_path):
  53. os.makedirs(label_train_path)
  54. shutil.copy(txt_path + '/' + label_name, label_train_path)
  55. shutil.copy(txt_path + '/' + 'classes.txt', label_train_path)
  56. # 标签-val
  57. images2 = []
  58. for image in os.listdir(new_file_path + '/' + 'images' + '/' + 'val'):
  59. images2.append(os.path.splitext(image)[0])
  60. for label_name in os.listdir(txt_path):
  61. label_name = os.path.splitext(label_name)[0]
  62. if label_name in images2:
  63. label_name = label_name + ".txt"
  64. label_val_path = new_file_path + '/' + 'labels' + '/' + 'val'
  65. if not os.path.exists(label_val_path):
  66. os.makedirs(label_val_path)
  67. shutil.copy(txt_path + '/' + label_name, label_val_path)
  68. shutil.copy(txt_path + '/' + 'classes.txt', label_val_path)
  69. # 删除新文件夹下对应标签的图片
  70. for name in os.listdir(new_file_path):
  71. if name.endswith('.jpg'):
  72. os.remove(os.path.join(new_file_path, name))
  73. if image_name in labels:
  74. image_name = image_name + ".jpg"
  75. shutil.copy(image_path + '/' + image_name, new_file_path)
  76. # 计算训练集与验证集数量
  77. images = []
  78. for image in os.listdir(new_file_path):
  79. images.append(image)
  80. total = len(images)
  81. train_images = images[0:int(train_rate * total)]
  82. val_images = images[int(train_rate * total):int((train_rate + val_rate) * total)]
  83. # 图片-train
  84. for image in train_images:
  85. print(image)
  86. old_path = new_file_path + '/' + image
  87. new_path1 = new_file_path + '/' + 'images' + '/' + 'train'
  88. # new_path1 = new_file_path + '/' + 'train' + '/' + 'images'
  89. if not os.path.exists(new_path1):
  90. os.makedirs(new_path1)
  91. # new_path = new_path1 + '/' + image
  92. shutil.copy(old_path, new_path1)
  93. # 图片-val
  94. for image in val_images:
  95. old_path = new_file_path + '/' + image
  96. new_path1 = new_file_path + '/' + 'images' + '/' + 'val'
  97. # new_path1 = new_file_path + '/' + 'val' + '/' + 'images'
  98. if not os.path.exists(new_path1):
  99. os.makedirs(new_path1)
  100. # new_path = new_path1 + '/' + image
  101. shutil.copy(old_path, new_path1)
  102. # 标签-train
  103. images1 = []
  104. for image in os.listdir(new_file_path + '/' + 'images' + '/' + 'train'):
  105. images1.append(os.path.splitext(image)[0])
  106. for label_name in os.listdir(txt_path):
  107. label_name = os.path.splitext(label_name)[0]
  108. if label_name in images1:
  109. label_name = label_name + ".txt"
  110. label_train_path = new_file_path + '/' + 'labels' + '/' + 'train'
  111. if not os.path.exists(label_train_path):
  112. os.makedirs(label_train_path)
  113. shutil.copy(txt_path + '/' + label_name, label_train_path)
  114. shutil.copy(txt_path + '/' + 'classes.txt', label_train_path)
  115. # 标签-val
  116. images2 = []
  117. for image in os.listdir(new_file_path + '/' + 'images' + '/' + 'val'):
  118. images2.append(os.path.splitext(image)[0])
  119. for label_name in os.listdir(txt_path):
  120. label_name = os.path.splitext(label_name)[0]
  121. if label_name in images2:
  122. label_name = label_name + ".txt"
  123. label_val_path = new_file_path + '/' + 'labels' + '/' + 'val'
  124. if not os.path.exists(label_val_path):
  125. os.makedirs(label_val_path)
  126. shutil.copy(txt_path + '/' + label_name, label_val_path)
  127. shutil.copy(txt_path + '/' + 'classes.txt', label_val_path)
  128. # 删除新文件夹下对应标签的图片
  129. for name in os.listdir(new_file_path):
  130. if name.endswith('.jpg'):
  131. os.remove(os.path.join(new_file_path, name))

二、下载yolov5-5.0代码和预训练权重

下载地址:ultralytics/yolov5 at v5.0 (github.com)

将整个yolov5压缩包下载下来(注意路径不要有中文),发现是没有预训练权重文件的,这时我们要自己下载一个,这里我下载yolo5s.pt,放于yolov5-5.0文件夹中

下载链接:Release v5.0 - YOLOv5-P6 1280 models, AWS, Supervise.ly and YouTube integrations · ultralytics/yolov5 (github.com)

将自己的数据集文件VOCdevkit放到yolov5-5.0文件夹中

三、修改配置文件

首先要在data文件夹里新建一个配置文件myvoc.yaml,修改为你的类别。

  1. # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
  2. train: /mnt/yolov5-5.0/VOCdevkit/images/train #训练集路径
  3. val: /mnt/yolov5-5.0/VOCdevkit/images/val #验证集路径
  4. # number of classes
  5. nc: 3
  6. # class names
  7. names: ['yellow bell pepper', 'red bell pepper','green bell pepper' ]

然后是model文件夹里复制一下yolo5s.yaml并命名为yolo5s_mine.yaml。修改类别的数量。本来nc=80,这里改为3

四、训练

4.1下载矩池云网盘

下载链接:矩池云 - 主机市场 (matpool.com)

下载后,将yolov5-5.0文件夹拖拽上传到矩池云网盘

4.2租用GPU云服务器

选择环境

win + r,输入cmd,打开命令提示符,输入SSH命令和密码,再输入代码,进入网盘

 cd /mnt

输入代码,进入 yolov5-5.0

 cd yolov5-5.0/

由于系统镜像是yolov5-6.1,输入代码,需要配置环境

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

4.3模型训练

输入代码,开始训练(由于系统镜像是yolov5-6.1,所以还需要配置环境)

python3 train.py --weights yolov5s.pt --data data/myvoc.yaml --workers 4 --batch-size 20 --epochs 10  #epochs为训练轮数,可改

出现下列结果,则训练成功

下载得到的best.pt,即为最好的训练模型

参考

8. 测试效果 (itprojects.cn)

yolov5-5.0训练完整步骤_yolov5 训练-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/478966
推荐阅读
相关标签
  

闽ICP备14008679号