[CVAT] CVAT custom model auto-annotations 설정 방법
CVAT custom model auto-annotations 설정
CVAT custom model을 이용해 auto-annotations 하는 방법을 알아보겠습니다.Auto-Annotations를 설치전 필요한 CVAT 설치는 여기를, Nuctl 설치는 여기를 먼저 참고해 진행하세요.
1. 파일 설정
위 CVAT와 Nuctl 설치가 완료되었다면 deploy하려는 폴더 구조를 생성합니다.폴더 구조는 본인의 cvat clone 경로에서 아래 명령어를 실행하세요. (저는 cd ~/projects/cvat 경로에서 실행)
아래 경로는 본인에게 맞게 수정해도 무방합니다.
sudo mkdir -p serverless/pytorch/mymodel/yolo-custom/nuclio
cd serverless/pytorch/mymodel/yolo-custom/nuclio
function-gpu.yaml
sudo vi function-gpu.yaml
아래 클래스(person, helmet)는 본인 환경에 맞게 수정하세요.
metadata:
name: pth-yolo-custom
namespace: cvat
annotations:
name: My Custom YOLO
type: detector
framework: pytorch
spec: |
[
{ "id": 0, "name": "person" },
{ "id": 1, "name": "helmet" }
]
spec:
description: Custom trained YOLO model
runtime: 'python:3.10'
handler: main:handler
eventTimeout: 30s
build:
image: cvat.pth.yolo.custom
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 ultralytics
- kind: WORKDIR
value: /opt/nuclio
triggers:
myHttpTrigger:
maxWorkers: 1
kind: 'http'
workerAvailabilityTimeoutMilliseconds: 10000
attributes:
maxRequestBodySize: 33554432 # 32MB
resources:
limits:
nvidia.com/gpu: 1
platform:
attributes:
restartPolicy:
name: always
maximumRetryCount: 3
mountMode: volume
main.py
sudo vi main.py
import json
import base64
import io
from PIL import Image
from ultralytics import YOLO
def init_context(context):
context.logger.info("Init context... 0%")
model_path = "/opt/nuclio/best.pt"
if os.path.exists(model_path):
context.logger.info(f"Loading custom model from {model_path}")
else:
raise Exception("Check model path")
model = YOLO(model_path)
context.user_data.model = model
context.logger.info("Init context...100%")
def handler(context, event):
context.logger.info("Run model")
data = event.body
buf = io.BytesIO(base64.b64decode(data["image"]))
threshold = float(data.get("threshold", 0.5))
context.user_data.model.conf = threshold
image = Image.open(buf)
results = context.user_data.model.predict(image, conf=threshold)[0]
detections = []
for box in results.boxes:
xtl, ytl, xbr, ybr = box.xyxy[0].tolist()
label_id = int(box.cls[0].item())
conf = float(box.conf[0].item())
detections.append({
"confidence": str(conf),
"label": context.user_data.model.names[label_id],
"points": [xtl, ytl, xbr, ybr],
"type": "rectangle",
})
return context.Response(
body=json.dumps(detections),
headers={},
content_type='application/json',
status_code=200
)
모델 가중치 파일 복사
마지막으로 본인의 모델 가중치 파일을 복사하세요.(경로는 본인에게 맞게 수정)sudo cp your_model_path/best.pt serverless/pytorch/mymodel/yolo-custom/nuclio/
2. CVAT custom model 배포
위 3개의 파일이 준비되었다면 이제 배포를 진행합니다.자신의 cvat clone한 path 기준에서 아래 명령어를 실행합니다.
./serverless/deploy_gpu.sh serverless/pytorch/mymodel/yolo-custom
nuctl get function --platform local
참고 문헌
| [1] | https://docs.cvat.ai/docs/guides/serverless-tutorial/ |
댓글
댓글 쓰기