yolov8 label_studio 半自动化标注
yolov8 label_studio 半自动化标注
- 一.参考链接
- 二.操作步骤
- 1.创建容器
- 2.安装依赖
- 3.测试yolo的推理
- 4.测试yolo的训练
- 5.启动label-studio
- 6.打开浏览器:`http://IP:8081`
- 7.将标注样本用于训练
本文演示label_studio如何用于yolo模型的半自动标注
演示视频
yolov8 label_studio 半自动化标注
一.参考链接
- Ultralytics YOLO11 任务
- Label Studio Quick start
二.操作步骤
1.创建容器
- A.删除旧的容器
docker stop yolov8_label_studio
docker rm yolov8_label_studio
- B.创建启动容器的脚本
cat> run_yolov8_label_studio_docker.sh <<-'EOF'
container_name="yolov8_label_studio"
if [ $(docker ps -a -q -f name=^/${container_name}$) ]; then
echo "容器 '$container_name' 已经存在。"
else
echo "容器 '$container_name' 不存在。正在创建..."
docker run --gpus all --shm-size=32g -id -e NVIDIA_VISIBLE_DEVICES=all \
--privileged --net=host -v $PWD:/home -w /home \
--name $container_name --hostname=$container_name \
nvcr.io/nvidia/pytorch:23.07-py3 /bin/bash
fi
docker start $container_name
docker exec -ti $container_name bash
EOF
- C.启动容器
bash run_yolov8_label_studio_docker.sh
2.安装依赖
apt update
apt install -y tmux
pip install ultralytics==8.3.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-python==4.7.0.72 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-contrib-python==4.7.0.72 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 下载测试模型及图片
cd /home
wget -O best.pt https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11x.pt
wget -O bus.jpg https://ultralytics.com/images/bus.jpg
pip install label-studio -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install git+https://github.com/HumanSignal/label-studio-ml-backend.git
pip install git+https://github.com/HumanSignal/label-studio-sdk.git
pip install gunicorn==22.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
cd /home
git clone https://github.com/seblful/label-studio-yolov8-backend.git
3.测试yolo的推理
cd /home
yolo predict model=yolo11x.pt source='/home/bus.jpg'
4.测试yolo的训练
cd /home
rm -rf coco8*
wget -O /home/coco8.zip https://github.com/ultralytics/assets/releases/download/v0.0.0/coco8.zip
wget -O coco8.yaml https://raw.githubusercontent.com/ultralytics/ultralytics/refs/heads/main/ultralytics/cfg/datasets/coco8.yaml
unzip coco8.zip
sed -i "s/path:.*/path: \/home\/coco8/g" coco8.yaml
yolo detect train data=coco8.yaml model=yolo11n.yaml epochs=16 imgsz=640
5.启动label-studio
- A.打开tmux,创建二个视口
tmux new -s lm
- B.视口一:启动ml-backend
cd /home/label-studio-yolov8-backend
export TASK_TYPE="detection"
export MODEL_DIR="/home"
gunicorn --bind :${PORT:-9090} --workers ${WORKERS:-1} --threads ${THREADS:-4} --timeout 0 _wsgi:app
- C.视口二:启动label-studio
label-studio
6.打开浏览器:http://IP:8081
- 创建登录帐号
- 创建项目
- 导入图片集
- 设置任务类型:目标分割
- 配置ML-Backend:http://127.0.0.1:9090
- 开始标注
- 导出YOLO格式的标注,输出为一个zip包,里面是图片及label
7.将标注样本用于训练
- A.创建数据预处理脚本
cat> process_yolo_datasets.py <<-'EOF'
import yaml
import os
import glob
import sys
import shutil
import random
def Mkdir(path):
if not os.path.exists(path):
os.mkdir(path)
if len(sys.argv)!=3:
print(f"{sys.argv[0]} data_root data_yaml_path")
os._exit(0)
data_root=sys.argv[1]
data_yaml_path=sys.argv[2]
all_images=[os.path.basename(x.strip()).split(".")[0] for x in glob.glob(os.path.join(data_root,"images/*.jpg"))]
random.shuffle(all_images)
total_count=len(all_images)
train_count=max(1,int(total_count*0.95)) #分割比例
train_list=all_images[:train_count]
val_list=all_images[train_count:]
Mkdir(os.path.join(data_root,"images/train"))
Mkdir(os.path.join(data_root,"images/val"))
Mkdir(os.path.join(data_root,"labels/train"))
Mkdir(os.path.join(data_root,"labels/val"))
for i in train_list:
shutil.move(os.path.join(data_root,f"images/{i}.jpg"),os.path.join(data_root,f"images/train/{i}.jpg"))
shutil.move(os.path.join(data_root,f"labels/{i}.txt"),os.path.join(data_root,f"labels/train/{i}.txt"))
for i in val_list:
shutil.move(os.path.join(data_root,f"images/{i}.jpg"),os.path.join(data_root,f"images/val/{i}.jpg"))
shutil.move(os.path.join(data_root,f"labels/{i}.txt"),os.path.join(data_root,f"labels/val/{i}.txt"))
dataset_yaml={}
dataset_yaml['path']=data_root
dataset_yaml['train']="images/train"
dataset_yaml['val']="images/val"
dataset_yaml['names']={}
with open(os.path.join(data_root,"classes.txt"),"r") as f:
for i,v in enumerate([x.strip() for x in f.readlines()]):
dataset_yaml['names'][i]=v
with open(data_yaml_path, 'w', encoding='utf-8') as file:
yaml.dump(dataset_yaml, file, allow_unicode=True)
EOF
- B.处理导出的样本
rm -rf demo_dataset
unzip project-1-at-2024-12-26-11-13-a196fb0e.zip -d demo_dataset
python process_yolo_datasets.py $PWD/demo_dataset data.yaml
- C.开始训练
yolo detect train data=data.yaml model=yolo11n.yaml epochs=16 imgsz=640