当前位置:   article > 正文

yolov4训练步骤_yolov4训练过程

yolov4训练过程

一、

打开对应路径        E:\darknet-master\build\darknet\x64

然后在目录下新建个文件夹train

train 文件夹里放这些东西

pictures文件夹是存放图片和xml文件的

举个列子,我这里放红蓝方块的图片和红蓝方块xml文件

可以看到我蓝色方块标签是A3,红色方块标签是A1

 图片序号有时候需要重新命名

所以我们需要用到

双击打开

根据注释修改

1、先将jpg图片重新命名

打开cmd 使用python脚本

回车运行

 

出现ok即为结束

2、重新命名xml文件

还是之前那个change_name.py

 稍作更改继续命令行运行脚本

出现ok就是结束

 这时候我们的pictures文件夹就是这个样子的

可以用labeling打开看看是否对应

对应没有出错。

然后就是获取txt文本

这时候需要用到

文本内部是这样的,我们需要根据注释进行修改 

  1. import xml.etree.ElementTree as ET
  2. import os
  3. classes = ["A1","A2","A3","A4","b","L1","L2","L3","L4","L5","L6","L7","L8","L9","L10","L11","L12","L13","L14","L15","L16","L17","L18","L19","L20","M21","M22","M23","M24","M25","M26","M27","M28","M29","M30","M31","M32","M33","M34","M35","M36","TLS","WZ","LZ"] # 这里是你的所有分类的名称
  4. myRoot = r'd:\Desktop\打标好的' # 这里是你项目的根目录
  5. train_xmlRoot = myRoot + r'/train'
  6. train_txtRoot = myRoot + r'/train'
  7. train_imageRoot = myRoot + r'/train'
  8. def getFile_name(file_dir):
  9. L = []
  10. for root, dirs, files in os.walk(file_dir):
  11. print(files)
  12. for file in files:
  13. if os.path.splitext(file)[1] == '.jpg':
  14. L.append(os.path.splitext(file)[0]) # L.append(os.path.join(root, file))
  15. return L
  16. def convert(size, box):
  17. dw = 1. / size[0]
  18. dh = 1. / size[1]
  19. x = (box[0] + box[1]) / 2.0
  20. y = (box[2] + box[3]) / 2.0
  21. w = box[1] - box[0]
  22. h = box[3] - box[2]
  23. x = x * dw
  24. w = w * dw
  25. y = y * dh
  26. h = h * dh
  27. return (x, y, w, h)
  28. def convert_annotation(xmlRoot,txtRoot,image_id):
  29. in_file = open(xmlRoot + '\\%s.xml' % (image_id),encoding='utf-8')
  30. out_file = open(txtRoot + '\\%s.txt' % (image_id), 'w',encoding='utf-8') # 生成txt格式文件
  31. tree = ET.parse(in_file)
  32. root = tree.getroot()
  33. size = root.find('size')
  34. w = int(size.find('width').text)
  35. h = int(size.find('height').text)
  36. for obj in root.iter('object'):
  37. cls = obj.find('name').text
  38. if cls not in classes:
  39. continue
  40. cls_id = classes.index(cls)
  41. xmlbox = obj.find('bndbox')
  42. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
  43. float(xmlbox.find('ymax').text))
  44. bb = convert((w, h), b)
  45. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  46. image_ids_train = getFile_name(train_imageRoot)
  47. list_file_train = open(myRoot + r'\train.txt', 'w')
  48. for image_id in image_ids_train:
  49. list_file_train.write(train_imageRoot + '\\%s.jpg\n' % (image_id))
  50. convert_annotation(train_xmlRoot,train_txtRoot,image_id)
  51. list_file_train.close() # 只生成训练集,自己根据自己情况决定

 就比如这次我们是红蓝方块,他们标签一个是A1一个是A3,就需要把

改为 

 然后项目根目录是E:\darknet-master\build\darknet\x64\train,所以也需要改

