• 笔记记录--基于ccpd数据集利用Paddle OCR训练车牌检测模型


    1-- 环境搭建

    安装Paddle OCR参考

    ① 创建环境

    1. conda create -n paddle_env python=3.8
    2. conda activate paddle_env

    ② 安装paddlepaddle
    # 切换cuda版本为11.1(根据个人实际修改)

    1. sudo gedit ~/.bashrc
    2. source ~/.bashrc

    # 安装paddlepaddle

    python -m pip install paddlepaddle-gpu==2.3.0.post111 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html 

    ③ 安装依赖
    # 安装PaddleOCR whl

    pip install "paddleocr>=2.0.1"

    # 版面分析

    pip3 install -U https://paddleocr.bj.bcebos.com/whl/layoutparser-0.0.0-py3-none-any.whl

    2 -- 数据集划分

    # 采用ccpd数据集的challenge系列
    # 45003用于训练集,5000用于验证集

    数据集文件转移代码

    1. import shutil
    2. import os
    3. def remove_file(old_path, new_path):
    4. filelist = os.listdir(old_path) # 列出该目录下的所有文件,listdir返回的文件列表是不包含路径的。
    5. i = 0
    6. for file in filelist:
    7. src = os.path.join(old_path, file)
    8. dst = os.path.join(new_path, file)
    9. if i < 5000:
    10. shutil.move(src, dst)
    11. else:
    12. break
    13. i = i + 1
    14. if __name__ == '__main__':
    15. remove_file(r"/civi/Chinese_license_plate_Note/detection/dataset/ccpd_challenge", r"/civi/Chinese_license_plate_Note/detection/dataset/test_dataset")

    # 创建label标注文件

    格式参考

     代码样例

    1. import os
    2. words_list = [
    3. "A", "B", "C", "D", "E",
    4. "F", "G", "H", "J", "K",
    5. "L", "M", "N", "P", "Q",
    6. "R", "S", "T", "U", "V",
    7. "W", "X", "Y", "Z", "0",
    8. "1", "2", "3", "4", "5",
    9. "6", "7", "8", "9"
    10. ]
    11. con_list = [
    12. "皖", "沪", "津", "渝", "冀",
    13. "晋", "蒙", "辽", "吉", "黑",
    14. "苏", "浙", "京", "闽", "赣",
    15. "鲁", "豫", "鄂", "湘", "粤",
    16. "桂", "琼", "川", "贵", "云",
    17. "西", "陕", "甘", "青", "宁",
    18. "新"
    19. ]
    20. if __name__ == "__main__":
    21. points = []
    22. label = []
    23. for item in os.listdir(os.path.join('/civi/Chinese_license_plate_Note/detection/dataset/test_dataset/')): # 遍历图片
    24. _, _, bbox, points, label, _, _ = item.split('-') # 分割文件名
    25. points = points.split('_') # 分割四个坐标点
    26. tmp = points
    27. points = []
    28. for _ in tmp:
    29. points.append([int(_.split('&')[0]), int(_.split('&')[1])])
    30. # print(points)
    31. label = label.split('_')
    32. con = con_list[int(label[0])]
    33. words = [words_list[int(_)] for _ in label[1:]]
    34. label = con + ''.join(words)
    35. label = '"' + label + '"'
    36. file_name = item
    37. List = '[{"transcription": ' + label + ', "points": ' + str(points) + '}]'
    38. line = file_name + '\t' + List + '\n'
    39. with open('/civi/Chinese_license_plate_Note/detection/dataset/' + 'test_label.txt', 'a', encoding='UTF-8') as f:
    40. f.write(line)

    上述代码博主犯了一个错误,就是CCPD数据集的四个坐标是从右下坐标顺时针开始的,而OCR检测的标注文件,其坐标要求从左上顺时针开始,所以上述代码修改为:

    1. import os
    2. words_list = [
    3. "A", "B", "C", "D", "E",
    4. "F", "G", "H", "J", "K",
    5. "L", "M", "N", "P", "Q",
    6. "R", "S", "T", "U", "V",
    7. "W", "X", "Y", "Z", "0",
    8. "1", "2", "3", "4", "5",
    9. "6", "7", "8", "9"
    10. ]
    11. con_list = [
    12. "皖", "沪", "津", "渝", "冀",
    13. "晋", "蒙", "辽", "吉", "黑",
    14. "苏", "浙", "京", "闽", "赣",
    15. "鲁", "豫", "鄂", "湘", "粤",
    16. "桂", "琼", "川", "贵", "云",
    17. "西", "陕", "甘", "青", "宁",
    18. "新"
    19. ]
    20. if __name__ == "__main__":
    21. points = []
    22. label = []
    23. for item in os.listdir(os.path.join('/civi/Chinese_license_plate_Note/detection/dataset/test_dataset/')): # 遍历图片
    24. _, _, bbox, points, label, _, _ = item.split('-') # 分割文件名
    25. points = points.split('_') # 分割四个坐标点
    26. tmp = points
    27. points = []
    28. points.append([int(tmp[2].split('&')[0]), int(tmp[2].split('&')[1])])
    29. points.append([int(tmp[3].split('&')[0]), int(tmp[3].split('&')[1])])
    30. points.append([int(tmp[0].split('&')[0]), int(tmp[0].split('&')[1])])
    31. points.append([int(tmp[1].split('&')[0]), int(tmp[1].split('&')[1])])
    32. label = label.split('_')
    33. con = con_list[int(label[0])]
    34. words = [words_list[int(_)] for _ in label[1:]]
    35. label = con + ''.join(words)
    36. label = '"' + label + '"'
    37. file_name = item
    38. List = '[{"transcription": ' + label + ', "points": ' + str(points) + '}]'
    39. line = file_name + '\t' + List + '\n'
    40. with open('/civi/Chinese_license_plate_Note/detection/dataset/' + 'test_label.txt', 'a', encoding='UTF-8') as f:
    41. f.write(line)

    3-- 训练模型

    训练参考

    ① 下载预训练模型(DB-Net)

     下载地址

    ② 配置config文件

    示例:(注释部分需要留意并修改)

    1. Global:
    2. use_gpu: true # 是否使用gpu
    3. epoch_num: 200 # epoch数目
    4. log_smooth_window: 20
    5. print_batch_step: 2
    6. save_model_dir: /civi/Chinese_license_plate_Note/detection/Models_Well_trained/200epochs # 保存模型的地址
    7. save_epoch_step: 10 # 保存模型的间隔
    8. # evaluation is run every 5000 iterations after the 4000th iteration
    9. eval_batch_step: [3000, 2000]
    10. cal_metric_during_train: False
    11. pretrained_model: /civi/Chinese_license_plate_Note/detection/pretrain/ch_ppocr_server_v2.0_det_train/best_accuracy # 预训练模型的地址
    12. checkpoints:
    13. save_inference_dir:
    14. use_visualdl: False
    15. infer_img: /civi/Chinese_license_plate_Note/detection/test_img/test6.22.png # 测试图片
    16. save_res_path: /civi/Chinese_license_plate_Note/detection/Models_Well_trained/100epochs/det_db/predicts_db.txt
    17. Architecture:
    18. model_type: det
    19. algorithm: DB
    20. Transform:
    21. Backbone:
    22. name: ResNet
    23. layers: 18
    24. disable_se: True
    25. Neck:
    26. name: DBFPN
    27. out_channels: 256
    28. Head:
    29. name: DBHead
    30. k: 50
    31. Loss:
    32. name: DBLoss
    33. balance_loss: true
    34. main_loss_type: DiceLoss
    35. alpha: 5
    36. beta: 10
    37. ohem_ratio: 3
    38. Optimizer:
    39. name: Adam
    40. beta1: 0.9
    41. beta2: 0.999
    42. lr:
    43. name: Cosine
    44. learning_rate: 0.001
    45. warmup_epoch: 2
    46. regularizer:
    47. name: 'L2'
    48. factor: 0
    49. PostProcess:
    50. name: DBPostProcess
    51. thresh: 0.3
    52. box_thresh: 0.6
    53. max_candidates: 1000
    54. unclip_ratio: 1.5
    55. Metric:
    56. name: DetMetric
    57. main_indicator: hmean
    58. Train:
    59. dataset:
    60. name: SimpleDataSet
    61. data_dir: /civi/Chinese_license_plate_Note/detection/dataset/train_dataset/ # 训练集图片
    62. label_file_list:
    63. - /civi/Chinese_license_plate_Note/detection/dataset/train_label.txt # 训练集标签
    64. ratio_list: [1.0]
    65. transforms:
    66. - DecodeImage: # load image
    67. img_mode: BGR
    68. channel_first: False
    69. - DetLabelEncode: # Class handling label
    70. - IaaAugment:
    71. augmenter_args:
    72. - { 'type': Fliplr, 'args': { 'p': 0.5 } }
    73. - { 'type': Affine, 'args': { 'rotate': [-10, 10] } }
    74. - { 'type': Resize, 'args': { 'size': [0.5, 3] } }
    75. - EastRandomCropData:
    76. size: [960, 960]
    77. max_tries: 50
    78. keep_ratio: true
    79. - MakeBorderMap:
    80. shrink_ratio: 0.4
    81. thresh_min: 0.3
    82. thresh_max: 0.7
    83. - MakeShrinkMap:
    84. shrink_ratio: 0.4
    85. min_text_size: 8
    86. - NormalizeImage:
    87. scale: 1./255.
    88. mean: [0.485, 0.456, 0.406]
    89. std: [0.229, 0.224, 0.225]
    90. order: 'hwc'
    91. - ToCHWImage:
    92. - KeepKeys:
    93. keep_keys: ['image', 'threshold_map', 'threshold_mask', 'shrink_map', 'shrink_mask'] # the order of the dataloader list
    94. loader:
    95. shuffle: True
    96. drop_last: False
    97. batch_size_per_card: 8 # batchsize
    98. num_workers: 4
    99. Eval:
    100. dataset:
    101. name: SimpleDataSet
    102. data_dir: /civi/Chinese_license_plate_Note/detection/dataset/test_dataset/ # 验证集图片
    103. label_file_list:
    104. - /civi/Chinese_license_plate_Note/detection/dataset/test_label.txt # 验证集标签
    105. transforms:
    106. - DecodeImage: # load image
    107. img_mode: BGR
    108. channel_first: False
    109. - DetLabelEncode: # Class handling label
    110. - DetResizeForTest:
    111. # image_shape: [736, 1280]
    112. - NormalizeImage:
    113. scale: 1./255.
    114. mean: [0.485, 0.456, 0.406]
    115. std: [0.229, 0.224, 0.225]
    116. order: 'hwc'
    117. - ToCHWImage:
    118. - KeepKeys:
    119. keep_keys: ['image', 'shape', 'polys', 'ignore_tags']
    120. loader:
    121. shuffle: False
    122. drop_last: False
    123. batch_size_per_card: 1 # must be 1
    124. num_workers: 2

    ③ 开始训练

    # 这里博主使用多卡训练,gpu编号为1和2

    1. python3 -m paddle.distributed.launch --gpus '1,2' tools/train.py \
    2. -c /civi/Chinese_license_plate_Note/detection/test.yml

    ## 未完待续

  • 相关阅读:
    Mybatis Plus配置多个数据源
    JAVA基础(十四)
    2022杭电多校赛第八场
    关于GIS空间分析的几点思路
    jsp获取数据 jsp直接获取后端数据 获取input选中的值 单选 没 checked属性
    【python】爬虫系列Day03--url传参
    Bootstrap的CSS类积累学习
    vue3-基础知识(4)- 组件
    [计算机入门] Windows功能的安装与卸载
    SVN自动更新
  • 原文地址:https://blog.csdn.net/weixin_43863869/article/details/125414476