Writing in the front

This is my first technical blog post and the beginning of what I hope will be a good habit of documenting and summarizing projects before the start of my graduate career. To be clear, the goal of this blog is to make deep learning easier for more people, so it’s also a summary of the pitfalls.

Know your hardware

At the beginning of model training, you must first understand your hardware device. The purpose of this project is to deploy the trained model on the eageboardFz3B board, so before this, you must first understand the version and dependency of the model that your hardware device can bear. The line is summarized as follows:

Eageboard dependent version number
paddlepaddle < = 1.8.5
PaddleDetection Realse 0.5
PaddleSlim = 2.9.0

The software platform

Baidu Flying slurry training platform AIstudio. The project address

Model training

Because of the requirements of the project, we have to train our own data set, not the open data set. Of course, if your project can find an alternative data set on the Internet, you can also choose the open data set, so you don’t need to annotate the data.

1. Prepare data sets

The annotation of data set is mainly about the annotation of VOC data set. Since I finally choose to use the data set in VOC format, the content of converting Labelme into COCO format is omitted. If necessary, please refer to [help document](How to train custom data set — PaddleDetection 0.1 document).

Tool use

Use Sprite annotation Assistant to complete annotation tasks,

The image folder path is the source image path. Num_class is separated by English comma. It is better to write the class name in English as well. Remember to save CTr+S after annotation otherwise it will not be overwritten. After exporting the data set, the XML files required for the VOC format are ready. It should replace the class keyword object

2. Use aiStudio platform for training

2.1 Creating a Project

AI Studio provides scripts and Jupyter Notebooks; For example, ssD_mobilenet model needs to be deployed to the terminal, so it is necessary to know the computing power of your terminal in advance. This is particularly important in practice, because the previous work did not do a good job in the investigation, leading to the training of yolov3 model because the model was too large and the board could not provide the required computing power, so the expected effect could not be achieved. Finally, only the lightweight model can be used to complete the detection task. The performance of common models on edgeBoard is shown in the following table.

The above picture is from a blogger, but the original blogger was not found due to the long time between the collation and the author’s summary. If there is any infringement, please contact me to delete (the pen core). After knowing these, we can start programming. Here I choose to download the installation package, [you can also download directly from Gitee but in order to prevent version problems](PaddleDetection: PaddleDetection aims to provide rich, easy-to-use target detection models for industry and academia – Gitee.com), select Release_0.5 to download directly. Then run the following command to decompress the package to the root directory.! Unzip PaddleDetection - release - 0.5. Zip

Extract data set

! unzip data/data130283/trytwo3.zip -d work/

Partition data set

import random
importTXT and val.txt random. Seed (2020)
xml_dir  = '/home/aistudio/work/trytwo/ANNOTATIONS'# tag file address img_dir ='/home/aistudio/work/trytwo/IMAGES'Path_list = list()for img in os.listdir(img_dir):
    img_path = os.path.join(img_dir,img)
    xml_path = os.path.join(xml_dir,img.replace('png'.'xml'))
    path_list.append((img_path, xml_path))
random.shuffle(path_list)
ratio = 0.9
train_f = open('/home/aistudio/work/train.txt'.'w'Val_f = open()'/home/aistudio/work/val.txt' ,'w')# generate validation filefor i ,content in enumerate(path_list):
    img, xml = content
    text = img + ' ' + xml + '\n'
    if i < len(path_list) * ratio:
        train_f.write(text)
    else: val_F.write (text) train_F.close () val_F.close ()'Slope'.'Numone'.'Freeezone'.'NumTwo'.'STOP'.'CASD'.'GasStation'.'GrossWalk'.'Cone']# set the category you want to detectwith open('/home/aistudio/work/label_list.txt'.'w') as f:
    for text in label:
        f.write(text+'\n')
Copy the code

Parameter configuration

Set the path configuration parameters for your data set under the path paddledisage-release -0.5/configs/ SSD/ssd_mobilenet_v1_VOC. yml. Here are the parameters you may need to change. num_classes

TrainReader:
  inputs_def:
    image_shape: [3.300.300]
    fields: ['image'.'gt_bbox'.'gt_class']
  dataset:
    !VOCDataSet
    anno_path: train.txt #dataset_dir: /home/Aistudio /work # needs to be modifieduse_default_label: false# Default to True, change tofalse, the same belowsample_transforms:
  
  EvalReader:
  inputs_def:
    image_shape: [3.300.300]
    fields: ['image'.'gt_bbox'.'gt_class'.'im_shape'.'im_id'.'is_difficult']
  dataset:
    !VOCDataSet
    anno_path: val.txt #dataset_dir: /home/Aistudio/work # modifieduse_default_label: false           
  sample_transforms:
  
  TestReader:
  inputs_def:
    image_shape: [3.300.300]
    fields: ['image'.'im_id'.'im_shape']
  dataset:
    !ImageFolder
    anno_path: /home/Aistudio/work/label_list. TXT # changeuse_default_label: false
  sample_transforms:
 
Copy the code

Start training

! Python PaddleDetection – release – 0.5 / tools/train. Py – cPaddleDetection – release – 0.5 / configs/SSD/ssd_mobilenet_v1_voc yml Seeing such information is considered a successful beginning of training

At the end of the training, it will automatically generate an Output folder, and then we can predict an image and see what it looks like and then you can modify it for your own needs

Model to predict

python PaddleDetection-release-0.5/tools/infer.py \
-c PaddleDetection-release-0.5/configs/ssd/ssd_mobilenet_v1_voc.yml \
-o weights=output/ssd_mobilenet_v1_voc/best_model.pdparams \
--infer_img=work/trytwo/IMAGES/1010.png
Copy the code

The export model

 python PaddleDetection-release-0.5/tools/export_model.py -c PaddleDetection-release-0.5/configs/ssd/ssd_mobilenet_v1_voc.yml --output_dir=./inference_model -o weights=output/ssd_mobilenet_v1_voc/best_model.pdparams TestReader.inputs_def.image_shape=[3.300.300]
Copy the code

PaddleDemo is required to deploy the model, just follow the JSON file sample configuration, and you have completed all the steps that can be done on the AIStudio platform.

Video to predict

python PaddleDetection-release-0.5/deploy/python/infer.py --model_dir=inference_model/ssd_mobilenet_v1_voc --video_file=testes.mp4 --thresh=0.8
Copy the code

The results of

Model deployment and tuning

You can see that the model does not always behave as you would expect when deployed to an end device, so this requires further tuning work, some parameter tuning reference