Technology Sharing

The domestic framework PaddleClas combines with Swanlab for weed classification

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Project Introduction

Weeds are one of the main problems in agriculture, posing a serious threat to crop growth and yield. Traditional manual identification and management methods are inefficient and imprecise, so advanced computer vision technology is needed to improve the efficiency and quality of agricultural production. As a deep learning model, ResNet performs well in processing complex image classification tasks. It can not only effectively solve the problems of diverse and complex weeds in farmland, but also promote the development of intelligent agriculture, reduce dependence on chemical pesticides, and achieve the goal of sustainable agricultural development. By using ResNet for weed classification, farmers can be provided with smarter and more accurate agricultural management solutions, which can promote the improvement of agricultural production efficiency and the modernization of the agricultural industry. Therefore, this project uses the domestic framework PaddleClas+Swanlab+Gradio+Swanhub for weed classification experiments.

PaddlePaddle is an enterprise-level deep learning platform developed by Baidu, which is designed to support the full process of deep learning applications from model development to deployment. It provides a wealth of tools and libraries to support a variety of deep learning tasks, including image processing, natural language processing, speech recognition, etc.PaddlePaddle

PaddleClas is a tool library in the Paddle framework specifically for image classification tasks. It provides a complete set of end-to-end solutions, including data processing, model definition, training, evaluation, and deployment, aiming to help developers quickly build and deploy efficient image classification models.PaddleClas

SwanLab is an open source, lightweight AI experiment tracking tool that improves the ML experiment tracking and collaboration experience by providing a friendly API combined with hyperparameter tracking, indicator recording, online collaboration and other functions.Welcome to SwanLab | SwanLab Official Documentation

Swanhub is an open source model collaboration and sharing community developed by Geek Studio. It provides AI developers with functions such as AI model hosting, training records, model result display, and API rapid deployment.Welcome to Swanhub

Gradio is an open source Python library designed to help data scientists, researchers, and developers working in the field of machine learning quickly create and share user interfaces for machine learning models.Gradio

2. Preparation

2.1 Environment Installation

Install the following 3 libraries:

  1. paddle
  2. swanlab
  3. gradio

Installation command:

pip install paddle swanlab gradio

2.2 Download the dataset

Weed Classification Dataset: DeepWeeds

  1. DeepWeeds
  2. --images
  3. ----1.jpg
  4. ----2.jpg
  5. --train.txt
  6. --val.txt
  7. --test.txt
  8. --classnames.txt

Their respective functions and significance:

1. DeepWeeds folder: This folder is used to store the image folder images, training set test set validation set files and label files

2. images folder: This folder is used to save training, testing, and validation image folders.

3. train.txt, val.txt, test.txt files: These files are used to save the training, test, and validation set image paths and categories.

4. classnames file: used to save category labels

2.3 Download PaddleClas framework

Model link:PaddleClas Model

After decompression, you will get the PaddleClas folder.

2.4 Create a file directory

Create app.py in the PaddleClas folder.

Its function is to run the Gradio Demo script

3. ResNet model training

3.1 Modify configuration

First, find ppcls-->configs-->ImageNet-->Res2Net-->Res2Net50_14w_8s.yaml in the PaddleClas folder.

Modify epochs to 100, class class_num to 9, training image path, verification image path and label file. A total of 7 changes were made.

  1. # global configs
  2. Global:
  3. checkpoints: null
  4. pretrained_model: null
  5. output_dir: ./output/
  6. device: gpu
  7. save_interval: 1
  8. eval_during_train: True
  9. eval_interval: 1
  10. epochs: 100###########################1##############################
  11. print_batch_step: 10
  12. use_visualdl: False
  13. # used for static mode and model export
  14. image_shape: [3, 224, 224]
  15. save_inference_dir: ./inference
  16. # model architecture
  17. Arch:
  18. name: Res2Net50_14w_8s
  19. class_num: 9############################2##############################
  20. # loss function config for traing/eval process
  21. Loss:
  22. Train:
  23. - CELoss:
  24. weight: 1.0
  25. epsilon: 0.1
  26. Eval:
  27. - CELoss:
  28. weight: 1.0
  29. Optimizer:
  30. name: Momentum
  31. momentum: 0.9
  32. lr:
  33. name: Cosine
  34. learning_rate: 0.1
  35. regularizer:
  36. name: 'L2'
  37. coeff: 0.0001
  38. # data loader for train and eval
  39. DataLoader:
  40. Train:
  41. dataset:
  42. name: ImageNetDataset
  43. image_root: ./weeds/images/#################3#######################
  44. cls_label_path: ./weeds/train.txt###########4########################
  45. transform_ops:
  46. - DecodeImage:
  47. to_rgb: True
  48. channel_first: False
  49. - RandCropImage:
  50. size: 224
  51. - RandFlipImage:
  52. flip_code: 1
  53. - NormalizeImage:
  54. scale: 1.0/255.0
  55. mean: [0.485, 0.456, 0.406]
  56. std: [0.229, 0.224, 0.225]
  57. order: ''
  58. batch_transform_ops:
  59. - MixupOperator:
  60. alpha: 0.2
  61. sampler:
  62. name: DistributedBatchSampler
  63. batch_size: 64
  64. drop_last: False
  65. shuffle: True
  66. loader:
  67. num_workers: 4
  68. use_shared_memory: True
  69. Eval:
  70. dataset:
  71. name: ImageNetDataset
  72. image_root: ./DeepWeeds/images/###############5#######################
  73. cls_label_path: ./DeepWeeds/val.txt###########6########################
  74. transform_ops:
  75. - DecodeImage:
  76. to_rgb: True
  77. channel_first: False
  78. - ResizeImage:
  79. resize_short: 256
  80. - CropImage:
  81. size: 224
  82. - NormalizeImage:
  83. scale: 1.0/255.0
  84. mean: [0.485, 0.456, 0.406]
  85. std: [0.229, 0.224, 0.225]
  86. order: ''
  87. sampler:
  88. name: DistributedBatchSampler
  89. batch_size: 64
  90. drop_last: False
  91. shuffle: False
  92. loader:
  93. num_workers: 4
  94. use_shared_memory: True
  95. Infer:
  96. infer_imgs: docs/images/inference_deployment/whl_demo.jpg
  97. batch_size: 10
  98. transforms:
  99. - DecodeImage:
  100. to_rgb: True
  101. channel_first: False
  102. - ResizeImage:
  103. resize_short: 256
  104. - CropImage:
  105. size: 224
  106. - NormalizeImage:
  107. scale: 1.0/255.0
  108. mean: [0.485, 0.456, 0.406]
  109. std: [0.229, 0.224, 0.225]
  110. order: ''
  111. - ToCHWImage:
  112. PostProcess:
  113. name: Topk
  114. topk: 5
  115. class_id_map_file: ./DeepWeeds/classnaems.txt###########7##################
  116. Metric:
  117. Train:
  118. Eval:
  119. - TopkAcc:
  120. topk: [1, 5]

