- 建立yolov8库的本地镜像
git clone https://github.com/ultralytics/ultralytics.git
cd ultralytics/
- 创建yolov8的本地镜像
在./ultralytics路径下,准备Dockerfile_cvat,内容如下:
FROM ultralytics/ultralytics:latest # yolov5官方镜像
RUN mkdir -p /opt/nuclio # 创建工作目录,该路径是CVAT默认工作路径,必须有
WORKDIR /opt/nuclio # 指定工作目录
COPY . /opt/nuclio # 复制当前路径下所有的文件到docker中
这里的FROM 的意思就是我们所需要映射的v8的环境镜像,没有环境我们是运行不了代码的,FROM后面的地址可以在这个网站上进行拉取ultralytics,我们点击这里的copy即可复制命令
docker pull ultralytics/ultralytics:latest
docker images # 查看镜像是否拉取成功
回到刚才创建的Dockerfile_cvat文件目录下执行
docker build -t 你的镜像名称 -f Dockerfile_cvat .
docker images # 查看镜像是否拉取成功
- 在cvat中构建我们的工作空间
进入/cvat/serverless/pytorch路径下,新建yolov8/nuclio文件夹
新建一个function-gpu.yaml的文件:
metadata:
name: pth-ultralytics-yolov8-bublle
namespace: cvat
annotations:
name: yolov8_bublle
type: detector
framework: pytorch
spec: |
[
{ "id": 1, "name": "crack"},
{ "id": 2, "name": "bubble"}
]
spec:
description: yolov8_bublle from ultralytics
runtime: 'python:3.8'
handler: main:handler
eventTimeout: 30s
build:
image: yolov8_bublle:latest
baseImage: ultralytics/ultralytics:latest
directives:
preCopy:
- kind: USER
value: root
- kind: RUN
value: apt update && apt install --no-install-recommends -y libglib2.0-0
- kind: RUN
value: pip install supervision
- kind: WORKDIR
value: /opt/nuclio
triggers:
myHttpTrigger:
maxWorkers: 1
kind: 'http'
workerAvailabilityTimeoutMilliseconds: 10000
attributes:
maxRequestBodySize: 67108864 # 64MB
platform:
attributes:
restartPolicy:
name: always
maximumRetryCount: 3
mountMode: volume
main.py,cvat会在工作空间的这个py脚本中寻找init_context方法和handler方法(前者用来初始化模型等,后者用来推理并返回cvat可以接收的结果)
import io
import base64
import json
import cv2
import numpy as np
from ultralytics import YOLO
# Initialize your model
def init_context(context):
context.logger.info('Init context... 0%')
model = YOLO('yolov8_bubble.pt')
context.user_data.model_handler = model
context.logger.info('Init context...100%')
# Inference endpoint
def handler(context, event):
context.logger.info('Run custom yolov8 model')
data = event.body
image_buffer = io.BytesIO(base64.b64decode(data['image']))
image = cv2.imdecode(np.frombuffer(image_buffer.getvalue(), np.uint8), cv2.IMREAD_COLOR)
results = context.user_data.model_handler(image)
result = results[0]
boxes = result.boxes.data[:,:4]
confs = result.boxes.conf
clss = result.boxes.cls
class_name = result.names
detections = []
threshold = 0.1
for box, conf, cls in zip(boxes, confs, clss):
label = class_name[int(cls)]
if conf >= threshold:
# must be in this format
detections.append({
'confidence': str(float(conf)),
'label': label,
'points': box.tolist(),
'type': 'rectangle',
})
return context.Response(body=json.dumps(detections), headers={},
content_type='application/json', status_code=200)
最后我们cd到cvat/serverless路径下,然后打开终端,输入指令:
./deploy_gpu.sh pytorch/yolov8/nuclio/ #你刚才创建的文件夹路径
到nuclio界面下查看function是否运行健康,nuclio界面示例如下图:
在CVAT标注页面测试是否正常加载yolov8_bubble,并顺利标注
此次部署参考了以下文档,特此声明:
- CVAT半自动标注:如何添加自己的预训练模型
- 【深度学习】ubuntu系统下docker部署cvat的自动标注功能(yolov8 segmentation)
- custom-yolov8-auto-annotation-cvat-blueprint
- 官网文档
补充一下安装nuclio步骤
1.先进入cvat目录下,停止服务
docker compose down
运行如下命令来启动Cvat
docker compose -f docker-compose.yml -f components/serverless/docker-compose.serverless.yml up -d
如果想要停止,请运行
docker compose -f docker-compose.yml -f components/serverless/docker-compose.serverless.yml down
2.安装unctl
找到Cvat目录下的components/serverless/docker-compose.serverless.yml,安装对应版本好的nuctl
wget https://github.com/nuclio/nuclio/releases/download/<version>/nuctl-<version>-linux-amd64
3.加上执行权限,创建软连接
sudo chmod +x nuctl-<version>-linux-amd64
sudo ln -sf $(pwd)/nuctl-<version>-linux-amd64 /usr/local/bin/nuctl
- 构建nuclio项目
./serverless/deploy_cpu.sh serverless/openvino/dextr
./serverless/deploy_cpu.sh serverless/openvino/omz/public/yolo-v3-tf
- 访问
访问localhost:8070