paddlexClas 实现物体分类模型训练

58 阅读2分钟
  1. 安装paddle-gpu

    先试用conda 创建一个新的环境,然后在环境安装 python -m pip install paddlepaddle-gpu==3.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/

  2. 安装paddlex pip install paddlex

  3. 安装 paddlex[base] pip install "paddlex[base]"

  4. 安装 PaddleClas paddlex --install PaddleClas

  5. 调整数据集

    数据集需要满足标准格式,具体请参考案例

image.png

  1. 验证数据集

    验证数据集是否合法

    python main.py -c paddlex/configs/modules/image_classification/PP-LCNet_x1_0.yaml \ [](https://paddlepaddle.github.io/PaddleX/latest/practical_tutorials/image_classification_garbage_tutorial.html#__codelineno-2-2)-o Global.mode=check_dataset \ [](https://paddlepaddle.github.io/PaddleX/latest/practical_tutorials/image_classification_garbage_tutorial.html#__codelineno-2-3)-o Global.dataset_dir=./dataset/trash40/

  2. 训练模型

    注意修改文件的训练数据集地址,同时在模型训练的时候默认会加载paddlex包下面的 paddleClas包下面的训练模型配置文件,会使用到两个配置文件注意

    paddlex 模型训练配置文件

Global:
  model: PP-LCNet_x1_0
  mode: check_dataset # check_dataset/train/evaluate/predict/predict
  dataset_dir: "E:/pyworkspace/PaddleX/dataset/trash40"
  device: gpu:0
  output: "output"

CheckDataset:
  convert: 
    enable: False
    src_dataset_type: null
  split: 
    enable: False
    train_percent: null
    val_percent: null

Train:
  num_classes: 102
  epochs_iters: 20
  batch_size: 32
  learning_rate: 0.1
  config_path: ./pretrained/PP-LCNet_x1_0_pretrained.pdparams
  pretrain_weight_path: https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-LCNet_x1_0_pretrained.pdparams
  warmup_steps: 5
  resume_path: null
  log_interval: 1
  eval_interval: 1
  save_interval: 1

Evaluate:
  weight_path: "output/best_model/best_model.pdparams"
  log_interval: 1

Export:
  weight_path: https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-LCNet_x1_0_pretrained.pdparams

Predict:
  batch_size: 1
  model_dir: "output/best_model/inference"
  input: "https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_image_classification_001.jpg"
  kernel_option:
    run_mode: paddle

paddleclas模型训练配置文件如下

Global:
  checkpoints: null
  pretrained_model: null
  output_dir: ./output/
  device: gpu
  save_interval: 1
  eval_during_train: True
  eval_interval: 1
  epochs: 360
  print_batch_step: 10
  use_visualdl: False
  # used for static mode and model export
  image_shape: [3, 224, 224]
  save_inference_dir: ./inference

# mixed precision
AMP:
  use_amp: False
  use_fp16_test: False
  scale_loss: 128.0
  use_dynamic_loss_scaling: True
  use_promote: False
  # O1: mixed fp16, O2: pure fp16
  level: O1


# model architecture
Arch:
  name: PPLCNet_x1_0
  class_num: 1000
 
# loss function config for traing/eval process
Loss:
  Train:
    - CELoss:
        weight: 1.0
        epsilon: 0.1
  Eval:
    - CELoss:
        weight: 1.0


Optimizer:
  name: Momentum
  momentum: 0.9
  lr:
    name: Cosine
    learning_rate: 0.8
    warmup_epoch: 5
  regularizer:
    name: 'L2'
    coeff: 0.00003


# data loader for train and eval
DataLoader:
  Train:
    dataset:
      name: ImageNetDataset
      image_root: ./dataset/ILSVRC2012/
      cls_label_path: ./dataset/ILSVRC2012/train_list.txt
      transform_ops:
        - DecodeImage:
            to_rgb: True
            channel_first: False
        - RandCropImage:
            size: 224
        - RandFlipImage:
            flip_code: 1
        - NormalizeImage:
            scale: 1.0/255.0
            mean: [0.485, 0.456, 0.406]
            std: [0.229, 0.224, 0.225]
            order: ''

    sampler:
      name: DistributedBatchSampler
      batch_size: 512
      drop_last: False
      shuffle: True
    loader:
      num_workers: 4
      use_shared_memory: True

  Eval:
    dataset: 
      name: ImageNetDataset
      image_root: ./dataset/ILSVRC2012/
      cls_label_path: ./dataset/ILSVRC2012/val_list.txt
      transform_ops:
        - DecodeImage:
            to_rgb: True
            channel_first: False
        - ResizeImage:
            resize_short: 256
        - CropImage:
            size: 224
        - NormalizeImage:
            scale: 1.0/255.0
            mean: [0.485, 0.456, 0.406]
            std: [0.229, 0.224, 0.225]
            order: ''
    sampler:
      name: DistributedBatchSampler
      batch_size: 64
      drop_last: False
      shuffle: False
    loader:
      num_workers: 4
      use_shared_memory: True

Infer:
  infer_imgs: docs/images/inference_deployment/whl_demo.jpg
  batch_size: 10
  transforms:
    - DecodeImage:
        to_rgb: True
        channel_first: False
    - ResizeImage:
        resize_short: 256
    - CropImage:
        size: 224
    - NormalizeImage:
        scale: 1.0/255.0
        mean: [0.485, 0.456, 0.406]
        std: [0.229, 0.224, 0.225]
        order: ''
    - ToCHWImage:
  PostProcess:
    name: Topk
    topk: 5
    class_id_map_file: ppcls/utils/imagenet1k_label_list.txt

Metric:
  Train:
    - TopkAcc:
        topk: [1, 5]
  Eval:
    - TopkAcc:
        topk: [1, 5]

python main.py -c paddlex/configs/modules/image_classification/PP-LCNet_x1_0.yaml \ [](https://paddlepaddle.github.io/PaddleX/latest/practical_tutorials/image_classification_garbage_tutorial.html#__codelineno-4-2)-o Global.mode=train \ [](https://paddlepaddle.github.io/PaddleX/latest/practical_tutorials/image_classification_garbage_tutorial.html#__codelineno-4-3)-o Global.dataset_dir=./dataset/trash40 \ [](https://paddlepaddle.github.io/PaddleX/latest/practical_tutorials/image_classification_garbage_tutorial.html#__codelineno-4-4)-o Train.num_classes=40

  1. 模型评估

python main.py -c paddlex/configs/modules/image_classification/PP-LCNet_x1_0.yaml \ [](https://paddlepaddle.github.io/PaddleX/latest/practical_tutorials/image_classification_garbage_tutorial.html#__codelineno-5-2)-o Global.mode=evaluate \ [](https://paddlepaddle.github.io/PaddleX/latest/practical_tutorials/image_classification_garbage_tutorial.html#__codelineno-5-3)-o Global.dataset_dir=./dataset/trash40

  1. 模型部署

    安装 paddlex --install serving

paddlex --serve --pipeline .\my_path\image_classification_clothes.yaml

INFO: Started server process [63108] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)