PaddleOCR车牌检测识别训练、部署

需要的文件 1) CCPD2019 数据集。2) 安装paddleOCR 和paddle 等环境。 3)本机显卡3080,cuda 11.0 cudnn 8.0.5 paddle 2.0.2 paddleocr 2.0.6。

  1. 下载数据集:
    ccpd中科大数据集
    数据集
  • 本次使用的数据集为CCPD2019车牌数据集
  • 该数据集在合肥市的停车场采集得来,采集时间早上7:30到晚上10:00。停车场采集人员手持Android POS机对停车场的车辆拍照并手工标注车牌位置。拍摄的车牌照片涉及多种复杂环境,包括模糊、倾斜、阴雨天、雪天等等。CCPD数据集一共包含将近30万张图片,每种图片大小720x1160x3。一共包含8项,具体如下:

PaddleOCR车牌检测识别训练、部署_第1张图片

  • CCPD数据集没有专门的标注文件,每张图像的文件名就是对应的数据标注(label)
    • 例如:025-95_113-154&383_386&473-386&473_177&454_154&383_363&402-0_0_22_27_27_33_16-37-15.jpg 由分隔符’-'分为几个部分:
    1. 025为区域
    2. 95_113 对应两个角度, 水平95°, 竖直113°
    3. 154&383_386&473对应边界框坐标:左上(154, 383), 右下(386, 473)
    4. 386&473_177&454_154&383_363&402对应四个角点坐标
    5. 0_0_22_27_27_33_16为车牌号码 映射关系如下: 第一个为省份0 对应省份字典皖, 后面的为字母和文字, 查看ads字典.如0为A, 22为Y…

provinces = [“皖”, “沪”, “津”, “渝”, “冀”, “晋”, “蒙”, “辽”, “吉”, “黑”, “苏”, “浙”, “京”, “闽”, “赣”, “鲁”, “豫”, “鄂”, “湘”, “粤”, “桂”, “琼”, “川”, “贵”, “云”, “藏”, “陕”, “甘”, “青”, “宁”, “新”, “警”, “学”, “O”]
alphabets = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’,
‘X’, ‘Y’, ‘Z’, ‘O’]
ads = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’,
‘Y’, ‘Z’, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘O’]

# 解压数据集
CCPD2019.zip --> CCPD
  1. 数据转化,生成数据列表
import os, cv2
import random
words_list = [
    "A", "B", "C", "D", "E",
    "F", "G", "H", "J", "K", 
    "L", "M", "N", "P", "Q", 
    "R", "S", "T", "U", "V", 
    "W", "X", "Y", "Z", "0", 
    "1", "2", "3", "4", "5", 
    "6", "7", "8", "9" ]

con_list = [
    "皖", "沪", "津", "渝", "冀",
    "晋", "蒙", "辽", "吉", "黑",
    "苏", "浙", "京", "闽", "赣",
    "鲁", "豫", "鄂", "湘", "粤",
    "桂", "琼", "川", "贵", "云",
    "西", "陕", "甘", "青", "宁",
    "新"]

count = 0
total = []
# E:\Paddle\PaddleOCR-release-2.1\work\CCPD
paths = os.listdir('E:\Paddle\PaddleOCR-release-2.1\work\CCPD')
for path in paths:
    for item in os.listdir(os.path.join('E:\Paddle\PaddleOCR-release-2.1\work\CCPD', path)):
        new_path = os.path.join(path, item)
        _, _, bbox, points, label, _, _ = item.split('-')
        points = points.split('_')
        points = [_.split('&') for _ in points]
        tmp = points[-2:]+points[:2]
        points = []
        for point in tmp:
            points.append([int(_) for _ in point])
        label = label.split('_')
        con = con_list[int(label[0])]
        words = [words_list[int(_)] for _ in label[1:]]
        label = con+''.join(words)
        line = new_path+'\t'+'[{"transcription": "%s", "points": %s}]' % (' ', str(points))
        line = line[:]+'\n'
        total.append(line)
        print(len(total))

random.shuffle(total)

with open('E:\Paddle\PaddleOCR-release-2.1\work\\train.txt', 'w', encoding='UTF-8') as f:
    for line in total[:-1431]:
        f.write(line)

with open('E:\Paddle\PaddleOCR-release-2.1\work\dev.txt', 'w', encoding='UTF-8') as f:
    for line in total[-1431:]:
        f.write(line)

  1. 下载检测和识别预训练模型
检测预训练模型
https://paddle-imagenet-models-name.bj.bcebos.com/MobileNetV3_large_x0_5_pretrained.tar

