• 【yolov8目标检测】使用yolov8训练自己的数据集


    目录

    准备数据集 

    python安装yolov8 

    配置yaml 

    从0开始训练

    从预训练模型开始训练


    准备数据集 

    首先得准备好数据集,你的数据集至少包含images和labels,严格来说你的images应该包含训练集train、验证集val和测试集test,不过为了简单说明使用步骤,其中test可以不要,val和train可以用同一个,因此我这里只用了一个images

    其中images装的是图片数据,labels装的是与图片一一对应同名的yolo格式txt,即类别号,经过归一化的中心x和y坐标以及宽和高

    python安装yolov8 

    然后我们开始准备yolov8,使用python的API的话就比较简单,首先安装一下yolov8

    用pip的话安装的话

    pip install ultralytics

    使用pycharm安装的话

    配置yaml 

    安装完了之后,差不多就可以开始了,我们首先看看官方给的代码

    1. from ultralytics import YOLO
    2. # Load a model
    3. model = YOLO("yolov8n.yaml") # build a new model from scratch
    4. model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training)
    5. # Use the model
    6. model.train(data="coco128.yaml", epochs=3) # train the model
    7. metrics = model.val() # evaluate model performance on the validation set
    8. results = model("https://ultralytics.com/images/bus.jpg") # predict on an image
    9. path = model.export(format="onnx") # export the model to ONNX format

    其中迷惑的是yolov8n.yaml、yolov8n.pt和coco128.yaml这几个文件,yolov8n.yaml是yolov8的配置,yolov8n.pt是预训练的模型,coco128.yaml是coco数据集的配置参数

    因此如果我们想要训练自己的模型的话,需要修改一下配置文件,首先到GitHub上下载yolov8n.yaml和coco128.yaml下来,这两个文件的位置有可能会变,所以最好在仓库上直接搜索

    大概长这样,你也可以自己创建,然后把内容复制进去

    yolov8n.yaml

    1. # Ultralytics YOLO 🚀, AGPL-3.0 license
    2. # YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
    3. # Parameters
    4. nc: 80 # number of classes
    5. scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
    6. # [depth, width, max_channels]
    7. n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
    8. s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
    9. m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
    10. l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
    11. x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
    12. # YOLOv8.0n backbone
    13. backbone:
    14. # [from, repeats, module, args]
    15. - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
    16. - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
    17. - [-1, 3, C2f, [128, True]]
    18. - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
    19. - [-1, 6, C2f, [256, True]]
    20. - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
    21. - [-1, 6, C2f, [512, True]]
    22. - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
    23. - [-1, 3, C2f, [1024, True]]
    24. - [-1, 1, SPPF, [1024, 5]] # 9
    25. # YOLOv8.0n head
    26. head:
    27. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
    28. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
    29. - [-1, 3, C2f, [512]] # 12
    30. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
    31. - [[-1, 4], 1, Concat, [1]] # cat backbone P3
    32. - [-1, 3, C2f, [256]] # 15 (P3/8-small)
    33. - [-1, 1, Conv, [256, 3, 2]]
    34. - [[-1, 12], 1, Concat, [1]] # cat head P4
    35. - [-1, 3, C2f, [512]] # 18 (P4/16-medium)
    36. - [-1, 1, Conv, [512, 3, 2]]
    37. - [[-1, 9], 1, Concat, [1]] # cat head P5
    38. - [-1, 3, C2f, [1024]] # 21 (P5/32-large)
    39. - [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)

    coco128.yaml

    1. # Ultralytics YOLO 🚀, AGPL-3.0 license
    2. # COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics
    3. # Example usage: yolo train data=coco128.yaml
    4. # parent
    5. # ├── ultralytics
    6. # └── datasets
    7. # └── coco128 ← downloads here (7 MB)
    8. # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
    9. path: ../datasets/coco128 # dataset root dir
    10. train: images/train2017 # train images (relative to 'path') 128 images
    11. val: images/train2017 # val images (relative to 'path') 128 images
    12. test: # test images (optional)
    13. # Classes
    14. names:
    15. 0: person
    16. 1: bicycle
    17. 2: car
    18. 3: motorcycle
    19. 4: airplane
    20. 5: bus
    21. 6: train
    22. 7: truck
    23. 8: boat
    24. 9: traffic light
    25. 10: fire hydrant
    26. 11: stop sign
    27. 12: parking meter
    28. 13: bench
    29. 14: bird
    30. 15: cat
    31. 16: dog
    32. 17: horse
    33. 18: sheep
    34. 19: cow
    35. 20: elephant
    36. 21: bear
    37. 22: zebra
    38. 23: giraffe
    39. 24: backpack
    40. 25: umbrella
    41. 26: handbag
    42. 27: tie
    43. 28: suitcase
    44. 29: frisbee
    45. 30: skis
    46. 31: snowboard
    47. 32: sports ball
    48. 33: kite
    49. 34: baseball bat
    50. 35: baseball glove
    51. 36: skateboard
    52. 37: surfboard
    53. 38: tennis racket
    54. 39: bottle
    55. 40: wine glass
    56. 41: cup
    57. 42: fork
    58. 43: knife
    59. 44: spoon
    60. 45: bowl
    61. 46: banana
    62. 47: apple
    63. 48: sandwich
    64. 49: orange
    65. 50: broccoli
    66. 51: carrot
    67. 52: hot dog
    68. 53: pizza
    69. 54: donut
    70. 55: cake
    71. 56: chair
    72. 57: couch
    73. 58: potted plant
    74. 59: bed
    75. 60: dining table
    76. 61: toilet
    77. 62: tv
    78. 63: laptop
    79. 64: mouse
    80. 65: remote
    81. 66: keyboard
    82. 67: cell phone
    83. 68: microwave
    84. 69: oven
    85. 70: toaster
    86. 71: sink
    87. 72: refrigerator
    88. 73: book
    89. 74: clock
    90. 75: vase
    91. 76: scissors
    92. 77: teddy bear
    93. 78: hair drier
    94. 79: toothbrush
    95. # Download script/URL (optional)
    96. download: https://ultralytics.com/assets/coco128.zip

    然后修改yolov8n.yaml,把nc的数值改成你的数据集的类别数,我这里的数据集只有乌骨鸡和狮头鹅两个

    1. # Ultralytics YOLO 🚀, AGPL-3.0 license
    2. # YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
    3. # Parameters
    4. nc: 2 # number of classes
    5. scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
    6. # [depth, width, max_channels]
    7. n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
    8. s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
    9. m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
    10. l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
    11. x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
    12. # YOLOv8.0n backbone
    13. backbone:
    14. # [from, repeats, module, args]
    15. - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
    16. - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
    17. - [-1, 3, C2f, [128, True]]
    18. - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
    19. - [-1, 6, C2f, [256, True]]
    20. - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
    21. - [-1, 6, C2f, [512, True]]
    22. - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
    23. - [-1, 3, C2f, [1024, True]]
    24. - [-1, 1, SPPF, [1024, 5]] # 9
    25. # YOLOv8.0n head
    26. head:
    27. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
    28. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
    29. - [-1, 3, C2f, [512]] # 12
    30. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
    31. - [[-1, 4], 1, Concat, [1]] # cat backbone P3
    32. - [-1, 3, C2f, [256]] # 15 (P3/8-small)
    33. - [-1, 1, Conv, [256, 3, 2]]
    34. - [[-1, 12], 1, Concat, [1]] # cat head P4
    35. - [-1, 3, C2f, [512]] # 18 (P4/16-medium)
    36. - [-1, 1, Conv, [512, 3, 2]]
    37. - [[-1, 9], 1, Concat, [1]] # cat head P5
    38. - [-1, 3, C2f, [1024]] # 21 (P5/32-large)
    39. - [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)

    然后修改coco128.yaml,我把文件名也改成了data.yaml,path改成images和labels的上一级目录地址,train改成训练集相对于path的地址,val也是改成验证集的相对于path的地址,我这里训练集和验证集用的是同一个嘿嘿嘿,然后把test注释掉,因为我没用测试集,还有就是names那里改成你的训练集的类别名,并把多余的类别删掉

    1. # Ultralytics YOLO 🚀, AGPL-3.0 license
    2. # COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics
    3. # Example usage: yolo train data=coco128.yaml
    4. # parent
    5. # ├── ultralytics
    6. # └── datasets
    7. # └── coco128 ← downloads here (7 MB)
    8. # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
    9. path: C:/Users/Yezi/Desktop/人工智能实训/HW2/data # dataset root dir
    10. train: images # train images (relative to 'path') 128 images
    11. val: images # val images (relative to 'path') 128 images
    12. #test: # test images (optional)
    13. # Classes
    14. names:
    15. 0: goose
    16. 1: chicken

    这样子就配置好了

    然后开始训练

    从0开始训练

    下面是从0开始训练的过程

    其实训练的代码就两行

    1. model = YOLO("yolov8n.yaml") # build a new model from scratch
    2. model.train(data="data.yaml", epochs=5) # train the model

    不过从0开始训练的效果并不好,下面是我自己的测试代码,由于我电脑比较烂,GPU摆不上用场,所以只能用cpu训练

    1. from ultralytics import YOLO
    2. import matplotlib.pyplot as plt
    3. model = YOLO("yolov8n.yaml") # build a new model from scratch
    4. model.train(data="data.yaml", epochs=30, device='cpu') # train the model
    5. model.val(data="data.yaml")
    6. results = model(r"C:\Users\Yezi\Desktop\人工智能实训\HW2\data\images\00909.jpg") # predict on an image
    7. plt.imshow(results[0].plot())
    8. plt.show()
    9. results = model(r"C:\Users\Yezi\Desktop\人工智能实训\HW2\data\images\100318.jpg") # predict on an image
    10. plt.imshow(results[0].plot())
    11. plt.show()

    预训练模型开始训练

    官方推荐用预训练好的模型开始训练

    首先下载一个官方预训练好的模型

    我这里下载的是yolov8n

    然后使用预训练模型训练我的数据集

    1. from ultralytics import YOLO
    2. import matplotlib.pyplot as plt
    3. model=YOLO("yolov8n.pt")
    4. model.train(data="data.yaml", epochs=30, device='cpu') # train the model
    5. model.val(data="data.yaml")
    6. results = model(r"C:\Users\Yezi\Desktop\人工智能实训\HW2\data\images\00909.jpg") # predict on an image
    7. plt.imshow(results[0].plot())
    8. plt.show()
    9. results = model(r"C:\Users\Yezi\Desktop\人工智能实训\HW2\data\images\100318.jpg") # predict on an image
    10. plt.imshow(results[0].plot())
    11. plt.show()

     乌骨鸡的效果是这样的

    狮头鹅的效果是这样的

  • 相关阅读:
    Codeforces Round #790 (Div. 4) G. White-Black Balanced Subtrees 感觉很好的树形dp的板子题
    好的plm软件有哪些?plm软件排行榜
    查看进程与对应的线程
    Linux TCP 单机优化
    【PTHREAD】线程属性
    VS正确引入头文件仍然报C2065等错(不同编码代码页导致)
    如何手动获取spring/springboot中的IOC容器(全局上下文对象)?
    PlotNeuralNet resnet34和resnet18绘图
    阅读开源软件源码的心得体会
    如何学习深度学习
  • 原文地址:https://blog.csdn.net/weixin_62264287/article/details/133868548