改完之后是这样子的

  1. import xml.etree.ElementTree as ET
  2. import os
  3. classes = ["A1","A3"] # 这里是你的所有分类的名称
  4. myRoot = r'E:\darknet-master\build\darknet\x64\train' # 这里是你项目的根目录
  5. train_xmlRoot = myRoot + r'/pictures' #这是你xml文件夹
  6. train_txtRoot = myRoot + r'/pictures' #这是你txt文件夹
  7. train_imageRoot = myRoot + r'/pictures' #这是你image文件夹 我反正都放一起了所以是同一个文件夹
  8. def getFile_name(file_dir):
  9. L = []
  10. for root, dirs, files in os.walk(file_dir):
  11. print(files)
  12. for file in files:
  13. if os.path.splitext(file)[1] == '.jpg':
  14. L.append(os.path.splitext(file)[0]) # L.append(os.path.join(root, file))
  15. return L
  16. def convert(size, box):
  17. dw = 1. / size[0]
  18. dh = 1. / size[1]
  19. x = (box[0] + box[1]) / 2.0
  20. y = (box[2] + box[3]) / 2.0
  21. w = box[1] - box[0]
  22. h = box[3] - box[2]
  23. x = x * dw
  24. w = w * dw
  25. y = y * dh
  26. h = h * dh
  27. return (x, y, w, h)
  28. def convert_annotation(xmlRoot,txtRoot,image_id):
  29. in_file = open(xmlRoot + '\\%s.xml' % (image_id),encoding='utf-8')
  30. out_file = open(txtRoot + '\\%s.txt' % (image_id), 'w',encoding='utf-8') # 生成txt格式文件
  31. tree = ET.parse(in_file)
  32. root = tree.getroot()
  33. size = root.find('size')
  34. w = int(size.find('width').text)
  35. h = int(size.find('height').text)
  36. for obj in root.iter('object'):
  37. cls = obj.find('name').text
  38. if cls not in classes:
  39. continue
  40. cls_id = classes.index(cls)
  41. xmlbox = obj.find('bndbox')
  42. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
  43. float(xmlbox.find('ymax').text))
  44. bb = convert((w, h), b)
  45. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  46. image_ids_train = getFile_name(train_imageRoot)
  47. list_file_train = open(myRoot + r'\train.txt', 'w')
  48. for image_id in image_ids_train:
  49. list_file_train.write(train_imageRoot + '\\%s.jpg\n' % (image_id))
  50. convert_annotation(train_xmlRoot,train_txtRoot,image_id)
  51. list_file_train.close() # 只生成训练集,自己根据自己情况决定

然后运行脚本

 

 然后你的pictures文件夹里就会有txt文本了

然后就是配置 coco.data coco.names  还有 yolov4.cfg

先配置coco.names

保存即可

