2019年12月30日 星期一

MNN neural network training step

-----------------------------------
A1. Preinstall
-----------------------------------
sudo apt install python3-lxml
(Do not install python3-pyqt5 python3-pyqt6)

pip3 install --force-reinstall --upgrade --break-system-packages --target=/usr/lib/python3/dist-packages ultralytics MNN onnx onnxslim onnxruntime 

pip3 install --force-reinstall --upgrade --break-system-packages --target=/usr/lib/python3/dist-packages labelImg anylabeling

new version recommend:
pip3 install --force-reinstall --upgrade --break-system-packages --target=/usr/lib/python3/dist-packages labelImg x-anylabeling-cvhub[cpu]

-----------------------------------
B1. LabelImg create dataset
-----------------------------------
/usr/local/bin/labelImg

-----------------------------------
B2. Directories structure
-----------------------------------
mnndataset/
|-- yolo.yaml                  # yolo train config file
|-- images/                    # all images
|   |-- train/                 # training images
|   |   |-- worker_001.jpg
|   |   |-- worker_002.png
|   |   `-- ...
|   |-- val/                   # validation images
|   |   |-- worker_101.jpg
|   |   `-- ...
|   `-- test/                  # test images (optional)
|       `-- worker_201.jpg
`-- labels/                    # all YOLO label files (.txt)
    |-- train/                 # must match images/train filenames
    |   |-- worker_001.txt
    |   |-- worker_002.txt
    |   `-- ...
    |-- val/                   # must match images/val filenames
    |   |-- worker_101.txt
    |   `-- ...
    `-- test/                  # optional
        `-- worker_201.txt

-----------------------------------
B3. gedit /mnt/upload/projects/mnndataset/yolo.yaml
-----------------------------------
path: /mnt/upload/projects/mnndataset
train: images/train
val: images/val
test: images/test

nc: 1
names:
  0: car
  
xxxxx.txt example:
0 0.451200 0.523000 0.210000 0.480000

-----------------------------------
B4. train
-----------------------------------
start train:
yolo detect train data=/mnt/upload/projects/mnndataset/yolo.yaml model=yolov8n.pt epochs=100 imgsz=640 batch=16

resume train:
yolo detect train resume=True

add train epochs:
yolo detect train \
data=/mnt/upload/projects/mnndataset/yolo.yaml \
model=runs/detect/train/weights/last.pt \
epochs=200 imgsz=640 batch=16 device=0

Device Table
+------------+-----------------------------------+
| device     | Meaning                           |
+------------+-----------------------------------+
| device=0   | Use the first GPU, most common    |
| device=1   | Use the second GPU                |
| device=0,1 | Use two GPUs together             |
| device=cpu | Use CPU only, very slow           |
| not set    | Auto select, use GPU if available |
+------------+-----------------------------------+

convert to .mnn
python3 -c "from ultralytics import YOLO; YOLO('/mnt/upload/projects/runs/detect/train/weights/best.pt').export(format='mnn', imgsz=640)"

Model Table
+---------+---------+--------+---------+----------+
| Model   | Meaning | Params | Speed   | Accuracy |
+---------+---------+--------+---------+----------+
| yolov8n | nano    | ~3.2M  | Fastest | Lowest   |
| yolov8s | small   | ~11.2M | Fast    | Medium   |
| yolov8m | medium  | ~25.9M | Normal  | Higher   |
| yolov8l | large   | ~43.7M | Slow    | High     |
| yolov8x | xlarge  | ~68.2M | Slowest | Highest  |
+---------+---------+--------+---------+----------+

-----------------------------------
C1. Anylabeling create dataset
-----------------------------------
python3 -c \
"import os, json, glob
from PIL import Image
img_dir = '/mnt/upload/projects/mnndataset/images/train'
label_dir = '/mnt/upload/projects/mnndataset/labels/train'
for tf in glob.glob(label_dir + '/*.txt'):
    name = os.path.basename(tf).replace('.txt','')
    img_path = None
    for ext in ['.jpg','.png','.jpeg']:
        p = os.path.join(img_dir, name + ext)
        if os.path.exists(p):
            img_path = p
            break
    if not img_path: continue
    w, h = Image.open(img_path).size
    shapes = []
    for line in open(tf).read().strip().splitlines():
        if not line.strip(): continue
        c, xc, yc, bw, bh = map(float, line.split())
        x1 = (xc - bw/2) * w
        y1 = (yc - bh/2) * h
        x2 = (xc + bw/2) * w
        y2 = (yc + bh/2) * h
        shapes.append({'label':'car','points':[[x1,y1],[x2,y2]],'shape_type':'rectangle','flags':{}})
    data = {'version':'0.0.0','flags':{},'shapes':shapes,'imagePath':os.path.basename(img_path),'imageData':None,'imageWidth':w,'imageHeight':h}
    json.dump(data, open(os.path.join(img_dir, name + '.json'), 'w'))
print('done')"

#after run this command put xxxxx.jpg xxxxx.json in the same directory

-----------------------------------
C2. gedit /mnt/upload/projects/mnndataset/anylabel.yaml
-----------------------------------
name: userAI
display_name: MNN YOLOv8
type: yolov8
model_path: /mnt/upload/asustor_projects/mnndataset/yolov8n.onnx
input_width: 640
input_height: 640
confidence_threshold: 0.85
score_threshold: 0.5
nms_threshold: 0.5
classes:
    - car

-----------------------------------
C3. Labeling 20% of all pictures then train
-----------------------------------
yolo detect train data=/mnt/upload/projects/mnndataset/yolo.yaml model=yolov8n.pt epochs=100 imgsz=640 batch=16

-----------------------------------
C4. Auto labeling
-----------------------------------
run: /usr/lib/python3/dist-packages/bin/anylabeling
click left down a brain like icon "Auto Labeling" 
~or
run: /usr/lib/python3/dist-packages/bin/xanylabeling
click top right a cross like icon "Auto Labeling" 

in "Auto" field select anylabel.yaml

Press Run

ps: If the wrong category is identified, you can modify the corresponding config.yaml file under ~/anylabeling_data/models. For example: ~/anylabeling_data/models/yolov8x-r20230415/config.yaml

refer to:
https://github.com/CVHub520/X-AnyLabeling/blob/main/docs/en/get_started.md

沒有留言:

張貼留言