3.2 Using Swanlab

Find tools-->train.py in the PaddleClas folder. Initialize swanlab

  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import sys
  19. __dir__ = os.path.dirname(os.path.abspath(__file__))
  20. sys.path.append(os.path.abspath(os.path.join(__dir__, '../')))
  21. from ppcls.utils import config
  22. from ppcls.engine.engine import Engine
  23. import swanlab
  24. # -*- coding: utf-8 -*-
  25. if __name__ == "__main__":
  26. args = config.parse_args()
  27. config = config.get_config(
  28. args.config, overrides=args.override, show=False)
  29. config.profiler_options = args.profiler_options
  30. engine = Engine(config, mode="train")
  31. ## 初始化swanlab
  32. swanlab.init(
  33. experiment_name="Swanlab_ResNet50_PaddleClas",
  34. description="Train ResNet50 for weeds classification.",
  35. project="Swanhub_Weeds_Classification",
  36. config={
  37. "model": "ResNet50",
  38. "optim": "Adam",
  39. "lr": 0.001,
  40. "batch_size": 64,
  41. "num_epochs": 100,
  42. "num_class": 9,
  43. }
  44. )
  45. engine.train()

In PaddleClas, find ppcls-->engine-->train-->utils.py and add the following code:

  1. swanlab.log({"train_lr_msg": lr_msg.split(": ")[1]}) #
  2. swanlab.log({"train_CELoss": metric_msg.split(",")[0].split(': ')[1]}) ##
  3. swanlab.log({'train_loss': metric_msg.split(",")[1].split(': ')[1]})

In the PaddleClas folder, find ppcls-->engine-->engine.py and add the following code:

swanlab.log({'best_metric': best_metric.get('metric')})

3.3 Model Training

Enter the following command in the console:

python -m paddle.distributed.launch tools/train.py -c ./ppcls/configs/ImageNet/Res2Net/Res2Net50_14w_8s.yaml

View experiment details at swanlab

The experimental results are as follows:

swanlab View experimental results

3.4 Model Reasoning

Enter the following code into the console:

python tools/infer.py -c ./ppcls/configs/ImageNet/Res2Net/Res2Net50_14w_8s.yaml -o Infer.infer_imgs=./DeepWeeds/infer/01.jpg -o Global.pretrained_model=./output/Res2Net50_14w_8s/best_model

4. DarkNet53 model training

4.1 Modify configuration

First, find ppcls-->configs-->ImageNet-->DarkNet-->DarkNet53.yaml in the PaddleClas folder.