识别推理模型
https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_rec_infer.tar

  1. 检测模型训练
python tools/train.py -c det.yml
  • det.yml文件
Global:
  use_gpu: true
  epoch_num: 5
  log_smooth_window: 20
  print_batch_step: 10
  save_model_dir: output/det_vlpr/
  save_epoch_step: 1
  eval_batch_step: [1000, 1000]
  load_static_weights: True
  cal_metric_during_train: False
  pretrained_model: pretrain_models/MobileNetV3_large_x0_5_pretrained
  checkpoints:
  save_inference_dir: inference/det_vlpr
  use_visualdl: False
  infer_img: doc/imgs_en/img_10.jpg
  save_res_path: output/det_vlpr/predicts.txt

Architecture:
  model_type: det
  algorithm: DB
  Transform:
  Backbone:
    name: MobileNetV3
    scale: 0.5
    model_name: large
    disable_se: True
  Neck:
    name: DBFPN
    out_channels: 96
  Head:
    name: DBHead
    k: 50

Loss:
  name: DBLoss
  balance_loss: true
  main_loss_type: DiceLoss
  alpha: 5
  beta: 10
  ohem_ratio: 3

Optimizer:
  name: Adam
  beta1: 0.9
  beta2: 0.999
  lr:
    name: Cosine
    learning_rate: 0.001
    warmup_epoch: 1
  regularizer:
    name: 'L2'
    factor: 0

PostProcess:
  name: DBPostProcess
  thresh: 0.3
  box_thresh: 0.6
  max_candidates: 1000
  unclip_ratio: 1.5

Metric:
  name: DetMetric
  main_indicator: hmean

Train:
  dataset:
    name: SimpleDataSet
    data_dir: CCPD
    label_file_list:
      - CCPD/train.txt
    ratio_list: [1.0]
    transforms:
      - DecodeImage: # load image
          img_mode: BGR
          channel_first: False
      - DetLabelEncode: # Class handling label
      - IaaAugment:
          augmenter_args:
            - {
      'type': Fliplr, 'args': {
      'p': 0.5 } }
            - {
      'type': Affine, 'args': {
      'rotate': [-10, 10] } }
            - {
      'type': Resize, 'args': {
      'size': [0.5, 3] } }
      - EastRandomCropData:
          size: [960, 960]
          max_tries: 50
          keep_ratio: true
      - MakeBorderMap:
          shrink_ratio: 0.4
          thresh_min: 0.3
          thresh_max: 0.7
      - MakeShrinkMap:
          shrink_ratio: 0.4
          min_text_size: 8
      - NormalizeImage:
          scale: 1./255.
          mean: [0.485, 0.456, 0.406]
          std: [0.229, 0.224, 0.225]
          order: 'hwc'
      - ToCHWImage:
      - KeepKeys:
          keep_keys: ['image', 'threshold_map', 'threshold_mask', 'shrink_map', 'shrink_mask'] # the order of the dataloader list
  loader:
    shuffle: True
    drop_last: False
    batch_size_per_card: 32
    num_workers: 0

Eval:
  dataset:
    name: SimpleDataSet
    data_dir: CCPD
    label_file_list:
      - CCPD/dev.txt
    transforms:
      - DecodeImage: # load image
          img_mode: BGR
          channel_first: False
      - DetLabelEncode: # Class handling label
      - DetResizeForTest:
      - NormalizeImage:
          scale: 1./255.
          mean: [0.485, 0.456, 0.406]
          std: [0.229, 0.224, 0.225]
          order: 'hwc'
      - ToCHWImage:
      - KeepKeys:
          keep_keys: ['image', 'shape', 'polys', 'ignore_tags']
  loader:
    shuffle: False
    drop_last: False
    batch_size_per_card: 1 # must be 1
    num_workers: 0


  1. 模型测试和模型导出
# 模型测试
python tools/infer_det.py -c det_vlpr.yml -o Global.infer_img=test0.jpg Global.pretrained_model=output/det_vlpr/best_accuracy Global.load_static_weights=False

# 模型导出
python tools/export_model.py -c output/det_vlpr/config.yml -o Global.pretrained_model=output/det_vlpr/best_accuracy Global.load_static_weights=False Global.save_inference_dir=./inference/det_vlpr

  1. 模型部署

PaddleOCR车牌检测识别训练、部署_第2张图片
PaddleOCR车牌检测识别训练、部署_第3张图片

你可能感兴趣的:(Paddle)