然后配置 yolov4.cfg

  1. [net]
  2. # Testing
  3. #batch=1
  4. #subdivisions=1
  5. # Training
  6. batch=64 #每64个样本更新一次参数
  7. subdivisions=16 #将batch分割为64个子batch 训练中出现out of memory 则改大到64或更大
  8. width=512 #width和height为32倍数 越大训练的效果越好 但是不能超过图像高宽 验证耗时也越大
  9. height=512
  10. channels=3
  11. momentum=0.9
  12. decay=0.0005
  13. angle=0
  14. saturation = 1.5 #饱和度
  15. exposure = 1.5 #曝光
  16. hue=.01 #色调
  17. learning_rate=0.0001
  18. burn_in=1000
  19. max_batches = 20000 #训练的最大批次数 值越大耗时越久 class*2000;不能小于图片数量;不能小于6k
  20. policy=steps
  21. steps=40000,45000 #max_batches*0.80.9的值
  22. scales=.1,.1
  23. #weights_reject_freq=1001
  24. #ema_alpha=0.9998
  25. #equidistant_point=1000
  26. #num_sigmas_reject_badlabels=3
  27. #badlabels_rejection_percentage=0.2
  28. [convolutional]
  29. batch_normalize=1
  30. filters=32
  31. size=3
  32. stride=2
  33. pad=1
  34. activation=leaky
  35. [convolutional]
  36. batch_normalize=1
  37. filters=64
  38. size=3
  39. stride=2
  40. pad=1
  41. activation=leaky
  42. [convolutional]
  43. batch_normalize=1
  44. filters=64
  45. size=3
  46. stride=1
  47. pad=1
  48. activation=leaky
  49. [route]
  50. layers=-1
  51. groups=2
  52. group_id=1
  53. [convolutional]
  54. batch_normalize=1
  55. filters=32
  56. size=3
  57. stride=1
  58. pad=1
  59. activation=leaky
  60. [convolutional]
  61. batch_normalize=1
  62. filters=32
  63. size=3
  64. stride=1
  65. pad=1
  66. activation=leaky
  67. [route]
  68. layers = -1,-2
  69. [convolutional]
  70. batch_normalize=1
  71. filters=64
  72. size=1
  73. stride=1
  74. pad=1
  75. activation=leaky
  76. [route]
  77. layers = -6,-1
  78. [maxpool]
  79. size=2
  80. stride=2
  81. [convolutional]
  82. batch_normalize=1
  83. filters=128
  84. size=3
  85. stride=1
  86. pad=1
  87. activation=leaky
  88. [route]
  89. layers=-1
  90. groups=2
  91. group_id=1
  92. [convolutional]
  93. batch_normalize=1
  94. filters=64
  95. size=3
  96. stride=1
  97. pad=1
  98. activation=leaky
  99. [convolutional]
  100. batch_normalize=1
  101. filters=64
  102. size=3
  103. stride=1
  104. pad=1
  105. activation=leaky
  106. [route]
  107. layers = -1,-2
  108. [convolutional]
  109. batch_normalize=1
  110. filters=128
  111. size=1
  112. stride=1
  113. pad=1
  114. activation=leaky
  115. [route]
  116. layers = -6,-1
  117. [maxpool]
  118. size=2
  119. stride=2
  120. [convolutional]
  121. batch_normalize=1
  122. filters=256
  123. size=3
  124. stride=1
  125. pad=1
  126. activation=leaky
  127. [route]
  128. layers=-1
  129. groups=2
  130. group_id=1
  131. [convolutional]
  132. batch_normalize=1
  133. filters=128
  134. size=3
  135. stride=1
  136. pad=1
  137. activation=leaky
  138. [convolutional]
  139. batch_normalize=1
  140. filters=128
  141. size=3
  142. stride=1
  143. pad=1
  144. activation=leaky
  145. [route]
  146. layers = -1,-2
  147. [convolutional]
  148. batch_normalize=1
  149. filters=256
  150. size=1
  151. stride=1
  152. pad=1
  153. activation=leaky
  154. [route]
  155. layers = -6,-1
  156. [maxpool]
  157. size=2
  158. stride=2
  159. [convolutional]
  160. batch_normalize=1
  161. filters=512
  162. size=3
  163. stride=1
  164. pad=1
  165. activation=leaky
  166. ##################################
  167. [convolutional]
  168. batch_normalize=1
  169. filters=256
  170. size=1
  171. stride=1
  172. pad=1
  173. activation=leaky
  174. [convolutional]
  175. batch_normalize=1
  176. filters=512
  177. size=3
  178. stride=1
  179. pad=1
  180. activation=leaky
  181. [convolutional]
  182. size=1
  183. stride=1
  184. pad=1
  185. filters=51 #找离yolo最近的 (classes+5*3(下面的mask的值)
  186. activation=linear
  187. [yolo]
  188. mask = 3,4,5
  189. anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
  190. classes=12 #总共有几个类别
  191. num=6
  192. jitter=.3
  193. scale_x_y = 1.05
  194. cls_normalizer=1.0
  195. iou_normalizer=0.07
  196. iou_loss=ciou
  197. ignore_thresh = .7
  198. truth_thresh = 1
  199. random=0
  200. resize=1.5
  201. nms_kind=greedynms
  202. beta_nms=0.6
  203. #new_coords=1
  204. #scale_x_y = 2.0
  205. [route]
  206. layers = -4
  207. [convolutional]
  208. batch_normalize=1
  209. filters=128
  210. size=1
  211. stride=1
  212. pad=1
  213. activation=leaky
  214. [upsample]
  215. stride=2
  216. [route]
  217. layers = -1, 23
  218. [convolutional]
  219. batch_normalize=1
  220. filters=256
  221. size=3
  222. stride=1
  223. pad=1
  224. activation=leaky
  225. [convolutional]
  226. size=1
  227. stride=1
  228. pad=1
  229. filters=51 #同理
  230. activation=linear
  231. [yolo]
  232. mask = 1,2,3
  233. anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
  234. classes=12 #这边也要设置
  235. num=6
  236. jitter=.3
  237. scale_x_y = 1.05
  238. cls_normalizer=1.0
  239. iou_normalizer=0.07
  240. iou_loss=ciou
  241. ignore_thresh = .7
  242. truth_thresh = 1
  243. random=0
  244. resize=1.5
  245. nms_kind=greedynms
  246. beta_nms=0.6
  247. #new_coords=1
  248. #scale_x_y = 2.0