Modify epochs to 100, class class_num to 9, training image path, verification image path and label file. A total of 7 changes were made.

  1. # global configs
  2. Global:
  3. checkpoints: null
  4. pretrained_model: null
  5. output_dir: ./output/
  6. device: gpu
  7. save_interval: 1
  8. eval_during_train: True
  9. eval_interval: 1
  10. epochs: 100
  11. print_batch_step: 10
  12. use_visualdl: False
  13. # used for static mode and model export
  14. image_shape: [3, 256, 256]
  15. save_inference_dir: ./inference
  16. # model architecture
  17. Arch:
  18. name: DarkNet53
  19. class_num: 9
  20. # loss function config for traing/eval process
  21. Loss:
  22. Train:
  23. - CELoss:
  24. weight: 1.0
  25. epsilon: 0.1
  26. Eval:
  27. - CELoss:
  28. weight: 1.0
  29. Optimizer:
  30. name: Momentum
  31. momentum: 0.9
  32. lr:
  33. name: Cosine
  34. learning_rate: 0.1
  35. regularizer:
  36. name: 'L2'
  37. coeff: 0.0001
  38. # data loader for train and eval
  39. DataLoader:
  40. Train:
  41. dataset:
  42. name: ImageNetDataset
  43. image_root: F:/datasets/DeepWeeds/images
  44. cls_label_path: F:/datasets/DeepWeeds/train.txt
  45. transform_ops:
  46. - DecodeImage:
  47. to_rgb: True
  48. channel_first: False
  49. - RandCropImage:
  50. size: 256
  51. - RandFlipImage:
  52. flip_code: 1
  53. - NormalizeImage:
  54. scale: 1.0/255.0
  55. mean: [0.485, 0.456, 0.406]
  56. std: [0.229, 0.224, 0.225]
  57. order: ''
  58. batch_transform_ops:
  59. - MixupOperator:
  60. alpha: 0.2
  61. sampler:
  62. name: DistributedBatchSampler
  63. batch_size: 64
  64. drop_last: False
  65. shuffle: True
  66. loader:
  67. num_workers: 4
  68. use_shared_memory: True
  69. Eval:
  70. dataset:
  71. name: ImageNetDataset
  72. image_root: F:/datasets/DeepWeeds/images
  73. cls_label_path: F:/datasets/DeepWeeds/val.txt
  74. transform_ops:
  75. - DecodeImage:
  76. to_rgb: True
  77. channel_first: False
  78. - ResizeImage:
  79. resize_short: 292
  80. - CropImage:
  81. size: 256
  82. - NormalizeImage:
  83. scale: 1.0/255.0
  84. mean: [0.485, 0.456, 0.406]
  85. std: [0.229, 0.224, 0.225]
  86. order: ''
  87. sampler:
  88. name: DistributedBatchSampler
  89. batch_size: 64
  90. drop_last: False
  91. shuffle: False
  92. loader:
  93. num_workers: 4
  94. use_shared_memory: True
  95. Infer:
  96. infer_imgs: docs/images/inference_deployment/whl_demo.jpg
  97. batch_size: 10
  98. transforms:
  99. - DecodeImage:
  100. to_rgb: True
  101. channel_first: False
  102. - ResizeImage:
  103. resize_short: 292
  104. - CropImage:
  105. size: 256
  106. - NormalizeImage:
  107. scale: 1.0/255.0
  108. mean: [0.485, 0.456, 0.406]
  109. std: [0.229, 0.224, 0.225]
  110. order: ''
  111. - ToCHWImage:
  112. PostProcess:
  113. name: Topk
  114. topk: 5
  115. class_id_map_file: F:/datasets/DeepWeeds/classnames
  116. Metric:
  117. Train:
  118. Eval:
  119. - TopkAcc:
  120. topk: [1, 5]

4.2 Using Swanlab

Find tools->train.py in the PaddleClas folder. Modify the initialization of swanlab

  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import sys
  19. __dir__ = os.path.dirname(os.path.abspath(__file__))
  20. sys.path.append(os.path.abspath(os.path.join(__dir__, '../')))
  21. from ppcls.utils import config
  22. from ppcls.engine.engine import Engine
  23. import swanlab
  24. # -*- coding: utf-8 -*-
  25. if __name__ == "__main__":
  26. args = config.parse_args()
  27. config = config.get_config(
  28. args.config, overrides=args.override, show=False)
  29. config.profiler_options = args.profiler_options
  30. engine = Engine(config, mode="train")
  31. ## 初始化swanlab
  32. swanlab.init(
  33. experiment_name="Swanlab_DrakNet53_PaddleClas",
  34. description="Train DarkNet53 for weeds classification.",
  35. project="Swanhub_Weeds_Classification",
  36. config={
  37. "model": "DarkNet53",
  38. "optim": "Adam",
  39. "lr": 0.001,
  40. "batch_size": 64,
  41. "num_epochs": 100,
  42. "num_class": 9,
  43. }
  44. )
  45. engine.train()

4.3 Model Training

Enter the following command in the console:

python -m paddle.distributed.launch tools/train.py -c ./ppcls/configs/ImageNet/DarkNet/DarknetNet53.yaml

View experiment details at swanlab

The experimental results are as follows:

4.4 Model Reasoning

Enter the following code into the console:

python tools/infer.py -c ./ppcls/configs/ImageNet/DarkNet/DarkNet53.yaml -o Infer.infer_imgs=./DeepWeeds/infer/01.jpg -o Global.pretrained_model=./output/DarkNet53/best_model

4.5 Swanlab Results Display

As can be seen from the figure, the effect of the ResNet50 model is better than that of the DarkNet53 model. Swanlab provides a convenient comparison chart function.

5. Gradio Demo

To be continued...

6. Swanhub uploads and demonstrates the demo

To be continued...