根据注释修改

比如红蓝方块就两类,训练批次数=4000,steps=3200,3600 #max_batches*0.8和0.9的值

classes=2 然后改 距离yolo最近的 filters=21 改完后是这样子的

  1. [net]
  2. # Testing
  3. #batch=1
  4. #subdivisions=1
  5. # Training
  6. batch=64 #每64个样本更新一次参数
  7. subdivisions=16 #将batch分割为64个子batch 训练中出现out of memory 则改大到64或更大
  8. width=512 #width和height为32倍数 越大训练的效果越好 但是不能超过图像高宽 验证耗时也越大
  9. height=512
  10. channels=3
  11. momentum=0.9
  12. decay=0.0005
  13. angle=0
  14. saturation = 1.5 #饱和度
  15. exposure = 1.5 #曝光
  16. hue=.01 #色调
  17. learning_rate=0.0001
  18. burn_in=1000
  19. max_batches = 4000 #训练的最大批次数 值越大耗时越久 class*2000;不能小于图片数量;不能小于6k
  20. policy=steps
  21. steps=3200,3600 #max_batches*0.80.9的值
  22. scales=.1,.1
  23. #weights_reject_freq=1001
  24. #ema_alpha=0.9998
  25. #equidistant_point=1000
  26. #num_sigmas_reject_badlabels=3
  27. #badlabels_rejection_percentage=0.2
  28. [convolutional]
  29. batch_normalize=1
  30. filters=32
  31. size=3
  32. stride=2
  33. pad=1
  34. activation=leaky
  35. [convolutional]
  36. batch_normalize=1
  37. filters=64
  38. size=3
  39. stride=2
  40. pad=1
  41. activation=leaky
  42. [convolutional]
  43. batch_normalize=1
  44. filters=64
  45. size=3
  46. stride=1
  47. pad=1
  48. activation=leaky
  49. [route]
  50. layers=-1
  51. groups=2
  52. group_id=1
  53. [convolutional]
  54. batch_normalize=1
  55. filters=32
  56. size=3
  57. stride=1
  58. pad=1
  59. activation=leaky
  60. [convolutional]
  61. batch_normalize=1
  62. filters=32
  63. size=3
  64. stride=1
  65. pad=1
  66. activation=leaky
  67. [route]
  68. layers = -1,-2
  69. [convolutional]
  70. batch_normalize=1
  71. filters=64
  72. size=1
  73. stride=1
  74. pad=1
  75. activation=leaky
  76. [route]
  77. layers = -6,-1
  78. [maxpool]
  79. size=2
  80. stride=2
  81. [convolutional]
  82. batch_normalize=1
  83. filters=128
  84. size=3
  85. stride=1
  86. pad=1
  87. activation=leaky
  88. [route]
  89. layers=-1
  90. groups=2
  91. group_id=1
  92. [convolutional]
  93. batch_normalize=1
  94. filters=64
  95. size=3
  96. stride=1
  97. pad=1
  98. activation=leaky
  99. [convolutional]
  100. batch_normalize=1
  101. filters=64
  102. size=3
  103. stride=1
  104. pad=1
  105. activation=leaky
  106. [route]
  107. layers = -1,-2
  108. [convolutional]
  109. batch_normalize=1
  110. filters=128
  111. size=1
  112. stride=1
  113. pad=1
  114. activation=leaky
  115. [route]
  116. layers = -6,-1
  117. [maxpool]
  118. size=2
  119. stride=2
  120. [convolutional]
  121. batch_normalize=1
  122. filters=256
  123. size=3
  124. stride=1
  125. pad=1
  126. activation=leaky
  127. [route]
  128. layers=-1
  129. groups=2
  130. group_id=1
  131. [convolutional]
  132. batch_normalize=1
  133. filters=128
  134. size=3
  135. stride=1
  136. pad=1
  137. activation=leaky
  138. [convolutional]
  139. batch_normalize=1
  140. filters=128
  141. size=3
  142. stride=1
  143. pad=1
  144. activation=leaky
  145. [route]
  146. layers = -1,-2
  147. [convolutional]
  148. batch_normalize=1
  149. filters=256
  150. size=1
  151. stride=1
  152. pad=1
  153. activation=leaky
  154. [route]
  155. layers = -6,-1
  156. [maxpool]
  157. size=2
  158. stride=2
  159. [convolutional]
  160. batch_normalize=1
  161. filters=512
  162. size=3
  163. stride=1
  164. pad=1
  165. activation=leaky
  166. ##################################
  167. [convolutional]
  168. batch_normalize=1
  169. filters=256
  170. size=1
  171. stride=1
  172. pad=1
  173. activation=leaky
  174. [convolutional]
  175. batch_normalize=1
  176. filters=512
  177. size=3
  178. stride=1
  179. pad=1
  180. activation=leaky
  181. [convolutional]
  182. size=1
  183. stride=1
  184. pad=1
  185. filters=21 #找离yolo最近的 (classes+5*3(下面的mask的值)
  186. activation=linear
  187. [yolo]
  188. mask = 3,4,5
  189. anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
  190. classes=2 #总共有几个类别
  191. num=6
  192. jitter=.3
  193. scale_x_y = 1.05
  194. cls_normalizer=1.0
  195. iou_normalizer=0.07
  196. iou_loss=ciou
  197. ignore_thresh = .7
  198. truth_thresh = 1
  199. random=0
  200. resize=1.5
  201. nms_kind=greedynms
  202. beta_nms=0.6
  203. #new_coords=1
  204. #scale_x_y = 2.0
  205. [route]
  206. layers = -4
  207. [convolutional]
  208. batch_normalize=1
  209. filters=128
  210. size=1
  211. stride=1
  212. pad=1
  213. activation=leaky
  214. [upsample]
  215. stride=2
  216. [route]
  217. layers = -1, 23
  218. [convolutional]
  219. batch_normalize=1
  220. filters=256
  221. size=3
  222. stride=1
  223. pad=1
  224. activation=leaky
  225. [convolutional]
  226. size=1
  227. stride=1
  228. pad=1
  229. filters=21 #同理
  230. activation=linear
  231. [yolo]
  232. mask = 1,2,3
  233. anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
  234. classes=2 #这边也要设置
  235. num=6
  236. jitter=.3
  237. scale_x_y = 1.05
  238. cls_normalizer=1.0
  239. iou_normalizer=0.07
  240. iou_loss=ciou
  241. ignore_thresh = .7
  242. truth_thresh = 1
  243. random=0
  244. resize=1.5
  245. nms_kind=greedynms
  246. beta_nms=0.6
  247. #new_coords=1
  248. #scale_x_y = 2.0

这就配置好了yolov4.cfg

最后配置coco.data

然后在

 路径下打开终端输入训练指令

darknet.exe detector train train/coco.data train/yolov4.cfg train/yolov4-tiny.conv.29

等待训练结束就行。然后就会在你之前的

这个位置出现权重文件,之后用这个权重文件检测图片,是对应目标标签即可。 

1、识别图片格式:./darknet.exe detector test <.data文件位置> <.cfg文件位置> <权重文件位置> <识别文件位置>
darknet.exe detector test train/coco.data train/yolov4.cfg train/backup/yolov4_last.weights train/pictures/1.jpg

就会有下面这种结果,标签和物体对上了,置信度也高。


2、调用电脑摄像头识别
darknet.exe detector demo  D:/desktop/shape/shape.data D:/desktop/shape/yolov4-tiny.cfg D:/desktop/shape/F.weights  -c 0

3、继续上次模型的训练

darknet.exe detector train train/coco.data train/yolov4.cfg train/yolov4_last.weights

就是权重从预训练模型yolov4-tiny.conv.29变为之前训练得到的权重yolov4_last.weights,如果报错的话,更改cfg文件里max_batches  #训练的最大批次数 让cfg文件和之前训练的不一样就可以了。

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

闽ICP备14008679号